Skip to content

Commit

Permalink
PetPolygonProfile Entity 제거, Embedded타입으로 Pet Entity에 삽입
Browse files Browse the repository at this point in the history
PetPolygonProfile Entity 제거, Embedded타입으로 Pet Entity에 삽입
  • Loading branch information
xGreenNarae authored Oct 15, 2023
2 parents 6b8d06e + 5b392bf commit 94ffd1e
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 123 deletions.
3 changes: 3 additions & 0 deletions animory/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
// flyway
implementation 'org.flywaydb:flyway-core'
implementation 'org.flywaydb:flyway-mysql'

// api-doc (swagger)
implementation 'org.springdoc:springdoc-openapi-ui:1.7.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
package com.daggle.animory.domain.pet.dto;

import com.daggle.animory.domain.pet.entity.Pet;
import com.daggle.animory.domain.pet.entity.PetPolygonProfile;
import lombok.Builder;

@Builder
public record PetPolygonProfileDto(
int intelligence,
int affinity,
int athletic,
int adaptability,
int activeness
int intelligence,
int affinity,
int athletic,
int adaptability,
int activeness
) {

public PetPolygonProfile toEntity(final Pet pet) {
public PetPolygonProfile toEntity() {
return PetPolygonProfile.builder()
.pet(pet)
.intelligence(intelligence)
.affinity(affinity)
.athletic(athletic)
.adaptability(adaptability)
.activeness(activeness)
.build();
.intelligence(intelligence)
.affinity(affinity)
.athletic(athletic)
.adaptability(adaptability)
.activeness(activeness)
.build();
}

public static PetPolygonProfileDto fromEntity(final PetPolygonProfile petPolygonProfile) {
return PetPolygonProfileDto.builder()
.intelligence(petPolygonProfile.getIntelligence())
.affinity(petPolygonProfile.getAffinity())
.athletic(petPolygonProfile.getAthletic())
.adaptability(petPolygonProfile.getAdaptability())
.activeness(petPolygonProfile.getActiveness())
.build();
.intelligence(petPolygonProfile.getIntelligence())
.affinity(petPolygonProfile.getAffinity())
.athletic(petPolygonProfile.getAthletic())
.adaptability(petPolygonProfile.getAdaptability())
.activeness(petPolygonProfile.getActiveness())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Pet toEntity(final Shelter shelter, final String imageUrl, final String v
.profileImageUrl(imageUrl)
.profileShortFormUrl(videoUrl)
.size(size)
.petPolygonProfile(petPolygonProfileDto.toEntity())
.shelter(shelter)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

@Builder
public record PetUpdateRequestDto(
@NotNull(message = "이름을 입력해주세요.") String name,
@NotNull(message = "나이를 입력해주세요.") String age,
@NotNull(message = "종을 입력해주세요.") PetType type,
float weight,
String size,
@NotNull(message = "성별을 입력해주세요.") Sex sex,
String vaccinationStatus,
@NotNull(message = "입양 상태를 입력해주세요.") AdoptionStatus adoptionStatus,
NeutralizationStatus neutralizationStatus,
String description,
LocalDate protectionExpirationDate,
PetPolygonProfileDto petPolygonProfileDto
@NotNull(message = "이름을 입력해주세요.") String name,
@NotNull(message = "나이를 입력해주세요.") String age,
@NotNull(message = "종을 입력해주세요.") PetType type,
float weight,
String size,
@NotNull(message = "성별을 입력해주세요.") Sex sex,
String vaccinationStatus,
@NotNull(message = "입양 상태를 입력해주세요.") AdoptionStatus adoptionStatus,
NeutralizationStatus neutralizationStatus,
String description,
LocalDate protectionExpirationDate,
PetPolygonProfileDto petPolygonProfileDto
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public record PetDto(
) {


public static PetDto fromEntity(final Pet pet, final PetPolygonProfile petPolygonProfile) {
public static PetDto fromEntity(final Pet pet) {
return PetDto.builder()
.shelterInfo(ShelterInfoDto.fromEntity(pet.getShelter()))
.name(pet.getName())
Expand All @@ -42,7 +42,7 @@ public static PetDto fromEntity(final Pet pet, final PetPolygonProfile petPolygo
.adoptionStatus(pet.getAdoptionStatus())
.profileImageUrl(pet.getProfileImageUrl())
.size(pet.getSize())
.petPolygonProfileDto(PetPolygonProfileDto.fromEntity(petPolygonProfile))
.petPolygonProfileDto(PetPolygonProfileDto.fromEntity(pet.getPetPolygonProfile()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public record PetRegisterInfoDto(
PetPolygonProfileDto petPolygonProfileDto
) {

public static PetRegisterInfoDto fromEntity(final Pet registerPet,
final PetPolygonProfile petPolygonProfile) {
public static PetRegisterInfoDto fromEntity(final Pet registerPet) {
return PetRegisterInfoDto.builder()
.name(registerPet.getName())
.age(PetAgeToBirthDateConverter.birthDateToAge(registerPet.getBirthDate()))
Expand All @@ -41,7 +40,7 @@ public static PetRegisterInfoDto fromEntity(final Pet registerPet,
.description(registerPet.getDescription())
.profileImageUrl(registerPet.getProfileImageUrl())
.profileShortFormUrl(registerPet.getProfileShortFormUrl())
.petPolygonProfileDto(PetPolygonProfileDto.fromEntity(petPolygonProfile))
.petPolygonProfileDto(PetPolygonProfileDto.fromEntity(registerPet.getPetPolygonProfile()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class Pet extends BaseEntity {

private String size;

@Embedded
private PetPolygonProfile petPolygonProfile;

@ManyToOne
@JoinColumn(name = "shelter_id")
private Shelter shelter;
Expand Down Expand Up @@ -91,6 +94,7 @@ public void updateInfo(final PetUpdateRequestDto petUpdateRequestDto) {
this.neutralizationStatus = petUpdateRequestDto.neutralizationStatus();
this.adoptionStatus = petUpdateRequestDto.adoptionStatus();
this.protectionExpirationDate = petUpdateRequestDto.protectionExpirationDate();
this.petPolygonProfile = petUpdateRequestDto.petPolygonProfileDto().toEntity();
this.description = petUpdateRequestDto.description();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@

import javax.persistence.*;

@Entity
@Embeddable
@Getter
@Builder
@AllArgsConstructor
@Table(name = "pet_polygon_profile")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PetPolygonProfile {

@Id
private Integer id;

@MapsId
@OneToOne
@JoinColumn(name = "pet_id")
private Pet pet;

private int intelligence;

private int affinity;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,46 @@
import com.daggle.animory.domain.pet.entity.Pet;
import com.daggle.animory.domain.pet.entity.PetType;
import com.daggle.animory.domain.shelter.entity.Province;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface PetRepository extends JpaRepository<Pet, Integer> {

@Query("select p " +
"from Pet p " +
"join fetch p.shelter s " +
"where s.address.province = :province " +
"and p.type = :petType")
"from Pet p " +
"join fetch p.shelter s " +
"where s.address.province = :province " +
"and p.type = :petType")
Slice<Pet> findSliceBy(PetType petType, Province province, Pageable searchCondition);

Slice<Pet> findSliceBy(Pageable pageable);

Page<Pet> findPageBy(Pageable pageable);

@Query("select p " +
"from Pet p " +
"where p.shelter.id = :shelterId")
"from Pet p " +
"where p.shelter.id = :shelterId")
Page<Pet> findByShelterId(Integer shelterId, Pageable pageable);

@Query(value = "SELECT p FROM Pet p"
+ " JOIN FETCH p.shelter"
+ " WHERE p.protectionExpirationDate IS NOT NULL"
+ " ORDER BY p.protectionExpirationDate ASC")
+ " JOIN FETCH p.shelter"
+ " WHERE p.protectionExpirationDate IS NOT NULL"
+ " ORDER BY p.protectionExpirationDate ASC")
List<Pet> findProfilesWithProtectionExpirationDate(Pageable pageable);

@Query(value = "SELECT p FROM Pet p"
+ " JOIN FETCH p.shelter"
+ " ORDER BY p.createdAt DESC")
+ " JOIN FETCH p.shelter"
+ " ORDER BY p.createdAt DESC")
List<Pet> findProfilesWithCreatedAt(Pageable pageable);

@Query(value = "SELECT p FROM Pet p"
+ " JOIN FETCH p.shelter"
+ " WHERE p.id = :petId")
+ " JOIN FETCH p.shelter"
+ " WHERE p.id = :petId")
Optional<Pet> findById(Integer petId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.daggle.animory.domain.account.entity.Account;
import com.daggle.animory.domain.pet.dto.response.*;
import com.daggle.animory.domain.pet.entity.Pet;
import com.daggle.animory.domain.pet.entity.PetPolygonProfile;
import com.daggle.animory.domain.pet.repository.PetPolygonRepository;
import com.daggle.animory.domain.pet.repository.PetRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
Expand All @@ -23,7 +21,6 @@
public class PetReadService {

private final PetRepository petRepository;
private final PetPolygonRepository petPolygonRepository;
private final PetValidator petValidator;

public PetProfilesDto getPetProfiles() {
Expand Down Expand Up @@ -54,31 +51,23 @@ public NewPetProfilesDto getPetNewProfiles(final Pageable pageable) {
public PetDto getPetDetail(final int petId) {
// petId로 Pet, PetPolygonProfile 얻어오기
final Pet pet = petRepository.findById(petId)
.orElseThrow(() -> new NotFound404("해당 동물이 존재하지 않습니다."));
.orElseThrow(() -> new NotFound404("해당 Pet이 존재하지 않습니다."));

final PetPolygonProfile petPolygonProfile = petPolygonRepository.findByPetId(petId)
.orElseThrow(() -> new NotFound404("등록된 다각형 프로필이 존재하지 않습니다."));

return PetDto.fromEntity(pet, petPolygonProfile);
return PetDto.fromEntity(pet);
}



// 기존의 펫 등록 정보 조회
public PetRegisterInfoDto getRegisterInfo(final Account account,
final int petId) {


// 펫 id로 Pet, PetPolygonProfile 얻어오기
final Pet registerPet = petRepository.findById(petId)
.orElseThrow(() -> new NotFound404("등록되지 않은 펫입니다."));

petValidator.validatePetUpdateAuthority(account, registerPet);

final PetPolygonProfile petPolygonProfile = petPolygonRepository.findByPetId(petId)
.orElseThrow(() -> new NotFound404("등록된 다각형 프로필이 존재하지 않습니다."));

// Pet -> PetRegisterInfoDto
return PetRegisterInfoDto.fromEntity(registerPet, petPolygonProfile);
return PetRegisterInfoDto.fromEntity(registerPet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.daggle.animory.domain.pet.dto.response.RegisterPetSuccessDto;
import com.daggle.animory.domain.pet.dto.response.UpdatePetSuccessDto;
import com.daggle.animory.domain.pet.entity.Pet;
import com.daggle.animory.domain.pet.entity.PetPolygonProfile;
import com.daggle.animory.domain.pet.repository.PetPolygonRepository;
import com.daggle.animory.domain.pet.repository.PetRepository;
import com.daggle.animory.domain.shelter.ShelterRepository;
import com.daggle.animory.domain.shelter.entity.Shelter;
Expand All @@ -28,7 +26,6 @@ public class PetWriteService {
private final S3FileRepository fileRepository;
private final ShelterRepository shelterRepository;
private final PetRepository petRepository;
private final PetPolygonRepository petPolygonRepository;

private final PetValidator petValidator;

Expand All @@ -50,9 +47,6 @@ public RegisterPetSuccessDto registerPet(final Account account,
final Pet registerPet = petRepository.save(
petRequestDTO.toEntity(shelter, imageUrl.toString(), videoUrl.toString()));

// 펫 다각형 프로필 DB 저장
petPolygonRepository.save(petRequestDTO.petPolygonProfileDto().toEntity(registerPet));

return new RegisterPetSuccessDto(registerPet.getId());
}

Expand All @@ -68,16 +62,12 @@ public UpdatePetSuccessDto updatePet(final Account account,

petValidator.validatePetUpdateAuthority(account, updatePet);

final PetPolygonProfile petPolygonProfile = petPolygonRepository.findByPetId(petId)
.orElseThrow(() -> new NotFound404("등록된 다각형 프로필이 존재하지 않습니다."));

// 이미지, 비디오 파일 업데이트
// TODO: 파일 수정 Rollback 처리
updateFile(updatePet, image, video);

// 펫 정보 업데이트
updatePet.updateInfo(petUpdateRequestDto);
petPolygonProfile.update(petUpdateRequestDto.petPolygonProfileDto());

return new UpdatePetSuccessDto(updatePet.getId());
}
Expand Down
8 changes: 7 additions & 1 deletion animory/src/main/resources/application-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ spring:
jpa:
show-sql: true
open-in-view: false
hibernate.ddl-auto: update
hibernate.ddl-auto: validate
properties:
hibernate.format_sql: true
# DB Scheme History Management
flyway:
url: ${DATABASE_URL}?allowPublicKeyRetrieval=true&useSSL=false
user: ${DATABASE_USERNAME}
password: ${DATABASE_PASSWORD}
locations: classpath:db/migration

# File Upload
servlet:
Expand Down
1 change: 1 addition & 0 deletions animory/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spring:
hibernate.ddl-auto: create
properties:
hibernate.format_sql: true
flyway.enabled: false # H2 Error

# File Upload
servlet:
Expand Down
Loading

0 comments on commit 94ffd1e

Please sign in to comment.