Skip to content

Commit

Permalink
Merge pull request #4 from EWHA-THON-E-BUS/feat/post
Browse files Browse the repository at this point in the history
feat: post 관련 기능 구현
  • Loading branch information
xyzwv authored Mar 15, 2024
2 parents 864f34d + fd83efc commit 464346c
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package EBus.EBusback.domain.heart.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import EBus.EBusback.domain.heart.dto.HeartRequestDto;
import EBus.EBusback.domain.heart.service.HeartService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/hearts")
public class HeartController {

private final HeartService heartService;

@PostMapping
public String clickHeart(@RequestBody HeartRequestDto requestDto) {
return heartService.createOrRemoveHeart(requestDto.getPostId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package EBus.EBusback.domain.heart.dto;

import lombok.Getter;

@Getter
public class HeartRequestDto {

private Long postId;
}
49 changes: 30 additions & 19 deletions src/main/java/EBus/EBusback/domain/heart/entity/Heart.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package EBus.EBusback.domain.heart.entity;

import EBus.EBusback.domain.member.entity.Member;

import EBus.EBusback.domain.post.entity.Post;
import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -13,25 +19,30 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Heart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, updatable = false)
private Long heartId;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, updatable = false)
private Long heartId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
private Post post;
@Column(nullable = false)
private Boolean isValid;

@Column(nullable = false)
private Boolean isValid;
@Builder
public Heart(Member member, Post post) {
this.member = member;
this.post = post;
this.isValid = false;
}

@Builder
public Heart(Member member, Post post){
this.member = member;
this.post = post;
}
public void updateHeart() {
this.isValid = !isValid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package EBus.EBusback.domain.heart.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import EBus.EBusback.domain.heart.entity.Heart;
import EBus.EBusback.domain.member.entity.Member;
import EBus.EBusback.domain.post.entity.Post;

public interface HeartRepository extends JpaRepository<Heart, Long> {

Optional<Heart> findByMemberAndPost(Member member, Post post);

Boolean existsByMemberAndPostAndIsValid(Member member, Post post, Boolean isValid);
}
44 changes: 44 additions & 0 deletions src/main/java/EBus/EBusback/domain/heart/service/HeartService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package EBus.EBusback.domain.heart.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import EBus.EBusback.domain.heart.entity.Heart;
import EBus.EBusback.domain.heart.repository.HeartRepository;
import EBus.EBusback.domain.member.entity.Member;
import EBus.EBusback.domain.post.entity.Post;
import EBus.EBusback.domain.post.repository.PostRepository;
import EBus.EBusback.global.SecurityUtil;
import lombok.RequiredArgsConstructor;

@Service
@Transactional
@RequiredArgsConstructor
public class HeartService {

private final HeartRepository heartRepository;
private final PostRepository postRepository;

public String createOrRemoveHeart(Long postId) {
Member member = SecurityUtil.getCurrentUser();
if (member == null)
throw new RuntimeException("사용자를 찾을 수 없습니다.");

Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("글을 찾을 수 없습니다."));

Heart heart = heartRepository.findByMemberAndPost(member, post)
.orElseGet(() -> heartRepository.save(Heart.builder().member(member).post(post).build()));

heart.updateHeart();

if (heart.getIsValid())
return "좋아요가 등록되었습니다.";
else
return "좋아요가 취소되었습니다.";
}

public Boolean existsHeart(Member member, Post post) {
return heartRepository.existsByMemberAndPostAndIsValid(member, post, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package EBus.EBusback.domain.post.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import EBus.EBusback.domain.post.dto.PostCreateResponseDto;
import EBus.EBusback.domain.post.dto.PostDetailResponseDto;
import EBus.EBusback.domain.post.dto.PostOutlineResponseDto;
import EBus.EBusback.domain.post.dto.PostRequestDto;
import EBus.EBusback.domain.post.service.PostService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/posts")
public class PostController {

private final PostService postService;

@PostMapping("/suggestion")
@ResponseStatus(HttpStatus.CREATED)
public PostCreateResponseDto createSuggestion(@RequestBody PostRequestDto requestDto) {
return postService.createPost(requestDto, true);
}

@PostMapping("/appreciation")
@ResponseStatus(HttpStatus.CREATED)
public PostCreateResponseDto createAppreciation(@RequestBody PostRequestDto requestDto) {
return postService.createPost(requestDto, false);
}

@GetMapping("/{postId}")
@ResponseStatus(HttpStatus.OK)
public PostDetailResponseDto findPost(@PathVariable Long postId) {
return postService.findPost(postId);
}

@GetMapping("/suggestion")
@ResponseStatus(HttpStatus.OK)
public List<PostOutlineResponseDto> findSuggestionList() {
return postService.findPostList(true);
}

@GetMapping("/appreciation")
@ResponseStatus(HttpStatus.OK)
public List<PostOutlineResponseDto> findAppreciationList() {
return postService.findPostList(false);
}

@DeleteMapping("/{postId}")
@ResponseStatus(HttpStatus.OK)
public String removePost(@PathVariable Long postId) {
postService.removePost(postId);
return "글이 삭제되었습니다.";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package EBus.EBusback.domain.post.dto;

import java.time.LocalDateTime;

import EBus.EBusback.domain.post.entity.Post;
import lombok.Getter;

@Getter
public class PostCreateResponseDto {

private Long postId;
private String title;
private String content;
private Boolean isSuggestion;
private LocalDateTime createdDate;

public PostCreateResponseDto(Post post) {
this.postId = post.getPostId();
this.title = post.getTitle();
this.content = post.getContent();
this.isSuggestion = post.getIsSuggestion();
this.createdDate = post.getCreatedDate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package EBus.EBusback.domain.post.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PostDetailResponseDto {

private PostCreateResponseDto post;
private Integer heartCount;
private PostMemberDto member;
}
12 changes: 12 additions & 0 deletions src/main/java/EBus/EBusback/domain/post/dto/PostMemberDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package EBus.EBusback.domain.post.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PostMemberDto {

private Boolean hasHeart;
private Boolean isWriter;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package EBus.EBusback.domain.post.dto;

import java.time.LocalDateTime;

import EBus.EBusback.domain.post.entity.Post;
import lombok.Getter;

@Getter
public class PostOutlineResponseDto {

private Long postId;
private String title;
private LocalDateTime createdDate;
private Integer heartCount;

public PostOutlineResponseDto(Post post, Integer heartCount) {
this.postId = post.getPostId();
this.title = post.getTitle();
this.createdDate = post.getCreatedDate();
this.heartCount = heartCount;
}
}
17 changes: 17 additions & 0 deletions src/main/java/EBus/EBusback/domain/post/dto/PostRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package EBus.EBusback.domain.post.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;

@Getter
public class PostRequestDto {

@NotBlank
@Size(min = 1, max = 30)
private String title;

@NotBlank
@Size(min = 1, max = 100)
private String content;
}
Loading

0 comments on commit 464346c

Please sign in to comment.