Skip to content

Commit 0de0ffb

Browse files
committed
mostly done exercise
1 parent 15cb580 commit 0de0ffb

5 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.launchcode.codingevents.controllers;
2+
3+
import jakarta.validation.Valid;
4+
import org.launchcode.codingevents.data.EventCategoryRepository;
5+
import org.launchcode.codingevents.models.EventCategory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.validation.Errors;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.ModelAttribute;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
15+
@Controller
16+
@RequestMapping("eventCategories")
17+
public class EventCategoryController {
18+
19+
@Autowired
20+
private EventCategoryRepository eventCategoryRepository;
21+
22+
@GetMapping
23+
public String displayAllCategories (Model model) {
24+
model.addAttribute("title", "All Categoires");
25+
model.addAttribute("categories", eventCategoryRepository.findAll());
26+
return "eventCategories/index";
27+
}
28+
29+
@GetMapping("create")
30+
public String renderCreateEventCategoryForm (Model model) {
31+
model.addAttribute("title", "Create Category");
32+
model.addAttribute(new EventCategory());
33+
return "eventCategories/create";
34+
}
35+
36+
@PostMapping("create")
37+
public String processCreateEventCategoryForm(@Valid @ModelAttribute EventCategory eventCategory, Errors errors, Model model) {
38+
if (errors.hasErrors()) {
39+
model.addAttribute("Title", "Create Category");
40+
model.addAttribute(new EventCategory());
41+
return "eventCategories/create";
42+
}
43+
44+
eventCategoryRepository.save(eventCategory);
45+
46+
return "redirect:";
47+
}
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.launchcode.codingevents.data;
2+
3+
import org.launchcode.codingevents.models.EventCategory;
4+
import org.springframework.data.repository.CrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface EventCategoryRepository extends CrudRepository<EventCategory, Integer> {
9+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.Size;
7+
8+
import java.util.Objects;
9+
10+
@Entity
11+
public class EventCategory {
12+
13+
@Id
14+
@GeneratedValue
15+
private int id;
16+
private String name;
17+
18+
@Size(min=3, message="Name must be at least 3 characters long")
19+
private String name;
20+
21+
public EventCategory(@Size(min = 3, message = "Name must be at least 3 characters long") String name) {
22+
this.name = name;
23+
}
24+
25+
public EventCategory() {}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public int getId() {
36+
return id;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return name;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) return true;
47+
if (o == null || getClass() != o.getClass()) return false;
48+
EventCategory that = (EventCategory) o;
49+
return id == that.id;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return Objects.hash(id);
55+
}
56+
}

src/main/resources/templates/eventCategories/create.html

Whitespace-only changes.
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 :: header"></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>

0 commit comments

Comments
 (0)