Skip to content

Commit 3ebbfa6

Browse files
committed
AbstractEntity studio
1 parent 8219dea commit 3ebbfa6

17 files changed

Lines changed: 473 additions & 32 deletions

File tree

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ repositories {
1717

1818
dependencies {
1919
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
20+
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
21+
implementation 'mysql:mysql-connector-java:8.0.32'
2022
implementation 'org.springframework.boot:spring-boot-starter-validation'
2123
implementation 'org.springframework.boot:spring-boot-starter-web'
2224
developmentOnly 'org.springframework.boot:spring-boot-devtools'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.launchcode.codingevents.controllers;
2+
3+
import jakarta.validation.Valid;
4+
import org.launchcode.codingevents.data.EventCategoryRepository;
5+
import org.launchcode.codingevents.models.EventCategory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.validation.Errors;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.ModelAttribute;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.ui.Model;
14+
@Controller
15+
@RequestMapping("eventCategories")
16+
public class EventCategoryController {
17+
18+
@Autowired
19+
private EventCategoryRepository eventCategoryRepository;
20+
@GetMapping
21+
public String displayAllCategories(Model model) {
22+
model.addAttribute("title", "All Categories");
23+
model.addAttribute("categories", eventCategoryRepository.findAll());
24+
return "eventCategories/index";
25+
}
26+
27+
@GetMapping("create")
28+
public String renderCreateEventCategoryForm(Model model) {
29+
model.addAttribute("title", "Create Category");
30+
model.addAttribute(new EventCategory());
31+
return "eventCategories/create";
32+
}
33+
34+
@PostMapping("create")
35+
public String processCreateEventCategoryForm(@Valid @ModelAttribute EventCategory eventCategory, Errors errors, Model model) {
36+
37+
if (errors.hasErrors()) {
38+
model.addAttribute("title", "Create Category");
39+
model.addAttribute(new EventCategory());
40+
return "eventCategories/create";
41+
}
42+
43+
eventCategoryRepository.save(eventCategory);
44+
return "redirect:/eventCategories";
45+
}
46+
}
47+
48+
Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,68 @@
11
package org.launchcode.codingevents.controllers;
22

3+
import jakarta.validation.Valid;
4+
import org.launchcode.codingevents.data.EventRepository;
5+
import org.launchcode.codingevents.models.Event;
6+
import org.launchcode.codingevents.models.EventType;
7+
import org.springframework.beans.factory.annotation.Autowired;
38
import org.springframework.stereotype.Controller;
4-
import org.springframework.web.bind.annotation.GetMapping;
5-
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.*;
610
import org.springframework.ui.Model;
7-
8-
import java.util.ArrayList;
9-
import java.util.List;
11+
import org.springframework.validation.Errors;
1012

1113
/**
1214
* Created by Chris Bay
1315
*/
1416
@Controller
1517
@RequestMapping("events")
1618
public class EventController {
19+
@Autowired
20+
private EventRepository eventRepository;
1721

1822
@GetMapping
1923
public String displayAllEvents(Model model) {
20-
List<String> events = new ArrayList<>();
21-
events.add("Code With Pride");
22-
events.add("Strange Loop");
23-
events.add("Apple WWDC");
24-
events.add("SpringOne Platform");
25-
model.addAttribute("events", events);
24+
model.addAttribute("title", "All Events");
25+
model.addAttribute("events", eventRepository.findAll());
2626
return "events/index";
2727
}
2828

29+
@GetMapping("create")
30+
public String displayCreateEventForm(Model model) {
31+
model.addAttribute("title", "Create Event");
32+
model.addAttribute(new Event());
33+
model.addAttribute("types", EventType.values());
34+
return "events/create";
35+
}
36+
37+
@PostMapping("create")
38+
public String processCreateEventForm(@ModelAttribute @Valid Event newEvent,
39+
Errors errors, Model model) {
40+
if(errors.hasErrors()) {
41+
model.addAttribute("title", "Create Event");
42+
return "events/create";
43+
}
44+
45+
eventRepository.save(newEvent);
46+
return "redirect:/events";
47+
}
48+
49+
@GetMapping("delete")
50+
public String displayDeleteEventForm(Model model) {
51+
model.addAttribute("title", "Delete Events");
52+
model.addAttribute("events", eventRepository.findAll());
53+
return "events/delete";
54+
}
55+
56+
@PostMapping("delete")
57+
public String processDeleteEventsForm(@RequestParam(required = false) int[] eventIds) {
58+
59+
if (eventIds != null) {
60+
for (int id : eventIds) {
61+
eventRepository.deleteById(id);
62+
}
63+
}
64+
65+
return "redirect:/events";
66+
}
67+
2968
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.launchcode.codingevents.data;
2+
3+
import org.launchcode.codingevents.models.EventCategory;
4+
import org.springframework.data.repository.CrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface EventCategoryRepository extends CrudRepository<EventCategory, Integer> {
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.launchcode.codingevents.models;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.MappedSuperclass;
7+
import jakarta.validation.constraints.Email;
8+
import jakarta.validation.constraints.NotBlank;
9+
import jakarta.validation.constraints.Size;
10+
11+
import java.util.Objects;
12+
13+
/**
14+
* Created by Chris Bay
15+
*/
16+
@MappedSuperclass
17+
public abstract class AbstractEntity {
18+
19+
@Id
20+
@GeneratedValue
21+
private int id;
22+
23+
public AbstractEntity() {}
24+
25+
26+
27+
public int getId() {
28+
return id;
29+
}
30+
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) return true;
35+
if (o == null || getClass() != o.getClass()) return false;
36+
AbstractEntity entity = (AbstractEntity) o;
37+
return id == entity.id;
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return Objects.hash(id);
43+
}
44+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.launchcode.codingevents.models;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.validation.constraints.Email;
7+
import jakarta.validation.constraints.NotBlank;
8+
import jakarta.validation.constraints.Size;
9+
10+
import java.util.Objects;
11+
12+
/**
13+
* Created by Chris Bay
14+
*/
15+
@Entity
16+
public class Event extends AbstractEntity {
17+
18+
19+
@NotBlank(message = "Name is required")
20+
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters")
21+
private String name;
22+
23+
@Size(max = 500, message = "Description too long!")
24+
private String description;
25+
26+
@NotBlank(message = "Email is required")
27+
@Email(message = "Invalid email. Try again.")
28+
private String contactEmail;
29+
30+
private EventType type;
31+
32+
public Event(String name, String description, String contactEmail, EventType type) {
33+
this.name = name;
34+
this.description = description;
35+
this.contactEmail = contactEmail;
36+
this.type = type;
37+
}
38+
39+
public Event() {}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public String getDescription() {
50+
return description;
51+
}
52+
53+
public void setDescription(String description) {
54+
this.description = description;
55+
}
56+
57+
public String getContactEmail() {
58+
return contactEmail;
59+
}
60+
61+
public void setContactEmail(String contactEmail) {
62+
this.contactEmail = contactEmail;
63+
}
64+
65+
public EventType getType() {
66+
return type;
67+
}
68+
69+
public void setType(EventType type) {
70+
this.type = type;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return name;
76+
}
77+
78+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.launchcode.codingevents.models;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.validation.constraints.Size;
7+
8+
import java.util.Objects;
9+
10+
@Entity
11+
public class EventCategory extends AbstractEntity {
12+
13+
14+
@Size(min=3, message="Name must be at least 3 characters long")
15+
private String name;
16+
17+
public EventCategory(@Size(min = 3, message = "Name must be at least 3 characters long") String name) {
18+
this.name = name;
19+
}
20+
21+
public EventCategory() {}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
32+
@Override
33+
public String toString() {
34+
return name;
35+
}
36+
37+
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.launchcode.codingevents.models;
2+
3+
/**
4+
* Created by Chris Bay
5+
*/
6+
public enum EventType {
7+
8+
CONFERENCE("Conference"),
9+
MEETUP("Meetup"),
10+
WORKSHOP("Workshop"),
11+
SOCIAL("Social");
12+
13+
private final String displayName;
14+
15+
EventType(String displayName) {
16+
this.displayName = displayName;
17+
}
18+
19+
public String getDisplayName() {
20+
return displayName;
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
11

2+
# Database connection settings
3+
spring.datasource.url=jdbc:mysql://localhost:3306/codingevents
4+
spring.datasource.username=bwulf
5+
spring.datasource.password=Launchcode
6+
7+
# Specify the DBMS
8+
spring.jpa.database = MYSQL
9+
10+
# Show or not log for each sql query
11+
spring.jpa.show-sql = false
12+
13+
# Hibernate ddl auto (create, create-drop, update)
14+
spring.jpa.hibernate.ddl-auto = update
15+
16+
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
17+
# stripped before adding them to the entity manager)
18+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect

0 commit comments

Comments
 (0)