|
| 1 | +package com.sapanywhere.app.controller; |
| 2 | + |
| 3 | +import org.apache.log4j.Logger; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.security.web.bind.annotation.AuthenticationPrincipal; |
| 6 | +import org.springframework.stereotype.Controller; |
| 7 | +import org.springframework.ui.Model; |
| 8 | +import org.springframework.validation.BindingResult; |
| 9 | +import org.springframework.web.bind.annotation.PathVariable; |
| 10 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 11 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 12 | +import com.sapanywhere.app.exception.BusinessException; |
| 13 | +import com.sapanywhere.app.model.calendar.CalendarPage; |
| 14 | +import com.sapanywhere.app.model.calendar.HolidayForm; |
| 15 | +import com.sapanywhere.app.service.HolidayService; |
| 16 | +import com.sapanywhere.app.service.user.UserInfo; |
| 17 | + |
| 18 | +@Controller |
| 19 | +public class CalendarController { |
| 20 | + |
| 21 | + private static Logger logger = Logger.getLogger(CalendarController.class); |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private HolidayService holidayService; |
| 25 | + |
| 26 | + @RequestMapping(value = "/calendar.html", method = RequestMethod.GET) |
| 27 | + public String indexPage(Model model) { |
| 28 | + logger.info("load calendar.html"); |
| 29 | + CalendarPage calendarPage = new CalendarPage( |
| 30 | + this.holidayService.findAllHolidaysThisYear()); |
| 31 | + model.addAttribute(calendarPage); |
| 32 | + return "/calendar/index"; |
| 33 | + } |
| 34 | + |
| 35 | + @RequestMapping(value = "/createHoliday.html", method = RequestMethod.GET) |
| 36 | + public String createPage(HolidayForm holidayForm) { |
| 37 | + logger.info("load createHoliday.html"); |
| 38 | + return "/calendar/create"; |
| 39 | + } |
| 40 | + |
| 41 | + @RequestMapping(value = "/createHoliday", method = RequestMethod.POST) |
| 42 | + public String createHoliday(HolidayForm holidayForm, BindingResult result) { |
| 43 | + if (result.hasErrors()) { |
| 44 | + return "/calendar/create"; |
| 45 | + } |
| 46 | + try { |
| 47 | + this.holidayService.create(holidayForm.parse()); |
| 48 | + } catch (BusinessException ex) { |
| 49 | + result.reject(ex.getCode()); |
| 50 | + return "/calendar/create"; |
| 51 | + } |
| 52 | + return "redirect:/calendar.html"; |
| 53 | + } |
| 54 | + |
| 55 | + @RequestMapping(value = "/holiday({id:[0-9]+})", method = RequestMethod.DELETE) |
| 56 | + public void deleteHoliday(@PathVariable Long id, |
| 57 | + @AuthenticationPrincipal UserInfo userInfo){ |
| 58 | + this.holidayService.delete(id, userInfo.getUser()); |
| 59 | + } |
| 60 | +} |
0 commit comments