|
| 1 | +package com.reprezen.demo.springboot.api.impl; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import javax.validation.Valid; |
| 9 | + |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.stereotype.Controller; |
| 13 | +import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.RequestBody; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestParam; |
| 17 | +import org.springframework.web.context.request.NativeWebRequest; |
| 18 | + |
| 19 | +import com.google.common.collect.Maps; |
| 20 | +import com.reprezen.demo.springboot.api.PetsApi; |
| 21 | +import com.reprezen.demo.springboot.model.NewPet; |
| 22 | +import com.reprezen.demo.springboot.model.Pet; |
| 23 | + |
| 24 | +import io.swagger.annotations.ApiParam; |
| 25 | + |
| 26 | +@Controller |
| 27 | +@RequestMapping("${openapi.swaggerPetstore.base-path:/api}") |
| 28 | +public class PetsApiImpl implements PetsApi { |
| 29 | + |
| 30 | + private final Map<Long, Pet> pets = Maps.newHashMap(); |
| 31 | + private long nextId = 0l; |
| 32 | + |
| 33 | + private final NativeWebRequest request; |
| 34 | + |
| 35 | + @org.springframework.beans.factory.annotation.Autowired |
| 36 | + public PetsApiImpl(NativeWebRequest request) { |
| 37 | + this.request = request; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Optional<NativeWebRequest> getRequest() { |
| 42 | + return Optional.ofNullable(request); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public ResponseEntity<Pet> addPet( |
| 47 | + @ApiParam(value = "Pet to add to the store", required = true) @Valid @RequestBody NewPet newPet) { |
| 48 | + Pet petToAdd = new Pet(); |
| 49 | + petToAdd.id(nextId++).name(newPet.getName()).tag(newPet.getTag()); |
| 50 | + pets.put(petToAdd.getId(), petToAdd); |
| 51 | + return new ResponseEntity<>(petToAdd, HttpStatus.CREATED); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public ResponseEntity<Void> deletePet( |
| 56 | + @ApiParam(value = "ID of pet to delete", required = true) @PathVariable("id") Long id) { |
| 57 | + if (!pets.containsKey(id)) { |
| 58 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 59 | + } |
| 60 | + pets.remove(id); |
| 61 | + return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public ResponseEntity<Pet> findPetById( |
| 66 | + @ApiParam(value = "ID of pet to fetch", required = true) @PathVariable("id") Long id) { |
| 67 | + if (!pets.containsKey(id)) { |
| 68 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 69 | + } |
| 70 | + return new ResponseEntity<>(pets.get(id), HttpStatus.ACCEPTED); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public ResponseEntity<List<Pet>> findPets( |
| 75 | + @ApiParam(value = "tags to filter by") @Valid @RequestParam(value = "tags", required = false) List<String> tags, |
| 76 | + @ApiParam(value = "maximum number of results to return") @Valid @RequestParam(value = "limit", required = false) Integer limitObject) { |
| 77 | + int limit = limitObject == null ? Integer.MAX_VALUE : limitObject; |
| 78 | + List<Pet> filteredPets = pets.values().stream()// |
| 79 | + .filter(pet -> (tags == null || tags.isEmpty()) ? true : tags.contains(pet.getTag()))// |
| 80 | + .limit(limit)// |
| 81 | + .collect(Collectors.toList()); |
| 82 | + return new ResponseEntity<>(filteredPets, HttpStatus.ACCEPTED); |
| 83 | + } |
| 84 | +} |
0 commit comments