-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 사진 앨범 도메인 추가 #37
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e181404
chore: 앨범 및 앨범-사진 관계 테이블 추가하는 마이그레이션 파일 추가
731759d
fix: sql 문법 오류 수정
a69ebdb
feat: album 도메인 및 CRUD API 추가
f15de95
fix: 마이그레이션 파일 수정
29488e8
feat: 앨범-사진 관계 도메인 생성 및 사진 조회 로직 수정
5693e5d
feat: 앨범 기준 사진 조회 API 추가
1172501
Merge branch 'develop' into feature/36-add-album-domain
kih1015 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/kr/kro/photoliner/domain/album/controller/AlbumController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package kr.kro.photoliner.domain.album.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import kr.kro.photoliner.domain.album.dto.request.AlbumCreateRequest; | ||
| import kr.kro.photoliner.domain.album.dto.request.AlbumDeleteRequest; | ||
| import kr.kro.photoliner.domain.album.dto.response.AlbumCreateResponse; | ||
| import kr.kro.photoliner.domain.album.dto.response.AlbumsResponse; | ||
| import kr.kro.photoliner.domain.album.service.AlbumService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.data.web.PageableDefault; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/v1/albums") | ||
| public class AlbumController { | ||
|
|
||
| private final AlbumService albumService; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<AlbumCreateResponse> createAlbum( | ||
| @Valid @RequestBody AlbumCreateRequest request | ||
| ) { | ||
| AlbumCreateResponse response = albumService.createAlbum(request); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(response); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<AlbumsResponse> getAlbums( | ||
| @RequestParam Long userId, | ||
| @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable | ||
| ) { | ||
| return ResponseEntity.ok(albumService.getAlbums(userId, pageable)); | ||
| } | ||
|
|
||
| @DeleteMapping | ||
| public ResponseEntity<Void> deletePhoto( | ||
| @Valid @RequestBody AlbumDeleteRequest request | ||
| ) { | ||
| albumService.deleteAlbums(request); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/kr/kro/photoliner/domain/album/dto/request/AlbumCreateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package kr.kro.photoliner.domain.album.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotEmpty; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record AlbumCreateRequest( | ||
| @NotNull | ||
| Long userId, | ||
| @NotEmpty | ||
| String name | ||
| ) { | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/kr/kro/photoliner/domain/album/dto/request/AlbumDeleteRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package kr.kro.photoliner.domain.album.dto.request; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record AlbumDeleteRequest( | ||
| List<Long> ids | ||
| ) { | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/kr/kro/photoliner/domain/album/dto/response/AlbumCreateResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package kr.kro.photoliner.domain.album.dto.response; | ||
|
|
||
| import kr.kro.photoliner.domain.album.model.Album; | ||
|
|
||
| public record AlbumCreateResponse( | ||
| InnerAlbum album | ||
| ) { | ||
|
|
||
| public static AlbumCreateResponse from(Album album) { | ||
| return new AlbumCreateResponse( | ||
| new InnerAlbum( | ||
| album.getId(), | ||
| album.getName() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public record InnerAlbum( | ||
| Long id, | ||
| String name | ||
| ) { | ||
|
|
||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
src/main/java/kr/kro/photoliner/domain/album/dto/response/AlbumsResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package kr.kro.photoliner.domain.album.dto.response; | ||
|
|
||
| import java.util.List; | ||
| import kr.kro.photoliner.domain.album.model.Album; | ||
| import org.springframework.data.domain.Page; | ||
|
|
||
| public record AlbumsResponse( | ||
| List<InnerAlbum> albums, | ||
| InnerPageInfo pageInfo | ||
| ) { | ||
|
|
||
| public static AlbumsResponse from(Page<Album> albumPage) { | ||
| return new AlbumsResponse( | ||
| albumPage.getContent().stream() | ||
| .map(InnerAlbum::from) | ||
| .toList(), | ||
| InnerPageInfo.from(albumPage) | ||
| ); | ||
| } | ||
|
|
||
| public record InnerAlbum( | ||
| Long id, | ||
| String name | ||
| ) { | ||
|
|
||
| public static InnerAlbum from(Album album) { | ||
| return new InnerAlbum( | ||
| album.getId(), | ||
| album.getName() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public record InnerPageInfo( | ||
| long totalElements, | ||
| int totalPages, | ||
| int currentPage, | ||
| int size, | ||
| boolean hasNext, | ||
| boolean hasPrevious | ||
| ) { | ||
|
|
||
| public static InnerPageInfo from(Page<Album> page) { | ||
| return new InnerPageInfo( | ||
| page.getTotalElements(), | ||
| page.getTotalPages(), | ||
| page.getNumber(), | ||
| page.getSize(), | ||
| page.hasNext(), | ||
| page.hasPrevious() | ||
| ); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/kr/kro/photoliner/domain/album/model/Album.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package kr.kro.photoliner.domain.album.model; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import kr.kro.photoliner.common.model.BaseEntity; | ||
| import kr.kro.photoliner.domain.user.model.User; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Table(name = "albums") | ||
| @Getter | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @Builder | ||
| public class Album extends BaseEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @NotNull | ||
| @Column(name = "name", nullable = false) | ||
| private String name; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
| @JoinColumn(name = "user_id", referencedColumnName = "id") | ||
| private User user; | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/kr/kro/photoliner/domain/album/model/Albums.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package kr.kro.photoliner.domain.album.model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record Albums( | ||
| List<Album> albums | ||
| ) { | ||
|
|
||
| public int count() { | ||
| return albums.size(); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/kr/kro/photoliner/domain/album/repository/AlbumRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package kr.kro.photoliner.domain.album.repository; | ||
|
|
||
| import kr.kro.photoliner.domain.album.model.Album; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface AlbumRepository extends JpaRepository<Album, Long> { | ||
|
|
||
| Album save(Album album); | ||
|
|
||
| Page<Album> findByUserId(Long userId, Pageable pageable); | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/kr/kro/photoliner/domain/album/service/AlbumService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package kr.kro.photoliner.domain.album.service; | ||
|
|
||
| import kr.kro.photoliner.domain.album.dto.request.AlbumCreateRequest; | ||
| import kr.kro.photoliner.domain.album.dto.request.AlbumDeleteRequest; | ||
| import kr.kro.photoliner.domain.album.dto.response.AlbumCreateResponse; | ||
| import kr.kro.photoliner.domain.album.dto.response.AlbumsResponse; | ||
| import kr.kro.photoliner.domain.album.model.Album; | ||
| import kr.kro.photoliner.domain.album.repository.AlbumRepository; | ||
| import kr.kro.photoliner.domain.user.model.User; | ||
| import kr.kro.photoliner.domain.user.repository.UserRepository; | ||
| import kr.kro.photoliner.global.code.ApiResponseCode; | ||
| import kr.kro.photoliner.global.exception.CustomException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AlbumService { | ||
|
|
||
| private final UserRepository userRepository; | ||
| private final AlbumRepository albumRepository; | ||
|
|
||
| @Transactional | ||
| public AlbumCreateResponse createAlbum(AlbumCreateRequest request) { | ||
| User user = userRepository.findUserById(request.userId()) | ||
| .orElseThrow(() -> CustomException.of(ApiResponseCode.NOT_FOUND_USER, "user id: " + request.userId())); | ||
| Album album = Album.builder() | ||
| .name(request.name()) | ||
| .user(user) | ||
| .build(); | ||
| Album savedAlbum = albumRepository.save(album); | ||
| return AlbumCreateResponse.from(savedAlbum); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public AlbumsResponse getAlbums(Long userId, Pageable pageable) { | ||
| Page<Album> albums = albumRepository.findByUserId(userId, pageable); | ||
| return AlbumsResponse.from(albums); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteAlbums(AlbumDeleteRequest request) { | ||
| albumRepository.deleteAllByIdInBatch(request.ids()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/kr/kro/photoliner/domain/photo/model/AlbumPhoto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package kr.kro.photoliner.domain.photo.model; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import java.util.Objects; | ||
| import kr.kro.photoliner.domain.album.model.Album; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @AllArgsConstructor | ||
| @Table(name = "albums_photos") | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class AlbumPhoto { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "album_id", nullable = false) | ||
| private Album album; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "photo_id", nullable = false) | ||
| private Photo photo; | ||
|
Comment on lines
+32
to
+34
Contributor
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. C앨범 애그리거트에서 사진 애그리거트를 직접 참조하는 것에 대해 어떻게 생각하시나요? |
||
|
|
||
| public boolean isIncludedInAlbum(Long albumId) { | ||
| return Objects.equals(album.getId(), albumId); | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/main/java/kr/kro/photoliner/domain/photo/model/AlbumPhotos.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package kr.kro.photoliner.domain.photo.model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record AlbumPhotos( | ||
| List<AlbumPhoto> albumPhotos | ||
| ) { | ||
| public List<Photo> getPhotoIncludedAlbum(Long albumId) { | ||
| return albumPhotos.stream() | ||
| .filter(albumPhoto -> albumPhoto.isIncludedInAlbum(albumId)) | ||
| .map(AlbumPhoto::getPhoto) | ||
| .toList(); | ||
| } | ||
|
|
||
| public List<Photo> getPhotoNotIncludedAlbum(Long albumId) { | ||
| return albumPhotos.stream() | ||
| .filter(albumPhoto -> albumPhoto.isIncludedInAlbum(albumId)) | ||
| .map(AlbumPhoto::getPhoto) | ||
| .toList(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
C
루트 애그리거트를 Album 이라고 생각하는데 어떤가요?
Album에서 리스트형태로 AlbumPhoto를 관리하고
AlbumPhoto는 Embeddable로 보는 것도 괜찮다고 생각합니다.
더불어 앨범이 삭제될경우 AlbumPhoto도 같이 삭제될 수 있도록 Cascade 설정도 할 필요가 있어 보여요.