-
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 #15 from study-hub-inu/dev
[Feat] : BookMark CRD 구현
- Loading branch information
Showing
34 changed files
with
536 additions
and
103 deletions.
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
src/main/java/kr/co/studyhubinu/studyhubserver/bookmark/controller/BookMarkController.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,41 @@ | ||
package kr.co.studyhubinu.studyhubserver.bookmark.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import kr.co.studyhubinu.studyhubserver.bookmark.dto.request.CreateBookMarkRequest; | ||
import kr.co.studyhubinu.studyhubserver.bookmark.dto.response.FindBookMarkResponse; | ||
import kr.co.studyhubinu.studyhubserver.bookmark.service.BookMarkService; | ||
import kr.co.studyhubinu.studyhubserver.user.dto.data.UserId; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/bookmark") | ||
public class BookMarkController { | ||
|
||
private final BookMarkService bookMarkService; | ||
|
||
@Operation(summary = "북마크 조회", description = "Http 헤더에 JWT accessToken 을 Json 형식으로 보내주시면 됩니다.") | ||
@GetMapping("") | ||
public ResponseEntity<Slice<FindBookMarkResponse>> findBookMark(UserId userId) { | ||
return ResponseEntity.ok(bookMarkService.findBookMark(userId.getId())); | ||
} | ||
|
||
@Operation(summary = "북마크 저장", description = "Http 헤더에 JWT accessToken, 바디에 PostId를 Json 형식으로 보내주시면 됩니다.") | ||
@PostMapping("") | ||
public ResponseEntity<Void> saveBookMark(UserId userId, CreateBookMarkRequest request) { | ||
bookMarkService.saveBookMark(userId.getId(), request); | ||
return new ResponseEntity<>(HttpStatus.CREATED); | ||
} | ||
|
||
@Operation(summary = "북마크 삭제", description = "바디에 BookMarkId 를 Json 형식으로 보내주시면 됩니다.") | ||
@DeleteMapping("/{bookMarkId}") | ||
public ResponseEntity<Void> deleteBookMark(@PathVariable("bookMarkId") Long bookMarkId) { | ||
bookMarkService.deleteBookMark(bookMarkId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
} |
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
19 changes: 19 additions & 0 deletions
19
...ain/java/kr/co/studyhubinu/studyhubserver/bookmark/dto/request/CreateBookMarkRequest.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,19 @@ | ||
package kr.co.studyhubinu.studyhubserver.bookmark.dto.request; | ||
|
||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Getter; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
|
||
@Getter | ||
public class CreateBookMarkRequest { | ||
|
||
@Schema(description = "게시글 id", example = "1") | ||
@NotBlank | ||
private Long postId; | ||
|
||
public CreateBookMarkRequest(Long postId) { | ||
this.postId = postId; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ain/java/kr/co/studyhubinu/studyhubserver/bookmark/dto/response/FindBookMarkResponse.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 kr.co.studyhubinu.studyhubserver.bookmark.dto.response; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class FindBookMarkResponse { | ||
|
||
private Long postId; | ||
|
||
private String title; | ||
private String content; | ||
private int leftover; | ||
|
||
|
||
public FindBookMarkResponse(Long postId, String title, String content, int leftover) { | ||
this.postId = postId; | ||
this.title = title; | ||
this.content = content; | ||
this.leftover = leftover; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/kr/co/studyhubinu/studyhubserver/bookmark/repository/BookMarkRepository.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,11 @@ | ||
package kr.co.studyhubinu.studyhubserver.bookmark.repository; | ||
|
||
import kr.co.studyhubinu.studyhubserver.bookmark.domain.BookMarkEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
|
||
@Repository | ||
public interface BookMarkRepository extends JpaRepository<BookMarkEntity, Long>, BookMarkRepositoryCustom { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...n/java/kr/co/studyhubinu/studyhubserver/bookmark/repository/BookMarkRepositoryCustom.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 kr.co.studyhubinu.studyhubserver.bookmark.repository; | ||
|
||
import kr.co.studyhubinu.studyhubserver.study.domain.StudyPostEntity; | ||
import org.springframework.data.domain.Slice; | ||
|
||
public interface BookMarkRepositoryCustom { | ||
|
||
Slice<StudyPostEntity> findPostByBookMark(Long userId); | ||
} |
36 changes: 36 additions & 0 deletions
36
...va/kr/co/studyhubinu/studyhubserver/bookmark/repository/BookMarkRepositoryCustomImpl.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,36 @@ | ||
package kr.co.studyhubinu.studyhubserver.bookmark.repository; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import kr.co.studyhubinu.studyhubserver.bookmark.domain.QBookMarkEntity; | ||
import kr.co.studyhubinu.studyhubserver.study.domain.QStudyPostEntity; | ||
import kr.co.studyhubinu.studyhubserver.study.domain.StudyPostEntity; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
import static kr.co.studyhubinu.studyhubserver.bookmark.domain.QBookMarkEntity.*; | ||
import static kr.co.studyhubinu.studyhubserver.study.domain.QStudyPostEntity.*; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class BookMarkRepositoryCustomImpl implements BookMarkRepositoryCustom { | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public Slice<StudyPostEntity> findPostByBookMark(Long userId) { | ||
QBookMarkEntity bookMark = bookMarkEntity; | ||
QStudyPostEntity post = studyPostEntity; | ||
|
||
return (Slice<StudyPostEntity>) jpaQueryFactory | ||
.select(post) | ||
.from(post) | ||
.join(bookMark) | ||
.where(bookMark.userId.eq(userId), bookMark.postId.eq(post.id)) | ||
.limit(100); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/kr/co/studyhubinu/studyhubserver/bookmark/service/BookMarkService.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,48 @@ | ||
package kr.co.studyhubinu.studyhubserver.bookmark.service; | ||
|
||
import kr.co.studyhubinu.studyhubserver.bookmark.domain.BookMarkEntity; | ||
import kr.co.studyhubinu.studyhubserver.bookmark.dto.request.CreateBookMarkRequest; | ||
import kr.co.studyhubinu.studyhubserver.bookmark.dto.response.FindBookMarkResponse; | ||
import kr.co.studyhubinu.studyhubserver.bookmark.repository.BookMarkRepository; | ||
import kr.co.studyhubinu.studyhubserver.exception.bookmark.BookMarkNotFoundException; | ||
import kr.co.studyhubinu.studyhubserver.study.domain.StudyPostEntity; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class BookMarkService { | ||
|
||
private final BookMarkRepository bookMarkRepository; | ||
|
||
public void saveBookMark(Long id, CreateBookMarkRequest request) { | ||
bookMarkRepository.save(BookMarkEntity.builder() | ||
.userId(id) | ||
.postId(request.getPostId()) | ||
.build()); | ||
} | ||
|
||
public void deleteBookMark(Long bookMarkId) { | ||
BookMarkEntity bookMark = bookMarkRepository.findById(bookMarkId).orElseThrow(BookMarkNotFoundException::new); | ||
bookMarkRepository.delete(bookMark); | ||
} | ||
|
||
public Slice<FindBookMarkResponse> findBookMark(Long id) { | ||
Slice<StudyPostEntity> postEntities = bookMarkRepository.findPostByBookMark(id); | ||
|
||
Slice<FindBookMarkResponse> responses = (Slice<FindBookMarkResponse>) postEntities.stream() | ||
.map(postEntity -> { | ||
FindBookMarkResponse response = new FindBookMarkResponse(postEntity.getId(), postEntity.getTitle(), postEntity.getContent(), postEntity.getStudyPerson()); | ||
return response; | ||
}) | ||
.collect(Collectors.toList()); | ||
return responses; | ||
} | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
src/main/java/kr/co/studyhubinu/studyhubserver/config/JPAConfig.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,21 @@ | ||
package kr.co.studyhubinu.studyhubserver.config; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
@Configuration | ||
public class JPAConfig { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory queryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
|
||
} |
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
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
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 |
---|---|---|
|
@@ -5,6 +5,5 @@ | |
@Getter | ||
public class JwtDto { | ||
|
||
private Long id; | ||
private String refreshToken; | ||
} |
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
Oops, something went wrong.