diff --git a/src/main/java/com/server/mappin/controller/LostController.java b/src/main/java/com/server/mappin/controller/LostController.java index 237c7e7..b624c9b 100644 --- a/src/main/java/com/server/mappin/controller/LostController.java +++ b/src/main/java/com/server/mappin/controller/LostController.java @@ -11,9 +11,11 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.persistence.Column; import java.io.IOException; @@ -26,12 +28,15 @@ public class LostController { private final LostService lostService; - @Operation(summary = "분실물 등록") + @Operation(summary = "분실물 등록", description = "Content-Type은 multipart/form-data이지만 info는 application/json입니다") @ApiResponse(content = @Content(schema = @Schema(implementation = LostRegisterResponseDto.class))) - @PutMapping("/lost/register") - public ResponseEntity registerLost(@ModelAttribute LostRegisterRequestDto lostRegisterRequestDto, Authentication authentication) throws IOException { + @PutMapping(value = "/lost/register", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity registerLost( + @ModelAttribute("image") MultipartFile file, + @RequestPart("info") LostRegisterRequestDto lostRegisterRequestDto, + Authentication authentication) throws IOException { try { - LostRegisterResponseDto lostRegisterResponseDto = lostService.registerLost(lostRegisterRequestDto, authentication.getName()); + LostRegisterResponseDto lostRegisterResponseDto = lostService.registerLost(lostRegisterRequestDto,file, authentication.getName()); return new ResponseEntity<>(lostRegisterResponseDto, HttpStatus.OK); } catch (IllegalStateException e) { return new ResponseEntity<>("에러가 발생했습니다", HttpStatus.CONFLICT); @@ -40,8 +45,8 @@ public ResponseEntity registerLost(@ModelAttribute LostRegisterRequestDto los @Operation(summary = "카테고리 검색", description = "분실물을 카테고리 별로 검색") @ApiResponse(content = @Content(schema = @Schema(implementation = FindByCategoryListResponseDto.class))) - @GetMapping("/search/category/{category_name}") - public ResponseEntity searchByCategory(@RequestParam(value = "category_name") String name) { + @GetMapping("lost/search?category={category_name}") + public ResponseEntity searchByCategory(@PathVariable(value = "category_name") String name) { try { FindByCategoryListResponseDto findByCategoryListResponseDto = lostService.findByCategory(name); return new ResponseEntity<>(findByCategoryListResponseDto, HttpStatus.OK); @@ -52,8 +57,8 @@ public ResponseEntity searchByCategory(@RequestParam(value = "category_name") @Operation(summary = "동 검색", description = "분실물을 동 별로 검색") @ApiResponse(content = @Content(schema = @Schema(implementation = FindByDongResponseDto.class))) - @GetMapping("/search/dong/{dong_name}") - public ResponseEntity searchByDong(@RequestParam(value = "dong_name") String dongName) { + @GetMapping("lost/search?dong={dong_name}") + public ResponseEntity searchByDong(@PathVariable(value = "dong_name") String dongName) { try { List byDong = lostService.findByDong(dongName); return new ResponseEntity<>(byDong, HttpStatus.OK); @@ -64,8 +69,8 @@ public ResponseEntity searchByDong(@RequestParam(value = "dong_name") String @Operation(summary = "가게 검색", description = "분실물을 가게이름 별로 검색") @ApiResponse(content = @Content(schema = @Schema(implementation = FindByShopResponseDto.class))) - @GetMapping("/search/shop/{shop_name}") - public ResponseEntity searchByShop(@RequestParam(value = "shop_name") String shopName) { + @GetMapping("lost/search?shop={shop_name}") + public ResponseEntity searchByShop(@PathVariable(value = "shop_name") String shopName) { try { List byDong = lostService.findByShop(shopName); return new ResponseEntity<>(byDong, HttpStatus.OK); @@ -76,8 +81,8 @@ public ResponseEntity searchByShop(@RequestParam(value = "shop_name") String @Operation(summary = "현재위치로 검색", description = "분실물을 현재위치로 검색") @ApiResponse(content = @Content(schema = @Schema(implementation = FindByDongResponseDto.class))) - @GetMapping("/search/location/{x}/{y}") - public ResponseEntity searchByCurrentLocation(@RequestParam(value = "x") Double x, @RequestParam(value = "y") Double y) { + @GetMapping("lost/search?x={x}&y={y}") + public ResponseEntity searchByCurrentLocation(@PathVariable(value = "x") Double x, @RequestParam(value = "y") Double y) { try { List byDong = lostService.findByCurrentLocation(x, y); return new ResponseEntity<>(byDong, HttpStatus.OK); diff --git a/src/main/java/com/server/mappin/controller/PostController.java b/src/main/java/com/server/mappin/controller/PostController.java index 975b248..976dde1 100644 --- a/src/main/java/com/server/mappin/controller/PostController.java +++ b/src/main/java/com/server/mappin/controller/PostController.java @@ -6,6 +6,7 @@ import com.server.mappin.dto.PostSearchResponseDto; import com.server.mappin.service.PostService; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -14,9 +15,11 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @@ -27,16 +30,20 @@ public class PostController { private final PostService postService; - @Operation(summary = "게시물 작성",description = "게시물을 작성합니다") + @Operation(summary = "게시물 작성",description = "Content-type은 multipart/form-data이지만 info는 application/json입니다") @ApiResponses({ @ApiResponse(responseCode ="200",description ="게시물 작성 성공",content = @Content(schema = @Schema(implementation = PostCreateResponseDto.class))), @ApiResponse(responseCode ="400",description ="게시물 작성 실패",content = @Content(schema = @Schema(implementation = PostCreateResponseDto.class))) }) - @PutMapping("/post") - public ResponseEntity create(@ModelAttribute PostCreateRequestDto postCreateDto, Authentication authentication) throws IOException { + @PutMapping(value = "/post/new",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity create( + @RequestPart("image")MultipartFile image, + @RequestPart("info") PostCreateRequestDto postCreateDto, + Authentication authentication + ) throws IOException { try{ String email = authentication.getName(); - PostCreateResponseDto postCreateResponseDto = postService.create(postCreateDto, email); + PostCreateResponseDto postCreateResponseDto = postService.create(postCreateDto,image, email); return new ResponseEntity<>(postCreateResponseDto,HttpStatus.OK); }catch (IllegalStateException e){ return new ResponseEntity<>("에러가 발생했습니다", HttpStatus.CONFLICT); diff --git a/src/main/java/com/server/mappin/dto/LostRegisterRequestDto.java b/src/main/java/com/server/mappin/dto/LostRegisterRequestDto.java index b19f2ec..10e8566 100644 --- a/src/main/java/com/server/mappin/dto/LostRegisterRequestDto.java +++ b/src/main/java/com/server/mappin/dto/LostRegisterRequestDto.java @@ -15,7 +15,6 @@ public class LostRegisterRequestDto { private String title; private String content; private String foundDate; - private MultipartFile image; private Double x; private Double y; private String dong; diff --git a/src/main/java/com/server/mappin/dto/PostCreateRequestDto.java b/src/main/java/com/server/mappin/dto/PostCreateRequestDto.java index fb72ea0..eabac26 100644 --- a/src/main/java/com/server/mappin/dto/PostCreateRequestDto.java +++ b/src/main/java/com/server/mappin/dto/PostCreateRequestDto.java @@ -12,7 +12,6 @@ public class PostCreateRequestDto { private String title; private String content; private String lostDate; - private MultipartFile image; private Double x; private Double y; private String category; diff --git a/src/main/java/com/server/mappin/service/LostService.java b/src/main/java/com/server/mappin/service/LostService.java index 69be694..3e8d31b 100644 --- a/src/main/java/com/server/mappin/service/LostService.java +++ b/src/main/java/com/server/mappin/service/LostService.java @@ -10,6 +10,7 @@ import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.time.LocalDate; @@ -70,7 +71,7 @@ public List findByCurrentLocation(Double x, Double y) { } @Transactional - public LostRegisterResponseDto registerLost(LostRegisterRequestDto lostRegisterRequestDto, String email) throws IOException { + public LostRegisterResponseDto registerLost(LostRegisterRequestDto lostRegisterRequestDto, MultipartFile image, String email) throws IOException { //String으로 받아온 yyyy-MM-dd를 LocalDate 형식으로 변환 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.parse(lostRegisterRequestDto.getFoundDate(),formatter); @@ -81,7 +82,7 @@ public LostRegisterResponseDto registerLost(LostRegisterRequestDto lostRegisterR String dong = mapService.getDong(lostRegisterRequestDto.getX(), lostRegisterRequestDto.getY()); Optional locationByDong = locationRepository.findLocationByDong(dong); //이미지 S3에 업로드 - String imageUrl = s3Service.upload(lostRegisterRequestDto.getImage(), "images"); + String imageUrl = s3Service.upload(image, "images"); if(memberRepositoryByEmail.isPresent() && categoryByName.isPresent() && locationByDong.isPresent()){ Member member = memberRepositoryByEmail.get(); Location location = locationByDong.get(); diff --git a/src/main/java/com/server/mappin/service/PostService.java b/src/main/java/com/server/mappin/service/PostService.java index b4ee4ba..7aa16d0 100644 --- a/src/main/java/com/server/mappin/service/PostService.java +++ b/src/main/java/com/server/mappin/service/PostService.java @@ -15,6 +15,7 @@ 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.time.LocalDate; @@ -37,7 +38,7 @@ public class PostService { @Transactional - public PostCreateResponseDto create(PostCreateRequestDto postCreateRequestDto, String email) throws IOException { + public PostCreateResponseDto create(PostCreateRequestDto postCreateRequestDto, MultipartFile image, String email) throws IOException { //String date -> LocalDate로 변경 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.parse(postCreateRequestDto.getLostDate(),formatter); @@ -46,7 +47,7 @@ public PostCreateResponseDto create(PostCreateRequestDto postCreateRequestDto, S Optional locationByDong = locationRepository.findLocationByDong(dong); Optional categoryByName = categoryRepository.findCategoryByName(postCreateRequestDto.getCategory()); Optional memberByEmail = memberRepository.findByEmail(email); - String imageUrl = s3Service.upload(postCreateRequestDto.getImage(), "images"); + String imageUrl = s3Service.upload(image, "images"); Location location; Category category; Member member;