Skip to content

Commit

Permalink
feat: 일지 엔티티 구현(1차)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beakjiyeon committed Apr 5, 2024
1 parent 14df19d commit 6762406
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.unit.daybook.domain.board.controller;

import com.unit.daybook.domain.board.dto.BoardAddReqDto;
import com.unit.daybook.domain.board.dto.BoardAddResDto;
import com.unit.daybook.domain.board.service.BoardService;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/board")
@RestController
public class BoardController {

private final BoardService boardService;

public BoardController(BoardService boardService) {
this.boardService = boardService;
}
@GetMapping("/{boardId}")
public String getBoard(@PathVariable("boardId") Long boardId) {
return "단일 조회 정보 " + boardId;
}

@GetMapping("/boards")
public String getBoards() {
return "목록 조회 정보";
}


@PostMapping
public BoardAddResDto addBoard(@RequestBody BoardAddReqDto boardAddReqDto) {
Long memberId = 1L; // TODO 인증
return boardService.addBoard(boardAddReqDto, memberId);

}

@PostMapping("/{boardId}")
public String modifyBoard(@PathVariable("boardId") Long boardId) {
return "수정 성공";
}

@DeleteMapping("/{boardId}")
public String deleteBoard(@PathVariable("boardId") Long boardId) {
return "삭제 성공";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.unit.daybook.domain.board.dto;

import com.unit.daybook.domain.board.entity.BoardEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@NoArgsConstructor
public class BoardAddReqDto {
private String content;
private Long respectBoardId;


public BoardEntity toEntity() {
BoardEntity entity = new BoardEntity();
entity.setContent(content);
entity.setRespectBoardId(respectBoardId);
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.unit.daybook.domain.board.dto;

import com.unit.daybook.domain.board.entity.BoardEntity;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class BoardAddResDto {
private Long boardId;
private String content;
private Long respectBoardId;
private LocalDateTime createdAt;

private LocalDateTime modifiedAt;

@Builder
public BoardAddResDto(BoardEntity entity) {
this.boardId = entity.getBoardId();
this.content = entity.getContent();
this.respectBoardId = entity.getRespectBoardId();
this.createdAt = null;
this.modifiedAt = null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.unit.daybook.domain.board.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Table(name = "board")
@Entity
public class BoardEntity {

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long boardId;

@Column
private String content;

@Column
private Long respectBoardId;



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.unit.daybook.domain.board.repository;

import com.unit.daybook.domain.board.entity.BoardEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BoardRepository extends JpaRepository<BoardEntity, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.unit.daybook.domain.board.service;

import com.unit.daybook.domain.board.dto.BoardAddReqDto;
import com.unit.daybook.domain.board.dto.BoardAddResDto;
import com.unit.daybook.domain.board.entity.BoardEntity;
import com.unit.daybook.domain.board.repository.BoardRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

@Service
public class BoardService {
public BoardService(BoardRepository boardRepository) {
this.boardRepository = boardRepository;
}

private final BoardRepository boardRepository;

@Transactional
public BoardAddResDto addBoard(BoardAddReqDto boardAddReqDto, Long memberId) {
BoardEntity entity = boardRepository.save(boardAddReqDto.toEntity());
return BoardAddResDto.builder()
.entity(entity)
.build();
}
}

0 comments on commit 6762406

Please sign in to comment.