Skip to content

Commit

Permalink
Merge pull request #8 from 42organization/feat/likeService-7
Browse files Browse the repository at this point in the history
[Feat] 좋아요 기능 추가 #7
  • Loading branch information
wken5577 authored Jan 10, 2023
2 parents 214220e + 274afb1 commit ab8940d
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 18 deletions.
22 changes: 21 additions & 1 deletion src/main/java/com/example/demo/PersistenceDateConverter.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
package com.example.demo;public class PersistenceDateConverter {
package com.example.demo;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.sql.Date;
import java.time.*;


//@Converter(autoApply = true)
public class PersistenceDateConverter implements AttributeConverter<LocalDateTime, Date> {

@Override
public Date convertToDatabaseColumn(LocalDateTime attribute) {
return Date.valueOf(attribute.toLocalDate());
}

@Override
public LocalDateTime convertToEntityAttribute(Date dbData) {
return dbData.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}
39 changes: 33 additions & 6 deletions src/main/java/com/example/demo/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.demo.controller;

import com.example.demo.domain.Post;
import com.example.demo.dto.AllPostResponseDto;
import com.example.demo.dto.PostModifyRequestDto;
import com.example.demo.dto.PostSaveRequestDto;
import com.example.demo.dto.PostResponseDto;
Expand All @@ -9,30 +11,55 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/post")
@RequiredArgsConstructor
public class PostController {
private final PostService postService;

@GetMapping()
@GetMapping
public PostResponseDto getPostById(@RequestParam Long postId) {
return postService.getPostById(postId);
}

@PostMapping()
@GetMapping("/all")
public AllPostResponseDto getAllPosts() {
List<Post> postList = postService.getAllPosts();
List<PostResponseDto> responseDtos = postList.stream().map(PostResponseDto::new)
.collect(Collectors.toList());
return new AllPostResponseDto(responseDtos);
}

@PostMapping
ResponseEntity<Long> savePost(@RequestBody PostSaveRequestDto postSaveRequestDto) {
Long id = postService.savePost(postSaveRequestDto.getWriter(), postSaveRequestDto.getInfo());
Long id = postService.savePost(postSaveRequestDto.getWriter(), postSaveRequestDto.getInfo()
,postSaveRequestDto.getTag());
return new ResponseEntity<Long>(id, HttpStatus.OK);
}

@PatchMapping()
@PatchMapping
ResponseEntity<Long> modifyPost(@RequestBody PostModifyRequestDto postRequestDto) {
Long id = postService.modifyPost(postRequestDto.getPostId(), postRequestDto.getInfo());
Long id = postService.modifyPost(postRequestDto.getPostId(), postRequestDto.getInfo()
,postRequestDto.getTag());
return new ResponseEntity<Long>(id, HttpStatus.OK);
}

@DeleteMapping()
@PatchMapping("/like")
ResponseEntity<Long> likePost(@RequestParam Long postId) {
Long id = postService.likePost(postId);
return new ResponseEntity<>(id, HttpStatus.OK);
}

@PatchMapping("/unlike")
ResponseEntity<Long> unlikePost(@RequestParam Long postId) {
Long id = postService.unlikePost(postId);
return new ResponseEntity<>(id, HttpStatus.OK);
}

@DeleteMapping
ResponseEntity<Long> deletePost(@RequestParam Long postId) {
Long id = postService.deletePost(postId);
return new ResponseEntity<Long>(id, HttpStatus.OK);
Expand Down
47 changes: 42 additions & 5 deletions src/main/java/com/example/demo/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,62 @@

import com.example.demo.util.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Post extends BaseTimeEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String info;
private String writer;
private Long likeCnt;
private Long viewCnt;
private String tag;

public Post(String info, String writer) {
private Post(String info, String writer) {
this.info = info;
this.writer = writer;
this.tag = "";
this.likeCnt = Long.valueOf(0);
this.viewCnt = Long.valueOf(0);
}

private Post(String info, String writer, String tag) {
this.info = info;
this.writer = writer;
this.tag = tag;
this.likeCnt = Long.valueOf(0);
this.viewCnt = Long.valueOf(0);
}

public static Post createPostWithoutTag(String info, String writer) {
return new Post(info, writer);
}

public static Post createPost(String info, String writer, String tag) {
return new Post(info, writer, tag);
}

public void addLike() {
this.likeCnt++;
}

public void cancelLike() {
if (this.likeCnt > 0)
this.likeCnt--;
}

public void addViewCount() {
this.viewCnt++;
}

public void modifyPost(String info) {
public void modifyPost(String info, String tag) {
this.info = info;
if (tag != null)
this.tag = tag;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/demo/dto/AllPostResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.demo.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor
@Getter
public class AllPostResponseDto {
private List<PostResponseDto> dataList;
private Long postCnt;

public AllPostResponseDto (List<PostResponseDto> dataList) {
this.dataList = dataList;
this.postCnt = Long.valueOf(dataList.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
public class PostModifyRequestDto {
private Long postId;
private String info;
private String tag;
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/demo/dto/PostResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ public class PostResponseDto {
private String info;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;
private Long likeCnt;
private Long viewCnt;
private String tag;

public PostResponseDto(Post post) {
this.writer = post.getWriter();
this.info = post.getInfo();
this.createdDate = post.getCreatedDate();
this.modifiedDate = post.getModifiedDate();
this.likeCnt = post.getLikeCnt();
this.viewCnt = post.getViewCnt();
this.tag = post.getTag();
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/example/demo/dto/PostSaveRequestDto.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.example.demo.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@Setter
@Getter
@AllArgsConstructor
public class PostSaveRequestDto {
private String writer;
private String info;
private String tag;
}
37 changes: 31 additions & 6 deletions src/main/java/com/example/demo/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,64 @@
import com.example.demo.domain.Post;
import com.example.demo.dto.PostResponseDto;
import com.example.demo.repository.PostRepository;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class PostService {
private final PostRepository postRepository;
@Transactional(readOnly = true)

public PostResponseDto getPostById(Long postId) {
Optional<Post> opPost = postRepository.findById(postId);
Post findedPost = opPost.orElseThrow(RuntimeException::new);
findedPost.addViewCount(); //get 메서드에서 조회수 증가시키는 로직 안좋은 것 같음..,,
return new PostResponseDto(findedPost);
}

public Long savePost(String writer, String info) {
Post post = new Post(info, writer);
postRepository.save(new Post(info, writer));
public Long savePost(String writer, String info, String tag) {
Post post;
if (tag == null)
post = Post.createPostWithoutTag(writer, info);
else
post = Post.createPost(writer, info, tag);
postRepository.save(post);
return post.getId();
}

public Long modifyPost(Long postId, String info) {
public Long modifyPost(Long postId, String info, String tag) {
Optional<Post> opPost = postRepository.findById(postId);
Post findedPost = opPost.orElseThrow(RuntimeException::new);
findedPost.modifyPost(info);
findedPost.modifyPost(info, tag);
return findedPost.getId();
}

public Long deletePost(Long postId) {
postRepository.deleteById(postId);
return postId;
}

public List<Post> getAllPosts() {
return postRepository.findAll();
}

public Long likePost(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(RuntimeException::new);
post.addLike();
return post.getId();
}

public Long unlikePost(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(RuntimeException::new);
post.cancelLike();
return post.getId();
}
}
56 changes: 56 additions & 0 deletions src/test/java/com/example/demo/api/PostControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.demo.api;

import com.example.demo.domain.Post;
import com.example.demo.dto.PostResponseDto;
import com.example.demo.dto.PostSaveRequestDto;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.PersistenceUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.api.Assertions.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
public class PostControllerTest {

@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@PersistenceUnit
EntityManagerFactory emf;
EntityManager em;

@Test
public void savePostTest() {
PostSaveRequestDto postRequest = new PostSaveRequestDto("hyunkyu", "info", "tag");

String url = "http://localhost:" + port + "/post";
ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, postRequest, Long.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
}

@Test
@Transactional
public void getPostTest(){
em = emf.createEntityManager();
EntityTransaction entityTransaction = em.getTransaction();
Post post = Post.createPost("content", "hyunkyu", "tag");
em.persist(post);

String url = "http://localhost:" + port + "/post?postId=" + post.getId();
ResponseEntity<PostResponseDto> responseEntity = restTemplate.getForEntity(url, PostResponseDto.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody().getInfo()).isEqualTo("content");
}
}

0 comments on commit ab8940d

Please sign in to comment.