-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: album 생성, 수정, 삭제 API #68
base: main
Are you sure you want to change the base?
Changes from all commits
3a2d8c2
d9f5685
8a43f61
1de70c9
16cf296
6e0cb8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.onebyte.life4cut.album.controller.dto; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.util.List; | ||
|
||
public record CreateAlbumRequest( | ||
@NotNull(message = "앨범의 이름을 입력해주세요") String name, | ||
List<@Min(value = 1, message = "올바른 memberUserIds를 입력해주세요") Long> memberUserIds, | ||
List<@Min(value = 1, message = "올바른 guestUserIds를 입력해주세요") Long> guestUserIds) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.onebyte.life4cut.album.controller.dto; | ||
|
||
public record CreateAlbumResponse(Long id) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.onebyte.life4cut.album.controller.dto; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotBlank; | ||
import java.util.List; | ||
|
||
public record UpdateAlbumRequest( | ||
@NotBlank(message = "앨범의 이름을 입력해주세요") String name, | ||
List<@Min(value = 1, message = "올바른 memberUserIds를 입력해주세요") Long> memberUserIds, | ||
List<@Min(value = 1, message = "올바른 guestUserIds를 입력해주세요") Long> guestUserIds) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,17 @@ | |
|
||
import com.onebyte.life4cut.album.domain.Album; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.transaction.Transactional; | ||
import java.util.Optional; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class AlbumRepositoryImpl implements AlbumRepository { | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
@PersistenceContext private EntityManager entityManager; | ||
|
||
public AlbumRepositoryImpl(JPAQueryFactory jpaQueryFactory) { | ||
this.jpaQueryFactory = jpaQueryFactory; | ||
|
@@ -23,4 +27,20 @@ public Optional<Album> findById(Long id) { | |
.where(album.id.eq(id), album.deletedAt.isNull()) | ||
.fetchOne()); | ||
} | ||
|
||
@Transactional | ||
public Album save(Album album) { | ||
entityManager.persist(album); | ||
return album; | ||
} | ||
|
||
@Override | ||
public void deleteById(Long id) { | ||
Album album = entityManager.find(Album.class, id); | ||
|
||
if (album != null) { | ||
album.softDelete(); | ||
entityManager.merge(album); | ||
} | ||
Comment on lines
+41
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사해요. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package com.onebyte.life4cut.album.service; | ||
|
||
import com.onebyte.life4cut.album.domain.Album; | ||
import com.onebyte.life4cut.album.domain.UserAlbum; | ||
import com.onebyte.life4cut.album.domain.vo.UserAlbumRole; | ||
import com.onebyte.life4cut.album.exception.AlbumNotFoundException; | ||
import com.onebyte.life4cut.album.exception.UserAlbumRolePermissionException; | ||
import com.onebyte.life4cut.album.repository.AlbumRepository; | ||
import com.onebyte.life4cut.album.repository.UserAlbumRepository; | ||
import jakarta.annotation.Nonnull; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
@@ -28,4 +33,97 @@ public UserAlbumRole getRoleInAlbum(@NonNull Long albumId, @NonNull Long userId) | |
|
||
return userAlbum.getRole(); | ||
} | ||
|
||
public Long createAlbum( | ||
@Nonnull String name, | ||
@Nonnull Long userId, | ||
List<Long> memberUserIds, | ||
List<Long> guestUserIds) { | ||
Set<Long> userIds = new HashSet<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set을 사용한 이유가 중복해서 사용자가 추가되지 않도록 하기위해서지? 밑에서 add 해서 새롭게 추가되었을 때만 디비에 적재하는거구. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 딱 맞음! 동일 사용자가 여러 권한을 갖지 않도록. |
||
|
||
Album album = Album.create(name); | ||
albumRepository.save(album); | ||
Long albumId = album.getId(); | ||
UserAlbum hostUserAlbum = UserAlbum.createHost(albumId, userId); | ||
|
||
userAlbumRepository.save(hostUserAlbum); | ||
userIds.add(userId); | ||
|
||
if (memberUserIds != null) { | ||
for (Long memberId : memberUserIds) { | ||
if (userIds.add(memberId)) { | ||
UserAlbum memberUserAlbum = UserAlbum.createMember(albumId, memberId); | ||
userAlbumRepository.save(memberUserAlbum); | ||
} | ||
} | ||
} | ||
if (guestUserIds != null) { | ||
for (Long guestId : guestUserIds) { | ||
if (userIds.add(guestId)) { | ||
UserAlbum guestUserAlbum = UserAlbum.createGuest(albumId, guestId); | ||
userAlbumRepository.save(guestUserAlbum); | ||
} | ||
} | ||
} | ||
|
||
return albumId; | ||
} | ||
|
||
public void deleteAlbum(Long albumId, @NonNull Long userId) { | ||
albumRepository.findById(albumId).orElseThrow(AlbumNotFoundException::new); | ||
|
||
UserAlbum userAlbum = | ||
userAlbumRepository | ||
.findByUserIdAndAlbumId(userId, albumId) | ||
.orElseThrow(UserAlbumRolePermissionException::new); | ||
|
||
if (userAlbum.isHost()) { | ||
albumRepository.deleteById(albumId); | ||
userAlbumRepository.deleteByAlbumId(albumId); | ||
} else { | ||
userAlbumRepository.delete(userAlbum); | ||
} | ||
} | ||
|
||
public void updateAlbum( | ||
Long albumId, String name, Long userId, List<Long> memberUserIds, List<Long> guestUserIds) { | ||
Album album = albumRepository.findById(albumId).orElseThrow(() -> new AlbumNotFoundException()); | ||
UserAlbum userAlbum = | ||
userAlbumRepository | ||
.findByUserIdAndAlbumId(userId, albumId) | ||
.orElseThrow(UserAlbumRolePermissionException::new); | ||
|
||
if (!userAlbum.isHost()) { | ||
throw new UserAlbumRolePermissionException(); | ||
} | ||
userAlbumRepository.deleteByAlbumId(albumId); | ||
if (name != null) { | ||
album.setName(name); | ||
} | ||
albumRepository.save(album); | ||
|
||
Set<Long> userIds = new HashSet<>(); | ||
|
||
UserAlbum hostUserAlbum = UserAlbum.createHost(albumId, userId); | ||
|
||
userAlbumRepository.save(hostUserAlbum); | ||
userIds.add(userId); | ||
|
||
if (memberUserIds != null) { | ||
for (Long memberId : memberUserIds) { | ||
if (userIds.add(memberId)) { | ||
UserAlbum memberUserAlbum = UserAlbum.createMember(albumId, memberId); | ||
userAlbumRepository.save(memberUserAlbum); | ||
} | ||
} | ||
} | ||
if (guestUserIds != null) { | ||
for (Long guestId : guestUserIds) { | ||
if (userIds.add(guestId)) { | ||
UserAlbum guestUserAlbum = UserAlbum.createGuest(albumId, guestId); | ||
userAlbumRepository.save(guestUserAlbum); | ||
} | ||
} | ||
} | ||
} | ||
Comment on lines
+105
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생성쪽과 동일한 로직인 것 같은데 맞다면 메서드로 빼도 좋을 것 같아 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞아요👍 반영하겠습니다. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.