Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.helan.videoafisha.backend.mvc.api;
- import com.helan.videoafisha.backend.data.CascadeRemover;
- import com.helan.videoafisha.backend.data.record.StatisticCountRecord;
- import com.helan.videoafisha.backend.data.repository.*;
- import com.helan.videoafisha.backend.data.specifications.BookingSpecifications;
- import com.helan.videoafisha.backend.data.specifications.LocationSpecifications;
- import com.helan.videoafisha.backend.mvc.security.MemberUserDetails;
- import com.helan.videoafisha.entitiy.*;
- import lombok.extern.log4j.Log4j2;
- import org.modelmapper.ModelMapper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Sort;
- import org.springframework.data.jpa.domain.Specification;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.stereotype.Controller;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.*;
- import java.time.LocalDate;
- import java.util.*;
- @Log4j2
- @Controller
- @RequestMapping(value = "/api/locations")
- public class LocationController {
- @Autowired
- private ModelMapper modelMapper;
- @Autowired
- private LocationRepository locationRepository;
- @Autowired
- private SiteRepository siteRepository;
- @Autowired
- private VideoRepository videoRepository;
- @Autowired
- private CascadeRemover cascadeRemover;
- @Autowired
- private LocationScheduleTimeRangeRepository locationScheduleTimeRangeRepository;
- @Autowired
- private BookingRepository bookingRepository;
- @Autowired
- private EntityPageBuilder entityPageBuilder;
- private static Sort SORT = Sort.by(Sort.Direction.ASC, "site.organization.title", "title");
- @PreAuthorize("hasAnyRole('CLIENT', 'ADMIN')")
- @RequestMapping(method = RequestMethod.GET)
- @ResponseBody
- public ResponseEntity list(
- @RequestParam(required = false) Optional<Integer> page,
- @RequestParam(required = false) Optional<Integer> pageSize,
- @RequestParam(value = "siteId", required = false) Optional<Site> site,
- @RequestParam(required = false) Optional<String> search,
- @RequestParam(required = false) Optional<List<Long>> filterById
- ) {
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
- Member member = ((MemberUserDetails) authentication.getDetails()).getMember();
- Specification<Location> specification = Specification.where(null);
- if (member.getRole() != Role.Admin) {
- if (site.isPresent() && !siteRepository.findByMembers(member).contains(site.get())) {
- return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
- } else {
- specification = specification.and(LocationSpecifications.byMember(member));
- }
- }
- if (site.isPresent()) {
- specification = specification.and(LocationSpecifications.bySite(site.get()));
- }
- if (search.isPresent()) {
- specification = specification.and(LocationSpecifications.titleContainsIgnoreCase(search.get()));
- }
- if (filterById.isPresent()) {
- specification = specification.and(LocationSpecifications.byId(filterById.get()));
- }
- EntityPageBuilder.EntityPage<Location, com.helan.videoafisha.dto.backend.frontend.reactjs.locations.list.Location> entityPage = entityPageBuilder.<Location, com.helan.videoafisha.dto.backend.frontend.reactjs.locations.list.Location>build(
- locationRepository,
- SORT,
- com.helan.videoafisha.dto.backend.frontend.reactjs.locations.list.Location.class,
- page,
- pageSize,
- Optional.of(specification)
- );
- if (!entityPage.getEntities().isEmpty()) {
- Map<Long, Long> devicesCount = StatisticCountRecord.listToMap(locationRepository.devicesCountPerLocationByLocations(entityPage.getEntities()));
- Map<Long, Long> bookingsCount = StatisticCountRecord.listToMap(locationRepository.bookingsCountPerLocationBySiteAndDateAndLocations(entityPage.getEntities(), LocalDate.now()));
- for (com.helan.videoafisha.dto.backend.frontend.reactjs.locations.list.Location modelLocation : entityPage.getModelsObject()) {
- modelLocation.setDevicesCount(devicesCount.getOrDefault(modelLocation.getId(), 0L));
- modelLocation.setBookingsCount(bookingsCount.getOrDefault(modelLocation.getId(), 0L));
- }
- }
- return ResponseEntity.ok(entityPage.getModelsObject());
- }
- @PreAuthorize("hasAnyRole('ADMIN')")
- @RequestMapping(value = "/workload", method = RequestMethod.GET)
- @ResponseBody
- public Object workload(
- @RequestParam LocalDate startDate,
- @RequestParam LocalDate endDate,
- @RequestParam(required = false) Optional<Integer> page,
- @RequestParam(required = false) Optional<Integer> pageSize,
- @RequestParam(required = false) Optional<String> search
- ) {
- Specification specification = Specification.where(null);
- if (search.isPresent()) {
- specification = specification.and(LocationSpecifications.titleContainsIgnoreCase(search.get()));
- }
- EntityPageBuilder.EntityPage<Location, com.helan.videoafisha.dto.backend.frontend.reactjs.locations.workload.Location> entityPage = entityPageBuilder.<Location, com.helan.videoafisha.dto.backend.frontend.reactjs.locations.workload.Location>build(
- locationRepository,
- SORT,
- com.helan.videoafisha.dto.backend.frontend.reactjs.locations.workload.Location.class,
- page,
- pageSize,
- Optional.of(specification)
- );
- Map<Long, Map<LocalDate, Long>> busyMillsByLocations = new HashMap<>();
- for (Booking booking : bookingRepository.findAll(BookingSpecifications.interceptActivePeriod(startDate, endDate))) {
- for (Location location : booking.getLocations()) {
- for (
- LocalDate localDate = startDate.isAfter(booking.getStartDate()) ? startDate : booking.getStartDate();
- localDate.compareTo(endDate.isBefore(booking.getEndDate()) ? endDate : booking.getEndDate()) <= 0;
- localDate = localDate.plusDays(1)
- ) {
- Map<LocalDate, Long> busyMills = busyMillsByLocations.getOrDefault(location.getId(), null);
- if (busyMills == null) {
- busyMills = new HashMap<>();
- busyMillsByLocations.put(location.getId(), busyMills);
- }
- busyMills.put(
- localDate,
- busyMills.getOrDefault(localDate, 0L) +
- booking.getPaidVideo().getVideo().getDuration() * booking.getDemonstrationCountPerHour()
- );
- }
- }
- }
- Map<Long, Long> devicesCount = StatisticCountRecord.listToMap(locationRepository.devicesCountPerLocation());
- for (com.helan.videoafisha.dto.backend.frontend.reactjs.locations.workload.Location modelLocation : entityPage.getModelsObject()) {
- modelLocation.setBusyMills(busyMillsByLocations.getOrDefault(modelLocation.getId(), new HashMap<>()));
- modelLocation.setDevicesCount(devicesCount.getOrDefault(modelLocation.getId(), 0L));
- }
- return entityPage.getModelsObject();
- }
- @PreAuthorize("hasRole('ADMIN')")
- @Transactional
- @RequestMapping(method = RequestMethod.POST)
- @ResponseBody
- public com.helan.videoafisha.dto.general.Location save(@RequestBody com.helan.videoafisha.dto.backend.frontend.reactjs.locations.by_id.Location modelLocation) {
- Location location = modelMapper.map(modelLocation, Location.class);
- if (modelLocation.getId() != null) {
- location.setScheduleTimeRanges(locationRepository.getOne(modelLocation.getId()).getScheduleTimeRanges());
- }
- locationRepository.save(location);
- return modelMapper.map(location, com.helan.videoafisha.dto.general.Location.class);
- }
- @PreAuthorize("hasRole('ADMIN')")
- @Transactional
- @RequestMapping(value = "/{id}/geolocation", method = RequestMethod.POST)
- @ResponseBody
- public com.helan.videoafisha.dto.general.Location saveGeolocation(
- @PathVariable("id") Long id,
- @RequestBody com.helan.videoafisha.dto.backend.frontend.reactjs.locations.with_geolocation.Geolocation modelGeolocation) {
- Location location = locationRepository.getOne(id);
- Geolocation geolocation = modelMapper.map(modelGeolocation, Geolocation.class);
- location.setGeolocation(geolocation);
- locationRepository.save(location);
- return modelMapper.map(location, com.helan.videoafisha.dto.general.Location.class);
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'CLIENT')")
- @RequestMapping(value = "/{id}", method = RequestMethod.GET)
- @ResponseBody
- public ResponseEntity byId(
- @PathVariable("id") Long id,
- @RequestParam(required = false, defaultValue = "") String variant) {
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
- Member member = ((MemberUserDetails) authentication.getDetails()).getMember();
- Location location = locationRepository.getOne(id);
- if (member.getRole() == Role.Client && !siteRepository.findByMembers(member).contains(location.getSite())) {
- return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
- } else {
- Class clazz;
- switch (variant) {
- case "with_geolocation":
- clazz = com.helan.videoafisha.dto.backend.frontend.reactjs.locations.with_geolocation.Location.class;
- break;
- case "":
- clazz = com.helan.videoafisha.dto.backend.frontend.reactjs.locations.by_id.Location.class;
- break;
- default:
- throw new RuntimeException(String.format("Unknown DTO variant %s", variant));
- }
- return ResponseEntity.ok(modelMapper.map(location, clazz));
- }
- }
- @PreAuthorize("hasRole('ADMIN')")
- @Transactional
- @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
- public ResponseEntity delete(@PathVariable("id") Long id) {
- cascadeRemover.removeLocation(locationRepository.getOne(id), new HashSet());
- return ResponseEntity.ok().build();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement