rest controller practice
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.example.restcontroller.board.controller;
|
||||
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.example.restcontroller.board.service.BoardService;
|
||||
import com.example.restcontroller.common.model.ResponseResult;
|
||||
import com.example.restcontroller.util.JWTUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
public class ApiBoardBookmarkController {
|
||||
|
||||
private final BoardService boardService;
|
||||
|
||||
@PutMapping("/api/board/{id}/bookmark")
|
||||
public ResponseEntity<?> chapter3_17(@PathVariable Long id,
|
||||
@RequestHeader("TOKEN") String token) {
|
||||
|
||||
String email = "";
|
||||
try {
|
||||
email = JWTUtils.getIssuer(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
|
||||
}
|
||||
|
||||
return ResponseResult.result(boardService.addBookmark(id, email));
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/bookmark/{id}")
|
||||
public ResponseEntity<?> chapter3_17_2(@PathVariable Long id,
|
||||
@RequestHeader("TOKEN") String token) {
|
||||
String email = "";
|
||||
try {
|
||||
email = JWTUtils.getIssuer(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
|
||||
}
|
||||
return ResponseResult.result(boardService.deleteBookmark(id, email));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.example.restcontroller.board.entity;
|
||||
|
||||
import com.example.restcontroller.user.entity.User;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Entity
|
||||
public class BoardBookmark {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(foreignKey = @ForeignKey(name = "FK_BOARD_BOOKMARK_USER_ID"))
|
||||
private User user;
|
||||
|
||||
// 북마크 정보
|
||||
private Long boardId;
|
||||
private Long boardTypeId;
|
||||
private String boardTitle;
|
||||
private String boardUrl;
|
||||
|
||||
private LocalDateTime regDate;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.restcontroller.board.repository;
|
||||
|
||||
import com.example.restcontroller.board.entity.BoardBookmark;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface BoardBookmarkRepository extends JpaRepository<BoardBookmark, Long> {
|
||||
}
|
||||
@@ -37,4 +37,8 @@ public interface BoardService {
|
||||
ServiceResult scrapBoard(Long id, String email);
|
||||
|
||||
ServiceResult deleteScrapBoard(Long id, String email);
|
||||
|
||||
ServiceResult addBookmark(Long id, String email);
|
||||
|
||||
ServiceResult deleteBookmark(Long id, String email);
|
||||
}
|
||||
|
||||
@@ -23,10 +23,11 @@ public class BoardServiceImpl implements BoardService {
|
||||
private final BoardRepository boardRepository;
|
||||
private final BoardTypeCustomRepository boardTypeCustomRepository;
|
||||
private final BoardHitsRepository boardHitsRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final BoardLikeRepository boardLikeRepository;
|
||||
private final BoardReportRepository boardReportRepository;
|
||||
private final BoardScrapRepository boardScrapRepository;
|
||||
private final BoardBookmarkRepository boardBookmarkRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
@@ -298,4 +299,59 @@ public class BoardServiceImpl implements BoardService {
|
||||
boardScrapRepository.delete(boardScrapEntity);
|
||||
return ServiceResult.success();
|
||||
}
|
||||
|
||||
private String getBoardUrl(Long boardId) {
|
||||
return String.format("/api/board/%d", boardId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult addBookmark(Long id, String email) {
|
||||
Optional<Board> board = boardRepository.findById(id);
|
||||
if (!board.isPresent()) {
|
||||
return ServiceResult.fail("게시글이 존재하지 않습니다");
|
||||
}
|
||||
Board boardEntity = board.get();
|
||||
|
||||
Optional<User> optionalUser = userRepository.findByEmail(email);
|
||||
if (!optionalUser.isPresent()) {
|
||||
return ServiceResult.fail("회원 정보가 존재하지 않습니다");
|
||||
}
|
||||
User userEntity = optionalUser.get();
|
||||
|
||||
BoardBookmark boardBookmark = BoardBookmark.builder()
|
||||
.user(userEntity)
|
||||
.boardId(boardEntity.getId())
|
||||
.boardTypeId(boardEntity.getBoardType().getId())
|
||||
.boardTitle(boardEntity.getTitle())
|
||||
.boardUrl(getBoardUrl(boardEntity.getId()))
|
||||
.regDate(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
boardBookmarkRepository.save(boardBookmark);
|
||||
return ServiceResult.success();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult deleteBookmark(Long id, String email) {
|
||||
Optional<BoardBookmark> optionalBoardBookmark = boardBookmarkRepository.findById(id);
|
||||
if (!optionalBoardBookmark.isPresent()) {
|
||||
return ServiceResult.fail("삭제할 북마크가 없습니다");
|
||||
}
|
||||
BoardBookmark boardBookmarkEntity = optionalBoardBookmark.get();
|
||||
|
||||
Optional<User> optionalUser = userRepository.findByEmail(email);
|
||||
if (!optionalUser.isPresent()) {
|
||||
return ServiceResult.fail("회원 정보가 존재하지 않습니다");
|
||||
}
|
||||
User userEntity = optionalUser.get();
|
||||
|
||||
if (boardBookmarkEntity.getUser().getId() != userEntity.getId()) {
|
||||
return ServiceResult.fail("본인의 스크랩만 삭제할 수 있습니다");
|
||||
}
|
||||
|
||||
boardBookmarkRepository.delete(boardBookmarkEntity);
|
||||
return ServiceResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +68,10 @@ public class User {
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<BoardScrap> boardScrapList = new ArrayList<>();
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<BoardBookmark> boardBookmarkList = new ArrayList<>();
|
||||
|
||||
@PrePersist
|
||||
public void prePersist() {
|
||||
status = status == null ? UserStatus.USING : status;
|
||||
|
||||
Reference in New Issue
Block a user