Skip to content

Commit

Permalink
feat: 앨범 편집 API 구현 (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
hojeong2747 committed Sep 3, 2024
1 parent 5c20cba commit 1dcf2f8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public enum ErrorDetails {

JOY_NOT_FOUND("J001", HttpStatus.NOT_FOUND.value(), "소확행을 찾을 수 없습니다."),
TODAY_JOY_NOT_FOUND("J100", HttpStatus.NOT_FOUND.value(), "오늘의 소확행을 찾을 수 없습니다."),
SAVING_JOY_NOT_FOUND("J001", HttpStatus.NOT_FOUND.value(), "앨범에 저장된 소확행 기록을 찾을 수 없습니다."),

ALBUM_NOT_FOUND("A001", HttpStatus.NOT_FOUND.value(), "앨범을 찾을 수 없습니다."),
ALREADY_EXIST_BOOKMARK("A002", HttpStatus.BAD_REQUEST.value(), "이미 저장한 앨범입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ public void addJoyToAlbum(Long joyId, AlbumSaveJoyRequest albumSaveJoyRequest, U
.orElseThrow(() -> CustomException.of(ErrorDetails.JOY_NOT_FOUND));
final Album album = albumRepository.findById(albumId)
.orElseThrow(() -> CustomException.of(ErrorDetails.ALBUM_NOT_FOUND));
final SavingJoy savingJoy = SavingJoy.createSavingJoy(joy, album);

// 앨범 내에서 가장 높은 order 값을 조회하여 1을 더해줌
Integer maxOrder = savingJoyRepository.findMaxOrderByAlbum(album);
Integer joyOrder = (maxOrder == null) ? 1 : maxOrder + 1;
final SavingJoy savingJoy = SavingJoy.createSavingJoy(joy, album, joyOrder);
savingJoyRepository.save(savingJoy);
}
}
Expand Down Expand Up @@ -308,4 +312,51 @@ public void reportAlbum(AlbumReportRequest albumReportRequest, Long albumId, Use
album.makeBlocked();
}

public void putAllAlbum(AlbumPutAllRequest albumPutAllRequest, Long albumId, UserPrincipal userPrincipal) {
final User user = UserServiceHelper.findExistingUser(userRepository, userPrincipal);
Album album = albumRepository.findById(albumId)
.orElseThrow(() -> CustomException.of(ErrorDetails.ALBUM_NOT_FOUND));

String name = albumPutAllRequest.name();
String description = albumPutAllRequest.description();
List<SavingJoyDto> joys = albumPutAllRequest.joys();
List<Long> deletedJoyIds = albumPutAllRequest.deletedJoyIds();

// 1. 삭제된 소확행 처리
if (deletedJoyIds != null && !deletedJoyIds.isEmpty()) {
savingJoyRepository.deleteAllByJoyJoyIdIn(deletedJoyIds);
}

for (SavingJoyDto joyDto :joys) {
// 2. 수정된 소확행 처리
if (joyDto.joyId() != null) {
// 소확행명 수정 - joy table update
Joy joy = joyRepository.findById(joyDto.joyId())
.orElseThrow(() -> CustomException.of(ErrorDetails.JOY_NOT_FOUND));
joy.modifyContents(joyDto.contents());

// 순서 세팅 - saving_joy table update
SavingJoy savingJoy = savingJoyRepository.findByJoyAndAlbum(joy, album)
.orElseThrow(() -> CustomException.of(ErrorDetails.SAVING_JOY_NOT_FOUND));
savingJoy.modifyJoyOrder(joyDto.joyOrder());
} else {
// 3. 새로 추가된 소확행 처리
Random random = new Random();
int bound = 24; // 소확행 썸네일 아이콘 번호 1 ~ 24 중 랜덤 생성

Joy joy;
if (album.getAlbumStatus().getIsOfficial()) {
joy = Joy.createOfficialJoy(user, 1 + random.nextInt(bound), joyDto.contents());
} else {
joy = Joy.createPersonalJoy(user, 1 + random.nextInt(bound), joyDto.contents());
}

joyRepository.save(joy);
SavingJoy savingJoy = SavingJoy.createSavingJoy(joy, album, joyDto.joyOrder());
savingJoyRepository.save(savingJoy);
}
}

album.modifyNameAndDes(name, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
import com.guttery.madii.domain.joy.domain.model.Joy;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface SavingJoyRepository extends JpaRepository<SavingJoy, Long> {

SavingJoy findByJoyAndAlbum(Joy joy, Album album);
Optional<SavingJoy> findByJoyAndAlbum(Joy joy, Album album);

void deleteByAlbumAlbumId(Long albumId);

Integer findMaxOrderByAlbum(Album album);

void deleteAllByJoyJoyIdIn(List<Long> deletedJoyIds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,22 @@ public void reportAlbum(@Valid @RequestBody AlbumReportRequest albumReportReques
albumService.reportAlbum(albumReportRequest, albumId, userPrincipal);
}


@PutMapping("/{albumId}/all")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "앨범 편집 성공",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "앨범 편집 API", description = "앨범 편집 API입니다.")
public void putAllAlbum(@Valid @RequestBody AlbumPutAllRequest albumPutAllRequest,
@PathVariable Long albumId,
@AuthenticationPrincipal final UserPrincipal userPrincipal) {
albumService.putAllAlbum(albumPutAllRequest, albumId, userPrincipal);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public static Joy createPersonalJoy(User user, Integer joyIconNum, String conten
return new Joy(user, JoyType.PERSONAL, joyIconNum, contents);
}

public static Joy createOfficialJoy(User user, Integer joyIconNum, String contents) {
return new Joy(user, JoyType.OFFICIAL, joyIconNum, contents);
}

public void modifyContents(String contents) {
this.contents = contents;
}
Expand Down

0 comments on commit 1dcf2f8

Please sign in to comment.