Skip to content
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

17.using persistence in a controller #49

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'org.springframework.boot' version '2.4.4'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
Expand All @@ -22,6 +22,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'mysql:mysql-connector-java'
implementation 'org.springframework.boot:spring-boot-starter-validation'
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
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.launchcode.codingevents.controllers;

import org.launchcode.codingevents.data.EventData;
import org.launchcode.codingevents.models.Event;
import org.launchcode.codingevents.models.EventType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;

/**
* Created by Chris Bay
Expand All @@ -17,24 +17,49 @@
@RequestMapping("events")
public class EventController {

private static List<String> events = new ArrayList<>();

@GetMapping
public String displayAllEvents(Model model) {
model.addAttribute("title", "All Events");
model.addAttribute("events", events);
model.addAttribute("events", EventData.getAll());
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";
}

@PostMapping("create")
public String processCreateEventForm(@RequestParam String eventName) {
events.add(eventName);
public String processCreateEventForm(@ModelAttribute @Valid Event newEvent,
Errors errors, Model model) {
if(errors.hasErrors()) {
model.addAttribute("title", "Create Event");
return "events/create";
}

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

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

@PostMapping("delete")
public String processDeleteEventsForm(@RequestParam(required = false) int[] eventIds) {

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

return "redirect:";
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/launchcode/codingevents/data/EventData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.launchcode.codingevents.data;

import org.launchcode.codingevents.models.Event;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* Created by Chris Bay
*/
public class EventData {

private static final Map<Integer, Event> events = new HashMap<>();

public static Collection<Event> getAll() {
return events.values();
}

public static Event getById(int id) {
return events.get(id);
}

public static void add(Event event) {
events.put(event.getId(), event);
}

public static void remove(int id) {
events.remove(id);
}

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

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

/**
* Created by Chris Bay
*/
@Repository
public interface EventRepository extends CrudRepository<Event, Integer> {
}
96 changes: 96 additions & 0 deletions src/main/java/org/launchcode/codingevents/models/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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
public class Event {

@Id
@GeneratedValue
private int id;

@NotBlank(message = "Name is required")
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters")
private String name;

@Size(max = 500, message = "Description too long!")
private String description;

@NotBlank(message = "Email is required")
@Email(message = "Invalid email. Try again.")
private String contactEmail;

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() {}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getContactEmail() {
return contactEmail;
}

public void setContactEmail(String contactEmail) {
this.contactEmail = contactEmail;
}

public EventType getType() {
return type;
}

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

public int getId() {
return id;
}

@Override
public String toString() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Event event = (Event) o;
return id == event.id;
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
22 changes: 22 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,22 @@
package org.launchcode.codingevents.models;

/**
* Created by Chris Bay
*/
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 @@
spring.datasource.url=jdbc:mysql://localhost:3306/coding_events_demo
spring.datasource.username=coding_events_demo
spring.datasource.password=Learn2code!

# Specify the DBMS
spring.jpa.database = MYSQL

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

# 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

5 changes: 4 additions & 1 deletion src/main/resources/static/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
body {
font-size: 18px;
font-size: 14px;
}
.error {
color: red;
}
35 changes: 32 additions & 3 deletions src/main/resources/templates/events/create.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body>
<body class="container">

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

<form method="post">
<input type="text" name="eventName">
<input type="submit" value="Create">
<div class="form-group">
<label>Name
<input th:field="${event.name}" class="form-control">
</label>
<p class="error" th:errors="${event.name}"></p>
</div>
<div class="form-group">
<label>Description
<input th:field="${event.description}" class="form-control">
</label>
<p class="error" th:errors="${event.description}"></p>
</div>
<div class="form-group">
<label>Contact Email
<input th:field="${event.contactEmail}" class="form-control">
</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>
</form>

</body>
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/templates/events/delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body class="container">

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

<form method="post">

<th:block th:each="event : ${events}">
<div class="form-group">
<label>
<span th:text="${event.name}"></span>
<input type="checkbox" name="eventIds" th:value="${event.id}" class="form-control">
</label>
</div>
</th:block>

<input type="submit" value="Delete" class="btn btn-danger">
</form>

</body>
</html>
25 changes: 19 additions & 6 deletions src/main/resources/templates/events/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body>
<body class="container">

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

<ul>
<th:block th:each="event : ${events}">
<li th:text="${event}"></li>
</th:block>
</ul>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<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>
</tr>
</table>

</body>
</html>
Loading