Skip to content

Commit

Permalink
Merge pull request #2 from eco-cycle/hyundong
Browse files Browse the repository at this point in the history
Hyundong
  • Loading branch information
hyundong-L authored Aug 23, 2024
2 parents b1fb1b8 + 2e7581f commit f50b6f7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<List<RecyclingMethodResponseDto>> 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, "재활용 방법 추가 성공");
}
}
Original file line number Diff line number Diff line change
@@ -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<RecyclingMethodResponseDto> getRecyclingMethodList() {
List<RecyclingMethod> recyclingMethod = recyclingMethodRepository.findAll();
List<RecyclingMethodResponseDto> 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<String> newImageUrlList = imageService.uploadImages(image);

recyclingMethodRepository.findByCategory(category).orElseGet(
() -> recyclingMethodRepository.save(
RecyclingMethod.builder()
.category(category)
.imageUrlList(newImageUrlList)
.build()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> imageUrlList;
}
Original file line number Diff line number Diff line change
@@ -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<RecyclingMethod, Long> {
Optional<RecyclingMethod> findByCategory(String category);
}
Original file line number Diff line number Diff line change
@@ -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<String> recyclingMethodImageUrlList;
}

0 comments on commit f50b6f7

Please sign in to comment.