11package org .launchcode .codingevents .controllers ;
22
33import jakarta .validation .Valid ;
4- import org .launchcode .codingevents .data .EventData ;
4+ import org .launchcode .codingevents .data .EventRepository ;
55import org .launchcode .codingevents .models .Event ;
66import org .launchcode .codingevents .models .EventType ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
78import org .springframework .stereotype .Controller ;
89import org .springframework .web .bind .annotation .*;
910import org .springframework .ui .Model ;
1011import org .springframework .validation .Errors ;
1112
12- import java .util .ArrayList ;
13- import java .util .List ;
14-
1513/**
1614 * Created by Chris Bay
1715 */
1816@ Controller
1917@ RequestMapping ("events" )
2018public class EventController {
19+ @ Autowired
20+ private EventRepository eventRepository ;
2121
2222 @ GetMapping
2323 public String displayAllEvents (Model model ) {
2424 model .addAttribute ("title" , "All Events" );
25- model .addAttribute ("events" , EventData . getAll ());
25+ model .addAttribute ("events" , eventRepository . findAll ());
2626 return "events/index" ;
2727 }
2828
@@ -42,14 +42,14 @@ public String processCreateEventForm(@ModelAttribute @Valid Event newEvent,
4242 return "events/create" ;
4343 }
4444
45- EventData . add (newEvent );
45+ eventRepository . save (newEvent );
4646 return "redirect:/events" ;
4747 }
4848
4949 @ GetMapping ("delete" )
5050 public String displayDeleteEventForm (Model model ) {
5151 model .addAttribute ("title" , "Delete Events" );
52- model .addAttribute ("events" , EventData . getAll ());
52+ model .addAttribute ("events" , eventRepository . findAll ());
5353 return "events/delete" ;
5454 }
5555
@@ -58,7 +58,7 @@ public String processDeleteEventsForm(@RequestParam(required = false) int[] even
5858
5959 if (eventIds != null ) {
6060 for (int id : eventIds ) {
61- EventData . remove (id );
61+ eventRepository . deleteById (id );
6262 }
6363 }
6464
0 commit comments