Skip to content

Commit

Permalink
Merge pull request #208 from 9uttery/feat/put-my-album-all-api-#206
Browse files Browse the repository at this point in the history
[Feature] 앨범 편집 API 기능 구현
  • Loading branch information
hojeong2747 authored Sep 3, 2024
2 parents 8dc0f42 + 1dcf2f8 commit 7659377
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 6 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
@@ -0,0 +1,23 @@
package com.guttery.madii.domain.albums.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.List;

@Schema(description = "앨범 편집 요청")
public record AlbumPutAllRequest(
@NotBlank(message = "앨범 제목은 필수입니다.")
@Schema(description = "앨범 제목", example = "겨울 필수 소확행 모음 zip")
@Size(max=30, message = "앨범 제목은 30자 이내로 입력해 주세요.")
String name,
@Schema(description = "앨범 내용", example = "이 소확행은 기분이 째질 때 츄라이해보면 좋은 소확행이에요.")
@Size(max=50, message = "앨범 내용은 50자 이내로 입력해 주세요.")
String description,
@Schema(description = "수정되거나 추가된 소확행 목록", example = "")
List<SavingJoyDto> joys,
@Schema(description = "삭제할 소확행 ID 목록", example = "")
List<Long> deletedJoyIds
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.guttery.madii.domain.albums.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;

public record SavingJoyDto(
@Schema(description = "소확행 아이디", example = "11")
Long joyId,
@Schema(description = "소확행 내용", example = "낮잠자기")
String contents,
@Schema(description = "소확행 정렬 순서", example = "3")
Integer joyOrder
) {
}
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 @@ -31,11 +31,19 @@ public class SavingJoy extends BaseTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "album_id", nullable = false)
private Album album; // 앨범 id
private Integer joyOrder; // 소확행 정렬 순서

public SavingJoy(Joy joy, Album album) {
public SavingJoy(Joy joy, Album album, Integer joyOrder) {
this.joy = joy;
this.album = album;
this.joyOrder = joyOrder;
}

public static SavingJoy createSavingJoy(Joy joy, Album album) { return new SavingJoy(joy, album); }
public static SavingJoy createSavingJoy(Joy joy, Album album, Integer joyOrder) {
return new SavingJoy(joy, album, joyOrder);
}

public void modifyJoyOrder(Integer joyOrder) {
this.joyOrder = joyOrder;
}
}
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 @@ -65,7 +65,8 @@ public void deleteFromSavingJoy(Long joyId, List<Long> beforeAlbumIds) {
.orElseThrow(() -> CustomException.of(ErrorDetails.JOY_NOT_FOUND));
final Album album = albumRepository.findById(albumId)
.orElseThrow(() -> CustomException.of(ErrorDetails.ALBUM_NOT_FOUND));
final SavingJoy savingJoy = savingJoyRepository.findByJoyAndAlbum(joy, album);
final SavingJoy savingJoy = savingJoyRepository.findByJoyAndAlbum(joy, album)
.orElseThrow(() -> CustomException.of(ErrorDetails.SAVING_JOY_NOT_FOUND));
savingJoyRepository.delete(savingJoy);
}
}
Expand All @@ -76,7 +77,11 @@ public void addToSavingJoy(Long joyId, List<Long> afterAlbumIds) {
.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
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 7659377

Please sign in to comment.