Skip to content

Commit 56e8db1

Browse files
author
John Woolbright
committed
eventCategory exercises
1 parent ad7588b commit 56e8db1

7 files changed

Lines changed: 170 additions & 2 deletions

File tree

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

src/main/resources/application.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Database connection settings
22
spring.datasource.url=jdbc:mysql://localhost:3306/coding_events
3-
spring.datasource.username=john
4-
spring.datasource.password=admin
3+
spring.datasource.username=user
4+
spring.datasource.password=password
55

66
# Specify the DBMS
77
spring.jpa.database = MYSQL
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>

src/main/resources/templates/fragments.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ <h1 th:text="${title}">Coding Events</h1>
1515
<li class="nav-item"><a class="nav-link" href="/events">All Events</a></li>
1616
<li class="nav-item"><a class="nav-link" href="/events/create">Create Event</a></li>
1717
<li class="nav-item"><a class="nav-link" href="/events/delete">Delete Events</a></li>
18+
<li class="nav-item"><a class="nav-link" href="/eventCategories">All Categories</a></li>
19+
<li class="nav-item"><a class="nav-link" href="/eventCategories/create">Create Category</a></li>
1820
</ul>
1921
</nav>
2022

0 commit comments

Comments
 (0)