Skip to content

Instantly share code, notes, and snippets.

@JuliaKrivonos
Created September 1, 2021 12:12
Show Gist options
  • Select an option

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

Select an option

Save JuliaKrivonos/2c6af31b47a818642aff5237a559e00e 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.exceptions.ScheduleNotFoundException;
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.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
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(ScheduleService scheduledService) {
this.scheduledService = scheduledService;
}
@GetMapping(value = "/}", consumes = MediaType.APPLICATION_JSON_VALUE, params = {"page", "size"})
@ResponseBody
ResponseEntity<List<ScheduleDto>> allSchedules(@RequestParam("page") int page,
@RequestParam("size") int size, UriComponentsBuilder uriBuilder) {
Page<Schedule> resultPage = scheduledService.findAllSchedule(page, size);
if (page > resultPage.getTotalPages()) {
return ResponseEntity.notFound().build();
}
List<ScheduleDto> scheduleDtoList = resultPage.stream()
.map(EntityMapper::mapToDto)
.collect(Collectors.toList());
return ResponseEntity.ok(scheduleDtoList);
}
@GetMapping(value = "/{id}}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
ResponseEntity<ScheduleDto> scheduleById(@PathVariable Long id) {
final Schedule scheduleById = scheduledService.findScheduleById(id);
if (Objects.isNull(scheduleById)) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(mapToDto(scheduleById));
}
@PostMapping("/")
ResponseEntity<ScheduleDto> createSchedule(@RequestBody final ScheduleDto scheduleDto) throws URISyntaxException {
final Schedule createdSchedule = scheduledService.saveSchedule(scheduleDto);
if (createdSchedule == null) {
return ResponseEntity.notFound().build();
} else {
URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(createdSchedule.getId())
.toUri();
return ResponseEntity.created(uri)
.body(mapToDto(createdSchedule));
}
}
@PutMapping("/{id}")
ResponseEntity<ScheduleDto> updateInfoSchedule(@RequestBody final ScheduleDto scheduleDto) {
final Schedule scheduleUpdated = scheduledService.updateSchedule(scheduleDto);
if (scheduleUpdated == null) {
return ResponseEntity.notFound().build();
} else {
URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(scheduleUpdated.getId())
.toUri();
return ResponseEntity.ok().body(mapToDto(scheduleUpdated));
}
}
@DeleteMapping("users/{id}")
public HttpStatus delete(@PathVariable final long id) {
try {
scheduledService.deleteSchedule(id);
return HttpStatus.OK;
} catch (Exception e) {
throw new ScheduleNotFoundException();
}
}
@PostMapping("/{id}")
ResponseEntity<ScheduleDto> addDaySchedule(@RequestBody final DayScheduleDto dayScheduleDto,
@PathVariable Long id) throws URISyntaxException {
final Schedule createdSchedule = scheduledService.addDaySchedule(dayScheduleDto, id);
if (createdSchedule == null) {
return ResponseEntity.notFound().build();
} else {
URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/days/{dayId}")
.buildAndExpand(createdSchedule.getId())
.toUri();
return ResponseEntity.created(uri)
.body(mapToDto(createdSchedule));
}
}
}
// @PostMapping("/")
// ResponseEntity<DayScheduleDto> addDaySchedule(@RequestBody final DayScheduleDto dayScheduleDto) throws URISyntaxException {
// final DaySchedule createdSchedule = scheduledService.addDaySchedule(dayScheduleDto, );
// if (createdSchedule == null) {
// return ResponseEntity.notFound().build();
// } else {
// URI uriOfNewResource = ServletUriComponentsBuilder.fromCurrentContextPath()
// .path("/users/{user}/customers/{customer}")
// .buildAndExpand(user, customer.getId())
// .toUri();
//
// HttpHeaders httpHeaders = new HttpHeaders();
// httpHeaders.setLocation(uriOfNewResource);
// return ResponseEntity.created(uri)
// .body(mapToDto(createdSchedule));
// }
// }
//
// @RequestMapping(method = RequestMethod.POST, value = "/{user}/customers")
// ResponseEntity<Customer> addCustomer(@PathVariable Long user, @RequestBody Customer c) {
// Customer customer = crmService.addCustomer(user, c.getFirstName(), c.getLastName());
// URI uriOfNewResource = ServletUriComponentsBuilder.fromCurrentContextPath()
// .path("/users/{user}/customers/{customer}")
// .buildAndExpand(user, customer.getId())
// .toUri();
// HttpHeaders httpHeaders = new HttpHeaders();
// httpHeaders.setLocation(uriOfNewResource);
// return new ResponseEntity<>(customer, httpHeaders, HttpStatus.CREATED);
// }
//@RequestMapping(value = "/", method = RequestMethod.GET)
//String index(Model model, //
// @QuerydslPredicate(root = User.class) Predicate predicate, //
// @PageableDefault(sort = { "lastname", "firstname" }) Pageable pageable, //
// @RequestParam MultiValueMap<String, String> parameters) {
//
// ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest();
// builder.replaceQueryParam("page", new Object[0]);
//
// model.addAttribute("baseUri", builder.build().toUri());
// model.addAttribute("users", repository.findAll(predicate, pageable));
//
// return "index";
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment