Skip to content

Instantly share code, notes, and snippets.

@JuliaKrivonos
Created September 2, 2021 11:34
Show Gist options
  • Select an option

  • Save JuliaKrivonos/b2a7db42979a4368ae73dd3349d82fc4 to your computer and use it in GitHub Desktop.

Select an option

Save JuliaKrivonos/b2a7db42979a4368ae73dd3349d82fc4 to your computer and use it in GitHub Desktop.
package org.jazzteam.fitnesclub.controllers;
import org.jazzteam.fitnesclub.dto.schedules.DayScheduleDto;
import org.jazzteam.fitnesclub.dto.schedules.DayScheduleForReplacementDto;
import org.jazzteam.fitnesclub.dto.schedules.ScheduleDto;
import org.jazzteam.fitnesclub.mappers.EntityMapper;
import org.jazzteam.fitnesclub.model.schedule.DaySchedule;
import org.jazzteam.fitnesclub.model.schedule.Schedule;
import org.jazzteam.fitnesclub.service.ScheduleService;
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.List;
import java.util.stream.Collectors;
import static org.jazzteam.fitnesclub.mappers.EntityMapper.mapToDto;
@RestController
@RequestMapping("/schedule")
public class ScheduleController {
private final ScheduleService scheduledService;
@Autowired
public ScheduleController(final ScheduleService scheduledService) {
this.scheduledService = scheduledService;
}
@GetMapping(value = "/",
produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Page<ScheduleDto>> allSchedulesPaged(
@PageableDefault(size = 5, page = 0, value = 5) Pageable pageable) {
final Page<ScheduleDto> resultPage = scheduledService
.findAllSchedule(pageable)
.map(EntityMapper::mapToDto);
return ResponseEntity.ok(resultPage);
}
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<List<ScheduleDto>> allSchedules() {
final List<ScheduleDto> resultPage = scheduledService
.findAllSchedule()
.stream()
.map(EntityMapper::mapToDto)
.collect(Collectors.toList());
return ResponseEntity.ok(resultPage);
}
@GetMapping(value = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<ScheduleDto> scheduleById(@PathVariable Long id) {
final Schedule scheduleById = scheduledService.findScheduleById(id);
return ResponseEntity.ok(mapToDto(scheduleById));
}
@PostMapping("/")
ResponseEntity<ScheduleDto> createSchedule(@RequestBody final ScheduleDto scheduleDto) {
final Schedule createdSchedule = scheduledService.saveSchedule(scheduleDto);
return ResponseEntity.ok(mapToDto(createdSchedule));
}
@PutMapping("/{id}")
ResponseEntity<ScheduleDto> updateInfoSchedule(@PathVariable long id,
@RequestBody final ScheduleDto scheduleDto) {
scheduleDto.setId(id);
final Schedule scheduleUpdated = scheduledService.updateSchedule(scheduleDto);
return ResponseEntity.ok(mapToDto(scheduleUpdated));
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteSchedule(@PathVariable final long id) {
scheduledService.deleteSchedule(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping(value = "/days", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Page<DayScheduleDto>> allDaysPaged(@PageableDefault(size = 5, page = 0, value = 5) Pageable pageable) {
final Page<DayScheduleDto> resultPage = scheduledService
.findAllDaysSchedule(pageable)
.map(EntityMapper::mapToDto);
return ResponseEntity.ok(resultPage);
}
@GetMapping(value = "/days/all", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<List<DayScheduleDto>> allDaySchedules() {
final List<DayScheduleDto> result = scheduledService
.findAllDaysSchedule()
.stream()
.map(EntityMapper::mapToDto)
.collect(Collectors.toList());
return ResponseEntity.ok(result);
}
@GetMapping(value = "/{scheduleId}/days", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<List<DayScheduleDto>> allDaySchedulesByScheduleId(@PathVariable Long scheduleId) {
final List<DayScheduleDto> result = scheduledService
.findAllScheduleByScheduleId(scheduleId)
.stream()
.map(EntityMapper::mapToDto)
.collect(Collectors.toList());
return ResponseEntity.ok(result);
}
@PostMapping("/{scheduleId}")
ResponseEntity<ScheduleDto> createDaySchedule(@RequestBody final DayScheduleDto dayScheduleDto,
@PathVariable Long scheduleId) {
final Schedule createdSchedule = scheduledService.saveDaySchedule(dayScheduleDto, scheduleId);
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDto(createdSchedule));
}
@PutMapping("/days/{id}/")
ResponseEntity<DayScheduleDto> createDayScheduleForReplacement(
@PathVariable long id,
@RequestBody final DayScheduleForReplacementDto dayScheduleForReplacementDto) {
final DaySchedule daySchedule = scheduledService.saveDayScheduleForReplacement(dayScheduleForReplacementDto, id);
return ResponseEntity.ok(mapToDto(daySchedule));
}
@DeleteMapping("/days/{id}/")
public ResponseEntity<Void> deleteDaySchedule(@PathVariable final long id) {
scheduledService.deleteDaySchedule(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