rest controller practice
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
public class ApiBoardScrapController {
|
||||
|
||||
private final BoardService boardService;
|
||||
|
||||
@PutMapping("/api/board/{id}/scrap")
|
||||
public ResponseEntity<?> chapter3_15(@PathVariable Long id,
|
||||
@RequestHeader("TOKEN") String token) {
|
||||
|
||||
String email = "";
|
||||
try {
|
||||
email = JWTUtils.getIssuer(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
|
||||
}
|
||||
|
||||
return ResponseResult.result(boardService.scrapBoard(id, email));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
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 BoardScrap {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(foreignKey = @ForeignKey(name = "FK_BOARD_SCRAP_USER_ID"))
|
||||
private User user;
|
||||
|
||||
// 스크랩 글정보
|
||||
private Long boardId;
|
||||
private Long boardTypeId;
|
||||
private Long boardUserId;
|
||||
private String boardTitle;
|
||||
private String boardContents;
|
||||
private LocalDateTime boardRegDate;
|
||||
|
||||
private LocalDateTime regDate;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.restcontroller.board.repository;
|
||||
|
||||
import com.example.restcontroller.board.entity.Board;
|
||||
import com.example.restcontroller.board.entity.BoardHits;
|
||||
import com.example.restcontroller.board.entity.BoardScrap;
|
||||
import com.example.restcontroller.user.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface BoardScrapRepository extends JpaRepository<BoardScrap, Long> {
|
||||
}
|
||||
@@ -33,4 +33,6 @@ public interface BoardService {
|
||||
ServiceResult addReport(Long id, String email, BoardReportInput boardReportInput);
|
||||
|
||||
List<BoardReport> boardReportList();
|
||||
|
||||
ServiceResult scrapBoard(Long id, String email);
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ public class BoardServiceImpl implements BoardService {
|
||||
private final UserRepository userRepository;
|
||||
private final BoardLikeRepository boardLikeRepository;
|
||||
private final BoardReportRepository boardReportRepository;
|
||||
private final BoardScrapRepository boardScrapRepository;
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
@@ -208,6 +209,7 @@ public class BoardServiceImpl implements BoardService {
|
||||
return ServiceResult.success();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult addReport(Long id, String email, BoardReportInput boardReportInput) {
|
||||
Optional<Board> board = boardRepository.findById(id);
|
||||
@@ -243,4 +245,34 @@ public class BoardServiceImpl implements BoardService {
|
||||
public List<BoardReport> boardReportList() {
|
||||
return boardReportRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult scrapBoard(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();
|
||||
|
||||
BoardScrap boardScrap = BoardScrap.builder()
|
||||
.user(userEntity)
|
||||
.boardId(boardEntity.getId())
|
||||
.boardTypeId(boardEntity.getBoardType().getId())
|
||||
.boardTitle(boardEntity.getTitle())
|
||||
.boardContents(boardEntity.getContent())
|
||||
.boardRegDate(boardEntity.getRegDate())
|
||||
.regDate(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
boardScrapRepository.save(boardScrap);
|
||||
|
||||
return ServiceResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.example.restcontroller.user.entity;
|
||||
|
||||
import com.example.restcontroller.board.entity.Board;
|
||||
import com.example.restcontroller.board.entity.BoardHits;
|
||||
import com.example.restcontroller.board.entity.BoardLike;
|
||||
import com.example.restcontroller.board.entity.BoardType;
|
||||
import com.example.restcontroller.board.entity.*;
|
||||
import com.example.restcontroller.notice.entity.Notice;
|
||||
import com.example.restcontroller.notice.entity.NoticeLike;
|
||||
import com.example.restcontroller.user.model.UserInput;
|
||||
@@ -67,6 +64,10 @@ public class User {
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<BoardLike> boardLikeList = new ArrayList<>();
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<BoardScrap> boardScrapList = new ArrayList<>();
|
||||
|
||||
@PrePersist
|
||||
public void prePersist() {
|
||||
status = status == null ? UserStatus.USING : status;
|
||||
|
||||
Reference in New Issue
Block a user