Skip to content

Commit

Permalink
Merge pull request #139 from Team-Umbba/fix/#138-album_api_modify
Browse files Browse the repository at this point in the history
[FEAT] Album  API 추가 기능 구현
  • Loading branch information
jun02160 authored Mar 10, 2024
2 parents d448f12 + 66c0253 commit 395a5a8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sopt.org.umbba.api.controller.album;

import static sopt.org.umbba.api.config.jwt.JwtProvider.*;
import static sopt.org.umbba.api.service.album.AlbumService.*;
import static sopt.org.umbba.common.exception.SuccessType.*;
import static sopt.org.umbba.external.s3.S3BucketPrefix.*;

Expand Down Expand Up @@ -60,7 +61,9 @@ public ApiResponse<PreSignedUrlDto> getImgPreSignedUrl(@RequestBody final AlbumI
@ResponseStatus(HttpStatus.OK)
public ApiResponse deleteAlbum(@PathVariable final Long albumId, final Principal principal) {
String imgUrl = albumService.deleteAlbum(albumId, getUserFromPrincial(principal));
s3Service.deleteS3Image(imgUrl);
if (!imgUrl.equals(ALBUM_EXAMPLE)) { // Example Album의 이미지는 삭제하지 X
s3Service.deleteS3Image(imgUrl);
}
return ApiResponse.success(DELETE_ALBUM_SUCCESS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ public class AlbumService {
private final AlbumRepository albumRepository;
private final UserRepository userRepository;

public static final String ALBUM_EXAMPLE = "example";

@Transactional
public Long createAlbum(final CreateAlbumRequestDto request, final String imgUrl, final Long userId) {

User user = getUserById(userId);
Parentchild parentchild = getParentchildByUser(user);

if (parentchild.isOverMaxAlbumLimit()) {
throw new CustomException(ErrorType.MAX_LIMIT_ALBUM_UPLOAD);
}

// 앨범을 처음 등록하는 경우
if (parentchild.getAlbumList().isEmpty() && !parentchild.isFirstAlbumUpload()) {
parentchild.updateFirstAlbumUpload();
}

Album album = Album.builder()
.title(request.getTitle())
.content(request.getContent())
Expand All @@ -50,6 +61,13 @@ public String deleteAlbum(final Long albumId, final Long userId) {

User user = getUserById(userId);
Parentchild parentchild = getParentchildByUser(user);

// Sample Album을 삭제할 경우
if (albumId.equals(0L)) {
parentchild.updateDeleteSampleAlbum();
return ALBUM_EXAMPLE;
}

Album album = getAlbumById(albumId);

album.deleteParentchild();
Expand All @@ -65,11 +83,21 @@ public List<AlbumResponseDto> getAlbumList(final Long userId) {
List<Album> albumList = albumRepository.findAllByParentchildOrderByCreatedAtDesc(
parentchild);

// Album을 아직 한번도 등록하지 않은 경우
if (albumList.isEmpty() && !parentchild.isFirstAlbumUpload() && !parentchild.isDeleteSampleAlbum()) {
return List.of(AlbumResponseDto.of(createAlbumExample()));
}

return albumList.stream()
.map(AlbumResponseDto::of)
.collect(Collectors.toList());
}

private Album createAlbumExample() {
return new Album(0L, "사진의 제목을 입력할 수 있어요", "사진에 대해 소개해요",
"imgUrl", "직성자");
}

private User getUserById(Long userId) { // TODO userId -> Parentchild 한번에 가져오기
return userRepository.findById(userId).orElseThrow(
() -> new CustomException(ErrorType.INVALID_USER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public enum ErrorType {
* 501 NOT_IMPLEMENTED
*/
NEED_MORE_QUESTION(HttpStatus.NOT_IMPLEMENTED, "남은 질문이 없습니다. 질문을 추가해주세요."),
MAX_LIMIT_ALBUM_UPLOAD(HttpStatus.NOT_IMPLEMENTED, "한 부모자식마다 최대 15개의 업로드까지만 허용합니다."),


;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ private Album(String title, String content, String imgUrl, String writer, Parent
this.parentchild = parentchild;
}

public Album(Long id, String title, String content, String imgUrl, String writer) {
this.id = id;
this.title = title;
this.content = content;
this.imgUrl = imgUrl;
this.writer = writer;
}

public void setParentchild(Parentchild parentchild) {
this.parentchild = parentchild;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ public void changeParentOnboardingAnswerList(List<OnboardingAnswer> onboardingAn

private boolean deleted = Boolean.FALSE;

private boolean isFirstAlbumUpload = false;

private boolean isDeleteSampleAlbum = false;

public void updateFirstAlbumUpload() {
this.isFirstAlbumUpload = true;
}
public void updateDeleteSampleAlbum() {
this.isDeleteSampleAlbum = true;
}


public void setQna(QnA qnA) {
if (qnaList.size() >= 7) {
throw new CustomException(ErrorType.ALREADY_QNA_LIST_FULL);
Expand All @@ -136,4 +148,8 @@ public void deleteAlbum(Album album) {
}
}

public boolean isOverMaxAlbumLimit() {
return getAlbumList().size() >= 15;
}

}

0 comments on commit 395a5a8

Please sign in to comment.