Skip to content

Commit

Permalink
[FEAT] 반려동물 정보 조회 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
a-young-kim committed Aug 29, 2024
1 parent 677df52 commit 1b87a78
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Account {
@Column(name = "account_color")
private String accountColor;

@ManyToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public String updateAccountImage(Long memberId, MultipartFile newImage) {
//url을 통해 S3에서 이미지 가져오기
String url = awsService.getImageUrl(petImage);

pet.updatePetImage(url);
pet.setPetImage(url);
petRepository.save(pet);

return url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fav.daengnyang.domain.pet.service.PetService;
import com.fav.daengnyang.domain.pet.service.dto.request.CreatedPetRequest;
import com.fav.daengnyang.domain.pet.service.dto.request.UpdatedPetRequest;
import com.fav.daengnyang.domain.pet.service.dto.response.GetPetResponse;
import com.fav.daengnyang.global.auth.dto.MemberPrincipal;
import com.fav.daengnyang.global.web.dto.response.SuccessResponse;
Expand Down Expand Up @@ -38,4 +39,11 @@ public ResponseEntity<SuccessResponse<GetPetResponse>> getPet(@AuthenticationPri
return ResponseEntity.ok(SuccessResponse.ok(response));
}

// 반려동물 정보 수정
@PatchMapping
public SuccessResponse<?> updatePet(@AuthenticationPrincipal MemberPrincipal memberPrincipal, @Valid UpdatedPetRequest updatedPetRequest){
petService.updatePet(updatedPetRequest, memberPrincipal.getMemberId());
return SuccessResponse.update();
}

}
17 changes: 7 additions & 10 deletions src/main/java/com/fav/daengnyang/domain/pet/entity/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fav.daengnyang.domain.member.entity.Member;
import com.fav.daengnyang.domain.pet.service.dto.request.CreatedPetRequest;
import com.fav.daengnyang.domain.pet.service.dto.request.UpdatedPetRequest;
import jakarta.persistence.*;
import lombok.*;

Expand Down Expand Up @@ -62,17 +63,13 @@ public static Pet createPet(CreatedPetRequest createdPetRequest, Member member,
.build();
}

public void updatePet(CreatedPetRequest createdPetRequest, String petImage) {
this.petName = createdPetRequest.getPetName();
this.petGender = createdPetRequest.getPetGender();
this.petType = createdPetRequest.getPetType();
this.petBirth = createdPetRequest.getPetBirth();
this.specie = createdPetRequest.getSpecie();
this.petImage = petImage;
public void updatePet(UpdatedPetRequest updatedPetRequest) {
this.petName = updatedPetRequest.getPetName();
this.petGender = updatedPetRequest.getPetGender();
this.petType = updatedPetRequest.getPetType();
this.petBirth = updatedPetRequest.getPetBirth();
this.specie = updatedPetRequest.getSpecie();
}

public void updatePetImage(String petImage){
this.petImage = petImage;
}
}

28 changes: 17 additions & 11 deletions src/main/java/com/fav/daengnyang/domain/pet/service/PetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fav.daengnyang.domain.pet.entity.Pet;
import com.fav.daengnyang.domain.pet.repository.PetRepository;
import com.fav.daengnyang.domain.pet.service.dto.request.CreatedPetRequest;
import com.fav.daengnyang.domain.pet.service.dto.request.UpdatedPetRequest;
import com.fav.daengnyang.domain.pet.service.dto.response.GetPetResponse;
import com.fav.daengnyang.global.aws.service.AwsService;
import com.fav.daengnyang.global.exception.CustomException;
Expand Down Expand Up @@ -39,17 +40,7 @@ public Long createPet(CreatedPetRequest createdPetRequest, Long memberId) {
String url = awsService.getImageUrl(petImage);
log.info("url : " + url);

// 펫 정보 조회
Optional<Pet> existingPetOptional = petRepository.findByMemberMemberId(memberId);
Pet pet;
if (existingPetOptional.isPresent()) {
pet = existingPetOptional.get();
pet.updatePet(createdPetRequest, url);
} else {
// 해당 url을 가진 Pet 객체가 없으면 새로 생성
pet = Pet.createPet(createdPetRequest, member, url);
}

Pet pet = Pet.createPet(createdPetRequest, member, url);
pet = petRepository.save(pet);
return pet.getPetId();
}
Expand All @@ -62,4 +53,19 @@ public GetPetResponse getPet(Long memberId) {

return GetPetResponse.createGetPetResponse(pet);
}

public void updatePet(UpdatedPetRequest updatedPetRequest, Long memberId) {
Pet pet = petRepository.findByMemberMemberId(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.PET_NOT_FOUND));

if(updatedPetRequest.getPetImage() != null) {
String petImage = awsService.uploadFile(updatedPetRequest.getPetImage(), memberId);
//url을 통해 S3에서 이미지 가져오기
String url = awsService.getImageUrl(petImage);
pet.setPetImage(url);
}

pet.updatePet(updatedPetRequest);
petRepository.save(pet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fav.daengnyang.domain.pet.service.dto.request;

import lombok.*;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.NotBlank;
import org.springframework.web.multipart.MultipartFile;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@ToString
public class UpdatedPetRequest {
@NotBlank
private String petName;
@NotBlank
private String petGender;
@NotBlank
private String petType;
private MultipartFile petImage;
private String petBirth;
private String specie;
}

0 comments on commit 1b87a78

Please sign in to comment.