|
| 1 | +package com.apina.api.controllers; |
| 2 | + |
| 3 | +import com.apina.api.dtos.GymDTO; |
| 4 | +import com.apina.api.services.GymService; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.http.HttpStatus; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.web.bind.annotation.*; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | +@RestController |
| 13 | +@RequestMapping("/api") |
| 14 | +public class GymController { |
| 15 | + private static final Logger LOGGER = LoggerFactory.getLogger(GymController.class); |
| 16 | + private final GymService gymService; |
| 17 | + |
| 18 | + public GymController(GymService gymService) { |
| 19 | + this.gymService = gymService; |
| 20 | + } |
| 21 | + |
| 22 | + @PostMapping("gym") |
| 23 | + @ResponseStatus(HttpStatus.CREATED) |
| 24 | + public GymDTO postGym(@RequestBody GymDTO gymDTO) { |
| 25 | + return gymService.save(gymDTO); |
| 26 | + } |
| 27 | + |
| 28 | + @PostMapping("gyms") |
| 29 | + @ResponseStatus(HttpStatus.CREATED) |
| 30 | + public List<GymDTO> postGyms(@RequestBody List<GymDTO> gymEntities) { |
| 31 | + return gymService.saveAll(gymEntities); |
| 32 | + } |
| 33 | + |
| 34 | + @GetMapping("gyms") |
| 35 | + public List<GymDTO> getGyms() { |
| 36 | + return gymService.findAll(); |
| 37 | + } |
| 38 | + |
| 39 | + @GetMapping("gym/{id}") |
| 40 | + public ResponseEntity<GymDTO> getGym(@PathVariable String id) { |
| 41 | + GymDTO gymDTO = gymService.findOne(id); |
| 42 | + if (gymDTO == null) |
| 43 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); |
| 44 | + return ResponseEntity.ok(gymDTO); |
| 45 | + } |
| 46 | + |
| 47 | + @GetMapping("gyms/{ids}") |
| 48 | + public List<GymDTO> getGyms(@PathVariable String ids) { |
| 49 | + List<String> listIds = List.of(ids.split(",")); |
| 50 | + return gymService.findAll(listIds); |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("gyms/city/{city}") |
| 54 | + public List<GymDTO> getGymsByCity(@PathVariable String city) { |
| 55 | + return gymService.findAllByCity(city); |
| 56 | + } |
| 57 | + |
| 58 | + @GetMapping("gyms/company/{company}") |
| 59 | + public List<GymDTO> getGymsByCompany(@PathVariable String company) { |
| 60 | + return gymService.findAllByCompany(company); |
| 61 | + } |
| 62 | + |
| 63 | + @GetMapping("gyms/openingTime/{openingTime}") |
| 64 | + public List<GymDTO> getGymsByOpeningTime(@PathVariable String openingTime) { |
| 65 | + return gymService.findAllByOpeningTime(openingTime); |
| 66 | + } |
| 67 | + |
| 68 | + @GetMapping("gyms/closingTime/{closingTime}") |
| 69 | + public List<GymDTO> getGymsByClosingTime(@PathVariable String closingTime) { |
| 70 | + return gymService.findAllByClosingTime(closingTime); |
| 71 | + } |
| 72 | + |
| 73 | + @GetMapping("gyms/address/{streetName}") |
| 74 | + public GymDTO getGymByAddress(@PathVariable String streetName) { |
| 75 | + return gymService.findByAddress(streetName); |
| 76 | + } |
| 77 | + |
| 78 | + @PutMapping("gym") |
| 79 | + public GymDTO putGym(@RequestBody GymDTO gymDTO) { |
| 80 | + return gymService.update(gymDTO); |
| 81 | + } |
| 82 | + |
| 83 | + @PutMapping("gyms") |
| 84 | + public Long putGyms(@RequestBody List<GymDTO> gymEntities) { |
| 85 | + return gymService.update(gymEntities); |
| 86 | + } |
| 87 | + |
| 88 | + @DeleteMapping("gyms") |
| 89 | + public Long deleteGyms() { |
| 90 | + return gymService.deleteAll(); |
| 91 | + } |
| 92 | + |
| 93 | + @DeleteMapping("gym/{id}") |
| 94 | + public Long deleteGym(@PathVariable String id) { |
| 95 | + return gymService.delete(id); |
| 96 | + } |
| 97 | + |
| 98 | + @DeleteMapping("gyms/{ids}") |
| 99 | + public Long deleteGyms(@PathVariable String ids) { |
| 100 | + List<String> listIds = List.of(ids.split(",")); |
| 101 | + return gymService.delete(listIds); |
| 102 | + } |
| 103 | + |
| 104 | + @GetMapping("gyms/count") |
| 105 | + public Long getCount() { |
| 106 | + return gymService.count(); |
| 107 | + } |
| 108 | + |
| 109 | + @ExceptionHandler(RuntimeException.class) |
| 110 | + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) |
| 111 | + public final Exception handleAllExceptions(RuntimeException e) { |
| 112 | + LOGGER.error("Internal server error.", e); |
| 113 | + return e; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments