11package org .launchcode .codingevents .controllers ;
22
3+ import org .launchcode .codingevents .data .EventData ;
34import org .launchcode .codingevents .models .Event ;
45import org .springframework .stereotype .Controller ;
5- import org .springframework .web .bind .annotation .GetMapping ;
6- import org .springframework .web .bind .annotation .PostMapping ;
7- import org .springframework .web .bind .annotation .RequestMapping ;
6+ import org .springframework .web .bind .annotation .*;
87import org .springframework .ui .Model ;
9- import org .springframework .web .bind .annotation .RequestParam ;
108
119import java .util .ArrayList ;
1210import java .util .List ;
1816@ RequestMapping ("events" )
1917public class EventController {
2018
21- private static List <Event > events = new ArrayList <>();
22-
23- @ GetMapping
19+ @ GetMapping
2420 public String displayAllEvents (Model model ) {
2521 model .addAttribute ("title" , "All Events" );
26- model .addAttribute ("events" , events );
22+ model .addAttribute ("events" , EventData . getAll () );
2723 return "events/index" ;
2824 }
2925
@@ -34,8 +30,27 @@ public String displayCreateEventForm(Model model) {
3430 }
3531
3632 @ PostMapping ("create" )
37- public String processCreateEventForm (@ RequestParam String eventName , @ RequestParam String eventDescription ) {
38- events .add (new Event (eventName , eventDescription ));
33+ public String processCreateEventForm (@ ModelAttribute Event newEvent ) {
34+ EventData .add (newEvent );
35+ return "redirect:" ;
36+ }
37+
38+ @ GetMapping ("delete" )
39+ public String renderDeleteEventForm (Model model ) {
40+ model .addAttribute ("title" , "Delete Event" );
41+ model .addAttribute ("events" , EventData .getAll ());
42+ return "events/delete" ;
43+ }
44+
45+ @ PostMapping ("delete" )
46+ public String processDeleteEventsForm (@ RequestParam (required = false ) int [] eventIds ) {
47+
48+ if (eventIds != null ) {
49+ for (int id : eventIds ) {
50+ EventData .remove (id );
51+ }
52+ }
53+
3954 return "redirect:" ;
4055 }
4156
0 commit comments