diff --git a/src/main/java/com/example/cargive/domain/car/controller/CarController.java b/src/main/java/com/example/cargive/domain/car/controller/CarController.java index 852a301..2fc6120 100644 --- a/src/main/java/com/example/cargive/domain/car/controller/CarController.java +++ b/src/main/java/com/example/cargive/domain/car/controller/CarController.java @@ -11,6 +11,8 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; + @RestController @RequiredArgsConstructor @RequestMapping("/api/car") @@ -25,7 +27,7 @@ public ResponseEntity getCarList(@RequestParam Long memberId) { @PostMapping // 자동차 등록 public ResponseEntity createCar(@RequestPart(value = "request") @Valid CarRequest request, @RequestPart(value = "file") MultipartFile file, - @RequestParam Long memberId) { + @RequestParam Long memberId) throws IOException { carService.createCar(request, file, memberId); return BaseResponse.toResponseEntityContainsStatus(BaseResponseStatus.CREATED); } @@ -34,7 +36,7 @@ public ResponseEntity createCar(@RequestPart(value = "request") @V public ResponseEntity editCar(@RequestPart(value = "request") @Valid CarEditRequest request, @RequestPart(value = "file", required = false) MultipartFile file, @PathVariable Long carId, - @RequestParam Long memberId) { + @RequestParam Long memberId) throws IOException { carService.editCar(request, file, carId, memberId); return BaseResponse.toResponseEntityContainsStatus(BaseResponseStatus.SUCCESS); } diff --git a/src/main/java/com/example/cargive/domain/car/service/CarService.java b/src/main/java/com/example/cargive/domain/car/service/CarService.java index 7a2ef68..14ac7d5 100644 --- a/src/main/java/com/example/cargive/domain/car/service/CarService.java +++ b/src/main/java/com/example/cargive/domain/car/service/CarService.java @@ -12,11 +12,13 @@ import com.example.cargive.domain.tag.service.TagFindService; import com.example.cargive.global.base.BaseException; import com.example.cargive.global.base.BaseResponseStatus; +import com.example.cargive.global.service.S3Service; import lombok.RequiredArgsConstructor; 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; import java.util.ArrayList; import java.util.List; @@ -28,6 +30,8 @@ public class CarService { private final TagFindService tagFindService; private final TagCreateService tagCreateService; private final MemberRepository memberRepository; + private final S3Service s3Service; + private static final String S3_DIRECTORY_CAR = "car"; @Transactional(readOnly = true) public List getCarList(Long memberId) { @@ -35,7 +39,7 @@ public List getCarList(Long memberId) { } @Transactional - public void createCar(CarRequest carRequest, MultipartFile file, Long memberId) { + public void createCar(CarRequest carRequest, MultipartFile file, Long memberId) throws IOException { String imageUrl = getImageUrl(file); Member findMember = getMember(memberId); List createdTagList = getTagListByName(carRequest.getTagList()); @@ -48,7 +52,7 @@ public void createCar(CarRequest carRequest, MultipartFile file, Long memberId) } @Transactional - public void editCar(CarEditRequest request, MultipartFile file, Long carId, Long memberId) { + public void editCar(CarEditRequest request, MultipartFile file, Long carId, Long memberId) throws IOException { Car findCar = getCarById(carId); Member findMember = getMember(memberId); @@ -67,10 +71,10 @@ public void deleteCar(Long carId, Long memberId) { findMember.removeCar(findCar); } - // S3를 활용해서 이미지의 URL을 반환하는 메서드. 현재로써는 기능이 구현되어 있지 않기 때문에 임의의 문자열을 반환 - private String getImageUrl(MultipartFile file) { + // S3를 활용해서 이미지의 URL을 반환하는 메서드 + private String getImageUrl(MultipartFile file) throws IOException { if(file.isEmpty()) return ""; - return "ImageUrl"; + return s3Service.upload(file, S3_DIRECTORY_CAR); } // Car Entity에 등록된 사용자의 정보와 이용자의 정보를 확인하는 메서드 @@ -80,7 +84,7 @@ private void checkValidation(Car car, Member member) { } // 사용자로부터 입력 받은 정보를 통해서 Car Entity의 정보를 수정하는 메서드 - private void editCarInfo(Car car, CarEditRequest request, MultipartFile file) { + private void editCarInfo(Car car, CarEditRequest request, MultipartFile file) throws IOException { LocalDate recentCheck = request.getRecentCheck(); String imageUrl = getImageUrl(file); Long mileage = request.getMileage(); diff --git a/src/main/java/com/example/cargive/domain/member/controller/MemberController.java b/src/main/java/com/example/cargive/domain/member/controller/MemberController.java index 050cc28..fa3e387 100644 --- a/src/main/java/com/example/cargive/domain/member/controller/MemberController.java +++ b/src/main/java/com/example/cargive/domain/member/controller/MemberController.java @@ -9,6 +9,8 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; + @RestController @RequestMapping("/api/member") @RequiredArgsConstructor @@ -23,7 +25,7 @@ public ResponseEntity getMember(@RequestParam Long memberId) { @PutMapping public ResponseEntity editMember(@RequestParam Long memberId, @RequestPart(name = "request") MemberRequest request, - @RequestPart(name = "file") MultipartFile file) { + @RequestPart(name = "file") MultipartFile file) throws IOException { memberService.editMember(memberId, request, file); return BaseResponse.toResponseEntityContainsResult(BaseResponseStatus.SUCCESS); } diff --git a/src/main/java/com/example/cargive/domain/member/service/MemberService.java b/src/main/java/com/example/cargive/domain/member/service/MemberService.java index 2b01b71..786c1a6 100644 --- a/src/main/java/com/example/cargive/domain/member/service/MemberService.java +++ b/src/main/java/com/example/cargive/domain/member/service/MemberService.java @@ -6,16 +6,21 @@ import com.example.cargive.domain.member.repository.MemberRepository; import com.example.cargive.global.base.BaseException; import com.example.cargive.global.base.BaseResponseStatus; +import com.example.cargive.global.service.S3Service; import com.example.cargive.global.template.Status; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; + @Service @RequiredArgsConstructor public class MemberService { private final MemberRepository memberRepository; + private final S3Service s3Service; + private static final String S3_DIRECTORY_MEMBER = "member"; @Transactional(readOnly = true) public MemberResponse getMemberInfo(Long memberId) { @@ -26,7 +31,7 @@ public MemberResponse getMemberInfo(Long memberId) { } @Transactional - public void editMember(Long memberId, MemberRequest request, MultipartFile file) { + public void editMember(Long memberId, MemberRequest request, MultipartFile file) throws IOException { Member findMember = getMember(memberId); checkValidation(findMember); @@ -46,14 +51,14 @@ private void checkValidation(Member member) { throw new BaseException(BaseResponseStatus.MEMBER_STATUS_NOT_VALID_ERROR); } - private void editMemberInfo(Member member, MemberRequest request, MultipartFile file) { + private void editMemberInfo(Member member, MemberRequest request, MultipartFile file) throws IOException { String imageUrl = getImageUrl(file); member.editInfo(request.phoneNumber(), imageUrl); } - private String getImageUrl(MultipartFile file) { + private String getImageUrl(MultipartFile file) throws IOException { if(file.isEmpty()) return ""; - return "ImageUrl"; + return s3Service.upload(file, S3_DIRECTORY_MEMBER); } private Member getMember(Long memberId) { diff --git a/src/test/java/com/example/cargive/car/service/CarServiceTest.java b/src/test/java/com/example/cargive/car/service/CarServiceTest.java index 9434762..7a8c181 100644 --- a/src/test/java/com/example/cargive/car/service/CarServiceTest.java +++ b/src/test/java/com/example/cargive/car/service/CarServiceTest.java @@ -14,15 +14,18 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockMultipartFile; +import java.io.IOException; import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static com.example.cargive.car.fixture.CarFixture.*; -import static com.example.cargive.member.fixture.MemberFixture.*; -import static com.example.cargive.tag.fixture.TagFixture.*; -import static org.assertj.core.api.Assertions.*; +import static com.example.cargive.car.fixture.CarFixture.CAR_1; +import static com.example.cargive.member.fixture.MemberFixture.ASSAC; +import static com.example.cargive.member.fixture.MemberFixture.WIZ; +import static com.example.cargive.tag.fixture.TagFixture.TAG_1; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @DisplayName("Car [Service Layer] -> CarService 테스트") public class CarServiceTest extends ServiceTest { @@ -99,7 +102,7 @@ public void throwExceptionByInvalidMemberId() { @Test @DisplayName("자동차 정보 생성에 성공한다") - public void successCreateCar() { + public void successCreateCar() throws IOException { // when carService.createCar( getCarRequest(), @@ -136,7 +139,7 @@ public void throwExceptionByInvalidMemberId() { @Test @DisplayName("차량 정보 수정에 성공한다") - public void successEditCar() { + public void successEditCar() throws IOException { // when carService.editCar(getCarEditRequest(), new MockMultipartFile("Test Image", "test", null, new byte[]{}), diff --git a/src/test/java/com/example/cargive/member/service/MemberServiceTest.java b/src/test/java/com/example/cargive/member/service/MemberServiceTest.java index f8c7590..9708b4a 100644 --- a/src/test/java/com/example/cargive/member/service/MemberServiceTest.java +++ b/src/test/java/com/example/cargive/member/service/MemberServiceTest.java @@ -11,9 +11,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockMultipartFile; -import static com.example.cargive.member.fixture.MemberFixture.*; -import static org.assertj.core.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.*; +import java.io.IOException; + +import static com.example.cargive.member.fixture.MemberFixture.ASSAC; +import static com.example.cargive.member.fixture.MemberFixture.WIZ; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; @DisplayName("Member [Service Layer] -> MemberService 테스트") public class MemberServiceTest extends ServiceTest { @@ -109,7 +113,7 @@ public void throwExceptionByInvalidMember() { @Test @DisplayName("데이터 수정에 성공한다") - public void successEditMemberInfo() { + public void successEditMemberInfo() throws IOException { // given MemberRequest request = getMemberRequest(); MockMultipartFile file = new MockMultipartFile("image", "test",