Skip to content

Display errors #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: display-errors
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'mysql:mysql-connector-java'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.launchcode.codingevents.controllers;

import org.launchcode.codingevents.data.EventData;
import org.launchcode.codingevents.data.EventRepository;
import org.launchcode.codingevents.models.Event;
import org.launchcode.codingevents.models.EventType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
Expand All @@ -16,17 +18,23 @@
@RequestMapping("events")
public class EventController {

@Autowired
private EventRepository eventRepository;

//findAll, save, findById

@GetMapping
public String displayAllEvents(Model model) {
model.addAttribute("title", "All Events");
model.addAttribute("events", EventData.getAll());
model.addAttribute("events", eventRepository.findAll());
return "events/index";
}

@GetMapping("create")
public String displayCreateEventForm(Model model) {
model.addAttribute("title", "Create Event");
model.addAttribute(new Event());
model.addAttribute("types", EventType.values());
return "events/create";
}

Expand All @@ -35,17 +43,19 @@ public String processCreateEventForm(@ModelAttribute @Valid Event newEvent,
Errors errors, Model model) {
if(errors.hasErrors()) {
model.addAttribute("title", "Create Event");
model.addAttribute("types", EventType.values());

return "events/create";
}

EventData.add(newEvent);
eventRepository.save(newEvent);
return "redirect:";
}

@GetMapping("delete")
public String displayDeleteEventForm(Model model) {
model.addAttribute("title", "Delete Events");
model.addAttribute("events", EventData.getAll());
model.addAttribute("events", eventRepository.findAll());
return "events/delete";
}

Expand All @@ -54,7 +64,7 @@ public String processDeleteEventsForm(@RequestParam(required = false) int[] even

if (eventIds != null) {
for (int id : eventIds) {
EventData.remove(id);
eventRepository.deleteById(id);
}
}

Expand Down
32 changes: 0 additions & 32 deletions src/main/java/org/launchcode/codingevents/data/EventData.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.launchcode.codingevents.data;


import org.launchcode.codingevents.models.Event;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

//Naming convention for all data layer classes
//use the name of the class we want to store "Event" and "Repository" -- Should be an interface
@Repository
public interface EventRepository extends CrudRepository<Event, Integer> {


}
//Extending the CrudRepository interface gives us access to methods to
// perform all of the CRUD operations that we made happen in SQL
//CrudRepository methods to get info in and out of the tables of our relational database
37 changes: 27 additions & 10 deletions src/main/java/org/launchcode/codingevents/models/Event.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package org.launchcode.codingevents.models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Objects;

/**
* Created by Chris Bay
*/

@Entity
//this tag allows us to make this class a "persistent class" or an entity class
public class Event {

@Id
//This is a primary key
//@Id is an annotation that denotes
// that an integer id field is to be used as an id in the corresponding table in the database.
@GeneratedValue
//Wants the database to generate the primary key for us
private int id;
private static int nextId = 1;

@NotBlank(message = "Name is required")
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters")
Expand All @@ -24,18 +32,19 @@ public class Event {
@Email(message = "Invalid email. Try again.")
private String contactEmail;

public Event(String name, String description, String contactEmail) {
this();
private EventType type;

public Event(String name, String description, String contactEmail, EventType type) {
this.name = name;
this.description = description;
this.contactEmail = contactEmail;
}
this.type = type;

public Event() {
this.id = nextId;
nextId++;
}

public Event() {}
//always need an empty constructor in an empty class

public String getName() {
return name;
}
Expand Down Expand Up @@ -64,6 +73,14 @@ public int getId() {
return id;
}

public EventType getType() {
return type;
}

public void setType(EventType type) {
this.type = type;
}

@Override
public String toString() {
return name;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/launchcode/codingevents/models/EventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.launchcode.codingevents.models;

public enum EventType {

CONFERENCE("Conference"),
MEETUP ("Meetup"),
WORKSHOP("Workshop"),
SOCIAL("Social");

private final String displayName;

EventType(String displayName) {
this.displayName = displayName;
}

public String getDisplayName() {
return displayName;
}
}
16 changes: 16 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Database connection settings
spring.datasource.url=jdbc:mysql://localhost:3306/coding_events
spring.datasource.username=coding_events
spring.datasource.password=Learn2code!

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

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

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
12 changes: 12 additions & 0 deletions src/main/resources/templates/events/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
</label>
<p class="error" th:errors="${event.contactEmail}"></p>
</div>
<div class="form-group">
<label>Type
<select th:field="${event.type}">
<option th:each="type : ${types}"
th:value="${type}"
th:text="${type.displayName}">

</option>
</select>
</label>
</div>

<div class="form-group">
<input type="submit" value="Create" class="btn btn-success">
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/templates/events/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
<th>Name</th>
<th>Description</th>
<th>Contact Email</th>
<th>Type</th>
</tr>
</thead>
<tr th:each="event : ${events}">
<td th:text="${event.id}"></td>
<td th:text="${event.name}"></td>
<td th:text="${event.description}"></td>
<td th:text="${event.contactEmail}"></td>
<td th:text="${event.type.displayName}"></td>
<td>
<a th:href="@{/events/edit/{id}(id=${event.id})}">Edit</a>
</td>
</tr>
</table>

Expand Down