Skip to content

Commit 00fffe3

Browse files
Setting up relationship between events and event categories
1 parent 8535479 commit 00fffe3

7 files changed

Lines changed: 31 additions & 40 deletions

File tree

src/main/java/org/launchcode/codingevents/controllers/EventController.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import jakarta.validation.Valid;
44
import org.launchcode.codingevents.data.EventRepository;
5+
import org.launchcode.codingevents.data.EventCategoryRepository;
56
import org.launchcode.codingevents.models.Event;
6-
import org.launchcode.codingevents.models.EventType;
7+
import org.launchcode.codingevents.models.EventCategory;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.stereotype.Controller;
910
import org.springframework.web.bind.annotation.*;
1011
import org.springframework.ui.Model;
1112
import org.springframework.validation.Errors;
13+
import java.util.Optional;
1214

1315
/**
1416
* Created by Chris Bay
@@ -19,6 +21,9 @@ public class EventController {
1921
@Autowired
2022
private EventRepository eventRepository;
2123

24+
@Autowired
25+
private EventCategoryRepository eventCategoryRepository;
26+
2227
@GetMapping
2328
public String displayAllEvents(Model model) {
2429
model.addAttribute("title", "All Events");
@@ -30,7 +35,7 @@ public String displayAllEvents(Model model) {
3035
public String displayCreateEventForm(Model model) {
3136
model.addAttribute("title", "Create Event");
3237
model.addAttribute(new Event());
33-
model.addAttribute("types", EventType.values());
38+
model.addAttribute("categories", eventCategoryRepository.findAll());
3439
return "events/create";
3540
}
3641

src/main/java/org/launchcode/codingevents/models/Event.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.launchcode.codingevents.models;
22

33
import jakarta.persistence.Entity;
4+
import jakarta.persistence.ManyToOne;
45
import jakarta.validation.constraints.Email;
56
import jakarta.validation.constraints.NotBlank;
7+
import jakarta.validation.constraints.NotNull;
68
import jakarta.validation.constraints.Size;
79

810
/**
@@ -22,13 +24,15 @@ public class Event extends AbstractEntity {
2224
@Email(message = "Invalid email. Try again.")
2325
private String contactEmail;
2426

25-
private EventType type;
27+
@ManyToOne
28+
@NotNull(message = "Category is required")
29+
private EventCategory eventCategory;
2630

27-
public Event(String name, String description, String contactEmail, EventType type) {
31+
public Event(String name, String description, String contactEmail, EventCategory eventCategory) {
2832
this.name = name;
2933
this.description = description;
3034
this.contactEmail = contactEmail;
31-
this.type = type;
35+
this.eventCategory = eventCategory;
3236
}
3337

3438
public Event() {}
@@ -57,12 +61,12 @@ public void setContactEmail(String contactEmail) {
5761
this.contactEmail = contactEmail;
5862
}
5963

60-
public EventType getType() {
61-
return type;
64+
public EventCategory getEventCategory() {
65+
return eventCategory;
6266
}
6367

64-
public void setType(EventType type) {
65-
this.type = type;
68+
public void setEventCategory(EventCategory eventCategory) {
69+
this.eventCategory = eventCategory;
6670
}
6771

6872
@Override

src/main/java/org/launchcode/codingevents/models/EventCategory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.launchcode.codingevents.models;
22

33
import jakarta.persistence.Entity;
4+
import jakarta.persistence.OneToMany;
45
import jakarta.validation.constraints.Size;
6+
import java.util.ArrayList;
7+
import java.util.List;
58

69
/**
710
* Created by Chris Bay

src/main/java/org/launchcode/codingevents/models/EventType.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!DOCTYPE html>
22
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3-
<head th:replace="fragments :: head"></head>
3+
<head th:replace="~{fragments :: head}"></head>
44
<body class="container">
55

6-
<header th:replace="fragments :: header"></header>
6+
<header th:replace="~{fragments :: header}"></header>
77

88
<table class="table table-striped">
99
<thead>

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
<p class="error" th:errors="${event.contactEmail}"></p>
2828
</div>
2929
<div class="form-group">
30-
<label>Type
31-
<select th:field="${event.type}">
32-
<option th:each="type : ${types}"
33-
th:value="${type}"
34-
th:text="${type.displayName}"
30+
<label>Category
31+
<select th:field="${event.eventCategory}">
32+
<option th:each="eventCategory : ${categories}"
33+
th:value="${eventCategory.id}"
34+
th:text="${eventCategory.name}"
3535
></option>
3636
</select>
37+
<p class="error" th:errors="${event.eventCategory}"></p>
3738
</label>
3839
</div>
3940
<div class="form-group">

src/main/resources/templates/events/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
<th>Name</th>
1313
<th>Description</th>
1414
<th>Contact Email</th>
15-
<th>Type</th>
15+
<th>Category</th>
1616
</tr>
1717
</thead>
1818
<tr th:each="event : ${events}">
1919
<td th:text="${event.id}"></td>
2020
<td th:text="${event.name}"></td>
2121
<td th:text="${event.description}"></td>
2222
<td th:text="${event.contactEmail}"></td>
23-
<td th:text="${event.type.displayName}"></td>
23+
<td th:text="${event.eventCategory.name}"></td>
2424
</tr>
2525
</table>
2626

0 commit comments

Comments
 (0)