Skip to content

Commit

Permalink
UMC-EWHA#16 Feat : Upload Create, Read, Update board
Browse files Browse the repository at this point in the history
  • Loading branch information
dawoon08 committed Nov 14, 2022
1 parent 0bd69ab commit 90d353e
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public enum BaseResponseStatus {

//[PATCH] /users/{userIdx}
MODIFY_FAIL_USERNAME(false,4014,"유저네임 수정 실패"),
MODIFY_FAIL_CONTENT(false,4015,"유저네임 수정 실패"),

PASSWORD_ENCRYPTION_ERROR(false, 4011, "비밀번호 암호화에 실패하였습니다."),
PASSWORD_DECRYPTION_ERROR(false, 4012, "비밀번호 복호화에 실패하였습니다.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.example.demo.src.board;

import com.example.demo.config.BaseException;
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.src.board.model.*;
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 java.util.List;

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

@RestController
@RequestMapping("/app/boards")
Expand All @@ -37,7 +37,7 @@ public BoardController(BoardProvider boardProvider, BoardService boardService, J
* [POST] / boards
*/
@ResponseBody
@PostMapping("/upload")
@PostMapping("/post")
public BaseResponse<PostBoardRes> uploadBoard(@RequestBody PostBoardReq postBoardReq) {

if (postBoardReq.getTitle() == null) {
Expand All @@ -50,4 +50,42 @@ public BaseResponse<PostBoardRes> uploadBoard(@RequestBody PostBoardReq postBoar

return new BaseResponse<>(postBoardRes);
}

/*
*게시글 리스트 조회 API
* [GET] / boards
*/
@ResponseBody
@GetMapping("")
public BaseResponse<List<GetPostRes>> getPosts(@RequestParam(required = false) String writer){
try{
if(writer == null){
List<GetPostRes> getPostsRes = boardProvider.getPosts();
return new BaseResponse<>(getPostsRes);
}
List<GetPostRes> getPostRes = boardProvider.getPostByWriter(writer);
return new BaseResponse<>(getPostRes);

} catch (BaseException exception) {
return new BaseResponse<>((exception.getStatus()));
}
}

/*
*게시글 내용 수정 API
* [PATCH] / boards
*/
@ResponseBody
@PatchMapping("/{postIdx}")
public BaseResponse<String> modifyPostContent(@PathVariable("postIdx") int postIdx, @RequestBody Post post) {
try {
PatchPostReq patchPostReq = new PatchPostReq(postIdx, post.getContent());
boardService.modifyPostContent(patchPostReq);

String result = "게시글이 수정되었습니다.";
return new BaseResponse<>(result);
} catch (BaseException exception){
return new BaseResponse<>(exception.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.example.demo.src.board;

import com.example.demo.src.board.model.GetPostRes;
import com.example.demo.src.board.model.PatchPostReq;
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;
import java.util.List;

@Repository
public class BoardDao {
Expand All @@ -19,11 +20,43 @@ public void setDataSource(DataSource dataSource) {
}

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

String lastInsertIdQuery = "select last_insert_id()"; // 가장 마지막에 삽입된(생성된) id값은 가져온다.
return this.jdbcTemplate.queryForObject(lastInsertIdQuery, int.class); // 해당 쿼리문의 결과 마지막으로 삽인된 유저의 userIdx번호를 반환한다.
}

public List<GetPostRes> getPosts() {
String getPostsQuery = "select * from Posts";
return this.jdbcTemplate.query(getPostsQuery,
(rs, rowNum) -> new GetPostRes(
rs.getInt("postIdx"),
rs.getString("title"),
rs.getString("writer"),
rs.getString("content")
));
}

public List<GetPostRes> getPostsByWriter(String writer) {
String getPostsByWriterQuery = "select * from Posts where writer = ?";
String getPostsByWriterParams = writer;

return this.jdbcTemplate.query(getPostsByWriterQuery,
(rs, rowNum) -> new GetPostRes(
rs.getInt("postIdx"),
rs.getString("title"),
rs.getString("writer"),
rs.getString("content")),
getPostsByWriterParams
);
}

public int modifyPostContent(PatchPostReq patchPostReq){
String modifyPostContentQuery = "update Posts set content = ? where postIdx = ? ";
Object[] modifyPostContentParams = new Object[]{patchPostReq.getContent(), patchPostReq.getPostIdx()};

return this.jdbcTemplate.update(modifyPostContentQuery, modifyPostContentParams);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
package com.example.demo.src.board;

import com.example.demo.config.BaseException;
import com.example.demo.src.board.model.GetPostRes;
import com.example.demo.src.user.model.GetUserRes;
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.util.List;

import static com.example.demo.config.BaseResponseStatus.DATABASE_ERROR;

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

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

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

public List<GetPostRes> getPosts() throws BaseException {
try {
List<GetPostRes> getPostRes = boardDao.getPosts();
return getPostRes;
} catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}

public List<GetPostRes> getPostByWriter(String writer) throws BaseException {
try {
List<GetPostRes> getPostRes = boardDao.getPostsByWriter(writer);
return getPostRes;
} catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.demo.src.board;

import com.example.demo.config.BaseException;
import com.example.demo.config.BaseResponseStatus;
import com.example.demo.src.board.model.PatchPostReq;
import com.example.demo.src.board.model.PostBoardReq;
import com.example.demo.src.board.model.PostBoardRes;
import com.example.demo.utils.JwtService;
Expand All @@ -9,7 +11,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Clob;
import static com.example.demo.config.BaseResponseStatus.DATABASE_ERROR;
import static com.example.demo.config.BaseResponseStatus.MODIFY_FAIL_CONTENT;

@Service
public class BoardService {
Expand All @@ -29,15 +32,31 @@ public BoardService(BoardDao boardDao, BoardProvider boardProvider, JwtService j
// 게시글 업로드(POST)
public PostBoardRes uploadBoard(PostBoardReq postBoardReq) {
String title;
String writer;
String content;

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

writer = postBoardReq.getWriter();
postBoardReq.setWriter(writer);

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

int boardIdx = boardDao.uploadBoard(postBoardReq);
return new PostBoardRes(boardIdx);
}

//게시글 내용 수정
public void modifyPostContent(PatchPostReq patchPostReq) throws BaseException {
try {
int result = boardDao.modifyPostContent(patchPostReq);
if(result == 0){
throw new BaseException(MODIFY_FAIL_CONTENT);
}
} catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.demo.src.board.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class GetPostRes {
private int postIdx;
private String title;
private String writer;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.demo.src.board.model;

import lombok.*;

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

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class Post {
private int postIdx;
private String title;
private String writer;
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostBoardReq {
private String title;
private String writer;
private String content;
}
Binary file added logs/app.2022-11-12.0.gz
Binary file not shown.
Binary file added logs/error-2022-11-12.0.gz
Binary file not shown.

0 comments on commit 90d353e

Please sign in to comment.