Skip to content

Commit

Permalink
Merge pull request #45 from lotteon2/develop
Browse files Browse the repository at this point in the history
release
  • Loading branch information
wakkpu authored Jan 17, 2024
2 parents 0a0800e + eec3e13 commit ae42266
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dailyon.snsservice.controller.rest;

import com.dailyon.snsservice.dto.response.post.PostAdminPageResponse;
import com.dailyon.snsservice.service.post.PostAdminService;
import com.dailyon.snsservice.service.post.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin")
@RestController
@RequiredArgsConstructor
public class PostAdminController {

private final PostAdminService postAdminService;

@GetMapping("/posts")
public ResponseEntity<PostAdminPageResponse> getPosts(
@PageableDefault(
page = 0,
size = 5,
sort = {"id"},
direction = Sort.Direction.ASC)
Pageable pageable) {
PostAdminPageResponse postAdminPageResponse = postAdminService.getPostsForAdmin(pageable);
return ResponseEntity.ok(postAdminPageResponse);
}

@DeleteMapping("/posts")
public ResponseEntity<Void> bulkDeletePosts(@RequestParam(name = "postIds") List<Long> postIds) {
postAdminService.softBulkDeleteByIds(postIds);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.dailyon.snsservice.dto.response.post;

import com.dailyon.snsservice.entity.HashTag;
import com.dailyon.snsservice.entity.Post;
import java.util.List;
import java.util.stream.Collectors;
import lombok.*;
import org.springframework.data.domain.Page;

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class PostAdminPageResponse {

private int totalPages;
private long totalElements;
private List<PostAdminResponse> posts;

public static PostAdminPageResponse fromEntity(Page<Post> posts) {
return PostAdminPageResponse.builder()
.totalPages(posts.getTotalPages())
.totalElements(posts.getTotalElements())
.posts(
posts.getContent().stream()
.map(
post ->
PostAdminResponse.builder()
.id(post.getId())
.thumbnailImgUrl(post.getPostImage().getThumbnailImgUrl())
.hashTagNames(
post.getHashTags().stream()
.map(HashTag::getName)
.collect(Collectors.toList()))
.title(post.getTitle())
.description(post.getDescription())
.memberNickname(post.getMember().getNickname())
.build())
.collect(Collectors.toList()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.dailyon.snsservice.dto.response.post;

import java.util.List;
import lombok.*;

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class PostAdminResponse {

private Long id;
private String thumbnailImgUrl;
private List<String> hashTagNames;
private String title;
private String description;
private String memberNickname;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public abstract class DomainException extends RuntimeException {
Expand All @@ -12,7 +13,7 @@ public DomainException(String message) {
super(message);
}

public abstract int getStatusCode();
public abstract HttpStatus getStatusCode();

public void addValidation(String fieldName, String errorMessage) {
validation.put(fieldName, errorMessage);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dailyon.snsservice.exception.common;

import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;

public class EntityNotFoundException extends DomainException {
Expand All @@ -9,7 +11,7 @@ public EntityNotFoundException(String message) {
}

@Override
public int getStatusCode() {
return HttpServletResponse.SC_NOT_FOUND;
public HttpStatus getStatusCode() {
return HttpStatus.NOT_FOUND;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.dailyon.snsservice.exception.HashTagDuplicatedException;
import com.dailyon.snsservice.exception.common.CustomException;
import com.dailyon.snsservice.exception.common.DomainException;
import com.dailyon.snsservice.exception.common.EntityNotFoundException;
import com.dailyon.snsservice.exceptionhandler.response.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -41,6 +43,20 @@ public ResponseEntity<ErrorResponse> customException(CustomException e) {
return ResponseEntity.status(statusCode).body(errorResponse);
}

@ExceptionHandler(DomainException.class)
public ResponseEntity<ErrorResponse> domainException(DomainException e) {
HttpStatus statusCode = e.getStatusCode();

ErrorResponse errorResponse =
ErrorResponse.builder()
.code(statusCode)
.message(e.getMessage())
.validation(e.getValidation())
.build();

return ResponseEntity.status(statusCode).body(errorResponse);
}

@ExceptionHandler(MissingRequestHeaderException.class)
public ResponseEntity<ErrorResponse> missingRequestHeaderException(
MissingRequestHeaderException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ int updateCountsById(

@Query("select p from Post p where p.id = :id and p.member.id = :memberId")
Optional<Post> findByIdAndMemberId(Long id, Long memberId);

@Query(
value =
"select p from Post p "
+ "join fetch p.postImage "
+ "join fetch p.hashTags "
+ "join fetch p.member "
+ "where p.isDeleted = false",
countQuery = "select count(p) from Post p where p.isDeleted = false")
Page<Post> findAllByIdAscAndIsDeletedFalse(Pageable pageable);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("update Post p "
+ "set p.isDeleted = true "
+ "where p.id in :ids")
int softBulkDeleteByIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public interface PostRepository {
int updateCountsById(Long id, Integer viewCount, Integer likeCount, Integer commentCount);

PostDetailResponse findDetailByIdWithIsFollowingAndIsLike(Long id, Long memberId);

Page<Post> findAllByIdAscAndIsDeletedFalse(Pageable pageable);

int softBulkDeleteByIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,67 +228,79 @@ public PostDetailResponse findDetailByIdWithIsFollowingAndIsLike(Long id, Long m
followSubQuery.follower.id.eq(memberId))
.exists();

return query
.transform(
groupBy(post.id)
.as(
new QPostDetailResponse(
post.id,
post.title,
post.description,
post.stature,
post.weight,
postImage.imgUrl,
post.viewCount,
post.likeCount,
post.commentCount,
hasLikedCondition,
post.createdAt,
new QPostDetailMemberResponse(
member.id,
member.nickname,
member.profileImgUrl,
member.code,
isFollowingExpression),
set(new QPostDetailHashTagResponse(hashTag.id, hashTag.name)),
set(
new QPostImageProductDetailResponse(
postImageProductDetail.id,
postImageProductDetail.productId,
postImageProductDetail.productSize,
postImageProductDetail.leftGapPercent,
postImageProductDetail.topGapPercent)))))
.get(id);
PostDetailResponse postDetailResponse = query
.transform(
groupBy(post.id)
.as(
new QPostDetailResponse(
post.id,
post.title,
post.description,
post.stature,
post.weight,
postImage.imgUrl,
post.viewCount,
post.likeCount,
post.commentCount,
hasLikedCondition,
post.createdAt,
new QPostDetailMemberResponse(
member.id,
member.nickname,
member.profileImgUrl,
member.code,
isFollowingExpression),
set(new QPostDetailHashTagResponse(hashTag.id, hashTag.name)),
set(
new QPostImageProductDetailResponse(
postImageProductDetail.id,
postImageProductDetail.productId,
postImageProductDetail.productSize,
postImageProductDetail.leftGapPercent,
postImageProductDetail.topGapPercent)))))
.get(id);
return postDetailResponse;
} else {
return query
.transform(
groupBy(post.id)
.as(
new QPostDetailResponse(
post.id,
post.title,
post.description,
post.stature,
post.weight,
postImage.imgUrl,
post.viewCount,
post.likeCount,
post.commentCount,
post.createdAt,
new QPostDetailMemberResponse(
member.id, member.nickname, member.profileImgUrl, member.code),
set(new QPostDetailHashTagResponse(hashTag.id, hashTag.name)),
set(
new QPostImageProductDetailResponse(
postImageProductDetail.id,
postImageProductDetail.productId,
postImageProductDetail.productSize,
postImageProductDetail.leftGapPercent,
postImageProductDetail.topGapPercent)))))
.get(id);
PostDetailResponse postDetailResponse = query
.transform(
groupBy(post.id)
.as(
new QPostDetailResponse(
post.id,
post.title,
post.description,
post.stature,
post.weight,
postImage.imgUrl,
post.viewCount,
post.likeCount,
post.commentCount,
post.createdAt,
new QPostDetailMemberResponse(
member.id, member.nickname, member.profileImgUrl, member.code),
set(new QPostDetailHashTagResponse(hashTag.id, hashTag.name)),
set(
new QPostImageProductDetailResponse(
postImageProductDetail.id,
postImageProductDetail.productId,
postImageProductDetail.productSize,
postImageProductDetail.leftGapPercent,
postImageProductDetail.topGapPercent)))))
.get(id);
return postDetailResponse;
}
}

@Override
public Page<Post> findAllByIdAscAndIsDeletedFalse(Pageable pageable) {
return postJpaRepository.findAllByIdAscAndIsDeletedFalse(pageable);
}

@Override
public int softBulkDeleteByIds(List<Long> ids) {
return postJpaRepository.softBulkDeleteByIds(ids);
}

private Long getMyPageTotalPageCount(Pageable pageable, Long memberId) {
return jpaQueryFactory
.select(post.count())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.dailyon.snsservice.service.post;

import com.dailyon.snsservice.dto.response.post.PostAdminPageResponse;
import com.dailyon.snsservice.entity.Post;
import com.dailyon.snsservice.repository.post.PostRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class PostAdminService {

private final PostRepository postRepository;

public PostAdminPageResponse getPostsForAdmin(Pageable pageable) {
Page<Post> posts = postRepository.findAllByIdAscAndIsDeletedFalse(pageable);
return PostAdminPageResponse.fromEntity(posts);
}

public void softBulkDeleteByIds(List<Long> ids) {
postRepository.softBulkDeleteByIds(ids);
}
}
Loading

0 comments on commit ae42266

Please sign in to comment.