Skip to content

Commit 8e95594

Browse files
committed
connected to SQL
1 parent 0986c52 commit 8e95594

5 files changed

Lines changed: 45 additions & 8 deletions

File tree

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ dependencies {
1919
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
2020
implementation 'org.springframework.boot:spring-boot-starter-web'
2121
implementation 'org.springframework.boot:spring-boot-starter-validation'
22+
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
23+
implementation 'mysql:mysql-connector-java:8.0.32'
2224
developmentOnly 'org.springframework.boot:spring-boot-devtools'
2325
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2426
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import jakarta.validation.Valid;
44
import org.launchcode.codingevents.data.EventData;
5+
import org.launchcode.codingevents.data.EventRepository;
56
import org.launchcode.codingevents.models.Event;
67
import org.launchcode.codingevents.models.EventType;
8+
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.stereotype.Controller;
810
import org.springframework.validation.Errors;
911
import org.springframework.web.bind.annotation.*;
@@ -19,10 +21,13 @@
1921
@RequestMapping("events")
2022
public class EventController {
2123

24+
@Autowired
25+
private EventRepository eventRepository;
26+
2227
@GetMapping
2328
public String displayAllEvents(Model model) {
2429
model.addAttribute("title", "All Events");
25-
model.addAttribute("events", EventData.getAll());
30+
model.addAttribute("events", eventRepository.findAll());
2631
return "events/index";
2732
}
2833

@@ -40,7 +45,7 @@ public String processCreateEventForm(@ModelAttribute @Valid Event newEvent, Erro
4045
model.addAttribute("title", "Create Event");
4146
return "events/create";
4247
}
43-
EventData.add(newEvent);
48+
eventRepository.save(newEvent);
4449
return "redirect:/events";
4550
}
4651

@@ -64,7 +69,7 @@ public String processEditForm(int eventId, String name, String description) {
6469
@GetMapping("delete")
6570
public String renderDeleteEventForm(Model model) {
6671
model.addAttribute("title", "Delete Event");
67-
model.addAttribute("events", EventData.getAll());
72+
model.addAttribute("events", eventRepository.findAll());
6873
return "events/delete";
6974
}
7075

@@ -73,7 +78,7 @@ public String processDeleteEventsForm(@RequestParam(required = false) int[] even
7378

7479
if (eventIds != null) {
7580
for (int id : eventIds) {
76-
EventData.remove(id);
81+
eventRepository.deleteById(id);
7782
}
7883
}
7984

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.launchcode.codingevents.data;
2+
3+
import org.launchcode.codingevents.models.Event;
4+
import org.springframework.data.repository.CrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface EventRepository extends CrudRepository<Event, Integer> {
9+
10+
11+
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package org.launchcode.codingevents.models;
22

3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
36
import jakarta.validation.constraints.Email;
47
import jakarta.validation.constraints.NotBlank;
58
import jakarta.validation.constraints.Size;
69
import java.util.Objects;
710

11+
@Entity
812
public class Event {
913

14+
@Id
15+
@GeneratedValue
1016
private int id;
11-
private static int nextId = 1;
1217

1318
@NotBlank(message ="Name is required.")
1419
@Size(min = 3, max =50, message = "Name must be more than 3 characters and less than 50 characters.")
@@ -24,12 +29,10 @@ public class Event {
2429
private EventType type;
2530

2631
public Event() {
27-
this.id = nextId;
28-
nextId++;
2932
}
3033

3134
public Event(String name, String description, String contactEmail, EventType type) {
32-
this();
35+
3336
this.name = name;
3437
this.description = description;
3538
this.contactEmail = contactEmail;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1+
# Database connection settings
2+
spring.datasource.url=jdbc:mysql://localhost:3306/coding_events
3+
spring.datasource.username = coding_events
4+
spring.datasource.password = SQL8Java!
15

6+
# Specify the DBMS
7+
spring.jpa.database = MYSQL
8+
9+
# Show or not log for each sql query
10+
spring.jpa.show-sql = false
11+
12+
# Hibernate ddl auto (create, create-drop, update)
13+
spring.jpa.hibernate.ddl-auto = update
14+
15+
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
16+
# stripped before adding them to the entity manager)
17+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect

0 commit comments

Comments
 (0)