-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from eco-cycle/hyundong
Hyundong
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...in/java/com/hackathon/ecocycle/domain/recycling_method/api/RecyclingMethodController.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,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, "재활용 방법 추가 성공"); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...va/com/hackathon/ecocycle/domain/recycling_method/application/RecyclingMethodService.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,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() | ||
) | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...in/java/com/hackathon/ecocycle/domain/recycling_method/domain/entity/RecyclingMethod.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,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; | ||
} |
10 changes: 10 additions & 0 deletions
10
...ckathon/ecocycle/domain/recycling_method/domain/repository/RecyclingMethodRepository.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,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); | ||
} |
17 changes: 17 additions & 0 deletions
17
...n/java/com/hackathon/ecocycle/domain/recycling_method/dto/RecyclingMethodResponseDto.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.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; | ||
} |