diff --git a/src/main/java/com/hackathon/ecocycle/domain/recycling_method/api/RecyclingMethodController.java b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/api/RecyclingMethodController.java new file mode 100644 index 0000000..6330c60 --- /dev/null +++ b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/api/RecyclingMethodController.java @@ -0,0 +1,35 @@ +package com.hackathon.ecocycle.domain.recycling_method.api; + +import com.hackathon.ecocycle.domain.recycling_method.application.RecyclingMethodService; +import com.hackathon.ecocycle.domain.recycling_method.dto.RecyclingMethodResponseDto; +import com.hackathon.ecocycle.global.image.exception.ImageNotFoundException; +import com.hackathon.ecocycle.global.template.ResponseTemplate; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.List; + +@RestController +@RequiredArgsConstructor +@Tag(name = "Recycling Method Controller", description = "Recycling Method API") +public class RecyclingMethodController { + private final RecyclingMethodService recyclingMethodService; + + @GetMapping("/recycling/method") + public ResponseTemplate> getRecyclingMethodList() { + return new ResponseTemplate<>(HttpStatus.OK, "재활용 방법 조회 성공", recyclingMethodService.getRecyclingMethodList()); + } + + @PostMapping("/recycling/method") + public ResponseTemplate addRecyclingMethod(@RequestParam("file") MultipartFile[] file, @RequestParam("category") String category) throws IOException, ImageNotFoundException { + recyclingMethodService.productAddImage(file, category); + return new ResponseTemplate<>(HttpStatus.CREATED, "재활용 방법 추가 성공"); + } +} diff --git a/src/main/java/com/hackathon/ecocycle/domain/recycling_method/application/RecyclingMethodService.java b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/application/RecyclingMethodService.java new file mode 100644 index 0000000..b44638a --- /dev/null +++ b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/application/RecyclingMethodService.java @@ -0,0 +1,55 @@ +package com.hackathon.ecocycle.domain.recycling_method.application; + +import com.hackathon.ecocycle.domain.recycling_method.domain.entity.RecyclingMethod; +import com.hackathon.ecocycle.domain.recycling_method.domain.repository.RecyclingMethodRepository; +import com.hackathon.ecocycle.domain.recycling_method.dto.RecyclingMethodResponseDto; +import com.hackathon.ecocycle.global.image.exception.ImageNotFoundException; +import com.hackathon.ecocycle.global.image.service.ImageService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +@Slf4j +public class RecyclingMethodService { + private final ImageService imageService; + private final RecyclingMethodRepository recyclingMethodRepository; + + @Transactional(readOnly = true) + public List getRecyclingMethodList() { + List recyclingMethod = recyclingMethodRepository.findAll(); + List recyclingMethodResponseDtoList = new ArrayList<>(); + + for (RecyclingMethod recyclingMethodEntity : recyclingMethod) { + recyclingMethodResponseDtoList.add( + RecyclingMethodResponseDto.builder() + .category(recyclingMethodEntity.getCategory()) + .recyclingMethodImageUrlList(recyclingMethodEntity.getImageUrlList()) + .build() + ); + } + + return recyclingMethodResponseDtoList; + } + + @Transactional + public void productAddImage(MultipartFile[] image, String category) throws IOException, ImageNotFoundException { + List newImageUrlList = imageService.uploadImages(image); + + recyclingMethodRepository.findByCategory(category).orElseGet( + () -> recyclingMethodRepository.save( + RecyclingMethod.builder() + .category(category) + .imageUrlList(newImageUrlList) + .build() + ) + ); + } +} diff --git a/src/main/java/com/hackathon/ecocycle/domain/recycling_method/domain/entity/RecyclingMethod.java b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/domain/entity/RecyclingMethod.java new file mode 100644 index 0000000..cea9586 --- /dev/null +++ b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/domain/entity/RecyclingMethod.java @@ -0,0 +1,29 @@ +package com.hackathon.ecocycle.domain.recycling_method.domain.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Entity +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Table(name = "recycling_method") +public class RecyclingMethod { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "recycling_method_id") + private Long id; + + @Column(name = "category") + private String category; + + @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER) + @CollectionTable(name = "recycling_methods", joinColumns = @JoinColumn(name = "recycling_method_id")) + private List imageUrlList; +} diff --git a/src/main/java/com/hackathon/ecocycle/domain/recycling_method/domain/repository/RecyclingMethodRepository.java b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/domain/repository/RecyclingMethodRepository.java new file mode 100644 index 0000000..665930d --- /dev/null +++ b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/domain/repository/RecyclingMethodRepository.java @@ -0,0 +1,10 @@ +package com.hackathon.ecocycle.domain.recycling_method.domain.repository; + +import com.hackathon.ecocycle.domain.recycling_method.domain.entity.RecyclingMethod; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface RecyclingMethodRepository extends JpaRepository { + Optional findByCategory(String category); +} \ No newline at end of file diff --git a/src/main/java/com/hackathon/ecocycle/domain/recycling_method/dto/RecyclingMethodResponseDto.java b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/dto/RecyclingMethodResponseDto.java new file mode 100644 index 0000000..19d9e1f --- /dev/null +++ b/src/main/java/com/hackathon/ecocycle/domain/recycling_method/dto/RecyclingMethodResponseDto.java @@ -0,0 +1,17 @@ +package com.hackathon.ecocycle.domain.recycling_method.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Getter +public class RecyclingMethodResponseDto { + private String category; + private List recyclingMethodImageUrlList; +}