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

Refector: Post API #51

Closed
wants to merge 2 commits into from
Closed
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
Binary file modified .gradle/7.2/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-10-28T15:14:51+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 11.0.11 (AdoptOpenJDK)"
date = "2023-10-28T16:41:57+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 17.0.9 (Homebrew)"
)
@Component
public class MemberMapperImpl implements MemberMapper {
Expand Down
40 changes: 18 additions & 22 deletions src/main/generated/com/example/just/Mapper/PostMapperImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import com.example.just.Dao.Comment;
import com.example.just.Dao.Member;
import com.example.just.Dao.Post;
import com.example.just.Dto.PostDto;
import com.example.just.Dto.PutPostDto;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.processing.Generated;
import org.springframework.stereotype.Component;

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-10-28T15:14:51+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 11.0.11 (AdoptOpenJDK)"
date = "2023-10-28T16:41:57+0900",
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.2.jar, environment: Java 17.0.9 (Homebrew)"
)
@Component
public class PostMapperImpl implements PostMapper {

@Override
public Post toEntity(PostDto dto) {
public Post toEntity(PutPostDto dto) {
if ( dto == null ) {
return null;
}
Expand All @@ -30,7 +30,6 @@ public Post toEntity(PostDto dto) {
post.setPost_tag( dto.getPost_tag() );
post.setPost_picture( dto.getPost_picture() );
post.setPost_create_time( dto.getPost_create_time() );
post.setPost_like( dto.getPost_like() );
post.setSecret( dto.getSecret() );
post.setEmoticon( dto.getEmoticon() );
post.setPost_category( dto.getPost_category() );
Expand All @@ -43,39 +42,36 @@ public Post toEntity(PostDto dto) {
if ( list1 != null ) {
post.setComments( new ArrayList<Comment>( list1 ) );
}
post.setBlamedCount( dto.getBlamedCount() );

return post;
}

@Override
public PostDto toDto(Post entity) {
public PutPostDto toDto(Post entity) {
if ( entity == null ) {
return null;
}

PostDto postDto = new PostDto();
PutPostDto putPostDto = new PutPostDto();

postDto.setPost_id( entity.getPost_id() );
postDto.setPost_content( entity.getPost_content() );
postDto.setPost_tag( entity.getPost_tag() );
postDto.setPost_like( entity.getPost_like() );
postDto.setPost_picture( entity.getPost_picture() );
postDto.setPost_create_time( entity.getPost_create_time() );
postDto.setSecret( entity.getSecret() );
postDto.setEmoticon( entity.getEmoticon() );
postDto.setPost_category( entity.getPost_category() );
putPostDto.setPost_id( entity.getPost_id() );
putPostDto.setPost_content( entity.getPost_content() );
putPostDto.setPost_tag( entity.getPost_tag() );
putPostDto.setPost_picture( entity.getPost_picture() );
putPostDto.setPost_create_time( entity.getPost_create_time() );
putPostDto.setSecret( entity.getSecret() );
putPostDto.setEmoticon( entity.getEmoticon() );
putPostDto.setPost_category( entity.getPost_category() );
List<Member> list = entity.getLikedMembers();
if ( list != null ) {
postDto.setLikedMembers( new ArrayList<Member>( list ) );
putPostDto.setLikedMembers( new ArrayList<Member>( list ) );
}
postDto.setMember( entity.getMember() );
putPostDto.setMember( entity.getMember() );
List<Comment> list1 = entity.getComments();
if ( list1 != null ) {
postDto.setComments( new ArrayList<Comment>( list1 ) );
putPostDto.setComments( new ArrayList<Comment>( list1 ) );
}
postDto.setBlamedCount( entity.getBlamedCount() );

return postDto;
return putPostDto;
}
}
15 changes: 9 additions & 6 deletions src/main/java/com/example/just/Controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.example.just.Dto.PostDto;

import com.example.just.Dto.PostPostDto;
import com.example.just.Dto.PutPostDto;
import com.example.just.Service.PostService;
import com.example.just.Service.ResponseGetPost;
import com.example.just.Service.ResponsePost;
Expand Down Expand Up @@ -37,6 +39,7 @@ public ResponseGetPost getPosts(@RequestParam Long request_page,
Long member_id = Long.valueOf(jwtProvider.getIdFromToken(token)); //토큰

String cursor = req.getHeader("viewed");

return postService.searchByCursor(cursor, request_page, member_id);
}

Expand All @@ -55,11 +58,11 @@ public ResponseEntity<Slice<Post>> getMyPosts(

*/

@Operation(summary = "게시글 작성 api", description = "post_content, post_tag는 null값이 발생하면" +
" 안됨\n" + "다른 건 null이와도 예외처리 완료\n 자기가 쓴 글이면 true")
@Operation(summary = "게시글 작성 api", description = "RequestBody에 null값이 있으면 안됨"
+ "\n 공개글이면 true 아니라면 false")
@PostMapping("/post/post")
public ResponsePost write(HttpServletRequest request,
@RequestBody PostDto postDto) {
@RequestBody PostPostDto postDto) {
String token = jwtProvider.getAccessToken(request);
System.out.println(token + "ㅋㅋ");
Long member_id = Long.valueOf(jwtProvider.getIdFromToken(token)); //토큰
Expand All @@ -75,12 +78,12 @@ public ResponsePost deletePost(@RequestParam Long post_id) {

@Operation(summary = "게시글 수정 api", description = "JSON넘길 때 null이 하나도 있으면 안됨 꼭 다채워서 넘기기")
@PutMapping("/put/post")
public ResponsePost putPost(@RequestParam Long post_id, HttpServletRequest request,
@RequestBody PostDto postDto) {
public ResponsePost putPost(HttpServletRequest request,
@RequestBody PutPostDto postDto) {
String token = jwtProvider.getAccessToken(request);

Long member_id = Long.valueOf(jwtProvider.getIdFromToken(token)); //토큰
return postService.putPost(post_id, member_id, postDto);
return postService.putPost(member_id, postDto);
}

@Operation(summary = "게시글 좋아요 api", description = "\n 자기가 좋아요 한 글이면 true")
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/example/just/Mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.example.just.Dao.Post;
import com.example.just.Dto.PostDto;
import com.example.just.Dto.PostPostDto;
import com.example.just.Dto.PutPostDto;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface PostMapper extends EntityMapper<PostDto, Post>{
public interface PostMapper extends EntityMapper<PutPostDto, Post>{
}
25 changes: 13 additions & 12 deletions src/main/java/com/example/just/Service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.example.just.Service;

import com.example.just.Dao.Member;
import com.example.just.Dao.Post;
import com.example.just.Dao.QPost;
import com.example.just.Dto.PostDto;
import com.example.just.Dto.PostPostDto;
import com.example.just.Dto.PutPostDto;
import com.example.just.Impl.MySliceImpl;
import com.example.just.Mapper.PostMapper;
import com.example.just.Repository.MemberRepository;
Expand All @@ -21,7 +24,6 @@
import java.util.stream.Collectors;



@Service
public class PostService {

Expand All @@ -46,7 +48,7 @@ public PostService(EntityManager em, JPAQueryFactory query) {
}


public ResponsePost write(Long member_id, PostDto postDto) { //글 작성
public ResponsePost write(Long member_id, PostPostDto postDto) { //글 작성

Optional<Member> optionalMember = memberRepository.findById(member_id);
if (!optionalMember.isPresent()) { //아이디 없을시 예외처리
Expand All @@ -67,7 +69,6 @@ public ResponsePost write(Long member_id, PostDto postDto) { //글 작성
}



//글 삭제
@Transactional
public ResponsePost deletePost(Long post_id) {
Expand All @@ -76,7 +77,6 @@ public ResponsePost deletePost(Long post_id) {
throw new NoSuchElementException("post_id의 값이 DB에 존재하지 않습니다:" + post_id);
}
Post post = optionalPost.get();
post.setMember(null);
try {
postRepository.delete(post);
} catch (Exception e) {
Expand All @@ -87,7 +87,9 @@ public ResponsePost deletePost(Long post_id) {
}

//글 수정
public ResponsePost putPost(Long post_id, Long member_id, PostDto postDto) {
public ResponsePost putPost(Long member_id, PutPostDto postDto) {
Long post_id = postDto.getPost_id();
System.out.println(post_id);
Optional<Post> optionalPost = postRepository.findById(post_id);
if (!optionalPost.isPresent()) { //아이디 없을시 예외처리
throw new NoSuchElementException("post_id의 값이 DB에 존재하지 않습니다:" + post_id);
Expand All @@ -96,18 +98,20 @@ public ResponsePost putPost(Long post_id, Long member_id, PostDto postDto) {
if (!optionalMember.isPresent()) { //아이디 없을시 예외처리
throw new NoSuchElementException("DB에 존재하지 않는 ID : " + member_id);
}

postDto.setPost_create_time(new Timestamp(System.currentTimeMillis()));

Member member = optionalMember.get(); //존재한다면 객체 생성
postDto.setMember(member);
Post post = postMapper.toEntity(postDto);
post.setPost_like(optionalPost.get().getPost_like());
postRepository.save(post);
ResponsePost responsePost = new ResponsePost(post, true);
return responsePost;
}

public ResponseGetPost searchByCursor(String cursor, Long limit, Long member_id) { //글 조


QPost post = QPost.post;
Set<Long> viewedPostIds = new HashSet<>();

Expand All @@ -116,7 +120,7 @@ public ResponseGetPost searchByCursor(String cursor, Long limit, Long member_id)
String[] viewedPostIdsArray = cursor.split(",");
viewedPostIds = new HashSet<>();
for (String viewedPostId : viewedPostIdsArray) {
viewedPostIds.add(Long.parseLong(viewedPostId));
viewedPostIds.add(Long.parseLong(viewedPostId.trim()));
}
}

Expand All @@ -128,7 +132,6 @@ public ResponseGetPost searchByCursor(String cursor, Long limit, Long member_id)
.orderBy(Expressions.numberTemplate(Double.class, "function('rand')").asc())
.limit(limit + 1)
.fetch();

// 가져온 글들의 ID를 저장합니다.
Set<Long> resultPostIds = results.stream().map(Post::getPost_id).collect(Collectors.toSet());
viewedPostIds.addAll(resultPostIds);
Expand All @@ -146,7 +149,8 @@ public ResponseGetPost searchByCursor(String cursor, Long limit, Long member_id)
}

// Slice 객체를 생성해서 반환합니다.
ResponseGetPost responseGetPost = new ResponseGetPost(new MySliceImpl<>(results, PageRequest.of(0, Math.toIntExact(limit)), hasNext, nextCursor), false);
ResponseGetPost responseGetPost = new ResponseGetPost(
new MySliceImpl<>(results, PageRequest.of(0, Math.toIntExact(limit)), hasNext, nextCursor), false);
return responseGetPost;

}
Expand Down Expand Up @@ -182,12 +186,9 @@ public Slice<Post> searchByMyPost(Long limit, Long member_id) {
*/




@Transactional
public ResponsePost postLikes(Long post_id, Long member_id) { //글 좋아요


Optional<Post> optionalPost = postRepository.findById(post_id);
if (!optionalPost.isPresent()) { //아이디 없을시 예외처리
throw new NoSuchElementException("post_id의 값이 DB에 존재하지 않습니다:" + post_id);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/example/just/Service/ResponseGetPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.example.just.Dao.Post;
import com.example.just.Impl.MySliceImpl;
import lombok.Getter;
import org.springframework.web.bind.annotation.GetMapping;

@Getter
public class ResponseGetPost {
private MySliceImpl<Post> mySlice;
private boolean value;
Expand Down
Loading