Skip to content

Commit 93ba2c6

Browse files
Maria UsinaMaria Usina
authored andcommitted
EventCategoryController
1 parent c0c002c commit 93ba2c6

6 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.launchcode.codingevents.controllers;
2+
3+
import jakarta.validation.Valid;
4+
import org.launchcode.codingevents.data.EventCategoryRepository;
5+
import org.launchcode.codingevents.data.EventRepository;
6+
import org.launchcode.codingevents.models.EventCategory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
10+
import org.springframework.validation.Errors;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.ModelAttribute;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
16+
@Controller
17+
@RequestMapping("eventCategories")
18+
public class EventCategoryController {
19+
20+
@Autowired
21+
private EventCategoryRepository eventCategoryRepository;
22+
23+
24+
@GetMapping
25+
public String displayAllCategories (Model model){
26+
model.addAttribute("title", "All Categories");
27+
model.addAttribute("categories", eventCategoryRepository.findAll());
28+
return "eventCategories/index";
29+
}
30+
31+
32+
@GetMapping("create")
33+
public String renderCreateEventCategoryForm(Model model){
34+
model.addAttribute("title", "Create Category");
35+
model.addAttribute(new EventCategory());
36+
return "eventCategories/create";
37+
}
38+
39+
@PostMapping("create")
40+
public String processCreateEventCategoryForm(@Valid @ModelAttribute EventCategory eventCategory,
41+
Errors errors, Model model){
42+
if(errors.hasErrors()){
43+
model.addAttribute("title", "Create Category");
44+
model.addAttribute(new EventCategory());
45+
return "eventCategories/create";
46+
}
47+
eventCategoryRepository.save(eventCategory);
48+
return "redirect:/eventCategories";
49+
50+
}
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.launchcode.codingevents.data;
2+
3+
import org.launchcode.codingevents.models.Event;
4+
import org.launchcode.codingevents.models.EventCategory;
5+
import org.springframework.data.repository.CrudRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface EventCategoryRepository extends CrudRepository<EventCategory, Integer> {
10+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.launchcode.codingevents.models;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.validation.constraints.NotBlank;
7+
import jakarta.validation.constraints.Size;
8+
9+
import java.util.Objects;
10+
11+
@Entity
12+
public class EventCategory {
13+
14+
@Id
15+
@GeneratedValue
16+
private int id;
17+
18+
@NotBlank(message = "Name is required")
19+
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters")
20+
private String name;
21+
22+
public EventCategory(String name) {
23+
this.name = name;
24+
}
25+
26+
public EventCategory(){};
27+
28+
public int getId() {
29+
return id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return name;
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (this == o) return true;
48+
if (o == null || getClass() != o.getClass()) return false;
49+
EventCategory that = (EventCategory) o;
50+
return id == that.id;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hashCode(id);
56+
}
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3+
<head th:replace="fragments :: head"></head>
4+
<body class="container">
5+
6+
<h1>Create Category</h1>
7+
8+
<header th:replace="fragments :: navigation"></header>
9+
10+
<form method="post">
11+
<div class="form-group">
12+
<label>Create Category
13+
<input th:field="${eventCategory.name}" class="form-control">
14+
</label>
15+
<span th:errors="${eventCategory.name}" class="error"></span>
16+
</div>
17+
<input type="submit" value="Create" class="btn btn-primary">
18+
</form>
19+
20+
</body>
21+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3+
<head th:replace="fragments :: head"></head>
4+
<body class="container">
5+
6+
<header th:replace="fragments :: navigation"></header>
7+
8+
<table class="table table-striped">
9+
<thead>
10+
<tr>
11+
<th>Category Name</th>
12+
</tr>
13+
</thead>
14+
<tr th:each="category : ${categories}">
15+
<td th:text="${category.name}"></td>
16+
</tr>
17+
18+
</table>
19+
20+
</body>
21+
</html>

src/main/resources/templates/fragments.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<li class="nav-item"><a class="nav-link" href="/events">All Events</a></li>
1717
<li class="nav-item"><a class="nav-link" href="/events/create">Create Event</a></li>
1818
<li class="nav-item"><a class="nav-link" href="/events/delete">Delete Event</a></li>
19+
<li class="nav-item"><a class="nav-link" href="/eventCategories/index">All Categories</a></li>
1920
</ul>
2021
</nav>
2122
<td th:fragment="address">1234 5th Street</td>

0 commit comments

Comments
 (0)