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

🔀 :: (#121) 게시글 수정 API #122

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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,5 +1,6 @@
package com.example.moizaspringserver.domain.feed.entity;

import com.example.moizaspringserver.domain.feed.presenstation.dto.request.UpdateFeedRequest;
import com.example.moizaspringserver.global.entity.BaseTimeEntity;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -49,4 +50,12 @@ public PublicFeed(String title, String content, Integer likeCount, Feed feed) {
this.likeCount = likeCount;
this.feed = feed;
}

public void updateFeedTitle(String title) {
this.title = title;
}
public void updateFeedContent(String content) {
this.content = content;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.example.moizaspringserver.domain.feed.facade;

import com.example.moizaspringserver.domain.feed.entity.Feed;
import com.example.moizaspringserver.domain.feed.entity.FeedAttachmentFile;
import com.example.moizaspringserver.domain.feed.entity.PublicFeed;
import com.example.moizaspringserver.domain.feed.exception.FeedAttachmentFileNotFoundException;
import com.example.moizaspringserver.domain.feed.exception.FeedNotFoundException;
import com.example.moizaspringserver.domain.feed.exception.PublicFeedNotFoundException;
import com.example.moizaspringserver.domain.feed.respository.FeedAttachmentFileRepository;
import com.example.moizaspringserver.domain.feed.respository.FeedRepository;
import com.example.moizaspringserver.domain.feed.respository.PublicFeedRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -11,10 +17,21 @@
public class FeedFacade {

private final FeedRepository feedRepository;
private final PublicFeedRepository publicFeedRepository;
private final FeedAttachmentFileRepository feedAttachmentFileRepository;

public Feed getFeedById(Integer id) {
return feedRepository.findById(id)
.orElseThrow(() -> FeedNotFoundException.EXCEPTION);
}

public PublicFeed getPublicFeedByFeedId(Feed feed) {
return publicFeedRepository.findByFeed(feed)
.orElseThrow(() -> PublicFeedNotFoundException.EXCEPTION);
}

public FeedAttachmentFile getFeedAttachmentFileByFeedId(Feed feed) {
return feedAttachmentFileRepository.findAllByFeed(feed)
.orElseThrow(() -> FeedAttachmentFileNotFoundException.EXCEPTION);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package com.example.moizaspringserver.domain.feed.presenstation;

import com.example.moizaspringserver.domain.feed.presenstation.dto.request.UpdateFeedRequest;
import com.example.moizaspringserver.domain.feed.service.DeleteFeedService;
import com.example.moizaspringserver.domain.feed.service.UpdateFeedService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RequiredArgsConstructor
@RequestMapping("/feeds")
@RestController
public class FeedController {

private final DeleteFeedService deleteFeedService;
private final UpdateFeedService updateFeedService;

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{feed-id}")
public void deleteFeed(@PathVariable("feed-id") Integer feedId) {
deleteFeedService.execute(feedId);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@PatchMapping("/{feed-id}")
public void updateFeed(@RequestBody @Valid UpdateFeedRequest request, @PathVariable("feed-id") Integer feedId) {
updateFeedService.execute(request, feedId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.moizaspringserver.domain.feed.presenstation.dto.request;

import com.example.moizaspringserver.domain.feed.type.FeedType;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Getter
@NoArgsConstructor
public class UpdateFeedRequest {

@NotBlank(message = "title은 Null, 공백, 띄어쓰기를 허용하지 않습니다.")
private String title;

@NotNull(message = "content는 Null일 수 없습니다.")
private String content;

@NotNull(message = "feed_type은 Null일 수 없습니다.")
private FeedType feedType;

@NotNull(message = "image_urls은 Null일 수 없습니다.")
private List<String> imageUrls;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import com.example.moizaspringserver.domain.feed.entity.PublicFeed;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface PublicFeedRepository extends JpaRepository<PublicFeed, Integer> {

void deleteByFeed(Feed feed);

Optional<PublicFeed> findByFeed(Feed feed);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.moizaspringserver.domain.feed.service;

import com.example.moizaspringserver.domain.feed.entity.Feed;
import com.example.moizaspringserver.domain.feed.entity.FeedAttachmentFile;
import com.example.moizaspringserver.domain.feed.entity.PublicFeed;
import com.example.moizaspringserver.domain.feed.facade.FeedFacade;
import com.example.moizaspringserver.domain.feed.presenstation.dto.request.UpdateFeedRequest;
import com.example.moizaspringserver.domain.user.entity.User;
import com.example.moizaspringserver.domain.user.facade.UserFacade;
import com.example.moizaspringserver.global.error.security.InvalidRoleException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class UpdateFeedService {

private final FeedFacade feedFacade;
private final UserFacade userFacade;

@Transactional
public void execute(UpdateFeedRequest request, Integer feedId) {

User user = userFacade.queryCurrentUser();
Feed feed = feedFacade.getFeedById(feedId);
PublicFeed publicFeed = feedFacade.getPublicFeedByFeedId(feed);
FeedAttachmentFile feedAttachmentFile = feedFacade.getFeedAttachmentFileByFeedId(feed);

if (!user.equals(feed.getUser())) {
Copy link
Member

@ericlee05 ericlee05 Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 if문을 publicFeed를 조회하기 이전에(라인 26과 27 사이) 넣으면 불필요한 조회(publicFeed, feedAttachmentFile)를 막을 수 있지 않을까요?

throw InvalidRoleException.EXCEPTION;
}
// TODO PR#105 머지시 리팩토링

publicFeed.updateFeedTitle(request.getTitle());
publicFeed.updateFeedContent(request.getContent());

// TODO 사진 올리는거 없어서 나중에 추가되면 변경
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.moizaspringserver.global.security;


import com.example.moizaspringserver.global.enums.UserType;
import com.example.moizaspringserver.global.filter.JwtTokenFilter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -63,7 +64,8 @@ protected void configure(HttpSecurity http) throws Exception {

// feeds
.antMatchers(HttpMethod.DELETE, "/feeds/{feed-id}").authenticated()

.antMatchers(HttpMethod.PATCH, "/feeds/{feed-id}").hasAnyAuthority(UserType.ROLE_STUDENT.name(), UserType.ROLE_GRADUATE.name())

// notice
.antMatchers(HttpMethod.GET, "/notices/{notice-id}").authenticated()

Expand Down