88 lines
3.2 KiB
Java
88 lines
3.2 KiB
Java
/*
|
|
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
|
|
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
|
|
*
|
|
*/
|
|
|
|
package ru.ulstu.meeting;
|
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.security.access.annotation.Secured;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import ru.ulstu.model.OffsetablePageRequest;
|
|
import ru.ulstu.model.UserRoleConstants;
|
|
|
|
import javax.validation.Valid;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.IntStream;
|
|
|
|
@Controller
|
|
@RequestMapping("meetings")
|
|
public class MeetingController {
|
|
private final static int DEFAULT_PAGE_SIZE = 10;
|
|
private final MeetingService meetingService;
|
|
|
|
public MeetingController(MeetingService meetingService) {
|
|
this.meetingService = meetingService;
|
|
}
|
|
|
|
@GetMapping("/meetings")
|
|
public String listMeetings(Model model,
|
|
@RequestParam Optional<Integer> page,
|
|
@RequestParam Optional<Integer> size) {
|
|
int currentPage = page.orElse(1);
|
|
int pageSize = size.orElse(DEFAULT_PAGE_SIZE);
|
|
|
|
Page<Meeting> meetingsPage = meetingService.getMeetings(new OffsetablePageRequest(currentPage - 1, pageSize));
|
|
model.addAttribute("meetings", meetingsPage);
|
|
int totalPages = meetingsPage.getTotalPages();
|
|
if (totalPages > 0) {
|
|
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
|
|
.boxed()
|
|
.collect(Collectors.toList());
|
|
model.addAttribute("pageNumbers", pageNumbers);
|
|
}
|
|
return "meetings";
|
|
}
|
|
|
|
@GetMapping("/editMeeting/{meetingId}")
|
|
@Secured({UserRoleConstants.ADMIN})
|
|
public String editMeeting(@PathVariable(value = "meetingId") Integer id, Model model) {
|
|
model.addAttribute("meeting", (id != null && id != 0) ? meetingService.getById(id) : new Meeting());
|
|
return "editMeeting";
|
|
}
|
|
|
|
@GetMapping("/meetings/{meetingId}")
|
|
public String viewMeeting(@PathVariable(value = "meetingId") Integer id, Model model) {
|
|
model.addAttribute("meeting", id != null ? meetingService.getById(id) : new Meeting());
|
|
return "viewMeeting";
|
|
}
|
|
|
|
@PostMapping("saveMeeting")
|
|
@Secured({UserRoleConstants.ADMIN})
|
|
public String saveNews(@Valid @ModelAttribute Meeting meeting, BindingResult result) {
|
|
if (result.hasErrors()) {
|
|
return "editMeeting";
|
|
}
|
|
meetingService.save(meeting);
|
|
return "redirect:/meetings/meetings";
|
|
}
|
|
|
|
@GetMapping("deleteMeeting/{meetingId}")
|
|
@Secured({UserRoleConstants.ADMIN})
|
|
public String delete(@PathVariable(value = "meetingId") Integer id) {
|
|
meetingService.delete(id);
|
|
return "redirect:/meetings/meetings";
|
|
}
|
|
}
|