Skip to content

Commit

Permalink
#10 feat: 나눔글 등록 API
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Feb 5, 2024
1 parent 442b2ef commit 77dc301
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.kkobugi.puremarket.giveaway.application;

import com.kkobugi.puremarket.common.BaseException;
import com.kkobugi.puremarket.common.gcs.GCSService;
import com.kkobugi.puremarket.giveaway.domain.dto.GiveawayListResponse;
import com.kkobugi.puremarket.giveaway.domain.dto.GiveawayResponse;
import com.kkobugi.puremarket.giveaway.domain.entity.Giveaway;
import com.kkobugi.puremarket.giveaway.repository.GiveawayRepository;
import com.kkobugi.puremarket.produce.domain.dto.GiveawayPostRequest;
import com.kkobugi.puremarket.user.application.AuthService;
import com.kkobugi.puremarket.user.domain.entity.User;
import com.kkobugi.puremarket.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -20,6 +25,11 @@
public class GiveawayService {
private final GiveawayRepository giveawayRepository;
private final AuthService authService;
private final UserRepository userRepository;
private final GCSService gcsService;

@Value("${spring.cloud.gcp.storage.bucket}")
private String bucketName;

// 나눔글 목록 조회
public GiveawayListResponse getGiveawayList() throws BaseException { // TODO: 나눔완료 글도 목록 조회 시 포함하도록 수정 팔요
Expand Down Expand Up @@ -60,4 +70,24 @@ public GiveawayResponse getGiveawayPost(Long giveawayIdx) throws BaseException {
throw new BaseException(DATABASE_ERROR);
}
}

// 나눔글 등록
public void postGiveaway(GiveawayPostRequest giveawayPostRequest) throws BaseException {
try {
User writer = userRepository.findByUserIdx(authService.getUserIdxFromToken()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));

// upload image
String fullPath = gcsService.uploadImage("giveaway", giveawayPostRequest.giveawayImage());
String giveawayImageUrl = "https://storage.googleapis.com/"+bucketName+"/"+fullPath;

Giveaway giveaway = new Giveaway(writer, giveawayPostRequest.title(), giveawayPostRequest.content(), giveawayImageUrl);
giveaway.setStatus(GIVEAWAY);

giveawayRepository.save(giveaway);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.kkobugi.puremarket.user.domain.entity.User;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
Expand All @@ -29,4 +30,12 @@ public class Giveaway extends BaseEntity {

@Column(nullable = false)
private String giveawayImage;

@Builder
public Giveaway(User user, String title, String content, String giveawayImage) {
this.user = user;
this.title = title;
this.content = content;
this.giveawayImage = giveawayImage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import com.kkobugi.puremarket.common.BaseException;
import com.kkobugi.puremarket.common.BaseResponse;
import com.kkobugi.puremarket.giveaway.application.GiveawayService;
import com.kkobugi.puremarket.produce.domain.dto.GiveawayPostRequest;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import static com.kkobugi.puremarket.common.constants.RequestURI.giveaway;
import static com.kkobugi.puremarket.common.enums.BaseResponseStatus.SUCCESS;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -41,4 +40,15 @@ public BaseResponse<?> getGiveawayList(@PathVariable Long giveawayIdx) {
return new BaseResponse<>(e.getStatus());
}
}

// 나눔글 등록
@PostMapping("")
public BaseResponse<?> postGiveaway(GiveawayPostRequest giveawayPostRequest) {
try {
giveawayService.postGiveaway(giveawayPostRequest);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kkobugi.puremarket.produce.domain.dto;

import org.springframework.web.multipart.MultipartFile;

public record GiveawayPostRequest(String title,
String content,
MultipartFile giveawayImage) {}

0 comments on commit 77dc301

Please sign in to comment.