Skip to content

Commit

Permalink
Merge branch 'fix/7-uploadStory' into feature/7-storyLike
Browse files Browse the repository at this point in the history
  • Loading branch information
5jisoo committed Aug 10, 2023
2 parents b79cc39 + 08f92be commit b095fc3
Show file tree
Hide file tree
Showing 23 changed files with 800 additions and 7 deletions.
1 change: 1 addition & 0 deletions place/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
13 changes: 11 additions & 2 deletions place/src/main/java/com/umc/place/common/BaseResponseStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ public enum BaseResponseStatus {
/**
* 2000: Request 오류
*/
INVALID_STORY_IDX(false, 2100, "잘못된 스토리 Idx 입니다"),

// user(2000~2099)
INVALID_USER_IDX(false, 2000, "잘못된 user Idx 입니다."),
NULL_TOKEN(false, 2001, "토큰 값을 입력해주세요."),
NULL_USER_IDX(false, 2002, "user Idx를 입력해주세요."),
NULL_PROVIDER(false, 2003, "소셜 이름을 입력해주세요."),
INVALID_PROVIDER(false, 2004, "잘못된 소셜 이름입니다."),
ALREADY_WITHDRAW_USER(false, 2005, "이미 탈퇴한 회원입니다."),
INVALID_TOKEN(false, 2006, "유효하지 않은 토큰 값입니다."),
UNSUPPORTED_TOKEN(false, 2007, "잘못된 형식의 토큰 값입니다."),
MALFORMED_TOKEN(false, 2008, "잘못된 구조의 토큰 값입니다."),

// story(2100~2199)
INVALID_STORY_IDX(false, 2100, "잘못된 스토리 Idx 입니다"),

// exhibition(2200~2299)
INVALID_EXHIBITION_IDX(false, 2200, "잘못된 전시회 Idx 입니다."),
Expand All @@ -31,6 +38,8 @@ public enum BaseResponseStatus {
* 3000: Response 오류
*/
// user(3000~3099)
EXPIRED_TOKEN(false, 3000, "만료된 토큰 값입니다."),
EXIST_NICKNAME(false, 3001, "이미 사용 중인 닉네임입니다."),

// story(3100~3199)

Expand Down
1 change: 1 addition & 0 deletions place/src/main/java/com/umc/place/common/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public class Constant {
public static final String ACTIVE = "active";
public static final String INACTIVE = "inactive";
public static final String LOGOUT = "logout";
}
32 changes: 32 additions & 0 deletions place/src/main/java/com/umc/place/common/config/AWSConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.umc.place.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
public class AWSConfig {

@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;

@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.umc.place.common.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.umc.place.common.service.S3Upload;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

@RequiredArgsConstructor
@RestController
public class FileUploadController {

private final S3Upload s3Upload;

@PostMapping("/upload")
public ResponseEntity<Object> uploadFile(MultipartFile[] multipartFileLis) throws IOException {
return ResponseEntity.ok(
s3Upload.upload(multipartFileLis)
);
}
}
79 changes: 79 additions & 0 deletions place/src/main/java/com/umc/place/common/service/S3Upload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.umc.place.common.service;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RequiredArgsConstructor
@Service
public class S3Upload {

@Value("${cloud.aws.s3.bucket}")
private String bucket;

private final AmazonS3Client amazonS3Client;
;

//https://jforj.tistory.com/261
public List<String> upload(MultipartFile[] multipartFileList) throws IOException {
List<String> imagePathList = new ArrayList<>();

for (MultipartFile multipartFile : multipartFileList) {
String originalName = multipartFile.getOriginalFilename(); // 파일 이름
long size = multipartFile.getSize(); // 파일 크기

ObjectMetadata objectMetaData = new ObjectMetadata();
objectMetaData.setContentType(multipartFile.getContentType());
objectMetaData.setContentLength(size);

// S3에 업로드
amazonS3Client.putObject(
new PutObjectRequest(bucket, originalName, multipartFile.getInputStream(), objectMetaData)
.withCannedAcl(CannedAccessControlList.PublicRead)
);

String imagePath = amazonS3Client.getUrl(bucket, originalName).toString(); // 접근가능한 URL 가져오기
imagePathList.add(imagePath);
}
return imagePathList;
}
}

// String s3FileName = UUID.randomUUID() + "-" + multipartFile.getOriginalFilename();
//
// ObjectMetadata objMeta = new ObjectMetadata();
// objMeta.setContentLength(multipartFile.getInputStream().available());
//
// amazonS3.putObject(bucket, s3FileName, multipartFile.getInputStream(), objMeta);
//
// return amazonS3.getUrl(bucket, s3FileName).toString();
// for(MultipartFile multipartFile: multipartFileList) {
// String originalName = multipartFile.getOriginalFilename(); // 파일 이름
// long size = multipartFile.getSize(); // 파일 크기
//
// ObjectMetadata objectMetaData = new ObjectMetadata();
// objectMetaData.setContentType(multipartFile.getContentType());
// objectMetaData.setContentLength(size);
//
// // S3에 업로드
// amazonS3Client.putObject(
// new PutObjectRequest(S3Bucket, originalName, multipartFile.getInputStream(), objectMetaData)
// .withCannedAcl(CannedAccessControlList.PublicRead)
// );
//
// String imagePath = amazonS3Client.getUrl(S3Bucket, originalName).toString(); // 접근가능한 URL 가져오기
// imagePathList.add(imagePath);
// }
//
// return new ResponseEntity<Object>(imagePathList, HttpStatus.OK);
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.umc.place.exhibition.dto;

import com.umc.place.exhibition.entity.Exhibition;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class SearchExhibitionsByNameResDto {

private List<SearchedExhibitionByName> searchedExhibitions = new ArrayList();

@Builder
public SearchExhibitionsByNameResDto(Page<Exhibition> exhibitions) {
this.searchedExhibitions
= exhibitions.stream()
.map(exhibition -> new SearchedExhibitionByName(exhibition))
.collect(Collectors.toList());
}

@Data
@NoArgsConstructor
public static class SearchedExhibitionByName {
private Long exhibitionIdx;
private String exhibitionName;

@Builder
public SearchedExhibitionByName(Exhibition exhibition) {
this.exhibitionIdx = exhibition.getExhibitionIdx();
this.exhibitionName = exhibition.getExhibitionName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
@Repository
public interface ExhibitionRepository extends JpaRepository<Exhibition, Long> {
Page<Exhibition> findByCategory(Category category, Pageable pageable); // 카테고리 기반 전체 조회(페이징)

Page<Exhibition> findAll(Pageable pageable); // 전체 조회(페이징)

boolean existsByCategory(Category category);

@Query("select case when count(e) > 0 then true else false end from Exhibition e where Function('replace', e.location, ' ', '') like %:location%")
Expand All @@ -25,4 +27,6 @@ public interface ExhibitionRepository extends JpaRepository<Exhibition, Long> {

@Query("select e from Exhibition e where Function('replace', e.location, ' ', '') like %:location%")
Page<Exhibition> findByLocationLike(@Param("location") String location, Pageable pageable);

Page<Exhibition> findByExhibitionNameContainingOrderByExhibitionName(String searchKeyword, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import com.umc.place.comment.service.CommentService;
import com.umc.place.common.BaseException;
import com.umc.place.common.BaseResponse;
import com.umc.place.exhibition.dto.SearchExhibitionsByNameResDto;
import com.umc.place.story.dto.StoryDetailResponseDto;
import com.umc.place.story.dto.StoryUploadRequestDto;
import com.umc.place.story.dto.StoryUploadResponseDto;
import com.umc.place.story.service.StoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import static com.umc.place.common.BaseResponseStatus.NULL_STORY;
Expand Down Expand Up @@ -42,6 +45,17 @@ public BaseResponse<StoryDetailResponseDto> getStoryDetail(@PathVariable Long st
}
}

@GetMapping("/search")
public BaseResponse<SearchExhibitionsByNameResDto> getExhibitionWhenUploadStory(
@PageableDefault(size = 5) Pageable pageable,
@RequestParam(required = false) String searchWord) {
try {
return new BaseResponse<>(storyService.searchExhibitionByName(searchWord, pageable));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

@PostMapping("/{storyIdx}/comment")
public BaseResponse<CommentUploadResDto> uploadStoryComment(@PathVariable Long storyIdx,
@RequestBody CommentUploadReqDto reqDto,
Expand Down
16 changes: 16 additions & 0 deletions place/src/main/java/com/umc/place/story/service/StoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.umc.place.comment.dto.CommentResDto;
import com.umc.place.comment.repository.CommentRepository;
import com.umc.place.common.BaseException;
import com.umc.place.exhibition.dto.SearchExhibitionsByNameResDto;
import com.umc.place.exhibition.entity.Exhibition;
import com.umc.place.exhibition.repository.ExhibitionRepository;
import com.umc.place.story.dto.StoryDetailResponseDto;
Expand All @@ -17,6 +18,8 @@
import com.umc.place.user.entity.User;
import com.umc.place.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -82,6 +85,19 @@ public StoryDetailResponseDto getStoryDetail(Long storyIdx, Long userId) throws
}
}

public SearchExhibitionsByNameResDto searchExhibitionByName(String searchWord, Pageable page) throws BaseException {
try {
searchWord = searchWord.trim();
Page<Exhibition> searchedExhibitions
= exhibitionRepository.findByExhibitionNameContainingOrderByExhibitionName(searchWord, page);
return SearchExhibitionsByNameResDto.builder()
.exhibitions(searchedExhibitions)
.build();
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

@Transactional
public StoryUploadResponseDto uploadStory(StoryUploadRequestDto storyUploadRequestDto, Long userId) throws BaseException {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.umc.place.user.OAuth2.dto;

import lombok.Data;

@Data
public class KakaoTokenResponse {
private String token_type;
private String access_token;
private int expires_in;
private String refresh_token;
private int refresh_token_expires_in;
private String scope;
}
Loading

0 comments on commit b095fc3

Please sign in to comment.