|
1 | 1 | package org.launchcode.codingevents.controllers; |
2 | 2 |
|
3 | | -import jakarta.validation.Valid; |
4 | | -import org.launchcode.codingevents.data.EventRepository; |
5 | | -import org.launchcode.codingevents.models.Event; |
6 | | -import org.launchcode.codingevents.models.EventType; |
7 | | -import org.springframework.beans.factory.annotation.Autowired; |
8 | 3 | import org.springframework.stereotype.Controller; |
9 | | -import org.springframework.web.bind.annotation.*; |
| 4 | +import org.springframework.web.bind.annotation.GetMapping; |
| 5 | +import org.springframework.web.bind.annotation.RequestMapping; |
10 | 6 | import org.springframework.ui.Model; |
11 | | -import org.springframework.validation.Errors; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
12 | 10 |
|
13 | 11 | /** |
14 | 12 | * Created by Chris Bay |
15 | 13 | */ |
16 | 14 | @Controller |
17 | 15 | @RequestMapping("events") |
18 | 16 | public class EventController { |
19 | | - @Autowired |
20 | | - private EventRepository eventRepository; |
21 | 17 |
|
22 | 18 | @GetMapping |
23 | 19 | public String displayAllEvents(Model model) { |
24 | | - model.addAttribute("title", "All Events"); |
25 | | - model.addAttribute("events", eventRepository.findAll()); |
| 20 | + List<String> events = new ArrayList<>(); |
| 21 | + events.add("Code With Pride"); |
| 22 | + events.add("Strange Loop"); |
| 23 | + events.add("Apple WWDC"); |
| 24 | + events.add("SpringOne Platform"); |
| 25 | + model.addAttribute("events", events); |
26 | 26 | return "events/index"; |
27 | 27 | } |
28 | 28 |
|
29 | | - @GetMapping("create") |
30 | | - public String displayCreateEventForm(Model model) { |
31 | | - model.addAttribute("title", "Create Event"); |
32 | | - model.addAttribute(new Event()); |
33 | | - model.addAttribute("types", EventType.values()); |
34 | | - return "events/create"; |
35 | | - } |
36 | | - |
37 | | - @PostMapping("create") |
38 | | - public String processCreateEventForm(@ModelAttribute @Valid Event newEvent, |
39 | | - Errors errors, Model model) { |
40 | | - if(errors.hasErrors()) { |
41 | | - model.addAttribute("title", "Create Event"); |
42 | | - return "events/create"; |
43 | | - } |
44 | | - |
45 | | - eventRepository.save(newEvent); |
46 | | - return "redirect:/events"; |
47 | | - } |
48 | | - |
49 | | - @GetMapping("delete") |
50 | | - public String displayDeleteEventForm(Model model) { |
51 | | - model.addAttribute("title", "Delete Events"); |
52 | | - model.addAttribute("events", eventRepository.findAll()); |
53 | | - return "events/delete"; |
54 | | - } |
55 | | - |
56 | | - @PostMapping("delete") |
57 | | - public String processDeleteEventsForm(@RequestParam(required = false) int[] eventIds) { |
58 | | - |
59 | | - if (eventIds != null) { |
60 | | - for (int id : eventIds) { |
61 | | - eventRepository.deleteById(id); |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - return "redirect:/events"; |
66 | | - } |
67 | | - |
68 | 29 | } |
0 commit comments