Skip to content

Commit

Permalink
UMC-EWHA#16 Feat : Upload Create board
Browse files Browse the repository at this point in the history
  • Loading branch information
dawoon08 committed Nov 12, 2022
1 parent d92b394 commit 0bd69ab
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public enum BaseResponseStatus {
POST_USERS_INVALID_EMAIL(false, 2016, "이메일 형식을 확인해주세요."),
POST_USERS_EXISTS_EMAIL(false,2017,"중복된 이메일입니다."),

// [POST] /boards
POST_BOARDS_EMPTY_TITLE(false, 2018, "제목을 입력해주세요."),
POST_BOARDS_EMPTY_CONTENT(false, 2019, "본문을 입력해주세요"),


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.demo.src.board;

import com.example.demo.config.BaseResponse;
import com.example.demo.src.board.model.PostBoardReq;
import com.example.demo.src.board.model.PostBoardRes;
import com.example.demo.src.user.model.PostUserRes;
import com.example.demo.utils.JwtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.example.demo.config.BaseResponseStatus.*;
import static com.example.demo.utils.ValidationRegex.isRegexEmail;

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

final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private final BoardProvider boardProvider;
@Autowired
private final BoardService boardService;
@Autowired
private final JwtService jwtService;

public BoardController(BoardProvider boardProvider, BoardService boardService, JwtService jwtService) {
this.boardProvider = boardProvider;
this.boardService = boardService;
this.jwtService = jwtService;
}

/*
*게시글 작성 API
* [POST] / boards
*/
@ResponseBody
@PostMapping("/upload")
public BaseResponse<PostBoardRes> uploadBoard(@RequestBody PostBoardReq postBoardReq) {

if (postBoardReq.getTitle() == null) {
return new BaseResponse<>(POST_BOARDS_EMPTY_TITLE);
}
if (postBoardReq.getContent() == null) {
return new BaseResponse<>(POST_BOARDS_EMPTY_CONTENT);
}
PostBoardRes postBoardRes = boardService.uploadBoard(postBoardReq);

return new BaseResponse<>(postBoardRes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.demo.src.board;

import com.example.demo.src.board.model.PostBoardReq;
import com.example.demo.src.board.model.PostBoardRes;
import com.example.demo.src.user.model.PostUserReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

@Repository
public class BoardDao {
private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public int uploadBoard(PostBoardReq postBoardReq) {
String uploadBoardQuery = "insert into Board (title, content) VALUES (?,?)"; // 실행될 동적 쿼리문
Object[] uploadBoardParams = new Object[]{postBoardReq.getTitle(), postBoardReq.getContent()}; // 동적 쿼리의 ?부분에 주입될 값
this.jdbcTemplate.update(uploadBoardQuery, uploadBoardParams);

String lastInsertIdQuery = "select last_insert_id()"; // 가장 마지막에 삽입된(생성된) id값은 가져온다.
return this.jdbcTemplate.queryForObject(lastInsertIdQuery, int.class); // 해당 쿼리문의 결과 마지막으로 삽인된 유저의 userIdx번호를 반환한다.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.demo.src.board;

import com.example.demo.utils.JwtService;
import org.springframework.stereotype.Service;

@Service
public class BoardProvider {
private final BoardDao boardDao;
private final JwtService jwtService;

public BoardProvider(BoardDao boardDao, JwtService jwtService) {
this.boardDao = boardDao;
this.jwtService = jwtService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.demo.src.board;

import com.example.demo.config.BaseException;
import com.example.demo.src.board.model.PostBoardReq;
import com.example.demo.src.board.model.PostBoardRes;
import com.example.demo.utils.JwtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Clob;

@Service
public class BoardService {
final Logger logger = LoggerFactory.getLogger(this.getClass());

private final BoardDao boardDao;
private final BoardProvider boardProvider;
private final JwtService jwtService;

@Autowired
public BoardService(BoardDao boardDao, BoardProvider boardProvider, JwtService jwtService) {
this.boardDao = boardDao;
this.boardProvider = boardProvider;
this.jwtService = jwtService;
}

// 게시글 업로드(POST)
public PostBoardRes uploadBoard(PostBoardReq postBoardReq) {
String title;
String content;

title = postBoardReq.getTitle();
postBoardReq.setTitle(title);

content = postBoardReq.getContent();
postBoardReq.setContent(content);

int boardIdx = boardDao.uploadBoard(postBoardReq);
return new PostBoardRes(boardIdx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo.src.board.model;

import lombok.*;


@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostBoardReq {
private String title;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.demo.src.board.model;

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
public class PostBoardRes {
private int idx;
}

0 comments on commit 0bd69ab

Please sign in to comment.