Created
September 2, 2021 14:09
-
-
Save JuliaKrivonos/fa600d30d93dc4b77a9943d88f4c1717 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.jazzteam.fitnesclub.controllers; | |
| import org.jazzteam.fitnesclub.dto.workouts.IndividualWorkoutDto; | |
| import org.jazzteam.fitnesclub.dto.workouts.ScheduleWorkoutDto; | |
| import org.jazzteam.fitnesclub.dto.workouts.WorkoutDto; | |
| import org.jazzteam.fitnesclub.mappers.EntityMapper; | |
| import org.jazzteam.fitnesclub.model.workouts.IndividualWorkout; | |
| import org.jazzteam.fitnesclub.model.workouts.ScheduleWorkout; | |
| import org.jazzteam.fitnesclub.service.WorkoutService; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.data.domain.Page; | |
| import org.springframework.data.domain.Pageable; | |
| import org.springframework.data.web.PageableDefault; | |
| import org.springframework.http.HttpStatus; | |
| import org.springframework.http.MediaType; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.web.bind.annotation.*; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| import static org.jazzteam.fitnesclub.mappers.EntityMapper.mapToDto; | |
| @RestController | |
| @RequestMapping("/workout") | |
| public class WorkoutController { | |
| private final WorkoutService workoutService; | |
| @Autowired | |
| public WorkoutController(final WorkoutService workoutService) { | |
| this.workoutService = workoutService; | |
| } | |
| @GetMapping(value = "/scheduled", | |
| produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<Page<ScheduleWorkoutDto>> allScheduleWorkoutsPaged( | |
| @PageableDefault(size = 5, value = 5) Pageable pageable) { | |
| final Page<ScheduleWorkoutDto> resultPage = workoutService | |
| .findAllScheduleWorkout(pageable) | |
| .map(EntityMapper::mapToDto); | |
| return ResponseEntity.ok(resultPage); | |
| } | |
| @GetMapping(value = "/individual", | |
| produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<Page<IndividualWorkoutDto>> allIndividualWorkoutsPaged( | |
| @PageableDefault(size = 5, value = 5) Pageable pageable) { | |
| final Page<IndividualWorkoutDto> resultPage = workoutService | |
| .findAllIndividualWorkout(pageable) | |
| .map(EntityMapper::mapToDto); | |
| return ResponseEntity.ok(resultPage); | |
| } | |
| @GetMapping(value = "/scheduled/all", produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<List<ScheduleWorkoutDto>> allScheduleWorkouts() { | |
| final List<ScheduleWorkoutDto> resultPage = workoutService | |
| .findAllScheduleWorkout() | |
| .stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| return ResponseEntity.ok(resultPage); | |
| } | |
| @GetMapping(value = "/individual/all", produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<List<IndividualWorkoutDto>> allIndividualWorkouts() { | |
| final List<IndividualWorkoutDto> resultPage = workoutService | |
| .findAllIndividualWorkout() | |
| .stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| return ResponseEntity.ok(resultPage); | |
| } | |
| @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<List<WorkoutDto>> allWorkouts() { | |
| List<WorkoutDto> individualWorkoutStream = workoutService.findAllIndividualWorkout() | |
| .stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| List<WorkoutDto> scheduleWorkoutStream = workoutService.findAllIndividualWorkout() | |
| .stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| final List<WorkoutDto> resultPage = new ArrayList<>(); | |
| resultPage.addAll(individualWorkoutStream); | |
| resultPage.addAll(scheduleWorkoutStream); | |
| return ResponseEntity.ok(resultPage); | |
| } | |
| @GetMapping(value = "/client/{clientId}/scheduled", produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<List<ScheduleWorkoutDto>> allScheduledWorkoutsByClient(@PathVariable final Long clientId) { | |
| final List<ScheduleWorkoutDto> allScheduleWorkoutByClient = workoutService | |
| .findAllScheduleWorkoutByClient(clientId).stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| return ResponseEntity.ok(allScheduleWorkoutByClient); | |
| } | |
| @GetMapping(value = "/client/{clientId}/individual", produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<List<IndividualWorkoutDto>> allIndividualWorkoutsByClient(@PathVariable final Long clientId) { | |
| final List<IndividualWorkoutDto> allScheduleWorkoutByClient = workoutService | |
| .findAllIndividualWorkoutByClient(clientId).stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| return ResponseEntity.ok(allScheduleWorkoutByClient); | |
| } | |
| @GetMapping(value = "/scheduled/{id}", | |
| produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<ScheduleWorkoutDto> scheduleWorkoutById(@PathVariable final Long id) { | |
| final ScheduleWorkout scheduleWorkoutById = workoutService | |
| .findScheduleWorkoutByIdOrElseThrow(id, IllegalArgumentException::new); | |
| return ResponseEntity.ok(mapToDto(scheduleWorkoutById)); | |
| } | |
| @GetMapping(value = "/individual/{id}", | |
| produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<IndividualWorkoutDto> individualWorkoutById(@PathVariable final Long id) { | |
| final IndividualWorkout individualWorkoutById = workoutService | |
| .findIndividualWorkoutByIdOrElseThrow(id, IllegalArgumentException::new); | |
| return ResponseEntity.ok(mapToDto(individualWorkoutById)); | |
| } | |
| @GetMapping(value = "/trainer/{trainerId}/scheduled", produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<List<ScheduleWorkoutDto>> allScheduledWorkoutsByTrainer(@PathVariable final Long trainerId) { | |
| final List<ScheduleWorkoutDto> allScheduleWorkoutByTrainer = workoutService | |
| .findAllScheduleWorkoutByTrainer(trainerId) | |
| .stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| return ResponseEntity.ok(allScheduleWorkoutByTrainer); | |
| } | |
| @GetMapping(value = "/trainer/{trainerId}/individual", produces = MediaType.APPLICATION_JSON_VALUE) | |
| ResponseEntity<List<IndividualWorkoutDto>> allIndividualWorkoutsByTrainer(@PathVariable Long trainerId) { | |
| final List<IndividualWorkoutDto> allScheduleWorkoutByClient = workoutService | |
| .findAllIndividualWorkoutByTrainer(trainerId) | |
| .stream() | |
| .map(EntityMapper::mapToDto) | |
| .collect(Collectors.toList()); | |
| return ResponseEntity.ok(allScheduleWorkoutByClient); | |
| } | |
| @PostMapping("/scheduled") | |
| ResponseEntity<ScheduleWorkoutDto> createScheduleWorkout(@RequestBody final ScheduleWorkoutDto scheduleWorkoutDto) { | |
| final ScheduleWorkout createdWorkout = workoutService | |
| .createScheduleWorkout(scheduleWorkoutDto); | |
| return ResponseEntity.ok(mapToDto(createdWorkout)); | |
| } | |
| @PostMapping("/client/{clientId}/individual") | |
| ResponseEntity<IndividualWorkoutDto> createIndividualWorkout( | |
| @RequestBody final IndividualWorkoutDto individualWorkoutDto, | |
| @PathVariable Long clientId) { | |
| final IndividualWorkout createdWorkout = workoutService | |
| .createIndividualWorkout(individualWorkoutDto, clientId); | |
| return ResponseEntity.ok(mapToDto(createdWorkout)); | |
| } | |
| @PutMapping("/client/{clientId}/individual") | |
| ResponseEntity<IndividualWorkoutDto> updateIndividualWorkout( | |
| @RequestBody final IndividualWorkoutDto individualWorkoutDto, | |
| @PathVariable Long clientId) { | |
| final IndividualWorkout createdWorkout = workoutService | |
| .updateIndividualWorkout(individualWorkoutDto, clientId); | |
| return ResponseEntity.ok(mapToDto(createdWorkout)); | |
| } | |
| @PutMapping("/individual") | |
| ResponseEntity<IndividualWorkoutDto> updateTimeOfVisitIndividualWorkout( | |
| @RequestBody final IndividualWorkoutDto individualWorkoutDto) { | |
| final IndividualWorkout createdWorkout = workoutService | |
| .updateTimeOfVisitIndividualWorkout(individualWorkoutDto); | |
| return ResponseEntity.ok(mapToDto(createdWorkout)); | |
| } | |
| @PutMapping("/scheduled") | |
| ResponseEntity<ScheduleWorkoutDto> updateStatusScheduleWorkout( | |
| @RequestBody final ScheduleWorkoutDto scheduleWorkout) { | |
| final ScheduleWorkout createdWorkout = workoutService | |
| .updateStatusScheduleWorkout(scheduleWorkout); | |
| return ResponseEntity.ok(mapToDto(createdWorkout)); | |
| } | |
| @PutMapping("/client/{clientId}/scheduled") | |
| ResponseEntity<ScheduleWorkoutDto> addClientOnScheduleWorkout( | |
| @RequestBody final ScheduleWorkoutDto scheduleWorkout, | |
| @PathVariable Long clientId) { | |
| final ScheduleWorkout createdWorkout = workoutService | |
| .addClientOnScheduleWorkout(scheduleWorkout, clientId); | |
| return ResponseEntity.ok(mapToDto(createdWorkout)); | |
| } | |
| @PutMapping("/scheduled/client/{clientId}/") | |
| ResponseEntity<ScheduleWorkoutDto> removeClientFromScheduleWorkout( | |
| @RequestBody final ScheduleWorkoutDto scheduleWorkout, | |
| @PathVariable Long clientId) { | |
| final ScheduleWorkout createdWorkout = workoutService | |
| .removeClientFromScheduleWorkout(scheduleWorkout, clientId); | |
| return ResponseEntity.ok(mapToDto(createdWorkout)); | |
| } | |
| @DeleteMapping("/individual/{id}") | |
| public ResponseEntity<Void> deleteIndividualWorkout(@PathVariable final long id) { | |
| workoutService.deleteIndividualWorkout(id); | |
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | |
| } | |
| @DeleteMapping("/scheduled/{id}") | |
| public ResponseEntity<Void> deleteScheduleWorkout(@PathVariable final long id) { | |
| workoutService.deleteScheduleWorkout(id); | |
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment