Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 브랜드 등록 API - 이미지 S3 Upload #25

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package com.example.couphoneserver.controller;

import com.example.couphoneserver.common.annotation.NoAuth;
import com.example.couphoneserver.common.exception.BadRequestException;
import com.example.couphoneserver.common.exception.InternalServerErrorException;
import com.example.couphoneserver.common.response.BaseResponse;
import com.example.couphoneserver.dto.brand.GetBrandDetailResponse;
import com.example.couphoneserver.dto.brand.GetBrandResponse;
import com.example.couphoneserver.dto.brand.PostBrandRequest;
import com.example.couphoneserver.dto.brand.PostBrandResponse;
import com.example.couphoneserver.service.BrandService;
import com.example.couphoneserver.utils.S3Uploader;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;
import java.util.List;

import static com.example.couphoneserver.common.response.status.BaseExceptionResponseStatus.BAD_REQUEST;
import static com.example.couphoneserver.common.response.status.BaseExceptionResponseStatus.FILE_UPLOAD_FAILED;

@Tag(name = "brands", description = "브랜드 관련 API")
@Slf4j
Expand All @@ -27,17 +33,26 @@
@RequestMapping("/brands")
public class BrandController {
private final BrandService brandService;
private final S3Uploader s3Uploader;

@PreAuthorize("hasRole('ADMIN')")
@PostMapping("")
@PostMapping(value = "", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
@Operation(summary = "브랜드 등록",
description = """
Request Body에 브랜드 이름, 보상 설명, 이미지 url, 카테고리 id 담아서 보내주세요!
- [ROLE_ADMIN ONLY]
- Header에 Access Token,
- Request Body에 request: 브랜드 이름(String), 보상 설명(String), 카테고리 id(Long) / file: 이미지(MultipartFile) 담아서 보내주세요!
""",
security = @SecurityRequirement(name = "bearerAuth"))
public BaseResponse<PostBrandResponse> postBrand(@RequestBody PostBrandRequest request) {
return new BaseResponse<>(brandService.saveBrand(request));
public BaseResponse<PostBrandResponse> postBrand(@RequestPart PostBrandRequest request, @RequestPart("file") MultipartFile multipartFile) {

String brandImageUrl = s3Uploader.upload(multipartFile);

if (brandImageUrl == null) {
throw new InternalServerErrorException(FILE_UPLOAD_FAILED);
}

return new BaseResponse<>(brandService.saveBrand(request, brandImageUrl));
}

@PreAuthorize("hasRole('MEMBER') or hasRole('ADMIN')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public class PostBrandRequest {
@NotNull
private String rewardDescription;

@Schema(description = "브랜드 로고 URL", example = "----")
@NotNull
private String brandImageUrl;
// @Schema(description = "브랜드 로고 URL", example = "----")
// @NotNull
// private String brandImageUrl;

@Schema(description = "카테고리 ID", example = "1")
@NotNull
private Long categoryId;

public Brand toEntity(Category category) {
public Brand toEntity(Category category, String brandImageUrl) {
return Brand.builder()
.name(name)
.rewardDescription(rewardDescription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import com.example.couphoneserver.repository.CategoryRepository;
import com.example.couphoneserver.repository.CouponItemRepository;
import com.example.couphoneserver.repository.MemberRepository;
import com.example.couphoneserver.utils.S3Uploader;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Principal;
Expand All @@ -34,7 +36,8 @@ public class BrandService {

private final MemberService memberService;

public PostBrandResponse saveBrand(PostBrandRequest request) {

public PostBrandResponse saveBrand(PostBrandRequest request, String brandImageUrl) {
// 카테고리 존재하는지 검사
Category category = findCategoryById(request.getCategoryId());

Expand All @@ -43,7 +46,7 @@ public PostBrandResponse saveBrand(PostBrandRequest request) {


// 브랜드 저장
Brand brand = brandRepository.save(request.toEntity(category));
Brand brand = brandRepository.save(request.toEntity(category, brandImageUrl));

if (brand == null) {
throw new DatabaseException(DATABASE_ERROR);
Expand Down