-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from EWHA-THON-E-BUS/feat/post
feat: post 관련 기능 구현
- Loading branch information
Showing
15 changed files
with
417 additions
and
49 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/EBus/EBusback/domain/heart/controller/HeartController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/EBus/EBusback/domain/heart/dto/HeartRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/EBus/EBusback/domain/heart/repository/HeartRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/main/java/EBus/EBusback/domain/heart/service/HeartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/EBus/EBusback/domain/post/controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "글이 삭제되었습니다."; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/EBus/EBusback/domain/post/dto/PostCreateResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/EBus/EBusback/domain/post/dto/PostDetailResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/main/java/EBus/EBusback/domain/post/dto/PostMemberDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/EBus/EBusback/domain/post/dto/PostOutlineResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/main/java/EBus/EBusback/domain/post/dto/PostRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.