api sample
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package com.kamco.cd.kamcoback.zoo;
|
||||
|
||||
import com.kamco.cd.kamcoback.zoo.dto.AnimalDto;
|
||||
import com.kamco.cd.kamcoback.zoo.dto.AnimalDto.Category;
|
||||
import com.kamco.cd.kamcoback.zoo.dto.AnimalDto.Species;
|
||||
import com.kamco.cd.kamcoback.zoo.service.AnimalService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping({"/api/animals", "/v1/api/animals"})
|
||||
public class AnimalApiController {
|
||||
|
||||
private final AnimalService animalService;
|
||||
|
||||
/**
|
||||
* 동물 생성
|
||||
*
|
||||
* @param req 동물 생성 요청
|
||||
* @return 생성된 동물 정보
|
||||
*/
|
||||
@PostMapping
|
||||
public ResponseEntity<AnimalDto.Basic> createAnimal(@RequestBody AnimalDto.AddReq req) {
|
||||
AnimalDto.Basic created = animalService.createAnimal(req);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(created);
|
||||
}
|
||||
|
||||
/**
|
||||
* UUID로 동물 조회
|
||||
*
|
||||
* @param uuid 동물 UUID
|
||||
* @return 동물 정보
|
||||
*/
|
||||
@GetMapping("/{uuid}")
|
||||
public ResponseEntity<AnimalDto.Basic> getAnimal(@PathVariable String uuid) {
|
||||
Long id = animalService.getAnimalByUuid(uuid);
|
||||
AnimalDto.Basic animal = animalService.getAnimal(id);
|
||||
return ResponseEntity.ok(animal);
|
||||
}
|
||||
|
||||
/**
|
||||
* UUID로 동물 삭제 (논리 삭제)
|
||||
*
|
||||
* @param uuid 동물 UUID
|
||||
* @return 삭제 성공 메시지
|
||||
*/
|
||||
@DeleteMapping("/{uuid}")
|
||||
public ResponseEntity<Void> deleteAnimal(@PathVariable String uuid) {
|
||||
Long id = animalService.getAnimalByUuid(uuid);
|
||||
animalService.deleteZoo(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 동물 검색 (페이징)
|
||||
*
|
||||
* @param name 동물 이름 (선택)
|
||||
* @param category 서식지 타입 (선택)
|
||||
* @param species 동물종 (선택) 개, 고양이등.
|
||||
* @param page 페이지 번호 (기본값: 0)
|
||||
* @param size 페이지 크기 (기본값: 20)
|
||||
* @param sort 정렬 조건 (예: "name,asc")
|
||||
* @return 페이징 처리된 동물 목록
|
||||
*/
|
||||
@GetMapping
|
||||
public ResponseEntity<Page<AnimalDto.Basic>> searchAnimals(
|
||||
@RequestParam(required = false) String name,
|
||||
@RequestParam(required = false) Category category,
|
||||
@RequestParam(required = false) Species species,
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "20") int size,
|
||||
@RequestParam(required = false) String sort) {
|
||||
AnimalDto.SearchReq searchReq =
|
||||
new AnimalDto.SearchReq(name, category, species, page, size, sort);
|
||||
Page<AnimalDto.Basic> animals = animalService.search(searchReq);
|
||||
return ResponseEntity.ok(animals);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user