forked from UMC-EWHA/umc-spring-3rd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UMC-EWHA#16 Feat : Upload Create board
- Loading branch information
Showing
7 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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번호를 반환한다. | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
SpringBootTemplate/src/main/java/com/example/demo/src/board/BoardService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
SpringBootTemplate/src/main/java/com/example/demo/src/board/model/PostBoardReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
10 changes: 10 additions & 0 deletions
10
SpringBootTemplate/src/main/java/com/example/demo/src/board/model/PostBoardRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |