feat: add zoo sample
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package com.kamco.cd.kamcoback.zoo;
|
||||
|
||||
import com.kamco.cd.kamcoback.zoo.dto.ZooDto;
|
||||
import com.kamco.cd.kamcoback.zoo.service.ZooService;
|
||||
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/zoos", "/v1/api/zoos"})
|
||||
public class ZooApiController {
|
||||
|
||||
private final ZooService zooService;
|
||||
|
||||
/**
|
||||
* 동물원 생성
|
||||
*
|
||||
* @param req 동물원 생성 요청
|
||||
* @return 생성된 동물원 정보 (동물 개수 포함)
|
||||
*/
|
||||
@PostMapping
|
||||
public ResponseEntity<ZooDto.Detail> createZoo(@RequestBody ZooDto.AddReq req) {
|
||||
ZooDto.Detail created = zooService.createZoo(req);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(created);
|
||||
}
|
||||
|
||||
/**
|
||||
* UUID로 동물원 조회
|
||||
*
|
||||
* @param uuid 동물원 UUID
|
||||
* @return 동물원 정보 (현재 동물 개수 포함)
|
||||
*/
|
||||
@GetMapping("/{uuid}")
|
||||
public ResponseEntity<ZooDto.Detail> getZoo(@PathVariable String uuid) {
|
||||
Long id = zooService.getZooByUuid(uuid);
|
||||
ZooDto.Detail zoo = zooService.getZoo(id);
|
||||
return ResponseEntity.ok(zoo);
|
||||
}
|
||||
|
||||
/**
|
||||
* UUID로 동물원 삭제 (논리 삭제)
|
||||
*
|
||||
* @param uuid 동물원 UUID
|
||||
* @return 삭제 성공 메시지
|
||||
*/
|
||||
@DeleteMapping("/{uuid}")
|
||||
public ResponseEntity<Void> deleteZoo(@PathVariable String uuid) {
|
||||
Long id = zooService.getZooByUuid(uuid);
|
||||
zooService.deleteZoo(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 동물원 검색 (페이징)
|
||||
*
|
||||
* @param name 동물원 이름 (선택)
|
||||
* @param location 위치 (선택)
|
||||
* @param page 페이지 번호 (기본값: 0)
|
||||
* @param size 페이지 크기 (기본값: 20)
|
||||
* @param sort 정렬 조건 (예: "name,asc")
|
||||
* @return 페이징 처리된 동물원 목록 (각 동물원의 현재 동물 개수 포함)
|
||||
*/
|
||||
@GetMapping
|
||||
public ResponseEntity<Page<ZooDto.Detail>> searchZoos(
|
||||
@RequestParam(required = false) String name,
|
||||
@RequestParam(required = false) String location,
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "20") int size,
|
||||
@RequestParam(required = false) String sort) {
|
||||
ZooDto.SearchReq searchReq = new ZooDto.SearchReq(name, location, page, size, sort);
|
||||
Page<ZooDto.Detail> zoos = zooService.search(searchReq);
|
||||
return ResponseEntity.ok(zoos);
|
||||
}
|
||||
}
|
||||
113
src/main/java/com/kamco/cd/kamcoback/zoo/dto/ZooDto.java
Normal file
113
src/main/java/com/kamco/cd/kamcoback/zoo/dto/ZooDto.java
Normal file
@@ -0,0 +1,113 @@
|
||||
package com.kamco.cd.kamcoback.zoo.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import java.time.ZonedDateTime;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
public class ZooDto {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class AddReq {
|
||||
|
||||
private String name;
|
||||
private String location;
|
||||
private String description;
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class Basic {
|
||||
|
||||
@JsonIgnore private Long id;
|
||||
private String uuid;
|
||||
private String name;
|
||||
private String location;
|
||||
private String description;
|
||||
|
||||
@JsonFormat(
|
||||
shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd'T'HH:mm:ssXXX",
|
||||
timezone = "Asia/Seoul")
|
||||
private ZonedDateTime createdDate;
|
||||
|
||||
@JsonFormat(
|
||||
shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd'T'HH:mm:ssXXX",
|
||||
timezone = "Asia/Seoul")
|
||||
private ZonedDateTime modifiedDate;
|
||||
|
||||
public Basic(
|
||||
Long id,
|
||||
String uuid,
|
||||
String name,
|
||||
String location,
|
||||
String description,
|
||||
ZonedDateTime createdDate,
|
||||
ZonedDateTime modifiedDate) {
|
||||
this.id = id;
|
||||
this.uuid = uuid;
|
||||
this.name = name;
|
||||
this.location = location;
|
||||
this.description = description;
|
||||
this.createdDate = createdDate;
|
||||
this.modifiedDate = modifiedDate;
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class Detail extends Basic {
|
||||
|
||||
private Long activeAnimalCount;
|
||||
|
||||
public Detail(
|
||||
Long id,
|
||||
String uuid,
|
||||
String name,
|
||||
String location,
|
||||
String description,
|
||||
ZonedDateTime createdDate,
|
||||
ZonedDateTime modifiedDate,
|
||||
Long activeAnimalCount) {
|
||||
super(id, uuid, name, location, description, createdDate, modifiedDate);
|
||||
this.activeAnimalCount = activeAnimalCount;
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class SearchReq {
|
||||
|
||||
// 검색 조건
|
||||
private String name;
|
||||
private String location;
|
||||
|
||||
// 페이징 파라미터
|
||||
private int page = 0;
|
||||
private int size = 20;
|
||||
private String sort;
|
||||
|
||||
public Pageable toPageable() {
|
||||
if (sort != null && !sort.isEmpty()) {
|
||||
String[] sortParams = sort.split(",");
|
||||
String property = sortParams[0];
|
||||
Sort.Direction direction =
|
||||
sortParams.length > 1
|
||||
? Sort.Direction.fromString(sortParams[1])
|
||||
: Sort.Direction.ASC;
|
||||
return PageRequest.of(page, size, Sort.by(direction, property));
|
||||
}
|
||||
return PageRequest.of(page, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.kamco.cd.kamcoback.zoo.service;
|
||||
|
||||
import com.kamco.cd.kamcoback.postgres.core.ZooCoreService;
|
||||
import com.kamco.cd.kamcoback.zoo.dto.ZooDto;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
@Transactional(readOnly = true)
|
||||
public class ZooService {
|
||||
private final ZooCoreService zooCoreService;
|
||||
|
||||
// 동물원의 UUID로 id조회
|
||||
public Long getZooByUuid(String uuid) {
|
||||
return zooCoreService.getDataByUuid(uuid).getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 동물원 생성
|
||||
*
|
||||
* @param req 동물원 생성 요청
|
||||
* @return 생성된 동물원 정보 (동물 개수 포함)
|
||||
*/
|
||||
@Transactional
|
||||
public ZooDto.Detail createZoo(ZooDto.AddReq req) {
|
||||
return zooCoreService.create(req);
|
||||
}
|
||||
|
||||
/**
|
||||
* 동물원 삭제 (논리 삭제)
|
||||
*
|
||||
* @param id 동물원 ID
|
||||
*/
|
||||
@Transactional
|
||||
public void deleteZoo(Long id) {
|
||||
zooCoreService.remove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 동물원 단건 조회
|
||||
*
|
||||
* @param id 동물원 ID
|
||||
* @return 동물원 정보 (동물 개수 포함)
|
||||
*/
|
||||
public ZooDto.Detail getZoo(Long id) {
|
||||
return zooCoreService.getOneById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 동물원 검색 (페이징)
|
||||
*
|
||||
* @param searchReq 검색 조건
|
||||
* @return 페이징 처리된 동물원 목록 (각 동물원의 동물 개수 포함)
|
||||
*/
|
||||
public Page<ZooDto.Detail> search(ZooDto.SearchReq searchReq) {
|
||||
return zooCoreService.search(searchReq);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user