Skip to content

Commit

Permalink
Merge pull request #114 from songhee1/feature/#110-discussion
Browse files Browse the repository at this point in the history
Refactor/#110 discussion
  • Loading branch information
songhee1 authored Jan 20, 2024
2 parents 0b867a8 + efde1b8 commit 56913a4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tavebalak.OTTify.common.BaseResponse;
import tavebalak.OTTify.community.dto.request.CommunitySubjectImageCreateDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectImageEditDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectCreateDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectEditDTO;
import tavebalak.OTTify.community.dto.request.ReplyCommentCreateDTO;
import tavebalak.OTTify.community.dto.request.ReplyCommentEditDTO;
import tavebalak.OTTify.community.dto.request.ReplyRecommentCreateDTO;
Expand All @@ -45,20 +47,22 @@ public class CommunityController {

@ApiOperation(value = "ํ† ๋ก ์ฃผ์ œ ์ƒ์„ฑ", notes = "ํšŒ์›์ด ์ž‘์„ฑํ•œ ํ† ๋ก ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.")
@ApiResponse(code = 200, message = "์„ฑ๊ณต์ ์œผ๋กœ ํ† ๋ก ์ฃผ์ œ๋ฅผ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.")
@PostMapping(value = "/subject", consumes = {"multipart/form-data"})
@PostMapping(value = "/subject", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public BaseResponse<String> registerSubject(
@Valid @ModelAttribute CommunitySubjectImageCreateDTO c) {
communityService.saveSubject(c);
@Valid @RequestPart(value = "dto") CommunitySubjectCreateDTO c,
@RequestPart(value = "file", required = false) MultipartFile image) {
communityService.saveSubject(c, image);
return BaseResponse.success("์„ฑ๊ณต์ ์œผ๋กœ ํ† ๋ก ์ฃผ์ œ๋ฅผ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
}

@ApiOperation(value = "ํ† ๋ก ์ฃผ์ œ ์ˆ˜์ •", notes = "ํšŒ์›์ด ์ž‘์„ฑํ•œ ํ† ๋ก ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ˆ˜์ •๋ฉ๋‹ˆ๋‹ค.")
@ApiResponse(code = 200, message = "์„ฑ๊ณต์ ์œผ๋กœ ํ† ๋ก ์ฃผ์ œ๋ฅผ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.")
@PutMapping(value = "/subject", consumes = {"multipart/form-data"})
@PutMapping(value = "/subject", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public BaseResponse<String> modifySubject(
@Valid @ModelAttribute CommunitySubjectImageEditDTO c)
@Valid @RequestPart(value = "dto") CommunitySubjectEditDTO c,
@RequestPart(value = "file", required = false) MultipartFile image)
throws NotFoundException {
communityService.modifySubject(c);
communityService.modifySubject(c, image);
return BaseResponse.success("์„ฑ๊ณต์ ์œผ๋กœ ํ† ๋ก ์ฃผ์ œ๋ฅผ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
Expand All @@ -26,6 +25,6 @@ public class CommunitySubjectImageEditDTO {
@ApiModelProperty(value = "ํ† ๋ก  ๋‚ด์šฉ", required = true)
private String content;

@ApiParam(value = "์ด๋ฏธ์ง€")
@ApiModelProperty(value = "์ด๋ฏธ์ง€")
private MultipartFile image;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package tavebalak.OTTify.community.service;

import org.springframework.data.domain.Pageable;
import tavebalak.OTTify.community.dto.request.CommunitySubjectImageCreateDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectImageEditDTO;
import org.springframework.web.multipart.MultipartFile;
import tavebalak.OTTify.community.dto.request.CommunitySubjectCreateDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectEditDTO;
import tavebalak.OTTify.community.dto.response.CommunityAriclesDTO;
import tavebalak.OTTify.community.dto.response.CommunitySubjectDTO;
import tavebalak.OTTify.community.dto.response.CommunitySubjectsListDTO;
import tavebalak.OTTify.community.entity.Community;

public interface CommunityService {

Community saveSubject(CommunitySubjectImageCreateDTO c);
Community saveSubject(CommunitySubjectCreateDTO c, MultipartFile image);

void modifySubject(CommunitySubjectImageEditDTO c);
void modifySubject(CommunitySubjectEditDTO c, MultipartFile image);

void deleteSubject(Long subjectId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tavebalak.OTTify.common.s3.AWSS3Service;
import tavebalak.OTTify.community.dto.request.CommunitySubjectCreateDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectEditDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectImageCreateDTO;
import tavebalak.OTTify.community.dto.request.CommunitySubjectImageEditDTO;
import tavebalak.OTTify.community.dto.response.CommentListsDTO;
import tavebalak.OTTify.community.dto.response.CommunityAriclesDTO;
import tavebalak.OTTify.community.dto.response.CommunitySubjectDTO;
Expand Down Expand Up @@ -59,12 +58,11 @@ public class CommunityServiceImpl implements CommunityService {
private static final String AWS_S3_DISCUSSION_DIR_NAME = "discussion-images";

@Override
public Community saveSubject(CommunitySubjectImageCreateDTO c) {
public Community saveSubject(CommunitySubjectCreateDTO c, MultipartFile image) {

String imageUrl = null;
System.out.println(c.getImage());
if (!c.getImage().isEmpty()) {
imageUrl = awss3Service.upload(c.getImage(), AWS_S3_DISCUSSION_DIR_NAME);
if (image != null) {
imageUrl = awss3Service.upload(image, AWS_S3_DISCUSSION_DIR_NAME);
}

Program program = isPresent(c);
Expand All @@ -81,7 +79,7 @@ public Community saveSubject(CommunitySubjectImageCreateDTO c) {
}

public Community save(CommunitySubjectCreateDTO c) {
Program program = isPresentNotImage(c);
Program program = isPresent(c);

Community community = Community.builder()
.title(c.getSubjectName())
Expand All @@ -92,21 +90,15 @@ public Community save(CommunitySubjectCreateDTO c) {
return communityRepository.save(community);
}

private Program isPresentNotImage(CommunitySubjectCreateDTO c) {
private Program isPresent(CommunitySubjectCreateDTO c) {
Optional<Program> optionalProgram = programRepository.findById(c.getProgramId());
return optionalProgram.orElseThrow(
() -> new NotFoundException(ErrorCode.SAVED_PROGRAM_NOT_FOUND));
}


private Program isPresent(CommunitySubjectImageCreateDTO c) {
Optional<Program> optionalProgram = programRepository.findById(c.getProgramId());
return optionalProgram.orElseThrow(
() -> new NotFoundException(ErrorCode.SAVED_PROGRAM_NOT_FOUND));
}

@Override
public void modifySubject(CommunitySubjectImageEditDTO c) {
public void modifySubject(CommunitySubjectEditDTO c, MultipartFile image) {
Community community = communityRepository.findById(c.getSubjectId())
.orElseThrow(() -> new NotFoundException(ErrorCode.COMMUNITY_NOT_FOUND));

Expand All @@ -117,11 +109,11 @@ public void modifySubject(CommunitySubjectImageEditDTO c) {
CommunitySubjectImageEditorDTO communitySubjectEditorDTOBuilder = community.toImageEdior();

String imageUrl = null;
if (!c.getImage().isEmpty()) {
if (image != null) {
if (community.getImageUrl() != null) {
awss3Service.delete(community.getImageUrl());
}
imageUrl = awss3Service.upload(c.getImage(), AWS_S3_DISCUSSION_DIR_NAME);
imageUrl = awss3Service.upload(image, AWS_S3_DISCUSSION_DIR_NAME);
} else {
if (community.getImageUrl() != null) {
awss3Service.delete(community.getImageUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void getFailBadIdTestRecommentContent() throws Exception, NotFoundExcepti
when(communityService.save(any())).thenReturn(response);
Community savedCommunity = communityService.save(registerSubjectRequest());

doNothing().when(communityService).modifySubject(any());
doNothing().when(communityService).modifySubject(any(), any());

CommunitySubjectEditDTO editDTO = CommunitySubjectEditDTO.builder()
.subjectId(savedCommunity.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ private CommunitySubjectCreateDTO registerSubjectRequest() {
.programId(1L)
.subjectName("์‚ฌ๋ž‘๊ณผ ์šฐ์ •์˜ ๋”ฐ๋œปํ•œ ์ด์•ผ๊ธฐ '์‘๋‹ตํ•˜๋ผ 1988'")
.content("'์‘๋‹ตํ•˜๋ผ 1988'์€ ์‚ฌ๋ž‘๊ณผ ์šฐ์ •์˜ ๋”ฐ๋œปํ•œ ์ด์•ผ๊ธฐ๋ฅผ ๊ทธ๋ ค๋ƒˆ์Šต๋‹ˆ๋‹ค. ์ด ๋“œ๋ผ๋งˆ๊ฐ€ ๋งŽ์€ ์‚ฌ๋ž‘์„ ๋ฐ›์€ ์ด์œ ์— ๋Œ€ํ•ด ํ† ๋ก ํ•ด๋ณด์„ธ์š”!")
.programTitle("์Šค์ฟผ๋“œ ๊ฒŒ์ž„")
.posterPath("https://image.tmdb.org/t/p/w500/8Pjd1MiCbY8s9Zrxbb8SvCpY7s7.jpg")
.build();
}

Expand All @@ -85,8 +83,6 @@ private Community toEntity(CommunitySubjectCreateDTO dto, User user) {
.id(1L)
.title(dto.getSubjectName())
.program(Program.testBuilder().id(dto.getProgramId())
.posterPath(dto.getPosterPath())
.title(dto.getProgramTitle())
.build())
.content(dto.getContent())
.build();
Expand Down Expand Up @@ -119,9 +115,6 @@ private CommunitySubjectEditDTO makeCommunitySubjectEditDTO() {
.subjectId(1L)
.subjectName("test-subject")
.content("test-content")
.programId(5L)
.programTitle("test-program-title")
.posterPath("test-poster")
.build();
}

Expand Down

0 comments on commit 56913a4

Please sign in to comment.