Skip to content

Commit

Permalink
feat: add api to delete profile image
Browse files Browse the repository at this point in the history
  • Loading branch information
aiaiaiai1 committed Jul 21, 2024
1 parent c793e8b commit 1918b8a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/main/java/gymmi/controller/MyPageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
Expand All @@ -29,6 +26,14 @@ public ResponseEntity<Void> editProfileImage(
return ResponseEntity.ok().build();
}

@DeleteMapping("/my/profile-image")
public ResponseEntity<Void> deleteProfileImage(
@Logined User user
) {
myPageService.deleteProfileImage(user);
return ResponseEntity.ok().build();
}

@PutMapping("/my/nickname/edit")
public ResponseEntity<Void> editMyPage(
@Logined User user,
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/gymmi/repository/ProfileImageRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gymmi.repository;

import gymmi.entity.Logined;
import gymmi.entity.ProfileImage;
import gymmi.exception.NotFoundResourcesException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand All @@ -12,4 +12,9 @@ public interface ProfileImageRepository extends JpaRepository<ProfileImage, Long
@Query("select p from ProfileImage p where p.owner.id =:userId")
Optional<ProfileImage> findByUserId(Long userId);

default ProfileImage getByUserId(Long userId) {
return findByUserId(userId)
.orElseThrow(() -> new NotFoundResourcesException("프로필 이미지가 존재하지 않습니다."));
}

}
2 changes: 2 additions & 0 deletions src/main/java/gymmi/service/ImageFileUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ImageFileUploader {

String upload(MultipartFile file, String fileName);

void delete(String filename);

}
8 changes: 7 additions & 1 deletion src/main/java/gymmi/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ public ProfileImageResponse setProfileImage(User loginedUser, MultipartFile prof
return new ProfileImageResponse(savedProfileImage);
}

@Transactional
public void deleteProfileImage(User loginedUser) {
ProfileImage profileImage = profileImageRepository.getByUserId(loginedUser.getId());
profileImageRepository.delete(profileImage);
imageFileUploader.delete(profileImage.getStoredName());
}

@Transactional
public void editMyPage(User loginedUser, EditingMyPageRequest request) {
if (userRepository.findByNickname(request.getNickname()).isPresent()) {
throw new AlreadyExistException("이미 존재하는 닉네임 입니다.");
}
loginedUser.changeNickname(request.getNickname());
}

}

0 comments on commit 1918b8a

Please sign in to comment.