-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from depromeet/feature/30-image
feat: ์ด๋ฏธ์ง ์ ๋ก๋/์์ /์กฐํ/์ญ์ ๊ธฐ๋ฅ ๊ตฌํ
- Loading branch information
Showing
30 changed files
with
789 additions
and
2 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
module-domain/src/main/java/com/depromeet/image/Image.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,42 @@ | ||
package com.depromeet.image; | ||
|
||
import com.depromeet.memory.Memory; | ||
import java.util.Optional; | ||
import lombok.Builder; | ||
|
||
public class Image { | ||
private Long id; | ||
private Memory memory; | ||
private String imageName; | ||
private String imageUrl; | ||
|
||
@Builder | ||
public Image(Long id, Memory memory, String imageName, String imageUrl) { | ||
this.id = id; | ||
this.memory = memory; | ||
this.imageName = imageName; | ||
this.imageUrl = imageUrl; | ||
} | ||
|
||
public void addMemoryToImage(Memory memory) { | ||
if (memory != null) { | ||
this.memory = memory; | ||
} | ||
} | ||
|
||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public Optional<Memory> getMemory() { | ||
return Optional.ofNullable(this.memory); | ||
} | ||
|
||
public String getImageName() { | ||
return this.imageName; | ||
} | ||
|
||
public String getImageUrl() { | ||
return this.imageUrl; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
module-independent/src/main/java/com/depromeet/type/image/ImageErrorType.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,25 @@ | ||
package com.depromeet.type.image; | ||
|
||
import com.depromeet.type.ErrorType; | ||
|
||
public enum ImageErrorType implements ErrorType { | ||
NOT_FOUND("IMAGE_1", "์ด๋ฏธ์ง๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค"); | ||
|
||
private final String code; | ||
private final String message; | ||
|
||
ImageErrorType(String code, String message) { | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public String getCode() { | ||
return this.code; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return this.message; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
module-independent/src/main/java/com/depromeet/type/image/ImageSuccessType.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,30 @@ | ||
package com.depromeet.type.image; | ||
|
||
import com.depromeet.type.SuccessType; | ||
|
||
public enum ImageSuccessType implements SuccessType { | ||
UPLOAD_IMAGES_SUCCESS("IMAGE_1", "์ด๋ฏธ์ง ์ ๋ก๋์ ์ฑ๊ณตํ์์ต๋๋ค"), | ||
ADD_MEMORY_TO_IMAGES_SUCCESS("IMAGE_2", "์ด๋ฏธ์ง์ memory๋ฅผ ์ถ๊ฐํ๋๋ฐ ์ฑ๊ณตํ์์ต๋๋ค"), | ||
UPDATE_IMAGES_SUCCESS("IMAGE_3", "์ด๋ฏธ์ง ์์ ์ ์ฑ๊ณตํ์์ต๋๋ค"), | ||
GET_IMAGES_SUCCESS("IMAGE_4", "์ด๋ฏธ์ง ์กฐํ์ ์ฑ๊ณตํ์์ต๋๋ค"), | ||
DELETE_IMAGES_SUCCESS("IMAGE_5", "memory์ ํด๋นํ๋ ์ด๋ฏธ์ง๋ฅผ ์ญ์ ํ๋๋ฐ ์ฑ๊ณตํ์์ต๋๋ค"); | ||
|
||
private final String code; | ||
|
||
private final String message; | ||
|
||
ImageSuccessType(String code, String message) { | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public String getCode() { | ||
return this.code; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return this.message; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
module-independent/src/main/java/com/depromeet/util/ImageNameUtil.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,13 @@ | ||
package com.depromeet.util; | ||
|
||
import java.util.UUID; | ||
|
||
public class ImageNameUtil { | ||
public static String createImageName( | ||
String originalImageName, String contentType, Long fileSize) { | ||
String imageName = originalImageName + "_" + contentType + "_" + fileSize; | ||
UUID imageUUID = UUID.nameUUIDFromBytes(imageName.getBytes()); | ||
|
||
return imageUUID.toString(); | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
...astructure/persistence-database/src/main/java/com/depromeet/image/entity/ImageEntity.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,72 @@ | ||
package com.depromeet.image.entity; | ||
|
||
import com.depromeet.image.Image; | ||
import com.depromeet.memory.entity.MemoryEntity; | ||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.util.Optional; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ImageEntity { | ||
@Id | ||
@Column(name = "image_id") | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@JoinColumn(name = "memory_id") | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
private MemoryEntity memory; | ||
|
||
@NotNull private String imageName; | ||
|
||
@NotNull private String imageUrl; | ||
|
||
@Builder | ||
public ImageEntity(Long id, MemoryEntity memory, String imageName, String imageUrl) { | ||
this.id = id; | ||
this.memory = memory; | ||
this.imageName = imageName; | ||
this.imageUrl = imageUrl; | ||
} | ||
|
||
public static ImageEntity from(Image image) { | ||
return ImageEntity.builder() | ||
.id(image.getId()) | ||
.memory( | ||
image.getMemory().isPresent() | ||
? MemoryEntity.from(image.getMemory().get()) | ||
: null) | ||
.imageName(image.getImageName()) | ||
.imageUrl(image.getImageUrl()) | ||
.build(); | ||
} | ||
|
||
public Image toModel() { | ||
return Image.builder() | ||
.id(this.id) | ||
.memory(this.memory == null ? null : this.memory.toModel()) | ||
.imageName(this.imageName) | ||
.imageUrl(this.imageUrl) | ||
.build(); | ||
} | ||
|
||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public Optional<MemoryEntity> getMemory() { | ||
return Optional.ofNullable(this.memory); | ||
} | ||
|
||
public String getImageName() { | ||
return this.imageName; | ||
} | ||
|
||
public String getImageUrl() { | ||
return this.imageUrl; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...persistence-database/src/main/java/com/depromeet/image/repository/ImageJpaRepository.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,17 @@ | ||
package com.depromeet.image.repository; | ||
|
||
import com.depromeet.image.entity.ImageEntity; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface ImageJpaRepository extends JpaRepository<ImageEntity, Long> { | ||
List<ImageEntity> findByMemoryId(Long memoryId); | ||
|
||
@Query(value = """ | ||
select i from ImageEntity i where i.id in :ids | ||
""") | ||
List<ImageEntity> findAllByIds(List<Long> ids); | ||
|
||
void deleteAllByMemoryId(Long memoryId); | ||
} |
23 changes: 23 additions & 0 deletions
23
...re/persistence-database/src/main/java/com/depromeet/image/repository/ImageRepository.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,23 @@ | ||
package com.depromeet.image.repository; | ||
|
||
import com.depromeet.image.Image; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface ImageRepository { | ||
Long save(Image image); | ||
|
||
List<Long> saveAll(List<Image> images); | ||
|
||
Optional<Image> findById(Long id); | ||
|
||
List<Image> findImagesByMemoryId(Long memoryId); | ||
|
||
List<Image> findImageByIds(List<Long> ids); | ||
|
||
void deleteById(Long id); | ||
|
||
void deleteAllByIds(List<Long> ids); | ||
|
||
void deleteAllByMemoryId(Long memoryId); | ||
} |
61 changes: 61 additions & 0 deletions
61
...ersistence-database/src/main/java/com/depromeet/image/repository/ImageRepositoryImpl.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,61 @@ | ||
package com.depromeet.image.repository; | ||
|
||
import com.depromeet.image.Image; | ||
import com.depromeet.image.entity.ImageEntity; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ImageRepositoryImpl implements ImageRepository { | ||
private final ImageJpaRepository imageJpaRepository; | ||
|
||
@Override | ||
public Long save(Image image) { | ||
ImageEntity imageEntity = imageJpaRepository.save(ImageEntity.from(image)); | ||
return imageEntity.getId(); | ||
} | ||
|
||
@Override | ||
public List<Long> saveAll(List<Image> images) { | ||
List<ImageEntity> memoryImageEntities = images.stream().map(ImageEntity::from).toList(); | ||
|
||
return imageJpaRepository.saveAll(memoryImageEntities).stream() | ||
.map(ImageEntity::getId) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public Optional<Image> findById(Long id) { | ||
return imageJpaRepository.findById(id).map(ImageEntity::toModel); | ||
} | ||
|
||
@Override | ||
public List<Image> findImagesByMemoryId(Long memoryId) { | ||
return imageJpaRepository.findByMemoryId(memoryId).stream() | ||
.map(ImageEntity::toModel) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public List<Image> findImageByIds(List<Long> ids) { | ||
return imageJpaRepository.findAllByIds(ids).stream().map(ImageEntity::toModel).toList(); | ||
} | ||
|
||
@Override | ||
public void deleteById(Long id) { | ||
imageJpaRepository.deleteById(id); | ||
} | ||
|
||
@Override | ||
public void deleteAllByIds(List<Long> ids) { | ||
imageJpaRepository.deleteAllById(ids); | ||
} | ||
|
||
@Override | ||
public void deleteAllByMemoryId(Long memoryId) { | ||
imageJpaRepository.deleteAllByMemoryId(memoryId); | ||
} | ||
} |
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
Oops, something went wrong.