Skip to content

Commit

Permalink
Merge pull request #36 from depromeet/feature/30-image
Browse files Browse the repository at this point in the history
feat: ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ/์ˆ˜์ •/์กฐํšŒ/์‚ญ์ œ ๊ธฐ๋Šฅ ๊ตฌํ˜„
  • Loading branch information
penrose15 authored Jul 14, 2024
2 parents 0042c19 + 44e7f1f commit 3500c10
Show file tree
Hide file tree
Showing 30 changed files with 789 additions and 2 deletions.
42 changes: 42 additions & 0 deletions module-domain/src/main/java/com/depromeet/image/Image.java
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;
}
}
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;
}
}
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;
}
}
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();
}
}
2 changes: 2 additions & 0 deletions module-infrastructure/persistence-database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate.validator:hibernate-validator:8.0.1.Final'
runtimeOnly 'com.mysql:mysql-connector-j'

testRuntimeOnly 'com.h2database:h2'
}
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;
}
}
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);
}
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);
}
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);
}
}
3 changes: 3 additions & 0 deletions module-presentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.12.6'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.6'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.6'

// s3
implementation 'io.awspring.cloud:spring-cloud-aws-starter-s3:3.1.1'
}
Loading

0 comments on commit 3500c10

Please sign in to comment.