diff --git a/build.gradle b/build.gradle index c4abeaa7b..667ab51e7 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/org/launchcode/codingevents/controllers/EventController.java b/src/main/java/org/launchcode/codingevents/controllers/EventController.java index 08bdcab79..507d92d6e 100644 --- a/src/main/java/org/launchcode/codingevents/controllers/EventController.java +++ b/src/main/java/org/launchcode/codingevents/controllers/EventController.java @@ -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; @@ -16,10 +18,15 @@ @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"; } @@ -27,6 +34,7 @@ public String displayAllEvents(Model model) { public String displayCreateEventForm(Model model) { model.addAttribute("title", "Create Event"); model.addAttribute(new Event()); + model.addAttribute("types", EventType.values()); return "events/create"; } @@ -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"; } @@ -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); } } diff --git a/src/main/java/org/launchcode/codingevents/data/EventData.java b/src/main/java/org/launchcode/codingevents/data/EventData.java deleted file mode 100644 index 89438ee52..000000000 --- a/src/main/java/org/launchcode/codingevents/data/EventData.java +++ /dev/null @@ -1,32 +0,0 @@ -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 events = new HashMap<>(); - - public static Collection 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); - } - -} diff --git a/src/main/java/org/launchcode/codingevents/data/EventRepository.java b/src/main/java/org/launchcode/codingevents/data/EventRepository.java new file mode 100644 index 000000000..6626fbb93 --- /dev/null +++ b/src/main/java/org/launchcode/codingevents/data/EventRepository.java @@ -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 { + + +} +//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 \ No newline at end of file diff --git a/src/main/java/org/launchcode/codingevents/models/Event.java b/src/main/java/org/launchcode/codingevents/models/Event.java index dd1ad6a35..0c7eca68f 100644 --- a/src/main/java/org/launchcode/codingevents/models/Event.java +++ b/src/main/java/org/launchcode/codingevents/models/Event.java @@ -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") @@ -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; } @@ -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; diff --git a/src/main/java/org/launchcode/codingevents/models/EventType.java b/src/main/java/org/launchcode/codingevents/models/EventType.java new file mode 100644 index 000000000..21a4fa8c8 --- /dev/null +++ b/src/main/java/org/launchcode/codingevents/models/EventType.java @@ -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; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b1378917..15bfcf445 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 diff --git a/src/main/resources/templates/events/create.html b/src/main/resources/templates/events/create.html index e819af567..7d09fd3fe 100644 --- a/src/main/resources/templates/events/create.html +++ b/src/main/resources/templates/events/create.html @@ -24,6 +24,18 @@

+
+ +
+
diff --git a/src/main/resources/templates/events/index.html b/src/main/resources/templates/events/index.html index feceed35c..359a114c1 100644 --- a/src/main/resources/templates/events/index.html +++ b/src/main/resources/templates/events/index.html @@ -12,6 +12,7 @@ Name Description Contact Email + Type @@ -19,6 +20,10 @@ + + + Edit +