Skip to content

Commit

Permalink
[Feat] S3를 필요한 API와 연결한다 (#59)
Browse files Browse the repository at this point in the history
feat: 사용자 및 차량 사진 추가 시 S3 업로드 연결

Co-authored-by: 길지운 <[email protected]>
  • Loading branch information
Ji-Un-Gil and wldns2577 authored Oct 15, 2023
1 parent 7f0ebbd commit b0b0da2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -25,7 +27,7 @@ public ResponseEntity<BaseResponse> getCarList(@RequestParam Long memberId) {
@PostMapping // 자동차 등록
public ResponseEntity<BaseResponse> 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);
}
Expand All @@ -34,7 +36,7 @@ public ResponseEntity<BaseResponse> createCar(@RequestPart(value = "request") @V
public ResponseEntity<BaseResponse> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,14 +30,16 @@ 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<CarResponse> getCarList(Long memberId) {
return getCarListByMemberId(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<Tag> createdTagList = getTagListByName(carRequest.getTagList());
Expand All @@ -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);

Expand All @@ -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에 등록된 사용자의 정보와 이용자의 정보를 확인하는 메서드
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +25,7 @@ public ResponseEntity<BaseResponse> getMember(@RequestParam Long memberId) {
@PutMapping
public ResponseEntity<BaseResponse> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -99,7 +102,7 @@ public void throwExceptionByInvalidMemberId() {

@Test
@DisplayName("자동차 정보 생성에 성공한다")
public void successCreateCar() {
public void successCreateCar() throws IOException {
// when
carService.createCar(
getCarRequest(),
Expand Down Expand Up @@ -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[]{}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit b0b0da2

Please sign in to comment.