[a51a591] | 1 | package com.example.web;
|
---|
| 2 |
|
---|
| 3 | import com.example.model.*;
|
---|
| 4 | import com.example.repository.*;
|
---|
| 5 | import com.example.service.UserService;
|
---|
| 6 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
| 7 | import org.springframework.stereotype.Controller;
|
---|
| 8 | import org.springframework.ui.Model;
|
---|
| 9 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
| 10 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
| 11 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 12 |
|
---|
| 13 | import javax.servlet.http.HttpServletRequest;
|
---|
| 14 | import javax.servlet.http.HttpServletResponse;
|
---|
| 15 | import javax.servlet.http.HttpSession;
|
---|
| 16 | import javax.transaction.Transactional;
|
---|
| 17 | import java.time.LocalDate;
|
---|
| 18 | import java.util.List;
|
---|
| 19 | import java.util.NoSuchElementException;
|
---|
| 20 |
|
---|
| 21 | @Controller
|
---|
| 22 | public class HostEventController {
|
---|
| 23 |
|
---|
| 24 | private final EventRepository eventRepository;
|
---|
| 25 | private final UserRepository userRepository;
|
---|
| 26 | private final LocationRepository locationRepository;
|
---|
| 27 | private final ClientRepository clientRepository;
|
---|
| 28 | private final BandRepository bandRepository;
|
---|
| 29 | private final CateringRepository cateringRepository;
|
---|
| 30 | private final PhotographerRepository photographerRepository;
|
---|
| 31 | private final UserService userService;
|
---|
| 32 |
|
---|
| 33 | private final WaiterRepository waiterRepository;
|
---|
| 34 | public HostEventController(EventRepository eventRepository, UserRepository userRepository,
|
---|
| 35 | LocationRepository locationRepository, ClientRepository clientRepository,
|
---|
| 36 | BandRepository bandRepository, CateringRepository cateringRepository,
|
---|
| 37 | PhotographerRepository photographerRepository, UserService userService, WaiterRepository waiterRepository) {
|
---|
| 38 | this.eventRepository = eventRepository;
|
---|
| 39 | this.userRepository = userRepository;
|
---|
| 40 | this.locationRepository = locationRepository;
|
---|
| 41 | this.clientRepository = clientRepository;
|
---|
| 42 | this.bandRepository = bandRepository;
|
---|
| 43 | this.cateringRepository = cateringRepository;
|
---|
| 44 | this.photographerRepository = photographerRepository;
|
---|
| 45 | this.userService = userService;
|
---|
| 46 | this.waiterRepository = waiterRepository;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | @GetMapping("/host_event")
|
---|
| 50 | public String getHostEventPage(Model model) {
|
---|
| 51 | model.addAttribute("locations", locationRepository.findAll());
|
---|
| 52 | model.addAttribute("bands", bandRepository.findAll());
|
---|
| 53 | model.addAttribute("caterings", cateringRepository.findAll());
|
---|
| 54 | model.addAttribute("photographers", photographerRepository.findAll());
|
---|
| 55 | return "host_event";
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Transactional
|
---|
| 59 | @PostMapping("/host_event")
|
---|
| 60 | public String handleHostEvent(@RequestParam String time,
|
---|
| 61 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
|
---|
| 62 | @RequestParam String location,
|
---|
| 63 | @RequestParam String type,
|
---|
| 64 | @RequestParam(required = false) String description,
|
---|
| 65 | @RequestParam List<Band> bands,
|
---|
| 66 | @RequestParam List<Catering> caterings,
|
---|
| 67 | @RequestParam List<Photographer> photographers,
|
---|
| 68 | HttpServletRequest req,
|
---|
| 69 | HttpServletResponse resp,
|
---|
| 70 | HttpSession session) {
|
---|
| 71 | Event event = null;
|
---|
| 72 | try {
|
---|
| 73 | Location location2 = locationRepository.findByAddress(location);
|
---|
| 74 |
|
---|
| 75 | User user = (User) userService.findByUsername((String) req.getSession().getAttribute("username")).get();
|
---|
| 76 | Client client = (Client) clientRepository.findById(user.getId()).get();
|
---|
| 77 | Admin admin = (Admin) userService.findByUsername("admin1").get();
|
---|
| 78 | admin.setNumber_events(admin.getNumber_events()+1);
|
---|
| 79 | client.setNumber_events(client.getNumber_events()+1);
|
---|
| 80 | eventRepository.save(new Event(time, date, location2, type, description, client, bands,caterings, photographers, admin));
|
---|
| 81 |
|
---|
| 82 | return "redirect:/home?UspeshnoZakazanNastan";
|
---|
| 83 | } catch (Exception e) {
|
---|
| 84 | return "redirect:/?error=" + e.getMessage();
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @GetMapping("/my_events")
|
---|
| 89 | public String getEventsForUser(HttpServletRequest req, Model model) {
|
---|
| 90 | User user = (User) userService.findByUsername((String) req.getSession().getAttribute("username")).get();
|
---|
| 91 | List<Event> events = null;
|
---|
| 92 | try {
|
---|
| 93 | Client client = (Client) clientRepository.findById(user.getId()).get();
|
---|
| 94 | events = client.getEvent();
|
---|
| 95 | }
|
---|
| 96 | catch (NoSuchElementException exception){
|
---|
| 97 | try{
|
---|
| 98 | Catering catering = (Catering) cateringRepository.findById(user.getId()).get();
|
---|
| 99 | events = catering.getEventList();
|
---|
| 100 | }
|
---|
| 101 | catch (NoSuchElementException exception1){
|
---|
| 102 | try {
|
---|
| 103 | Band band = (Band) bandRepository.findById(user.getId()).get();
|
---|
| 104 | events = band.getEventList();
|
---|
| 105 | }
|
---|
| 106 | catch (NoSuchElementException e){
|
---|
| 107 | try{
|
---|
| 108 | Photographer photographer = (Photographer) photographerRepository.findById(user.getId()).get();
|
---|
| 109 | events = photographer.getEventList();
|
---|
| 110 | }
|
---|
| 111 | catch (NoSuchElementException exception2){
|
---|
| 112 | try{
|
---|
| 113 | Waiter waiter = (Waiter) waiterRepository.findById(user.getId()).get();
|
---|
| 114 | events = waiter.getEventList();
|
---|
| 115 | }
|
---|
| 116 | catch (NoSuchElementException exception3){
|
---|
| 117 | events = eventRepository.findAll();
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | model.addAttribute("events",events);
|
---|
| 124 | return "my_events";
|
---|
| 125 | }
|
---|
| 126 | } |
---|