Skip to content

Commit 7f03976

Browse files
committed
New category creation - working
1 parent bfaeab0 commit 7f03976

7 files changed

Lines changed: 166 additions & 4 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.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 Categories");
25+
model.addAttribute("categories", eventCategoryRepository.findAll());
26+
27+
return "eventCategories/index";
28+
}
29+
30+
@GetMapping("create")
31+
public String renderCreateEventCategoryForm(Model model) {
32+
model.addAttribute("title", "Create Category");
33+
model.addAttribute(new EventCategory());
34+
35+
return "eventCategories/create";
36+
}
37+
38+
@PostMapping("create")
39+
public String processCreateEventCategoryForm(@Valid @ModelAttribute EventCategory eventCategory,
40+
Errors errors, Model model) {
41+
if(errors.hasErrors()) {
42+
model.addAttribute("title", "Create Category");
43+
model.addAttribute(new EventCategory());
44+
45+
return "eventCategories/create";
46+
}
47+
48+
eventCategoryRepository.save(eventCategory);
49+
return "redirect:/eventCategories";
50+
}
51+
52+
53+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.launchcode.codingevents.data;
2+
3+
import org.launchcode.codingevents.models.EventCategory;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface EventCategoryRepository extends CrudRepository<EventCategory, Integer> {
7+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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, max = 50, message = "Name must be between 3 and 50 characters")
18+
private String name;
19+
20+
public EventCategory(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.hashCode(id);
54+
}
55+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Database connection settings
22
spring.datasource.url=jdbc:mysql://localhost:3306/coding_events
3-
spring.datasource.username=user
4-
spring.datasource.password=password
3+
spring.datasource.username=coding_events
4+
spring.datasource.password=Learn2code!
55

66
# Specify the DBMS
77
spring.jpa.database = MYSQL
88

99
# Show or not log for each sql query
10-
spring.jpa.show-sql = false
10+
spring.jpa.show-sql = true
1111

1212
# Hibernate ddl auto (create, create-drop, update)
1313
spring.jpa.hibernate.ddl-auto = update
1414

1515
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
1616
# stripped before adding them to the entity manager)
17-
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
17+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
<p th:text="${errorMsg}" style="color:red;"></p>
9+
10+
<form method="post">
11+
<div class="form-group">
12+
<label>Name
13+
<input th:field="${eventCategory.name}" class="form-control">
14+
</label>
15+
<p class="error" th:errors="${eventCategory.name}"></p>
16+
</div>
17+
<br>
18+
<div class="form-group">
19+
<input type="submit" value="Create" class="btn btn-success">
20+
</div>
21+
</form>
22+
23+
24+
</body>
25+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
</table>
18+
19+
</body>
20+
</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)