feat: 들여쓰기
This commit is contained in:
@@ -13,22 +13,22 @@ import org.springframework.data.annotation.LastModifiedDate;
|
||||
@MappedSuperclass
|
||||
public class CommonDateEntity {
|
||||
|
||||
@CreatedDate
|
||||
@Column(name = "created_date", updatable = false, nullable = false)
|
||||
private ZonedDateTime createdDate;
|
||||
@CreatedDate
|
||||
@Column(name = "created_date", updatable = false, nullable = false)
|
||||
private ZonedDateTime createdDate;
|
||||
|
||||
@LastModifiedDate
|
||||
@Column(name = "modified_date", nullable = false)
|
||||
private ZonedDateTime modifiedDate;
|
||||
@LastModifiedDate
|
||||
@Column(name = "modified_date", nullable = false)
|
||||
private ZonedDateTime modifiedDate;
|
||||
|
||||
@PrePersist
|
||||
protected void onPersist() {
|
||||
this.createdDate = ZonedDateTime.now();
|
||||
this.modifiedDate = ZonedDateTime.now();
|
||||
}
|
||||
@PrePersist
|
||||
protected void onPersist() {
|
||||
this.createdDate = ZonedDateTime.now();
|
||||
this.modifiedDate = ZonedDateTime.now();
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
this.modifiedDate = ZonedDateTime.now();
|
||||
}
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
this.modifiedDate = ZonedDateTime.now();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import org.springframework.context.annotation.Configuration;
|
||||
@Configuration
|
||||
public class QueryDslConfig {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
private final EntityManager entityManager;
|
||||
|
||||
@Bean
|
||||
public JPAQueryFactory jpaQueryFactory() {
|
||||
return new JPAQueryFactory(entityManager);
|
||||
}
|
||||
@Bean
|
||||
public JPAQueryFactory jpaQueryFactory() {
|
||||
return new JPAQueryFactory(entityManager);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,67 +16,59 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
public class AnimalCoreService
|
||||
implements BaseCoreService<AnimalDto.Basic, Long, AnimalDto.SearchReq> {
|
||||
implements BaseCoreService<AnimalDto.Basic, Long, AnimalDto.SearchReq> {
|
||||
|
||||
private final AnimalRepository animalRepository;
|
||||
private final ZooRepository zooRepository;
|
||||
private final AnimalRepository animalRepository;
|
||||
private final ZooRepository zooRepository;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public AnimalDto.Basic getDataByUuid(String uuid) {
|
||||
AnimalEntity getZoo =
|
||||
animalRepository
|
||||
.getAnimalByUuid(uuid)
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new EntityNotFoundException(
|
||||
"Zoo not found with uuid: " + uuid));
|
||||
return getZoo.toDto();
|
||||
@Transactional(readOnly = true)
|
||||
public AnimalDto.Basic getDataByUuid(String uuid) {
|
||||
AnimalEntity getZoo =
|
||||
animalRepository
|
||||
.getAnimalByUuid(uuid)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Zoo not found with uuid: " + uuid));
|
||||
return getZoo.toDto();
|
||||
}
|
||||
|
||||
// AddReq를 받는 추가 메서드
|
||||
@Transactional
|
||||
public AnimalDto.Basic create(AnimalDto.AddReq req) {
|
||||
ZooEntity zoo = null;
|
||||
if (req.getZooId() != null) {
|
||||
zoo =
|
||||
zooRepository
|
||||
.getZooByUid(req.getZooId())
|
||||
.orElseThrow(
|
||||
() -> new EntityNotFoundException(" not found with id: " + req.getZooId()));
|
||||
}
|
||||
AnimalEntity entity = new AnimalEntity(req.getCategory(), req.getSpecies(), req.getName(), zoo);
|
||||
AnimalEntity saved = animalRepository.save(entity);
|
||||
return saved.toDto();
|
||||
}
|
||||
|
||||
// AddReq를 받는 추가 메서드
|
||||
@Transactional
|
||||
public AnimalDto.Basic create(AnimalDto.AddReq req) {
|
||||
ZooEntity zoo = null;
|
||||
if (req.getZooId() != null) {
|
||||
zoo =
|
||||
zooRepository
|
||||
.getZooByUid(req.getZooId())
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new EntityNotFoundException(
|
||||
"Zoo not found with id: " + req.getZooId()));
|
||||
}
|
||||
AnimalEntity entity =
|
||||
new AnimalEntity(req.getCategory(), req.getSpecies(), req.getName(), zoo);
|
||||
AnimalEntity saved = animalRepository.save(entity);
|
||||
return saved.toDto();
|
||||
}
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
AnimalEntity getAnimal =
|
||||
animalRepository
|
||||
.getAnimalByUid(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("getAnimal not found with id: " + id));
|
||||
getAnimal.deleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
AnimalEntity getZoo =
|
||||
animalRepository
|
||||
.getAnimalByUid(id)
|
||||
.orElseThrow(
|
||||
() -> new EntityNotFoundException("Zoo not found with id: " + id));
|
||||
getZoo.deleted();
|
||||
}
|
||||
@Override
|
||||
public AnimalDto.Basic getOneById(Long id) {
|
||||
AnimalEntity getAnimal =
|
||||
animalRepository
|
||||
.getAnimalByUid(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Zoo not found with id: " + id));
|
||||
return getAnimal.toDto();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnimalDto.Basic getOneById(Long id) {
|
||||
AnimalEntity getZoo =
|
||||
animalRepository
|
||||
.getAnimalByUid(id)
|
||||
.orElseThrow(
|
||||
() -> new EntityNotFoundException("Zoo not found with id: " + id));
|
||||
return getZoo.toDto();
|
||||
}
|
||||
@Override
|
||||
public Page<AnimalDto.Basic> search(AnimalDto.SearchReq searchReq) {
|
||||
|
||||
@Override
|
||||
public Page<AnimalDto.Basic> search(AnimalDto.SearchReq searchReq) {
|
||||
|
||||
Page<AnimalEntity> zooEntities = animalRepository.listAnimal(searchReq);
|
||||
return zooEntities.map(AnimalEntity::toDto);
|
||||
}
|
||||
Page<AnimalEntity> animalEntities = animalRepository.listAnimal(searchReq);
|
||||
return animalEntities.map(AnimalEntity::toDto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,66 +15,61 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@Transactional(readOnly = true)
|
||||
public class ZooCoreService implements BaseCoreService<ZooDto.Detail, Long, ZooDto.SearchReq> {
|
||||
|
||||
private final ZooRepository zooRepository;
|
||||
private final ZooRepository zooRepository;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ZooDto.Detail getDataByUuid(String uuid) {
|
||||
ZooEntity zoo =
|
||||
zooRepository
|
||||
.getZooByUuid(uuid)
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new EntityNotFoundException(
|
||||
"Zoo not found with uuid: " + uuid));
|
||||
return toDetailDto(zoo);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public ZooDto.Detail getDataByUuid(String uuid) {
|
||||
ZooEntity zoo =
|
||||
zooRepository
|
||||
.getZooByUuid(uuid)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Zoo not found with uuid: " + uuid));
|
||||
return toDetailDto(zoo);
|
||||
}
|
||||
|
||||
// AddReq를 받는 추가 메서드
|
||||
@Transactional
|
||||
public ZooDto.Detail create(ZooDto.AddReq req) {
|
||||
ZooEntity entity = new ZooEntity(req.getName(), req.getLocation(), req.getDescription());
|
||||
ZooEntity saved = zooRepository.save(entity);
|
||||
return toDetailDto(saved);
|
||||
}
|
||||
// AddReq를 받는 추가 메서드
|
||||
@Transactional
|
||||
public ZooDto.Detail create(ZooDto.AddReq req) {
|
||||
ZooEntity entity = new ZooEntity(req.getName(), req.getLocation(), req.getDescription());
|
||||
ZooEntity saved = zooRepository.save(entity);
|
||||
return toDetailDto(saved);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
ZooEntity zoo =
|
||||
zooRepository
|
||||
.getZooByUid(id)
|
||||
.orElseThrow(
|
||||
() -> new EntityNotFoundException("Zoo not found with id: " + id));
|
||||
zoo.deleted();
|
||||
}
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
ZooEntity zoo =
|
||||
zooRepository
|
||||
.getZooByUid(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Zoo not found with id: " + id));
|
||||
zoo.deleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZooDto.Detail getOneById(Long id) {
|
||||
ZooEntity zoo =
|
||||
zooRepository
|
||||
.getZooByUid(id)
|
||||
.orElseThrow(
|
||||
() -> new EntityNotFoundException("Zoo not found with id: " + id));
|
||||
return toDetailDto(zoo);
|
||||
}
|
||||
@Override
|
||||
public ZooDto.Detail getOneById(Long id) {
|
||||
ZooEntity zoo =
|
||||
zooRepository
|
||||
.getZooByUid(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Zoo not found with id: " + id));
|
||||
return toDetailDto(zoo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<ZooDto.Detail> search(ZooDto.SearchReq searchReq) {
|
||||
Page<ZooEntity> zooEntities = zooRepository.listZoo(searchReq);
|
||||
return zooEntities.map(this::toDetailDto);
|
||||
}
|
||||
@Override
|
||||
public Page<ZooDto.Detail> search(ZooDto.SearchReq searchReq) {
|
||||
Page<ZooEntity> zooEntities = zooRepository.listZoo(searchReq);
|
||||
return zooEntities.map(this::toDetailDto);
|
||||
}
|
||||
|
||||
// Entity -> Detail DTO 변환 (동물 개수 포함)
|
||||
private ZooDto.Detail toDetailDto(ZooEntity zoo) {
|
||||
Long activeAnimalCount = zooRepository.countActiveAnimals(zoo.getUid());
|
||||
return new ZooDto.Detail(
|
||||
zoo.getUid(),
|
||||
zoo.getUuid().toString(),
|
||||
zoo.getName(),
|
||||
zoo.getLocation(),
|
||||
zoo.getDescription(),
|
||||
zoo.getCreatedDate(),
|
||||
zoo.getModifiedDate(),
|
||||
activeAnimalCount);
|
||||
}
|
||||
// Entity -> Detail DTO 변환 (동물 개수 포함)
|
||||
private ZooDto.Detail toDetailDto(ZooEntity zoo) {
|
||||
Long activeAnimalCount = zooRepository.countActiveAnimals(zoo.getUid());
|
||||
return new ZooDto.Detail(
|
||||
zoo.getUid(),
|
||||
zoo.getUuid().toString(),
|
||||
zoo.getName(),
|
||||
zoo.getLocation(),
|
||||
zoo.getDescription(),
|
||||
zoo.getCreatedDate(),
|
||||
zoo.getModifiedDate(),
|
||||
activeAnimalCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,48 +28,48 @@ import lombok.NoArgsConstructor;
|
||||
@Table(name = "tb_animal")
|
||||
public class AnimalEntity extends CommonDateEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long uid;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long uid;
|
||||
|
||||
@Column(unique = true)
|
||||
private UUID uuid;
|
||||
@Column(unique = true)
|
||||
private UUID uuid;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Category category;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Category category;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Species species;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Species species;
|
||||
|
||||
private String name;
|
||||
private Boolean isDeleted;
|
||||
private String name;
|
||||
private Boolean isDeleted;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "zoo_id")
|
||||
private ZooEntity zoo;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "zoo_id")
|
||||
private ZooEntity zoo;
|
||||
|
||||
// Construct
|
||||
public AnimalEntity(Category category, Species species, String name, ZooEntity zoo) {
|
||||
this.uuid = UUID.randomUUID();
|
||||
this.category = category;
|
||||
this.species = species;
|
||||
this.name = name;
|
||||
this.isDeleted = false;
|
||||
this.zoo = zoo;
|
||||
}
|
||||
// Construct
|
||||
public AnimalEntity(Category category, Species species, String name, ZooEntity zoo) {
|
||||
this.uuid = UUID.randomUUID();
|
||||
this.category = category;
|
||||
this.species = species;
|
||||
this.name = name;
|
||||
this.isDeleted = false;
|
||||
this.zoo = zoo;
|
||||
}
|
||||
|
||||
public AnimalDto.Basic toDto() {
|
||||
return new AnimalDto.Basic(
|
||||
this.uid,
|
||||
this.uuid.toString(),
|
||||
this.name,
|
||||
this.category,
|
||||
this.species,
|
||||
super.getCreatedDate(),
|
||||
super.getModifiedDate());
|
||||
}
|
||||
public AnimalDto.Basic toDto() {
|
||||
return new AnimalDto.Basic(
|
||||
this.uid,
|
||||
this.uuid.toString(),
|
||||
this.name,
|
||||
this.category,
|
||||
this.species,
|
||||
super.getCreatedDate(),
|
||||
super.getModifiedDate());
|
||||
}
|
||||
|
||||
public void deleted() {
|
||||
this.isDeleted = true;
|
||||
}
|
||||
public void deleted() {
|
||||
this.isDeleted = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,44 +23,44 @@ import lombok.NoArgsConstructor;
|
||||
@Table(name = "tb_zoo")
|
||||
public class ZooEntity extends CommonDateEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long uid;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long uid;
|
||||
|
||||
@Column(unique = true, nullable = false)
|
||||
private UUID uuid;
|
||||
@Column(unique = true, nullable = false)
|
||||
private UUID uuid;
|
||||
|
||||
@Column(nullable = false, length = 200)
|
||||
private String name;
|
||||
@Column(nullable = false, length = 200)
|
||||
private String name;
|
||||
|
||||
@Column(length = 300)
|
||||
private String location;
|
||||
@Column(length = 300)
|
||||
private String location;
|
||||
|
||||
@Column(columnDefinition = "TEXT")
|
||||
private String description;
|
||||
@Column(columnDefinition = "TEXT")
|
||||
private String description;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Boolean isDeleted;
|
||||
@Column(nullable = false)
|
||||
private Boolean isDeleted;
|
||||
|
||||
@OneToMany(mappedBy = "zoo", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
private List<AnimalEntity> animals = new ArrayList<>();
|
||||
@OneToMany(mappedBy = "zoo", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
private List<AnimalEntity> animals = new ArrayList<>();
|
||||
|
||||
// Constructor
|
||||
public ZooEntity(String name, String location, String description) {
|
||||
this.uuid = UUID.randomUUID();
|
||||
this.name = name;
|
||||
this.location = location;
|
||||
this.description = description;
|
||||
this.isDeleted = false;
|
||||
}
|
||||
// Constructor
|
||||
public ZooEntity(String name, String location, String description) {
|
||||
this.uuid = UUID.randomUUID();
|
||||
this.name = name;
|
||||
this.location = location;
|
||||
this.description = description;
|
||||
this.isDeleted = false;
|
||||
}
|
||||
|
||||
// 논리 삭제
|
||||
public void deleted() {
|
||||
this.isDeleted = true;
|
||||
}
|
||||
// 논리 삭제
|
||||
public void deleted() {
|
||||
this.isDeleted = true;
|
||||
}
|
||||
|
||||
// 현재 활성 동물 개수 조회 (삭제되지 않은 동물만)
|
||||
public long getActiveAnimalCount() {
|
||||
return animals.stream().filter(animal -> !animal.getIsDeleted()).count();
|
||||
}
|
||||
// 현재 활성 동물 개수 조회 (삭제되지 않은 동물만)
|
||||
public long getActiveAnimalCount() {
|
||||
return animals.stream().filter(animal -> !animal.getIsDeleted()).count();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,4 +4,4 @@ import com.kamco.cd.kamcoback.postgres.entity.AnimalEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface AnimalRepository
|
||||
extends JpaRepository<AnimalEntity, Long>, AnimalRepositoryCustom {}
|
||||
extends JpaRepository<AnimalEntity, Long>, AnimalRepositoryCustom {}
|
||||
|
||||
@@ -7,9 +7,9 @@ import org.springframework.data.domain.Page;
|
||||
|
||||
public interface AnimalRepositoryCustom {
|
||||
|
||||
Optional<AnimalEntity> getAnimalByUid(Long uid);
|
||||
Optional<AnimalEntity> getAnimalByUid(Long uid);
|
||||
|
||||
Optional<AnimalEntity> getAnimalByUuid(String uuid);
|
||||
Optional<AnimalEntity> getAnimalByUuid(String uuid);
|
||||
|
||||
Page<AnimalEntity> listAnimal(AnimalDto.SearchReq req);
|
||||
Page<AnimalEntity> listAnimal(AnimalDto.SearchReq req);
|
||||
}
|
||||
|
||||
@@ -16,84 +16,81 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;
|
||||
|
||||
public class AnimalRepositoryImpl extends QuerydslRepositorySupport
|
||||
implements AnimalRepositoryCustom {
|
||||
implements AnimalRepositoryCustom {
|
||||
|
||||
private final JPAQueryFactory queryFactory;
|
||||
private final JPAQueryFactory queryFactory;
|
||||
|
||||
public AnimalRepositoryImpl(JPAQueryFactory queryFactory) {
|
||||
super(AnimalEntity.class);
|
||||
this.queryFactory = queryFactory;
|
||||
public AnimalRepositoryImpl(JPAQueryFactory queryFactory) {
|
||||
super(AnimalEntity.class);
|
||||
this.queryFactory = queryFactory;
|
||||
}
|
||||
|
||||
public Optional<AnimalEntity> getAnimalByUid(Long uid) {
|
||||
QAnimalEntity animal = QAnimalEntity.animalEntity;
|
||||
|
||||
return Optional.ofNullable(
|
||||
queryFactory.selectFrom(animal).where(animal.uid.eq(uid)).fetchFirst());
|
||||
}
|
||||
|
||||
public Optional<AnimalEntity> getAnimalByUuid(String uuid) {
|
||||
QAnimalEntity animal = QAnimalEntity.animalEntity;
|
||||
|
||||
return Optional.ofNullable(
|
||||
queryFactory.selectFrom(animal).where(animal.uuid.eq(UUID.fromString(uuid))).fetchFirst());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<AnimalEntity> listAnimal(AnimalDto.SearchReq req) {
|
||||
QAnimalEntity animal = QAnimalEntity.animalEntity;
|
||||
|
||||
Pageable pageable = req.toPageable();
|
||||
|
||||
List<AnimalEntity> content =
|
||||
queryFactory
|
||||
.selectFrom(animal)
|
||||
.where(
|
||||
animal.isDeleted.eq(false),
|
||||
eqCategory(animal, req.getCategory()),
|
||||
eqSpecies(animal, req.getSpecies()),
|
||||
likeName(animal, req.getName()))
|
||||
.offset(pageable.getOffset())
|
||||
.limit(pageable.getPageSize())
|
||||
.orderBy(animal.createdDate.desc())
|
||||
.fetch();
|
||||
|
||||
// count 쿼리
|
||||
Long total =
|
||||
queryFactory
|
||||
.select(animal.count())
|
||||
.from(animal)
|
||||
.where(
|
||||
animal.isDeleted.eq(false),
|
||||
eqCategory(animal, req.getCategory()),
|
||||
eqSpecies(animal, req.getSpecies()),
|
||||
likeName(animal, req.getName()))
|
||||
.fetchOne();
|
||||
|
||||
return new PageImpl<>(content, pageable, total);
|
||||
}
|
||||
|
||||
private BooleanExpression likeName(QAnimalEntity animal, String nameStr) {
|
||||
if (nameStr == null || nameStr.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return animal.name.containsIgnoreCase(nameStr.trim());
|
||||
}
|
||||
|
||||
public Optional<AnimalEntity> getAnimalByUid(Long uid) {
|
||||
QAnimalEntity animal = QAnimalEntity.animalEntity;
|
||||
|
||||
return Optional.ofNullable(
|
||||
queryFactory.selectFrom(animal).where(animal.uid.eq(uid)).fetchFirst());
|
||||
private BooleanExpression eqCategory(QAnimalEntity animal, Category category) {
|
||||
if (category == null) {
|
||||
return null;
|
||||
}
|
||||
return animal.category.eq(category);
|
||||
}
|
||||
|
||||
public Optional<AnimalEntity> getAnimalByUuid(String uuid) {
|
||||
QAnimalEntity animal = QAnimalEntity.animalEntity;
|
||||
|
||||
return Optional.ofNullable(
|
||||
queryFactory
|
||||
.selectFrom(animal)
|
||||
.where(animal.uuid.eq(UUID.fromString(uuid)))
|
||||
.fetchFirst());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<AnimalEntity> listAnimal(AnimalDto.SearchReq req) {
|
||||
QAnimalEntity animal = QAnimalEntity.animalEntity;
|
||||
|
||||
Pageable pageable = req.toPageable();
|
||||
|
||||
List<AnimalEntity> content =
|
||||
queryFactory
|
||||
.selectFrom(animal)
|
||||
.where(
|
||||
animal.isDeleted.eq(false),
|
||||
eqCategory(animal, req.getCategory()),
|
||||
eqSpecies(animal, req.getSpecies()),
|
||||
likeName(animal, req.getName()))
|
||||
.offset(pageable.getOffset())
|
||||
.limit(pageable.getPageSize())
|
||||
.orderBy(animal.createdDate.desc())
|
||||
.fetch();
|
||||
|
||||
// count 쿼리
|
||||
Long total =
|
||||
queryFactory
|
||||
.select(animal.count())
|
||||
.from(animal)
|
||||
.where(
|
||||
animal.isDeleted.eq(false),
|
||||
eqCategory(animal, req.getCategory()),
|
||||
eqSpecies(animal, req.getSpecies()),
|
||||
likeName(animal, req.getName()))
|
||||
.fetchOne();
|
||||
|
||||
return new PageImpl<>(content, pageable, total);
|
||||
}
|
||||
|
||||
private BooleanExpression likeName(QAnimalEntity animal, String nameStr) {
|
||||
if (nameStr == null || nameStr.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return animal.name.containsIgnoreCase(nameStr.trim());
|
||||
}
|
||||
|
||||
private BooleanExpression eqCategory(QAnimalEntity animal, Category category) {
|
||||
if (category == null) {
|
||||
return null;
|
||||
}
|
||||
return animal.category.eq(category);
|
||||
}
|
||||
|
||||
private BooleanExpression eqSpecies(QAnimalEntity animal, Species species) {
|
||||
if (species == null) {
|
||||
return null;
|
||||
}
|
||||
return animal.species.eq(species);
|
||||
private BooleanExpression eqSpecies(QAnimalEntity animal, Species species) {
|
||||
if (species == null) {
|
||||
return null;
|
||||
}
|
||||
return animal.species.eq(species);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ import org.springframework.data.domain.Page;
|
||||
|
||||
public interface ZooRepositoryCustom {
|
||||
|
||||
Page<ZooEntity> listZoo(ZooDto.SearchReq searchReq);
|
||||
Page<ZooEntity> listZoo(ZooDto.SearchReq searchReq);
|
||||
|
||||
Optional<ZooEntity> getZooByUuid(String uuid);
|
||||
Optional<ZooEntity> getZooByUuid(String uuid);
|
||||
|
||||
Optional<ZooEntity> getZooByUid(Long uid);
|
||||
Optional<ZooEntity> getZooByUid(Long uid);
|
||||
|
||||
Long countActiveAnimals(Long zooId);
|
||||
Long countActiveAnimals(Long zooId);
|
||||
}
|
||||
|
||||
@@ -20,68 +20,66 @@ import org.springframework.stereotype.Repository;
|
||||
@RequiredArgsConstructor
|
||||
public class ZooRepositoryImpl implements ZooRepositoryCustom {
|
||||
|
||||
private final JPAQueryFactory queryFactory;
|
||||
private final QZooEntity qZoo = QZooEntity.zooEntity;
|
||||
private final QAnimalEntity qAnimal = QAnimalEntity.animalEntity;
|
||||
private final JPAQueryFactory queryFactory;
|
||||
private final QZooEntity qZoo = QZooEntity.zooEntity;
|
||||
private final QAnimalEntity qAnimal = QAnimalEntity.animalEntity;
|
||||
|
||||
@Override
|
||||
public Page<ZooEntity> listZoo(ZooDto.SearchReq searchReq) {
|
||||
Pageable pageable = searchReq.toPageable();
|
||||
@Override
|
||||
public Page<ZooEntity> listZoo(ZooDto.SearchReq searchReq) {
|
||||
Pageable pageable = searchReq.toPageable();
|
||||
|
||||
JPAQuery<ZooEntity> query =
|
||||
queryFactory
|
||||
.selectFrom(qZoo)
|
||||
.where(
|
||||
qZoo.isDeleted.eq(false),
|
||||
nameContains(searchReq.getName()),
|
||||
locationContains(searchReq.getLocation()));
|
||||
JPAQuery<ZooEntity> query =
|
||||
queryFactory
|
||||
.selectFrom(qZoo)
|
||||
.where(
|
||||
qZoo.isDeleted.eq(false),
|
||||
nameContains(searchReq.getName()),
|
||||
locationContains(searchReq.getLocation()));
|
||||
|
||||
long total = query.fetchCount();
|
||||
long total = query.fetchCount();
|
||||
|
||||
List<ZooEntity> content =
|
||||
query.offset(pageable.getOffset())
|
||||
.limit(pageable.getPageSize())
|
||||
.orderBy(qZoo.createdDate.desc())
|
||||
.fetch();
|
||||
List<ZooEntity> content =
|
||||
query
|
||||
.offset(pageable.getOffset())
|
||||
.limit(pageable.getPageSize())
|
||||
.orderBy(qZoo.createdDate.desc())
|
||||
.fetch();
|
||||
|
||||
return new PageImpl<>(content, pageable, total);
|
||||
}
|
||||
return new PageImpl<>(content, pageable, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ZooEntity> getZooByUuid(String uuid) {
|
||||
return Optional.ofNullable(
|
||||
queryFactory
|
||||
.selectFrom(qZoo)
|
||||
.where(qZoo.uuid.eq(UUID.fromString(uuid)), qZoo.isDeleted.eq(false))
|
||||
.fetchOne());
|
||||
}
|
||||
@Override
|
||||
public Optional<ZooEntity> getZooByUuid(String uuid) {
|
||||
return Optional.ofNullable(
|
||||
queryFactory
|
||||
.selectFrom(qZoo)
|
||||
.where(qZoo.uuid.eq(UUID.fromString(uuid)), qZoo.isDeleted.eq(false))
|
||||
.fetchOne());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ZooEntity> getZooByUid(Long uid) {
|
||||
return Optional.ofNullable(
|
||||
queryFactory
|
||||
.selectFrom(qZoo)
|
||||
.where(qZoo.uid.eq(uid), qZoo.isDeleted.eq(false))
|
||||
.fetchOne());
|
||||
}
|
||||
@Override
|
||||
public Optional<ZooEntity> getZooByUid(Long uid) {
|
||||
return Optional.ofNullable(
|
||||
queryFactory.selectFrom(qZoo).where(qZoo.uid.eq(uid), qZoo.isDeleted.eq(false)).fetchOne());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countActiveAnimals(Long zooId) {
|
||||
Long count =
|
||||
queryFactory
|
||||
.select(qAnimal.count())
|
||||
.from(qAnimal)
|
||||
.where(qAnimal.zoo.uid.eq(zooId), qAnimal.isDeleted.eq(false))
|
||||
.fetchOne();
|
||||
@Override
|
||||
public Long countActiveAnimals(Long zooId) {
|
||||
Long count =
|
||||
queryFactory
|
||||
.select(qAnimal.count())
|
||||
.from(qAnimal)
|
||||
.where(qAnimal.zoo.uid.eq(zooId), qAnimal.isDeleted.eq(false))
|
||||
.fetchOne();
|
||||
|
||||
return count != null ? count : 0L;
|
||||
}
|
||||
return count != null ? count : 0L;
|
||||
}
|
||||
|
||||
private BooleanExpression nameContains(String name) {
|
||||
return name != null && !name.isEmpty() ? qZoo.name.contains(name) : null;
|
||||
}
|
||||
private BooleanExpression nameContains(String name) {
|
||||
return name != null && !name.isEmpty() ? qZoo.name.contains(name) : null;
|
||||
}
|
||||
|
||||
private BooleanExpression locationContains(String location) {
|
||||
return location != null && !location.isEmpty() ? qZoo.location.contains(location) : null;
|
||||
}
|
||||
private BooleanExpression locationContains(String location) {
|
||||
return location != null && !location.isEmpty() ? qZoo.location.contains(location) : null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user