Skip to content

Commit 6fb85f3

Browse files
committed
ch 17 exercises initial commit
1 parent bfaeab0 commit 6fb85f3

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.Event;
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
18+
public class EventCategoryController {
19+
20+
@Autowired
21+
private EventCategoryRepository eventCategoryRepository;
22+
23+
@GetMapping
24+
public String displayAllCategories(Model model) {
25+
model.addAttribute("title", "All Categories");
26+
model.addAttribute("categories", eventCategoryRepository.findAll());
27+
return "eventCategories/index";
28+
}
29+
30+
@GetMapping
31+
public String renderCreateEventCategoryForm (Model model) {
32+
model.addAttribute("title", "Create Category");
33+
model.addAttribute(new EventCategory());
34+
return "eventCategories/create";
35+
}
36+
37+
@PostMapping
38+
public String processCreateEventForm(@Valid @ModelAttribute EventCategory eventCategory,
39+
Errors errors, Model model) {
40+
if(errors.hasErrors()) {
41+
model.addAttribute("title", "Create Category");
42+
model.addAttribute(new EventCategory());
43+
return "eventCategories/create";
44+
}
45+
46+
eventCategoryRepository.save(eventCategory);
47+
return "redirect:/eventCategories";
48+
49+
}
50+
}
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+
17+
@Size(min=3, message="Name must be at least 3 characters long")
18+
private String name;
19+
20+
public EventCategory(@Size(min=3, message="Name must be at least 3 characters long") String name) {
21+
this.name=name;
22+
}
23+
24+
public EventCategory() {}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public int getId() {
35+
return id;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return name;
41+
}
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (this == o) return true;
46+
if (o == null || getClass() != o.getClass()) return false;
47+
EventCategory that = (EventCategory) o;
48+
return id == that.id;
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(id);
54+
}
55+
}
56+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
<form method="post">
9+
<div class="form-group">
10+
<label>Name
11+
<input th:field="${eventCategory.name}" class="form-control">
12+
</label>
13+
<span th:errors="${eventCategory.name}" class="error"></span>
14+
</div>
15+
<input type="submit" value="Create" class="btn btn-primary">
16+
</form>
17+
18+
</body>
19+
</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 :: 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)