22
33import jakarta .validation .Valid ;
44import org .launchcode .codingevents .data .EventData ;
5+ import org .launchcode .codingevents .data .EventRepository ;
56import org .launchcode .codingevents .models .Event ;
67import org .launchcode .codingevents .models .EventType ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
79import org .springframework .stereotype .Controller ;
810import org .springframework .validation .Errors ;
911import org .springframework .web .bind .annotation .*;
1921@ RequestMapping ("events" )
2022public 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
0 commit comments