Skip to content

Commit

Permalink
feat(#2) : 게시판 기본 CRUD 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
DebbieIsFree committed May 13, 2024
1 parent d703ee8 commit c1c141a
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.EnjoyTripBackend.controller;

import com.example.EnjoyTripBackend.dto.Board.BoardDto;
import com.example.EnjoyTripBackend.service.BoardService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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 static org.springframework.http.HttpStatus.CREATED;

@RestController
@RequestMapping("/api/boards")
@RequiredArgsConstructor
public class BoardController {

private final BoardService boardService;

@PostMapping("/")
public ResponseEntity<String> save(@Valid @RequestBody BoardDto boardDto){
Long id = boardService.save(boardDto);
return ResponseEntity.status(CREATED).body("게시글 등록 완료");
}

@GetMapping("/{id}")
public ResponseEntity<board> findById(@PathVariable("id") Long id) {
Board board = boardService.findById(id);
return ResponseEntity.status(OK).body(board);
}

@GetMapping
public ResponseEntity<List<board>> findAll() {
List<board> boardList = boardService.findAll();
return ResponseEntity.status(OK).body(boardList);
}

@PutMapping("/{id}")
public ResponseEntity<Long> update(@Valid @RequestBody BoardDto boardDto) {
Long boardId = boardService.update(boardDto);
return ResponseEntity.status(OK).body(boardId);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
boardService.delete(id);
return ResponseEntity.status(NO_CONTENT).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.EnjoyTripBackend.dto.Board;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.NoArgsConstructor;
import lombok.Getter;

@Getter
@NoArgsConstructor
public class BoardDto {

@NotNull
private Long memberId;

@NotEmpty
private String title;

@NotEmpty
private String content;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.EnjoyTripBackend.repository;


import com.example.EnjoyTripBackend.domain.Member;
import org.apache.ibatis.annotations.Mapper;

import java.util.Optional;

@Mapper
public interface BoardRepository {
Long save(Board board);
Board findById(Long id)
List<Board> findAll();
Long update(BoardDto boardDto);
void delete(Long id);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.EnjoyTripBackend.service;

import com.example.EnjoyTripBackend.domain.Board;
import com.example.EnjoyTripBackend.dto.Board.BoardDto;
import com.example.EnjoyTripBackend.repository.BoardRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class BoardService {

private final BoardRepository boardRepository;

@Transactional
public Long save(BoardDto boardDto) {
Board board = Board.builder()
.memberId(boardDto.getMemberId())
.title(boardDto.getTitle())
.content(boardDto.getContent())
.build();
return boardRepository.save(board);
}

public Board findById(Long id) {
return boardRepository.findById(id)
.orElseThrow(() -> new EnjoyTripException(ErrorCode.BOARD_NOT_FOUND));
}

public List<Board> findAll(){
return boardRepository.findAll();
// return boardRepository.findAll().stream().map(this::Board).collect(Collector.toList());
}

@Transactional
public Long update(BoardDto boardDto){
Optional<Board> optionalBoard = boardRepository.findById(boardDto.getId());
if (!optionalBoard.isPresent()) {
throw new EnjoyTripException(
"Board is not present in the database");
}
return boardRepository.update(boardDto);
}

public void delete(Long id){
Optional<Board> optionalBoard = boardRepository.findById(id);
if (!optionalBoard.isPresent()) {
throw new EnjoyTripException(
"Board is not present in the database");
}
boardRepository.delete(id);
}
}
35 changes: 35 additions & 0 deletions EnjoyTripBackend/src/main/resources/mapper/board-query.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.EnjoyTripBackend.repository.BoardRepository">
<insert id="save" parameterType="com.example.EnjoyTripBackend.domain.Board">
insert
into board
values (#{id}, #{memberId}, #{title}, #{content}, now(), now())
</insert>

<select id="findById" parameterType="long" resultType="com.example.EnjoyTripBackend.domain.Board">
select *
from board
where board_id = #{id}
</select>

<select id="findAll" resultType="com.example.EnjoyTripBackend.domain.Board">
select *
from board
</select>

<update id="update" parameterType="com.example.EnjoyTripBackend.dto.BoardDto" resultType="long">
update board
set member_id = #{memberId},
title = #{title},
content = #{content},
modifiedDate = #{modifiedDate}
where id = #{id}
</update>

<delete id="delete">
delete from board where id = #{id}
</delete>
</mapper>

0 comments on commit c1c141a

Please sign in to comment.