From 7dce582f03055f7dd106573d3636f702ccb22527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=AF=BC=ED=98=81?= <104811969+MinsFuture@users.noreply.github.com> Date: Wed, 12 Jun 2024 19:11:30 +0900 Subject: [PATCH] =?UTF-8?q?Chore=20:=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EB=B0=8F=20=ED=94=84=EB=A1=9C=ED=95=84=20=EC=84=B1?= =?UTF-8?q?=EC=A0=95=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95=20(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../itself/controller/AuthController.java | 67 ++++++++++--------- .../jwt/filter/JwtAuthorizationFilter.java | 2 +- .../member/repository/MemberRepository.java | 1 + .../domain/member/service/MemberService.java | 27 ++++++-- .../domain/pet/controller/PetController.java | 49 +++++++++----- .../pet/dto/request/PetCreateRequestDto.java | 2 +- .../pet/dto/response/PetResponseDto.java | 2 +- .../domain/pet/dto/response/PetResultDto.java | 15 +++++ .../gdsc/petwalk/domain/pet/entity/Pet.java | 17 ++--- .../domain/pet/service/PetService.java | 12 ++-- .../petwalk/domain/photo/entity/Photo.java | 5 ++ .../domain/photo/service/PhotoService.java | 16 ++++- .../service/WalkInvitationService.java | 2 +- 13 files changed, 141 insertions(+), 76 deletions(-) create mode 100644 src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResultDto.java diff --git a/src/main/java/com/gdsc/petwalk/auth/itself/controller/AuthController.java b/src/main/java/com/gdsc/petwalk/auth/itself/controller/AuthController.java index 9b4e0b2..936aecd 100644 --- a/src/main/java/com/gdsc/petwalk/auth/itself/controller/AuthController.java +++ b/src/main/java/com/gdsc/petwalk/auth/itself/controller/AuthController.java @@ -1,27 +1,22 @@ package com.gdsc.petwalk.auth.itself.controller; import com.gdsc.petwalk.auth.itself.dto.request.LoginRequestDto; -import com.gdsc.petwalk.auth.itself.dto.request.ProfileRequestDto; import com.gdsc.petwalk.auth.itself.dto.request.SignUpRequestDto; import com.gdsc.petwalk.auth.itself.dto.response.AuthResultDto; import com.gdsc.petwalk.auth.itself.dto.response.TokenResponseDto; import com.gdsc.petwalk.domain.member.service.MemberService; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.media.Content; -import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -35,44 +30,45 @@ public class AuthController { private final MemberService memberService; + @GetMapping("/check-duplicate") + @Operation(summary = "중복 이메일 체크 로직", description = "중복 이메일 체크 로직") + @ApiResponse(responseCode = "200", description = "회원가입 전에 이메일 체크. 이미 가입 된 이메일이 존재하면 true, 존재하지 않으면 false") + public ResponseEntity> checkDuplicate( + @RequestParam String email + ) { + boolean isExists = memberService.checkDuplicateEmail(email); + + return ResponseEntity.ok().body(AuthResultDto.builder() + .status(true) + .code(200) + .message("이미 가입 된 이메일이 존재하면 true, 존재하지 않으면 false") + .data(isExists) + .build() + ); + } + @PostMapping("/sign-up") - @Operation(summary = "회원가입 로직", description = "회원가입 로직") - @ApiResponse(responseCode = "200", description = "signUpRequestDto는 application/json 형식, uploadPhoto는 multipart/form-data 형식으로, 두 개를 한꺼번에 form-data 형식으로 보내주면 됨") - public ResponseEntity> signUp( + @Operation(summary = "회원가입 로직", description = "signUpRequestDto는 application/json 형식, uploadPhoto는 multipart/form-data 형식으로, 두 개를 한꺼번에 form-data 형식으로 보내주면 됨") + @ApiResponse(responseCode = "200", description = "token 반환") + public ResponseEntity> signUp( @RequestPart("signUpRequestDto") @Valid SignUpRequestDto request, @RequestPart("uploadPhoto") MultipartFile multipartFile) { - String savedEmail = memberService.signUp(request, multipartFile); + TokenResponseDto tokenResponseDto = memberService.signUp(request, multipartFile); log.info("회원가입 성공 email = {}", request.email()); - return ResponseEntity.ok().body(AuthResultDto.builder() + return ResponseEntity.ok().body(AuthResultDto.builder() .status(true) .code(200) .message("회원가입에 성공하였습니다") - .build()); - } - - @PostMapping("/profile") - @Operation(summary = "회원가입 후 프로필 설정", description = "profileRequestDto는 application/json 형식, uploadPhoto는 multipart/form-data 형식으로, 두 개를 한꺼번에 form-data 형식으로 보내주면 됨") - @ApiResponse(responseCode = "200") - public ResponseEntity> setProfile( - @RequestPart("profileRequestDto") ProfileRequestDto profileRequestDto, - @RequestPart("uploadPhoto") MultipartFile file - ) { - memberService.setMemberProfile(profileRequestDto, file); - - return ResponseEntity.ok().body(AuthResultDto.builder() - .status(true) - .code(200) - .message("프로필 설정에 성공하였습니다") + .data(tokenResponseDto) .build()); } @PostMapping("/login") - @Operation(summary = "로그인 로직", - description = "body에 Authorization은 accesstoken, Authorization-refresh는 refreshtoken", - responses = {@ApiResponse(responseCode = "200", description = "로그인 성공 시 토큰 반환", content = @Content(schema = @Schema(implementation = TokenResponseDto.class)))}) - public ResponseEntity login( + @Operation(summary = "로그인 로직", description = "로그인 로직") + @ApiResponse(responseCode = "200", description = "로그인 성공 시 token 반환") + public ResponseEntity> login( @RequestBody LoginRequestDto loginRequestDto) { log.info("테스트 용 login. 실제로 거치지 않음"); @@ -82,6 +78,11 @@ public ResponseEntity login( .refreshToken("리프레시 토큰") .build(); - return ResponseEntity.ok().body(tokenResponseDto); + return ResponseEntity.ok().body(AuthResultDto.builder() + .status(true) + .code(200) + .message("로그인 성공 !") + .data(tokenResponseDto) + .build()); } } diff --git a/src/main/java/com/gdsc/petwalk/auth/jwt/filter/JwtAuthorizationFilter.java b/src/main/java/com/gdsc/petwalk/auth/jwt/filter/JwtAuthorizationFilter.java index ca9ad23..5f15c51 100644 --- a/src/main/java/com/gdsc/petwalk/auth/jwt/filter/JwtAuthorizationFilter.java +++ b/src/main/java/com/gdsc/petwalk/auth/jwt/filter/JwtAuthorizationFilter.java @@ -50,7 +50,7 @@ protected boolean shouldNotFilter(HttpServletRequest request) throws ServletExce protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - String accesstoken = request.getHeader("Authorization"); + String accesstoken = request.getHeader("Authorization").substring(7); if (jwtService.validateToken(accesstoken)) { //JWT 토큰을 파싱해서 member 정보를 가져옴 diff --git a/src/main/java/com/gdsc/petwalk/domain/member/repository/MemberRepository.java b/src/main/java/com/gdsc/petwalk/domain/member/repository/MemberRepository.java index dd0a1d7..add7d9f 100644 --- a/src/main/java/com/gdsc/petwalk/domain/member/repository/MemberRepository.java +++ b/src/main/java/com/gdsc/petwalk/domain/member/repository/MemberRepository.java @@ -7,4 +7,5 @@ public interface MemberRepository extends JpaRepository { Optional findByEmail(String email); + boolean existsByEmail(String email); } diff --git a/src/main/java/com/gdsc/petwalk/domain/member/service/MemberService.java b/src/main/java/com/gdsc/petwalk/domain/member/service/MemberService.java index 740ee10..8e65130 100644 --- a/src/main/java/com/gdsc/petwalk/domain/member/service/MemberService.java +++ b/src/main/java/com/gdsc/petwalk/domain/member/service/MemberService.java @@ -2,6 +2,8 @@ import com.gdsc.petwalk.auth.itself.dto.request.ProfileRequestDto; import com.gdsc.petwalk.auth.itself.dto.request.SignUpRequestDto; +import com.gdsc.petwalk.auth.itself.dto.response.TokenResponseDto; +import com.gdsc.petwalk.auth.jwt.service.JwtService; import com.gdsc.petwalk.domain.member.entity.Member; import com.gdsc.petwalk.domain.member.entity.Role; import com.gdsc.petwalk.domain.member.repository.MemberRepository; @@ -17,11 +19,14 @@ import java.util.Optional; +import static com.gdsc.petwalk.domain.member.entity.Role.ROLE_USER; + @Service @Transactional @RequiredArgsConstructor public class MemberService { + private final JwtService jwtService; private final MemberRepository memberRepository; private final PhotoService photoService; private final PasswordEncoder passwordEncoder; @@ -39,7 +44,7 @@ public Member saveOrUpdate(Member member) { } } - public String signUp(SignUpRequestDto signUpRequest, MultipartFile multipartFile) { + public TokenResponseDto signUp(SignUpRequestDto signUpRequest, MultipartFile multipartFile) { Optional optionalExistingMember = memberRepository.findByEmail(signUpRequest.email()); @@ -51,17 +56,24 @@ public String signUp(SignUpRequestDto signUpRequest, MultipartFile multipartFile Member member = Member.builder() .nickName(signUpRequest.nickName()) .email(signUpRequest.email()) - .role(Role.ROLE_USER) + .role(ROLE_USER) .password(passwordEncoder.encode(signUpRequest.password())) .longitude(signUpRequest.longitude()) .latitude(signUpRequest.latitude()) .photoUrl(photoService.uploadAndGetUrl(multipartFile)) .build(); - return memberRepository.save(member).getEmail(); + memberRepository.save(member); + + TokenResponseDto tokenResponseDto = TokenResponseDto.builder() + .accessToken(jwtService.createAccessToken(member.getEmail(), ROLE_USER.name())) + .refreshToken(jwtService.createRefreshToken()) + .build(); + + return tokenResponseDto; } - public void setMemberProfile(ProfileRequestDto profileRequestDto, MultipartFile file){ + public void setMemberProfile(ProfileRequestDto profileRequestDto, MultipartFile file) { Member member = memberRepository.findByEmail(profileRequestDto.email()) .orElseThrow(() -> new UsernameNotFoundException("해당 Email에 해당하는 유저가 없습니다")); @@ -71,7 +83,7 @@ public void setMemberProfile(ProfileRequestDto profileRequestDto, MultipartFile } - public Member findMemberByEmail(String email) throws UsernameNotFoundException{ + public Member findMemberByEmail(String email) throws UsernameNotFoundException { // 예외처리 해야함 Member member = memberRepository.findByEmail(email) .orElseThrow(() -> new UsernameNotFoundException("해당 Email에 해당하는 유저가 없습니다")); @@ -79,7 +91,7 @@ public Member findMemberByEmail(String email) throws UsernameNotFoundException{ return member; } - public String saveRefresh(String email, String refreshToken){ + public String saveRefresh(String email, String refreshToken) { Member member = memberRepository.findByEmail(email) .orElseThrow(); @@ -90,4 +102,7 @@ public String saveRefresh(String email, String refreshToken){ return member.getRefresh(); } + public boolean checkDuplicateEmail(String email) { + return memberRepository.existsByEmail(email); + } } diff --git a/src/main/java/com/gdsc/petwalk/domain/pet/controller/PetController.java b/src/main/java/com/gdsc/petwalk/domain/pet/controller/PetController.java index a4dcd54..d375c50 100644 --- a/src/main/java/com/gdsc/petwalk/domain/pet/controller/PetController.java +++ b/src/main/java/com/gdsc/petwalk/domain/pet/controller/PetController.java @@ -4,9 +4,12 @@ import com.gdsc.petwalk.domain.pet.dto.request.PetCreateRequestDto; import com.gdsc.petwalk.domain.pet.dto.request.PetUpdateRequestDto; import com.gdsc.petwalk.domain.pet.dto.response.PetResponseDto; +import com.gdsc.petwalk.domain.pet.dto.response.PetResultDto; import com.gdsc.petwalk.domain.pet.entity.Pet; import com.gdsc.petwalk.domain.pet.service.PetService; import com.gdsc.petwalk.global.principal.PrincipalDetails; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; @@ -19,63 +22,73 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; import java.util.List; import java.util.Optional; @RestController -@RequestMapping("/api/members") +@RequestMapping("/api/pets") @RequiredArgsConstructor @Slf4j public class PetController { private final PetService petService; - @PostMapping("/pets") + @PostMapping @PreAuthorize("isAuthenticated()") - public ResponseEntity createPet( - @RequestBody PetCreateRequestDto petCreateRequestDto, - @AuthenticationPrincipal PrincipalDetails principalDetails){ - - Long savedId = petService.addPetToMember(petCreateRequestDto, principalDetails); - - return ResponseEntity.ok(savedId); + @Operation(summary = "회원가입 후 펫 생성 로직", description = "회원가입 후 펫 생성 로직") + @ApiResponse(responseCode = "200", description = "회원가입 후 펫 생성, 성공 시 등록 펫 id 값 반환") + public ResponseEntity> createPet( + @RequestPart("petCreateRequestDto") PetCreateRequestDto petCreateRequestDto, + @RequestPart("uploadPhoto") MultipartFile file, + @AuthenticationPrincipal PrincipalDetails principalDetails) { + + Long savedId = petService.addPetToMember(petCreateRequestDto, file, principalDetails); + + return ResponseEntity.ok().body(PetResultDto.builder() + .status(true) + .code(200) + .message("펫 등록 성공!") + .data(savedId) + .build()); } - @GetMapping("pets/{petId}") + @GetMapping("/{petId}") @PreAuthorize("isAuthenticated()") public ResponseEntity readPet( @AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable("petId") Long id - ){ + ) { PetResponseDto myPet = petService.findMyPet(principalDetails, id); return ResponseEntity.ok(myPet); } - @GetMapping("/pets") + @GetMapping() @PreAuthorize("isAuthenticated()") public ResponseEntity> myPets( - @AuthenticationPrincipal PrincipalDetails principalDetails){ + @AuthenticationPrincipal PrincipalDetails principalDetails) { List allPets = petService.findAllPets(principalDetails); return ResponseEntity.ok(allPets); } - @DeleteMapping("/pets/{petId}") + @DeleteMapping("/{petId}") @PreAuthorize("isAuthenticated()") public ResponseEntity deletePet( - @AuthenticationPrincipal PrincipalDetails principalDetails, - @PathVariable("petId") Long id - ){ + @AuthenticationPrincipal PrincipalDetails principalDetails, + @PathVariable("petId") Long id + ) { petService.deletePet(principalDetails, id); return ResponseEntity.ok().body("펫 삭제에 성공하였습니다"); } - @PutMapping("/pets/{petId}") + @PutMapping("/{petId}") @PreAuthorize("isAuthenticated()") public ResponseEntity updatePet( @RequestBody PetUpdateRequestDto petUpdateRequestDto, diff --git a/src/main/java/com/gdsc/petwalk/domain/pet/dto/request/PetCreateRequestDto.java b/src/main/java/com/gdsc/petwalk/domain/pet/dto/request/PetCreateRequestDto.java index 0e7142f..c11f4e7 100644 --- a/src/main/java/com/gdsc/petwalk/domain/pet/dto/request/PetCreateRequestDto.java +++ b/src/main/java/com/gdsc/petwalk/domain/pet/dto/request/PetCreateRequestDto.java @@ -1,6 +1,6 @@ package com.gdsc.petwalk.domain.pet.dto.request; -public record PetCreateRequestDto(String nickname, String gender, Integer age, String photoUrl, +public record PetCreateRequestDto(String nickname, String gender, Integer age, String description, String dogType) { } diff --git a/src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResponseDto.java b/src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResponseDto.java index 8035a85..676e1a5 100644 --- a/src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResponseDto.java +++ b/src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResponseDto.java @@ -19,7 +19,7 @@ public static List makeFrom(List pets){ .nickname(pet.getNickname()) .gender(pet.getGender()) .age(pet.getAge()) - .photoUrl(pet.getPhotoUrl()) + .photoUrl(pet.getPhotos().get(0).getPhotoUrl()) .description(pet.getDescription()) .dogType(pet.getDogType()) .build(); diff --git a/src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResultDto.java b/src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResultDto.java new file mode 100644 index 0000000..c7f78cc --- /dev/null +++ b/src/main/java/com/gdsc/petwalk/domain/pet/dto/response/PetResultDto.java @@ -0,0 +1,15 @@ +package com.gdsc.petwalk.domain.pet.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +@AllArgsConstructor +public class PetResultDto { + private Boolean status; + private int code; + private String message; + private T data; +} diff --git a/src/main/java/com/gdsc/petwalk/domain/pet/entity/Pet.java b/src/main/java/com/gdsc/petwalk/domain/pet/entity/Pet.java index 088c8b3..5407d65 100644 --- a/src/main/java/com/gdsc/petwalk/domain/pet/entity/Pet.java +++ b/src/main/java/com/gdsc/petwalk/domain/pet/entity/Pet.java @@ -2,6 +2,7 @@ import com.gdsc.petwalk.domain.member.entity.Member; import com.gdsc.petwalk.domain.pet.dto.request.PetUpdateRequestDto; +import com.gdsc.petwalk.domain.photo.entity.Photo; import com.gdsc.petwalk.domain.review.entity.Review; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -40,9 +41,6 @@ public class Pet { private String gender; private Integer age; - @Column(name = "photo_url") - private String photoUrl; - private String description; @Column(name = "dog_type") @@ -54,18 +52,21 @@ public class Pet { @OneToMany(mappedBy = "pet") private List reviews = new ArrayList<>(); + @OneToMany(mappedBy = "pet") + private List photos = new ArrayList<>(); + @Builder - public Pet(Long id, Member member, String nickname, String gender, Integer age, String photoUrl, String description, Integer likesCount, String dogType, List reviews) { + public Pet(Long id, Member member, String nickname, String gender, Integer age, String description, String dogType, Integer likesCount, List reviews, List photos) { this.id = id; this.member = member; this.nickname = nickname; this.gender = gender; this.age = age; - this.photoUrl = photoUrl; this.description = description; - this.likesCount = likesCount; this.dogType = dogType; + this.likesCount = likesCount; this.reviews = reviews; + this.photos = photos; } public void setPetOwner(Member member) { @@ -85,10 +86,6 @@ public void update(PetUpdateRequestDto request) { this.age = request.age(); } - if (request.photoUrl() != null) { - this.photoUrl = request.photoUrl(); - } - if (request.description() != null) { this.description = request.description(); } diff --git a/src/main/java/com/gdsc/petwalk/domain/pet/service/PetService.java b/src/main/java/com/gdsc/petwalk/domain/pet/service/PetService.java index 36e4cdd..403a8ec 100644 --- a/src/main/java/com/gdsc/petwalk/domain/pet/service/PetService.java +++ b/src/main/java/com/gdsc/petwalk/domain/pet/service/PetService.java @@ -7,10 +7,12 @@ import com.gdsc.petwalk.domain.pet.dto.response.PetResponseDto; import com.gdsc.petwalk.domain.pet.entity.Pet; import com.gdsc.petwalk.domain.pet.repository.PetRepository; +import com.gdsc.petwalk.domain.photo.service.PhotoService; import com.gdsc.petwalk.global.principal.PrincipalDetails; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; import java.util.List; import java.util.Optional; @@ -20,8 +22,9 @@ @RequiredArgsConstructor public class PetService { private final PetRepository petRepository; + private final PhotoService photoService; - public Long addPetToMember(PetCreateRequestDto request, PrincipalDetails principalDetails) { + public Long addPetToMember(PetCreateRequestDto request, MultipartFile file, PrincipalDetails principalDetails) { Member member = principalDetails.getMember(); @@ -30,13 +33,14 @@ public Long addPetToMember(PetCreateRequestDto request, PrincipalDetails princip .nickname(request.nickname()) .gender(request.gender()) .age(request.age()) - .photoUrl(request.photoUrl()) .description(request.description()) .dogType(request.dogType()) .likesCount(0) .build(); - pet.setPetOwner(member); + photoService.savePhotoToPet(file, pet); + + member.getPets().add(pet); Pet savePet = petRepository.save(pet); return savePet.getId(); @@ -51,7 +55,7 @@ public PetResponseDto findMyPet(PrincipalDetails principalDetails, Long id) { .nickname(pet.getNickname()) .gender(pet.getGender()) .age(pet.getAge()) - .photoUrl(pet.getPhotoUrl()) + .photoUrl(pet.getPhotos().get(0).getPhotoUrl()) .description(pet.getDescription()) .build(); } diff --git a/src/main/java/com/gdsc/petwalk/domain/photo/entity/Photo.java b/src/main/java/com/gdsc/petwalk/domain/photo/entity/Photo.java index 3f4c926..ccd445a 100644 --- a/src/main/java/com/gdsc/petwalk/domain/photo/entity/Photo.java +++ b/src/main/java/com/gdsc/petwalk/domain/photo/entity/Photo.java @@ -1,6 +1,7 @@ package com.gdsc.petwalk.domain.photo.entity; import com.gdsc.petwalk.domain.board.entity.Board; +import com.gdsc.petwalk.domain.pet.entity.Pet; import com.gdsc.petwalk.domain.walkinvitation.entity.WalkInvitation; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -29,6 +30,10 @@ public class Photo { private String photoUrl; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "pet_id") + private Pet pet; + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "walk_invitation_id") private WalkInvitation walkInvitation; diff --git a/src/main/java/com/gdsc/petwalk/domain/photo/service/PhotoService.java b/src/main/java/com/gdsc/petwalk/domain/photo/service/PhotoService.java index da0eaea..2389def 100644 --- a/src/main/java/com/gdsc/petwalk/domain/photo/service/PhotoService.java +++ b/src/main/java/com/gdsc/petwalk/domain/photo/service/PhotoService.java @@ -1,5 +1,6 @@ package com.gdsc.petwalk.domain.photo.service; +import com.gdsc.petwalk.domain.pet.entity.Pet; import com.gdsc.petwalk.domain.photo.entity.Photo; import com.gdsc.petwalk.domain.photo.repository.PhotoRepository; import com.gdsc.petwalk.domain.walkinvitation.entity.WalkInvitation; @@ -20,7 +21,7 @@ public class PhotoService { private final PhotoRepository photoRepository; private final CloudStorageService cloudStorageService; - public List savePhotos(MultipartFile[] multipartFiles, WalkInvitation walkInvitation) { + public List savePhotosToWalkInvitation(MultipartFile[] multipartFiles, WalkInvitation walkInvitation) { List savedPhotos = new ArrayList<>(); for (MultipartFile file : multipartFiles) { @@ -36,6 +37,19 @@ public List savePhotos(MultipartFile[] multipartFiles, WalkInvitation wal return savedPhotos; } + public List savePhotoToPet(MultipartFile file, Pet pet){ + List savedPhotos = new ArrayList<>(); + + String url = uploadAndGetUrl(file); + Photo photo = Photo.builder() + .photoUrl(url) + .pet(pet) + .build(); + savedPhotos.add(photoRepository.save(photo)); + + return savedPhotos; + } + public List getPhotosByWalkInvitationId(Long walkInvitationId) { List photos = photoRepository.findByWalkInvitationId(walkInvitationId); return photos.stream() diff --git a/src/main/java/com/gdsc/petwalk/domain/walkinvitation/service/WalkInvitationService.java b/src/main/java/com/gdsc/petwalk/domain/walkinvitation/service/WalkInvitationService.java index da8c444..84e8a52 100644 --- a/src/main/java/com/gdsc/petwalk/domain/walkinvitation/service/WalkInvitationService.java +++ b/src/main/java/com/gdsc/petwalk/domain/walkinvitation/service/WalkInvitationService.java @@ -66,7 +66,7 @@ public Long createWalkInvitation(WalkInvitaionCreateRequestDto request, .walkingStatus("산책 대기 중") .build(); - walkInvitation.setPhotoUrls(photoService.savePhotos(multipartFiles, walkInvitation)); + walkInvitation.setPhotoUrls(photoService.savePhotosToWalkInvitation(multipartFiles, walkInvitation)); walkInvitationRepository.save(walkInvitation);