-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: ์ฌ์ง ์กฐํ, ์์ , ์ญ์ API ๊ตฌํ
- Loading branch information
Showing
10 changed files
with
221 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
75 changes: 75 additions & 0 deletions
75
photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java
This file contains 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,75 @@ | ||
package kr.mafoo.photo.domain; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.annotation.Transient; | ||
import org.springframework.data.domain.Persistable; | ||
import org.springframework.data.relational.core.mapping.Column; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@Table("photo") | ||
public class PhotoEntity implements Persistable<String> { | ||
@Id | ||
@Column("id") | ||
private String photoId; | ||
|
||
@Column("url") | ||
private String photoUrl; | ||
|
||
@Column("owner_member_id") | ||
private String ownerMemberId; | ||
|
||
@Column("album_id") | ||
private String albumId; | ||
|
||
@CreatedDate | ||
@Column("created_at") | ||
private LocalDateTime createdAt; | ||
|
||
@LastModifiedDate | ||
@Column("updated_at") | ||
private LocalDateTime updatedAt; | ||
|
||
@Transient | ||
private boolean isNew = false; | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
if (obj == null || getClass() != obj.getClass()) return false; | ||
|
||
PhotoEntity that = (PhotoEntity) obj; | ||
return photoId.equals(that.photoId); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return photoId.hashCode(); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return photoId; | ||
} | ||
|
||
public PhotoEntity updateAlbumId(String albumId) { | ||
this.albumId = albumId; | ||
return this; | ||
} | ||
|
||
public static PhotoEntity newPhoto(String photoId, String photoUrl, String ownerMemberId) { | ||
PhotoEntity photo = new PhotoEntity(); | ||
photo.photoId = photoId; | ||
photo.photoUrl = photoUrl; | ||
photo.ownerMemberId = ownerMemberId; | ||
photo.isNew = true; | ||
return photo; | ||
} | ||
} |
This file contains 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
7 changes: 7 additions & 0 deletions
7
photo-service/src/main/java/kr/mafoo/photo/exception/PhotoNotFoundException.java
This file contains 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,7 @@ | ||
package kr.mafoo.photo.exception; | ||
|
||
public class PhotoNotFoundException extends DomainException { | ||
public PhotoNotFoundException() { | ||
super(ErrorCode.PHOTO_NOT_FOUND); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
photo-service/src/main/java/kr/mafoo/photo/repository/PhotoRepository.java
This file contains 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,9 @@ | ||
package kr.mafoo.photo.repository; | ||
|
||
import kr.mafoo.photo.domain.PhotoEntity; | ||
import org.springframework.data.r2dbc.repository.R2dbcRepository; | ||
import reactor.core.publisher.Flux; | ||
|
||
public interface PhotoRepository extends R2dbcRepository<PhotoEntity, String> { | ||
Flux<PhotoEntity> findAllByAlbumId(String ownerAlbumId); | ||
} |
71 changes: 71 additions & 0 deletions
71
photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java
This file contains 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,71 @@ | ||
package kr.mafoo.photo.service; | ||
|
||
import kr.mafoo.photo.domain.PhotoEntity; | ||
import kr.mafoo.photo.exception.AlbumNotFoundException; | ||
import kr.mafoo.photo.exception.PhotoNotFoundException; | ||
import kr.mafoo.photo.repository.AlbumRepository; | ||
import kr.mafoo.photo.repository.PhotoRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class PhotoService { | ||
private final PhotoRepository photoRepository; | ||
private final AlbumRepository albumRepository; | ||
|
||
public Flux<PhotoEntity> findAllByAlbumId(String albumId, String requestMemberId) { | ||
return albumRepository | ||
.findById(albumId) | ||
.switchIfEmpty(Mono.error(new AlbumNotFoundException())) | ||
.flatMapMany(albumEntity -> { | ||
if(!albumEntity.getOwnerMemberId().equals(requestMemberId)) { | ||
// ๋ด ์จ๋ฒ์ด ์๋๋ฉด ๊ทธ๋ฅ ์๋ ์จ๋ฒ ์ฒ๋ฆฌ | ||
return Mono.error(new AlbumNotFoundException()); | ||
} else { | ||
return photoRepository.findAllByAlbumId(albumId); | ||
} | ||
}); | ||
} | ||
|
||
public Mono<Void> deletePhotoById(String photoId, String requestMemberId) { | ||
return photoRepository | ||
.findById(photoId) | ||
.switchIfEmpty(Mono.error(new PhotoNotFoundException())) | ||
.flatMap(photoEntity -> { | ||
if(!photoEntity.getOwnerMemberId().equals(requestMemberId)) { | ||
// ๋ด ์ฌ์ง์ด ์๋๋ฉด ๊ทธ๋ฅ ์๋ ์ฌ์ง ์ฒ๋ฆฌ | ||
return Mono.error(new PhotoNotFoundException()); | ||
} else { | ||
return photoRepository.deleteById(photoId); | ||
} | ||
}); | ||
} | ||
|
||
public Mono<PhotoEntity> updatePhotoAlbumId(String photoId, String albumId, String requestMemberId) { | ||
return photoRepository | ||
.findById(photoId) | ||
.switchIfEmpty(Mono.error(new PhotoNotFoundException())) | ||
.flatMap(photoEntity -> { | ||
if(!photoEntity.getOwnerMemberId().equals(requestMemberId)) { | ||
// ๋ด ์ฌ์ง์ด ์๋๋ฉด ๊ทธ๋ฅ ์๋ ์ฌ์ง ์ฒ๋ฆฌ | ||
return Mono.error(new PhotoNotFoundException()); | ||
} else { | ||
return albumRepository | ||
.findById(albumId) | ||
.switchIfEmpty(Mono.error(new AlbumNotFoundException())) | ||
.flatMap(albumEntity -> { | ||
if(!albumEntity.getOwnerMemberId().equals(requestMemberId)) { | ||
// ๋ด ์จ๋ฒ์ด ์๋๋ฉด ๊ทธ๋ฅ ์๋ ์จ๋ฒ ์ฒ๋ฆฌ | ||
return Mono.error(new AlbumNotFoundException()); | ||
} else { | ||
return photoRepository.save(photoEntity.updateAlbumId(albumId)); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
photo-service/src/main/resources/db/migration/V2__createPhotoTable.sql
This file contains 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,10 @@ | ||
CREATE TABLE photo( | ||
`id` CHAR(26) PRIMARY KEY NOT NULL COMMENT '์ฌ์ง์์ด๋', | ||
`url` VARCHAR(255) NOT NULL COMMENT '์ฌ์งurl', | ||
`owner_member_id` CHAR(26) NOT NULL COMMENT '์ฌ์ง์์ ์์์ด๋', | ||
`album_id` CHAR(26) NULL COMMENT '์ฌ์ง์จ๋ฒ์์ด๋', | ||
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
INDEX `photo_idx1` (`owner_member_id`), | ||
INDEX `photo_idx2` (`album_id`) | ||
); |