File tree Expand file tree Collapse file tree
Spring Boot/codingevents/src/main/java/org/launchcode/codingevents Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package org .launchcode .codingevents .controllers ;
22
3+ import org .launchcode .codingevents .data .EventData ;
34import org .launchcode .codingevents .models .Event ;
45import org .springframework .stereotype .Controller ;
56import org .springframework .web .bind .annotation .GetMapping ;
1516@ RequestMapping ("events" )
1617public class EventController {
1718
18- private static List <Event > events = new ArrayList <>();
19-
2019 @ GetMapping
2120 public String displayAllEvents (Model model ) {
2221 model .addAttribute ("title" , "All Events" );
23- model .addAttribute ("events" , events );
22+ model .addAttribute ("events" , EventData . getAll () );
2423 return "events/index" ;
2524 }
2625
@@ -34,7 +33,7 @@ public String displayCreateEventForm(Model model) {
3433 // lives at /events/create
3534 @ PostMapping ("create" )
3635 public String processCreateEventForm (@ RequestParam String eventName , @ RequestParam String eventDescription ) {
37- events .add (new Event (eventName , eventDescription ));
36+ EventData .add (new Event (eventName , eventDescription ));
3837 return "redirect:/events" ;
3938 }
4039
Original file line number Diff line number Diff line change 1+ package org .launchcode .codingevents .data ;
2+
3+ import org .launchcode .codingevents .models .Event ;
4+
5+ import java .util .Collection ;
6+ import java .util .Collections ;
7+ import java .util .HashMap ;
8+ import java .util .Map ;
9+
10+ public class EventData {
11+
12+ private static final Map <Integer , Event > events = new HashMap <>();
13+
14+ public static Collection <Event > getAll () {
15+ return events .values ();
16+ }
17+
18+ public static Event getById (int id ) {
19+ return events .get (id );
20+ }
21+
22+ public static void add (Event event ) {
23+ events .put (event .getId (), event );
24+ }
25+
26+ public static void remove (int id ) {
27+ events .remove (id );
28+ }
29+
30+ }
You can’t perform that action at this time.
0 commit comments