11package org .launchcode .codingevents .controllers ;
22
33import jakarta .validation .Valid ;
4- import org .launchcode .codingevents .data .EventData ;
4+ import org .launchcode .codingevents .data .EventCategoryRepository ;
55import org .launchcode .codingevents .data .EventRepository ;
66import org .launchcode .codingevents .models .Event ;
7- import org .launchcode .codingevents .models .EventType ;
7+ import org .launchcode .codingevents .models .EventCategory ;
8+ import org .launchcode .codingevents .models .dto .EventTagDTO ;
89import org .springframework .beans .factory .annotation .Autowired ;
910import org .springframework .stereotype .Controller ;
10- import org .springframework .validation .Errors ;
1111import org .springframework .web .bind .annotation .*;
1212import org .springframework .ui .Model ;
13+ import org .springframework .validation .Errors ;
1314
14- import java .util .ArrayList ;
15- import java .util .List ;
15+ import java .util .Optional ;
1616
1717/**
1818 * Created by Chris Bay
@@ -24,51 +24,52 @@ public class EventController {
2424 @ Autowired
2525 private EventRepository eventRepository ;
2626
27+ @ Autowired
28+ private EventCategoryRepository eventCategoryRepository ;
29+
2730 @ GetMapping
28- public String displayAllEvents (Model model ) {
29- model .addAttribute ("title" , "All Events" );
30- model .addAttribute ("events" , eventRepository .findAll ());
31+ public String displayEvents (@ RequestParam (required = false ) Integer categoryId , Model model ) {
32+
33+ if (categoryId == null ) {
34+ model .addAttribute ("title" , "All Events" );
35+ model .addAttribute ("events" , eventRepository .findAll ());
36+ } else {
37+ Optional <EventCategory > result = eventCategoryRepository .findById (categoryId );
38+ if (result .isEmpty ()) {
39+ model .addAttribute ("title" , "Invalid Category ID: " + categoryId );
40+ } else {
41+ EventCategory category = result .get ();
42+ model .addAttribute ("title" , "Events in category: " + category .getName ());
43+ model .addAttribute ("events" , category .getEvents ());
44+ }
45+ }
46+
3147 return "events/index" ;
3248 }
3349
3450 @ GetMapping ("create" )
3551 public String displayCreateEventForm (Model model ) {
3652 model .addAttribute ("title" , "Create Event" );
3753 model .addAttribute (new Event ());
38- model .addAttribute ("types " , EventType . values ());
54+ model .addAttribute ("categories " , eventCategoryRepository . findAll ());
3955 return "events/create" ;
4056 }
4157
4258 @ PostMapping ("create" )
43- public String processCreateEventForm (@ ModelAttribute @ Valid Event newEvent , Errors errors , Model model ) {
59+ public String processCreateEventForm (@ ModelAttribute @ Valid Event newEvent ,
60+ Errors errors , Model model ) {
4461 if (errors .hasErrors ()) {
4562 model .addAttribute ("title" , "Create Event" );
4663 return "events/create" ;
4764 }
48- eventRepository .save (newEvent );
49- return "redirect:/events" ;
50- }
5165
52- @ GetMapping ("edit/{eventId}" )
53- public String displayEditForm (Model model , @ PathVariable int eventId ){
54- Event eventToEdit = EventData .getById (eventId );
55- model .addAttribute ("event" , eventToEdit );
56- String title = "Edit Event " + eventToEdit .getName () + " (id=" + eventToEdit .getId () + ")" ;
57- model .addAttribute ("title" , title );
58- return "events/edit" ;
59- }
60-
61- @ PostMapping ("edit" )
62- public String processEditForm (int eventId , String name , String description ) {
63- Event eventToEdit = EventData .getById (eventId );
64- eventToEdit .setName (name );
65- eventToEdit .setDescription (description );
66+ eventRepository .save (newEvent );
6667 return "redirect:/events" ;
6768 }
6869
6970 @ GetMapping ("delete" )
70- public String renderDeleteEventForm (Model model ) {
71- model .addAttribute ("title" , "Delete Event " );
71+ public String displayDeleteEventForm (Model model ) {
72+ model .addAttribute ("title" , "Delete Events " );
7273 model .addAttribute ("events" , eventRepository .findAll ());
7374 return "events/delete" ;
7475 }
@@ -82,7 +83,35 @@ public String processDeleteEventsForm(@RequestParam(required = false) int[] even
8283 }
8384 }
8485
85- return "redirect:" ;
86+ return "redirect:/events" ;
87+ }
88+
89+ @ GetMapping ("detail" )
90+ public String displayEventDetails (@ RequestParam Integer eventId , Model model ) {
91+
92+ Optional <Event > result = eventRepository .findById (eventId );
93+
94+ if (result .isEmpty ()) {
95+ model .addAttribute ("title" , "Invalid Event ID: " + eventId );
96+ } else {
97+ Event event = result .get ();
98+ model .addAttribute ("title" , event .getName () + " Details" );
99+ model .addAttribute ("event" , event );
100+ }
101+
102+ return "events/detail" ;
103+ }
104+ @ GetMapping ("add-tag" )
105+ public String displayAddTagForm (@ RequestParam Integer eventId , Model model ){
106+ Optional <Event > result = eventRepository .findById (eventId );
107+ Event event = result .get ();
108+ model .addAttribute ("title" , "Add Tag to: " + event .getName ());
109+ model .addAttribute ("tags" , tagRepository .findAll ());
110+ EventTagDTO eventTag = new EventTagDTO ();
111+ eventTag .setEvent (event );
112+ model .addAttribute ("eventTag" , eventTag );
113+ return "events/add-tag.html" ;
86114 }
87115
116+
88117}
0 commit comments