Skip to content

Commit cff69ac

Browse files
committed
"10/15/24"
1 parent 4086cca commit cff69ac

6 files changed

Lines changed: 142 additions & 4 deletions

File tree

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

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

3+
import jakarta.validation.Valid;
34
import org.launchcode.codingevents.data.EventData;
45
import org.launchcode.codingevents.models.Event;
56
import org.springframework.stereotype.Controller;
7+
import org.springframework.validation.Errors;
68
import org.springframework.web.bind.annotation.*;
79
import org.springframework.ui.Model;
810

@@ -26,11 +28,19 @@ public String displayAllEvents(Model model) {
2628
@GetMapping("create")
2729
public String displayCreateEventForm(Model model) {
2830
model.addAttribute("title", "Create Event");
31+
model.addAttribute(new Event());
2932
return "events/create";
3033
}
3134

3235
@PostMapping("create")
33-
public String processCreateEventForm(@ModelAttribute Event newEvent) {
36+
public String processCreateEventForm(@ModelAttribute @Valid Event newEvent,
37+
Errors errors,
38+
Model model) {
39+
if(errors.hasErrors()) {
40+
model.addAttribute("title", "Create Event");
41+
// model.addAttribute("errorMsg", "Bad data!");
42+
return "events/create";
43+
}
3444
EventData.add(newEvent);
3545
return "redirect:/events";
3646
}

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

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

3+
import jakarta.validation.constraints.*;
4+
5+
import java.time.LocalDate;
36
import java.util.Objects;
47

58
/**
@@ -10,16 +13,45 @@ public class Event {
1013
private int id;
1114
private static int nextId = 1;
1215

16+
@NotBlank(message = "Name is required.")
17+
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters")
1318
private String name;
19+
20+
@Size(max = 500, message = "Description too long!")
1421
private String description;
1522

16-
public Event(String name, String description) {
23+
@NotBlank(message = "Email is required.")
24+
@Email(message = "Invalid email. Try again.")
25+
private String contactEmail;
26+
27+
// Chapter Exercise Code Added
28+
@NotBlank(message = "Location is required.")
29+
@NotNull
30+
private String location;
31+
32+
@AssertTrue(message = "Registration must be marked 'True'.")
33+
private Boolean registrationRequired = true;
34+
35+
@Positive(message = "Attendees must be greater than 0.")
36+
private int numberOfAttendees;
37+
38+
@Future(message = "Event must have a future date.")
39+
private LocalDate eventDate;
40+
41+
public Event(String name, String description, String contactEmail,
42+
String location, int numberOfAttendees, LocalDate eventDate) {
1743
this.name = name;
1844
this.description = description;
45+
this.contactEmail = contactEmail;
46+
this.location = location;
47+
this.numberOfAttendees = numberOfAttendees;
48+
this.eventDate = eventDate;
1949
this.id = nextId;
2050
nextId++;
2151
}
2252

53+
public Event(){}
54+
2355
public String getName() {
2456
return name;
2557
}
@@ -36,10 +68,50 @@ public void setDescription(String description) {
3668
this.description = description;
3769
}
3870

71+
public String getContactEmail() {
72+
return contactEmail;
73+
}
74+
75+
public void setContactEmail(String contactEmail) {
76+
this.contactEmail = contactEmail;
77+
}
78+
79+
public String getLocation() {
80+
return location;
81+
}
82+
83+
public void setLocation (String location) {
84+
this.location = location;
85+
}
86+
3987
public int getId() {
4088
return id;
4189
}
4290

91+
public Boolean getRegistrationRequired() {
92+
return registrationRequired;
93+
}
94+
95+
public void setRegistrationRequired(Boolean registrationRequired) {
96+
this.registrationRequired = registrationRequired;
97+
}
98+
99+
public int getNumberOfAttendees() {
100+
return numberOfAttendees;
101+
}
102+
103+
public void setNumberOfAttendees(int numberOfAttendees) {
104+
this.numberOfAttendees = numberOfAttendees;
105+
}
106+
107+
public LocalDate getEventDate() {
108+
return eventDate;
109+
}
110+
111+
public void setEventDate(LocalDate eventDate) {
112+
this.eventDate = eventDate;
113+
}
114+
43115
@Override
44116
public String toString() {
45117
return name;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
body {
22
font-size: 18px;
3+
}
4+
.error {
5+
color: red;
36
}

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,62 @@
55

66
<header th:replace="fragments :: header"></header>
77

8+
<!--<p th:text="${errorMsg}" style="color:red;"></p>-->
9+
810
<form method="post">
911
<div class="form-group">
1012
<label>Name
11-
<input type="text" name="name" class="form-control">
13+
<!-- <input type="text" name="name" class="form-control">-->
14+
<input th:field="${event.name}" class="form-control">
1215
</label>
16+
<p class="error" th:errors="${event.name}"></p>
1317
</div>
1418
<div class="form-group">
1519
<label>Description
16-
<input type="text" name="description" class="form-control">
20+
<!-- <input type="text" name="description" class="form-control">-->
21+
<input th:field="${event.description}" class="form-control">
1722
</label>
23+
<p class="error" th:errors="${event.description}"></p>
24+
</div>
25+
<div class="form-group">
26+
<label>Contact Email
27+
<!-- <input type="text" name="contactEmail" class="form-control">-->
28+
<input th:field="${event.contactEmail}" class="form-control">
29+
</label>
30+
<p class="error" th:errors="${event.contactEmail}"></p>
31+
</div>
32+
<div class="form-group">
33+
<label>Location
34+
<!-- <input type="text" name="contactEmail" class="form-control">-->
35+
<input th:field="${event.location}" class="form-control">
36+
</label>
37+
<p class="error" th:errors="${event.location}"></p>
38+
</div>
39+
<div class="form-group">
40+
<label>Registration Required
41+
<!-- <input type="text" name="contactEmail" class="form-control">-->
42+
<input th:field="${event.registrationRequired}" class="form-control">
43+
</label>
44+
<p class="error" th:errors="${event.registrationRequired}"></p>
45+
</div>
46+
<div class="form-group">
47+
<label>Number of Attendees
48+
<!-- <input type="text" name="contactEmail" class="form-control">-->
49+
<input th:field="${event.numberOfAttendees}" class="form-control">
50+
</label>
51+
<p class="error" th:errors="${event.numberOfAttendees}"></p>
52+
</div>
53+
<div class="form-group">
54+
<label>Event Date
55+
<!-- <input type="text" name="contactEmail" class="form-control">-->
56+
<input th:field="${event.eventDate}" class="form-control">
57+
</label>
58+
<p class="error" th:errors="${event.eventDate}"></p>
1859
</div>
1960
<div class="form-group">
2061
<input type="submit" value="Create" class="btn btn-success">
2162
</div>
63+
2264
</form>
2365

2466
</body>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@
1111
<th>ID</th>
1212
<th>Name</th>
1313
<th>Description</th>
14+
<th>Contact Email</th>
15+
<th>Location</th>
16+
<th>Registration Required</th>
17+
<th>Number of Attendees</th>
18+
<th>Event Date</th>
1419
</tr>
1520
</thead>
1621
<tr th:each="event : ${events}">
1722
<td th:text="${event.id}"></td>
1823
<td th:text="${event.name}"></td>
1924
<td th:text="${event.description}"></td>
25+
<td th:text="${event.contactEmail}"></td>
26+
<td th:text="${event.location}"></td>
27+
<td th:text="${event.registrationRequired}"></td>
28+
<td th:text="${event.numberOfAttendees}"></td>
29+
<td th:text="${event.eventDate}"></td>
2030
</tr>
2131
</table>
2232

src/main/resources/templates/fragments.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>Coding Events</title>
66
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
77
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
8+
<link rel="stylesheet" th:href="@{/styles.css}">
89
</head>
910
<body>
1011

0 commit comments

Comments
 (0)