Skip to content

Commit

Permalink
refactor: 보유견종 PATCH API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Suxxxxhyun committed Jan 31, 2024
1 parent e31ff2f commit 50c9d1d
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 111 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
👤 팀원 소개
---

**박수현(팀장)**
**박수현(백엔드 및 Infra, 팀장)**
- 데이터베이스 설계
- nginx, docker, CICD를 통한 ec2배포
- 아키텍처 설계브리더 모아보기 거리순 정렬 및 페이징처리
Expand All @@ -39,7 +39,7 @@
- 분양신청내역 페이징 처리
- 회원 정보 수정

**오창현(프론트 부팀장)**
**오창현(프론트, 부팀장)**
- 로그인
- 회원가입
- 회원탈퇴
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static class ReviewUpdateRequestDto {
@Length(max = 3000, message = "내용은 최대 3000자까지 입력 가능합니다.")
@Schema(description = "내용", example = "정성스럽게 키우곘습니다!")
private String content;
@NotBlank(message = "이미지 삭제 여부는 필수 입력값입니다.")
@Schema(description = "이미지 삭제 여부")
private boolean deleteImages;
@NotEmpty(message = "이미지는 필수 입력값입니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.petree.domain.dog.controller;

import com.example.petree.domain.adopter.domain.Adopter;
import com.example.petree.domain.adopter.domain.Review;
import com.example.petree.domain.breeder.domain.Breeder;
import com.example.petree.domain.breeder.repository.BreederRepository;
import com.example.petree.domain.dog.domain.Dog;
Expand All @@ -11,6 +13,8 @@
import com.example.petree.domain.main_breed.domain.DogType;
import com.example.petree.domain.main_breed.domain.MainBreed;
import com.example.petree.domain.main_breed.service.MainBreedService;
import com.example.petree.domain.member.domain.Member;
import com.example.petree.domain.member.repository.MemberRepository;
import com.example.petree.global.Response;
import com.example.petree.global.ResponseSchema;
import com.example.petree.global.error.exception.MissingPrincipalException;
Expand All @@ -23,6 +27,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -70,12 +75,14 @@
@RequiredArgsConstructor
@RequestMapping("/api/breeder/dogs")
@Tag(name = "브리더 회원, 마이페이지 보유견종관리", description = "브리더 회원, 마이페이지 보유견종관리 API")
@Slf4j
public class PossessionController {

private final DogService dogService;
private final DogRepository dogRepository;
private final Response response;
private final BreederRepository breederRepository;
private final MemberRepository memberRepository;

@GetMapping
@Operation(
Expand Down Expand Up @@ -194,17 +201,37 @@ public ResponseEntity<?> update(Principal principal,
if (principal == null) {
throw new MissingPrincipalException();
}
Breeder breeder= breederRepository.findByEmail(principal.getName()).orElse(null);
if(breeder != null) {

dogService.deleteAndAddImages(breeder,id,possessionDogDto);
Dog dog = dogRepository.findById(id).orElse(null);

dogService.update(breeder ,id, possessionDogDto);
if (dog == null) {
return response.fail(HttpStatus.NOT_FOUND, "해당 ID의 보유견종 글을 찾을 수 없습니다.");
}

return response.success(HttpStatus.OK, possessionDogDto);
Member member = memberRepository.findByEmail(principal.getName()).orElse(null);

}else {
return response.fail(HttpStatus.FORBIDDEN, Map.of("message", "브리더 회원이 아닙니다."));}
if (member != null) {
if (member.getRole().getTitle().equals("BREEDER")) {

int maxImageCount = 4;
int currentImageCount = possessionDogDto.getDogImgFiles().size();
log.info(currentImageCount + "요청한 이미지 개수");
if(currentImageCount <= 0){
return response.fail(HttpStatus.BAD_REQUEST, "이미지는 필수로 등록해야합니다.");
}

if (currentImageCount > maxImageCount) {
return response.fail(HttpStatus.BAD_REQUEST, "이미지는 최대 " + maxImageCount + "개까지 업로드 가능합니다.");
}

dogService.update((Breeder) member, dog, possessionDogDto);
return response.success(HttpStatus.OK, "수정이 성공적으로 이루어졌습니다.");
} else {
return response.fail(HttpStatus.FORBIDDEN, "브리더 회원이 아닙니다.");
}
} else {
return response.fail(HttpStatus.UNAUTHORIZED, "로그인이 필요합니다.");
}
}


Expand Down
17 changes: 6 additions & 11 deletions src/main/java/com/example/petree/domain/dog/domain/DogImgFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.example.petree.domain.dog.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;

Expand All @@ -10,6 +13,9 @@
@Entity
@Table(name = "dog_img_file")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DogImgFile {
@Id
@GeneratedValue(strategy = GenerationType.AUTO,generator = "native")
Expand All @@ -18,18 +24,7 @@ public class DogImgFile {
private String originalFileName;
private String fileName;
private String fileUrl;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dog_id")
private Dog dog;

public DogImgFile (String originalFileName, String fileName, String fileUrl,Dog dog) {
this.originalFileName= originalFileName;
this.fileName= fileName;
this.fileUrl=fileUrl;
this.dog=dog;
}
public DogImgFile() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,37 @@ public List<MultipartFile> getDogImgFiles() {
public static class UpdateDogDto{

@Schema(description = "보유 견종 성별", example = "FEMALE")
@NotNull(message = " 강아지의 성별은 필수 입력 값입니다.")
private Gender gender;

@Schema(description = "보유 견종 출생일", example = "2021-03-21")
@NotNull(message = "강아지의 출생일은 필수 입력 값입니다.")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthDate;

@Schema(description = "보유 견종 이름", example = "이땅콩")
@NotBlank(message = "강아지의 이름은 필수 입력 값입니다.")
private String name;

@Schema(description = "보유 견종 특징", example = "3.7kg의 소형견")
@NotBlank(message = "강아지의 상세내용은 필수 입력 값입니다.")
@Size(max = 2000, message = "특징은 2000자 이내로 작성이 가능합니다.")
private String management;

@Schema(description = "보유 견종 입양 진행 상태",example = "AVAILABLE")
@NotBlank(message = "보유 견종 입양 진행 상태는 필수 입력 값입니다.")
private Status status;

@Schema(description = "삭제할 이미지 파일 이름 목록")
private List<String> imgNameToDelete;
@Schema(description = "삭제할 이미지 파일 ID값")
@NotBlank(message = "삭제할 이미지 파일 ID값은 필수 입력 값입니다.")
private List<String> imgIdToDelete;

@Schema(description = "이미지 업로드 여부 ", example = "false")
private boolean uploadImage;
@Schema(description = "이미지 삭제 여부 ", example = "false")
@NotBlank(message = "이미지 삭제 여부는 필수 입력 값입니다.")
private boolean isDeleteImages;

@Schema(description = "보유 견종의 이미지를 입력받는 리스트")
@NotBlank(message = "보유 견종의 이미지는 필수 입력 값입니다.")
@JsonSerialize(using = ToStringSerializer.class)
private List<MultipartFile> dogImgFiles;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface DogImgFileRepository extends JpaRepository<DogImgFile, Long> {
List<DogImgFile> findByDogId(Long id);

void deleteByOriginalFileNameAndDogId(String originalFileName,Long dogId);
void deleteByIdAndDogId(Long fileId,Long dogId);

DogImgFile findByOriginalFileNameAndDogId(String originalFileName,Long dogId);
DogImgFile findByIdAndDogId(Long fileId,Long dogId);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.example.petree.domain.dog.repository;

import com.example.petree.domain.adopter.domain.Review;
import com.example.petree.domain.dog.domain.Dog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
Expand All @@ -29,4 +33,9 @@ public interface DogRepository extends JpaRepository<Dog, Long>, JpaSpecificatio

Optional<Dog> findById(Long id);
void delete(Dog dog);

@Transactional
@Modifying
@Query("DELETE FROM DogImgFile WHERE dog = :dog")
void deleteDogImgFilesByDog(@Param("dog") Dog dog);
}
Loading

0 comments on commit 50c9d1d

Please sign in to comment.