Skip to content

Commit 191187d

Browse files
Maria UsinaMaria Usina
authored andcommitted
many-to-one branch
1 parent 6b7a05c commit 191187d

6 files changed

Lines changed: 22 additions & 38 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.launchcode.codingevents.controllers;
22

33
import jakarta.validation.Valid;
4+
import org.launchcode.codingevents.data.EventCategoryRepository;
45
import org.launchcode.codingevents.data.EventRepository;
56
import org.launchcode.codingevents.models.Event;
6-
import org.launchcode.codingevents.models.EventType;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.stereotype.Controller;
99
import org.springframework.validation.Errors;
@@ -25,6 +25,9 @@ public class EventController {
2525
@Autowired
2626
private EventRepository eventRepository;
2727

28+
@Autowired
29+
private EventCategoryRepository eventCategoryRepository;
30+
2831
@GetMapping
2932
public String displayAllEvents(Model model) {
3033
model.addAttribute("title", "All Events");
@@ -36,7 +39,7 @@ public String displayAllEvents(Model model) {
3639
public String displayCreateEventForm(Model model) {
3740
model.addAttribute("title", "Create Event");
3841
model.addAttribute(new Event());
39-
model.addAttribute("types", EventType.values());
42+
model.addAttribute("categories", eventCategoryRepository.findAll());
4043
return "events/create";
4144
}
4245

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import jakarta.persistence.GeneratedValue;
66
import jakarta.persistence.Id;
7+
import jakarta.persistence.ManyToOne;
78
import jakarta.validation.constraints.*;
89

910
import java.util.Objects;
@@ -27,12 +28,14 @@ public class Event extends AbstractEntity {
2728
@NotNull
2829
private String location;
2930

30-
private EventType type;
31+
@ManyToOne
32+
@NotNull(message = "Category is required")
33+
private EventCategory eventCategory;
3134

32-
public Event(String name, String description, String contactEmail, String location, EventType type) {
35+
public Event(String name, String description, String contactEmail, String location, EventCategory eventCategory) {
3336
this.name = name;
3437
this.description = description;
35-
this.type = type;
38+
this.eventCategory = eventCategory;
3639
this.location = location;
3740
this.contactEmail = contactEmail;
3841

@@ -72,17 +75,14 @@ public void setLocation(String location) {
7275
this.location = location;
7376
}
7477

75-
76-
public EventType getType() {
77-
return type;
78+
public EventCategory getEventCategory() {
79+
return eventCategory;
7880
}
7981

80-
public void setType(EventType type) {
81-
this.type = type;
82+
public void setEventCategory(EventCategory eventCategory) {
83+
this.eventCategory = eventCategory;
8284
}
8385

84-
85-
8686
@Override
8787
public String toString() {
8888
return name;

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

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

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ <h1>Create Event</h1>
2626

2727
<div class="form-group">
2828
<label>Type
29-
<select th:field="${event.type}">
30-
<option th:each="type : ${types}"
31-
th:value="${type}"
32-
th:text="${type.displayName}"
29+
<select th:field="${event.eventCategory}">
30+
<option th:each="eventCategory : ${categories}"
31+
th:value="${eventCategory.id}"
32+
th:text="${eventCategory.name}"
3333
></option>
3434
</select>
35+
<p class="error" th:errors="${event.eventCategory}"></p>
3536
</label>
3637
</div>
3738

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h1>All Events</h1>
1717
<th>ID</th>
1818
<th>Name</th>
1919
<th>Description</th>
20-
<th>Type</th>
20+
<th>Category</th>
2121
<th>Location</th>
2222
<th>Contact Email</th>
2323
</tr>
@@ -26,7 +26,7 @@ <h1>All Events</h1>
2626
<td th:text="${event.id}"></td>
2727
<td th:text="${event.name}"></td>
2828
<td th:text="${event.description}"></td>
29-
<td th:text="${event.type.displayName}"></td>
29+
<td th:text="${event.eventCategory.name}"></td>
3030
<td th:text="${event.location}"></td>
3131
<td th:text="${event.contactEmail}"></td>
3232
</tr>

src/main/resources/templates/fragments.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<body>
1313

1414
<nav th:fragment="navigation">
15-
<h1 th:text="${title}">Coding Events</h1>
1615
<ul class="nav">
1716
<li class="nav-item"><a class="nav-link" href="/events">All Events</a></li>
1817
<li class="nav-item"><a class="nav-link" href="/events/create">Create Event</a></li>

0 commit comments

Comments
 (0)