-
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 #7 from EWHA-THON-E-BUS/develop
[배포] Post, Notice API 개발 완료
- Loading branch information
Showing
20 changed files
with
561 additions
and
51 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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/EBus/EBusback/domain/notice/controller/NoticeController.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,51 @@ | ||
package EBus.EBusback.domain.notice.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.notice.dto.NoticeResponseDto; | ||
import EBus.EBusback.domain.notice.service.NoticeService; | ||
import EBus.EBusback.domain.post.dto.PostRequestDto; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/notices") | ||
public class NoticeController { | ||
|
||
private final NoticeService noticeService; | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public NoticeResponseDto createNotice(@RequestBody PostRequestDto requestDto) { | ||
return noticeService.createNotice(requestDto); | ||
} | ||
|
||
@GetMapping("/{noticeId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public NoticeResponseDto findNotice(@PathVariable Long noticeId) { | ||
return noticeService.findNotice(noticeId); | ||
} | ||
|
||
@GetMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
public List<NoticeResponseDto> findNoticeList() { | ||
return noticeService.findNoticeList(); | ||
} | ||
|
||
@DeleteMapping("/{noticeId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public String removeNotice(@PathVariable Long noticeId) { | ||
noticeService.removeNotice(noticeId); | ||
return "공지사항이 삭제되었습니다."; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/EBus/EBusback/domain/notice/dto/NoticeResponseDto.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.notice.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import EBus.EBusback.domain.notice.entity.Notice; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class NoticeResponseDto { | ||
|
||
private Long noticeId; | ||
private String title; | ||
private String content; | ||
private LocalDateTime createdDate; | ||
|
||
public NoticeResponseDto(Notice notice) { | ||
this.noticeId = notice.getNoticeId(); | ||
this.title = notice.getTitle(); | ||
this.content = notice.getContent(); | ||
this.createdDate = notice.getCreatedDate(); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/java/EBus/EBusback/domain/notice/repository/NoticeRepository.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,8 @@ | ||
package EBus.EBusback.domain.notice.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import EBus.EBusback.domain.notice.entity.Notice; | ||
|
||
public interface NoticeRepository extends JpaRepository<Notice, Long> { | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/EBus/EBusback/domain/notice/service/NoticeService.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,53 @@ | ||
package EBus.EBusback.domain.notice.service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import EBus.EBusback.domain.member.entity.Member; | ||
import EBus.EBusback.domain.member.entity.Role; | ||
import EBus.EBusback.domain.notice.dto.NoticeResponseDto; | ||
import EBus.EBusback.domain.notice.entity.Notice; | ||
import EBus.EBusback.domain.notice.repository.NoticeRepository; | ||
import EBus.EBusback.domain.post.dto.PostRequestDto; | ||
import EBus.EBusback.global.SecurityUtil; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class NoticeService { | ||
|
||
private final NoticeRepository noticeRepository; | ||
|
||
public NoticeResponseDto createNotice(PostRequestDto requestDto) { | ||
Member member = SecurityUtil.getCurrentUser(); | ||
if (!member.getRole().equals(Role.ADMIN)) | ||
throw new RuntimeException("공지사항은 관리자만 작성 가능합니다."); | ||
return new NoticeResponseDto(noticeRepository.save( | ||
Notice.builder().title(requestDto.getTitle()).content(requestDto.getContent()).writer(member).build() | ||
)); | ||
} | ||
|
||
public NoticeResponseDto findNotice(Long noticeId) { | ||
Notice notice = noticeRepository.findById(noticeId) | ||
.orElseThrow(() -> new RuntimeException("공지사항을 찾을 수 없습니다.")); | ||
return new NoticeResponseDto(notice); | ||
} | ||
|
||
public List<NoticeResponseDto> findNoticeList() { | ||
return noticeRepository.findAll().stream() | ||
.map(NoticeResponseDto::new).collect(Collectors.toList()); | ||
} | ||
|
||
public void removeNotice(Long noticeId) { | ||
Member member = SecurityUtil.getCurrentUser(); | ||
Notice notice = noticeRepository.findById(noticeId) | ||
.orElseThrow(() -> new RuntimeException("공지사항을 찾을 수 없습니다.")); | ||
if (!member.getRole().equals(Role.ADMIN)) | ||
throw new RuntimeException("관리자만 삭제할 수 있습니다."); | ||
noticeRepository.delete(notice); | ||
} | ||
} |
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 "글이 삭제되었습니다."; | ||
} | ||
|
||
} |
Oops, something went wrong.