Skip to content

Commit

Permalink
게시글 목록 조회 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeok-kong committed Jul 10, 2024
1 parent a5d694c commit 482830c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.kong.commonlibrary.util.JwtReader;
import me.kong.groupservice.domain.entity.State;
import me.kong.groupservice.domain.entity.post.Post;
import me.kong.groupservice.domain.entity.profile.Profile;
import me.kong.groupservice.dto.request.SavePostRequestDto;
import me.kong.groupservice.dto.response.PostListResponseDto;
import me.kong.groupservice.dto.response.PostResponseDto;
import me.kong.groupservice.mapper.PostMapper;
import me.kong.groupservice.service.PostService;
import me.kong.groupservice.service.ProfileService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static me.kong.commonlibrary.constant.HttpStatusResponseEntity.RESPONSE_OK;


@Slf4j
@RestController
@RequestMapping("/api/groups/{groupId}/posts")
@RequiredArgsConstructor
Expand All @@ -27,6 +32,18 @@ public class PostController {
private final ProfileService profileService;
private final JwtReader jwtReader;

@GetMapping
public ResponseEntity<Page<PostListResponseDto>> getPostList(
@PathVariable Long groupId,
@RequestParam(required = false, defaultValue = "GENERAL") State state,
@RequestParam(required = false , defaultValue = "0") int page,
@RequestParam(required = false, defaultValue = "10") int size) {

Page<Post> posts = postService.getRecentPosts(groupId, state, page, size);

return new ResponseEntity<>(postMapper.toDto(posts), HttpStatus.OK);
}

@PostMapping
public ResponseEntity<HttpStatus> createNewPost(@PathVariable Long groupId,
@RequestBody SavePostRequestDto dto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.kong.groupservice.dto.response;


import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@Builder
public class PostListResponseDto {
private Long id;
private String title;
private String content;
private Long groupId;
private Long profileId;
private String nickname;
private LocalDateTime updatedDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import me.kong.groupservice.domain.entity.post.Post;
import me.kong.groupservice.domain.entity.profile.Profile;
import me.kong.groupservice.dto.request.SavePostRequestDto;
import me.kong.groupservice.dto.response.PostListResponseDto;
import me.kong.groupservice.dto.response.PostResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;


Expand Down Expand Up @@ -34,4 +36,16 @@ public PostResponseDto toDto(Post post, Profile profile) {
.updatedDate(post.getUpdatedDate())
.build();
}

public Page<PostListResponseDto> toDto(Page<Post> posts) {
return posts.map(p -> PostListResponseDto.builder()
.id(p.getId())
.title(p.getTitle())
.content(p.getContent())
.groupId(p.getGroup().getId())
.profileId(p.getProfile().getId())
.nickname(p.getProfile().getNickname())
.updatedDate(p.getUpdatedDate())
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AuthService {

public boolean isGroupMember(Group group, Profile profile) {
return profile.getGroup().getId().equals(group.getId())
&& profile.getState() != State.GENERAL;
&& profile.getState() == State.GENERAL;
}

public boolean isGroupManager(Group group, Profile profile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import me.kong.groupservice.domain.entity.group.Group;
import me.kong.groupservice.domain.entity.post.Post;
import me.kong.groupservice.domain.entity.profile.Profile;
import me.kong.groupservice.domain.repository.GroupRepository;
import me.kong.groupservice.domain.repository.PostRepository;
import me.kong.groupservice.dto.request.SavePostRequestDto;
import me.kong.groupservice.mapper.PostMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -60,4 +63,11 @@ public void deletePost(Long id, @UserId Long userId, @GroupId Long groupId) {
Post post = findPost(id);
post.setState(State.DELETED);
}

@Transactional
public Page<Post> getRecentPosts(Long groupId, State state, int page, int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("id").descending());

return postRepository.findRecentPost(groupId, state, pageable);
}
}

0 comments on commit 482830c

Please sign in to comment.