diff --git a/src/main/java/com/example/moizaspringserver/domain/feed/entity/PublicFeed.java b/src/main/java/com/example/moizaspringserver/domain/feed/entity/PublicFeed.java index ab2f1535..db8b64a0 100644 --- a/src/main/java/com/example/moizaspringserver/domain/feed/entity/PublicFeed.java +++ b/src/main/java/com/example/moizaspringserver/domain/feed/entity/PublicFeed.java @@ -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; @@ -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; + } + } diff --git a/src/main/java/com/example/moizaspringserver/domain/feed/facade/FeedFacade.java b/src/main/java/com/example/moizaspringserver/domain/feed/facade/FeedFacade.java index c7da1c98..66ead4f4 100644 --- a/src/main/java/com/example/moizaspringserver/domain/feed/facade/FeedFacade.java +++ b/src/main/java/com/example/moizaspringserver/domain/feed/facade/FeedFacade.java @@ -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; @@ -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); + } } diff --git a/src/main/java/com/example/moizaspringserver/domain/feed/presenstation/FeedController.java b/src/main/java/com/example/moizaspringserver/domain/feed/presenstation/FeedController.java index 82e57c68..c5e1cfe6 100644 --- a/src/main/java/com/example/moizaspringserver/domain/feed/presenstation/FeedController.java +++ b/src/main/java/com/example/moizaspringserver/domain/feed/presenstation/FeedController.java @@ -1,16 +1,21 @@ 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}") @@ -18,4 +23,9 @@ 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); + } } diff --git a/src/main/java/com/example/moizaspringserver/domain/feed/presenstation/dto/request/UpdateFeedRequest.java b/src/main/java/com/example/moizaspringserver/domain/feed/presenstation/dto/request/UpdateFeedRequest.java new file mode 100644 index 00000000..1f46347f --- /dev/null +++ b/src/main/java/com/example/moizaspringserver/domain/feed/presenstation/dto/request/UpdateFeedRequest.java @@ -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 imageUrls; + +} diff --git a/src/main/java/com/example/moizaspringserver/domain/feed/respository/PublicFeedRepository.java b/src/main/java/com/example/moizaspringserver/domain/feed/respository/PublicFeedRepository.java index 27cc42e2..beb5eac3 100644 --- a/src/main/java/com/example/moizaspringserver/domain/feed/respository/PublicFeedRepository.java +++ b/src/main/java/com/example/moizaspringserver/domain/feed/respository/PublicFeedRepository.java @@ -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 { void deleteByFeed(Feed feed); + Optional findByFeed(Feed feed); + } diff --git a/src/main/java/com/example/moizaspringserver/domain/feed/service/UpdateFeedService.java b/src/main/java/com/example/moizaspringserver/domain/feed/service/UpdateFeedService.java new file mode 100644 index 00000000..e79bc353 --- /dev/null +++ b/src/main/java/com/example/moizaspringserver/domain/feed/service/UpdateFeedService.java @@ -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())) { + throw InvalidRoleException.EXCEPTION; + } + // TODO PR#105 머지시 리팩토링 + + publicFeed.updateFeedTitle(request.getTitle()); + publicFeed.updateFeedContent(request.getContent()); + + // TODO 사진 올리는거 없어서 나중에 추가되면 변경 + } +} diff --git a/src/main/java/com/example/moizaspringserver/global/security/SecurityConfig.java b/src/main/java/com/example/moizaspringserver/global/security/SecurityConfig.java index 7ee16e5b..f6028aab 100644 --- a/src/main/java/com/example/moizaspringserver/global/security/SecurityConfig.java +++ b/src/main/java/com/example/moizaspringserver/global/security/SecurityConfig.java @@ -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; @@ -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()