1. 게시물 생성 API
# model
postPostsReq
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PostPostsReq {
private int userIdx;
private String content;
private List<PostImgUrlReq> postImgUrls;
}
PostImgUrlReq
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PatchPostsReq {
private int userIdx;
private String content;
}
PostPostsRes
@Getter
@Setter
@AllArgsConstructor
public class PostPostsRes {
private int postIdx;
// 생성한 게시글의 인덱스 번호
}
# Controller
@ResponseBody
@PostMapping("")
public BaseResponse<PostPostsRes> createPosts(@RequestBody PostPostsReq postPostsReq) {
try{
if(postPostsReq.getContent().length()>450){
return new BaseResponse<>(BaseResponseStatus.POST_POSTS_INVALID_CONTENTS);
}
if(postPostsReq.getPostImgUrls().size()<1){
return new BaseResponse<>(BaseResponseStatus.POST_POSTS_EMPTY_IMGURL);
}
PostPostsRes postPostsRes = postService.createPosts(postPostsReq.getUserIdx(),postPostsReq);
return new BaseResponse<>(postPostsRes);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
# Service
public PostPostsRes createPosts(int userIdx, PostPostsReq postPostsReq) throws BaseException{
try{
int postIdx = postDao.insertPosts(userIdx, postPostsReq.getContent());
for(int i=0; i<postPostsReq.getPostImgUrls().size(); i++){
postDao.insertPostImgs(postIdx, postPostsReq.getPostImgUrls().get(i));
}
return new PostPostsRes(postIdx);
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
# Dao
public int insertPosts(int userIdx, String content){
String insertPostQuery = "insert into Post (userIdx, content) VALUES (?,?)";
Object[] insertPostParams = new Object[] {userIdx, content};
this.jdbcTemplate.update(insertPostQuery, insertPostParams);
String lastInsertIdxQuery = "select last_insert_id()";
return this.jdbcTemplate.queryForObject(lastInsertIdxQuery,int.class);
}
public int insertPostImgs(int postIdx, PostImgUrlReq postImgUrlReq){
String insertPostImgsQuery = "insert into PostImgUrl (postIdx, imgUrl) VALUES (?,?)";
Object[] insertPostImgsParams = new Object[]{postIdx, postImgUrlReq.getImgUrl()};
this.jdbcTemplate.update(insertPostImgsQuery, insertPostImgsParams);
String lastInsertIdxQuery = "select last_insert_id()";
return this.jdbcTemplate.queryForObject(lastInsertIdxQuery,int.class);
}
# postman
🤯 트러블 슈팅 > [오류] @NoArgsConstructor / deserialize(역직렬화)


# API 명세서
2. 게시물 수정 API
# model
PatchPostsReq
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PatchPostsReq {
private int userIdx;
private String content;
}
# Controller
@ResponseBody
@PatchMapping("/{postIdx}")
public BaseResponse<String> modifyPost(@PathVariable ("postIdx") int postIdx, @RequestBody PatchPostsReq patchPostsReq) {
if(patchPostsReq.getContent().length()>450){
return new BaseResponse<>(BaseResponseStatus.POST_POSTS_INVALID_CONTENTS);
}
try{
postService.modifyPost(patchPostsReq.getUserIdx(), postIdx, patchPostsReq);
String result = "게시글 수정이 완료되었습니다.";
return new BaseResponse<>(result);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
# Service
public void modifyPost(int userIdx, int postIdx, PatchPostsReq patchPostsReq) throws BaseException{
if(postProvider.checkUserExist(userIdx)==0){
throw new BaseException(USERS_EMPTY_USER_ID);
}
if(postProvider.checkPostExist(postIdx)==0){
throw new BaseException(POSTS_EMPTY_POST_ID);
}
try{
int result = postDao.updatePost(postIdx, patchPostsReq.getContent());
if(result==0){
throw new BaseException(MODIFY_FAIL_POST);
}
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
# Provider
public int checkUserExist(int userIdx) throws BaseException{
try{
return postDao.checkUserExist(userIdx);
} catch (Exception exception){
throw new BaseException(DATABASE_ERROR);
}
}
public int checkPostExist(int postIdx) throws BaseException{
try{
return postDao.checkPostExist(postIdx);
} catch (Exception exception){
throw new BaseException(DATABASE_ERROR);
}
}
# Dao
public int updatePost(int postIdx, String content){
String updatePostQuery = "UPDATE Post SET content=? WHERE postIdx=?;";
Object[] updatePostParams = new Object[] {content,postIdx};
return this.jdbcTemplate.update(updatePostQuery, updatePostParams);
}
public int checkUserExist(int userIdx){
String checkUserExistQuery = "select exists(select userIdx from User where userIdx = ?)";
int checkUserExistParams = userIdx;
return this.jdbcTemplate.queryForObject(checkUserExistQuery,
int.class,
checkUserExistParams);
}
public int checkPostExist(int postIdx){
String checkPostExistQuery = "select exists(select postIdx from Post where postIdx = ?)";
int checkPostExistParams = postIdx;
return this.jdbcTemplate.queryForObject(checkPostExistQuery,
int.class,
checkPostExistParams);
}
# postman


# API 명세서
3. 게시물 삭제 API
# Controller
@ResponseBody
@PatchMapping("/{postIdx}/status")
public BaseResponse<String> deletePost(@PathVariable ("postIdx") int postIdx) {
try{
postService.deletePost(postIdx);
String result = "게시글 삭제가 완료되었습니다.";
return new BaseResponse<>(result);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
# Service
public void deletePost(int postIdx) throws BaseException{
try{
int result = postDao.deletePost(postIdx);
if(result==0){
throw new BaseException(DELETE_FAIL_POST);
}
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
# Dao
public int deletePost(int postIdx){
String deletePostQuery = "UPDATE Post SET status='INACTIVE' WHERE postIdx=?;";
Object[] deletePostParams = new Object[] {postIdx};
return this.jdbcTemplate.update(deletePostQuery, deletePostParams);
}
# postman


# API 명세서
'Server > UMC 2기 Server' 카테고리의 다른 글
[UMC] Server 10주차 *실습* 로그인 API / JWT 발급 / 회원용 API 인증 (0) | 2022.05.27 |
---|---|
[UMC] Server 10주차 Paging / Transaction / 로그인 / 쿠키세션 / jwt / OAuth (0) | 2022.05.26 |
[UMC] Server 8주차 **GET** 유저 피드 조회 API / 게시물 리스트 조회 API (0) | 2022.05.12 |
[UMC] Server 7주차 유저 삭제 API (0) | 2022.05.07 |
[UMC] Server 7주차 Springboot / 유저 조회 API / API 명세서 작성 (0) | 2022.05.05 |
1. 게시물 생성 API
# model
postPostsReq
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PostPostsReq {
private int userIdx;
private String content;
private List<PostImgUrlReq> postImgUrls;
}
PostImgUrlReq
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PatchPostsReq {
private int userIdx;
private String content;
}
PostPostsRes
@Getter
@Setter
@AllArgsConstructor
public class PostPostsRes {
private int postIdx;
// 생성한 게시글의 인덱스 번호
}
# Controller
@ResponseBody
@PostMapping("")
public BaseResponse<PostPostsRes> createPosts(@RequestBody PostPostsReq postPostsReq) {
try{
if(postPostsReq.getContent().length()>450){
return new BaseResponse<>(BaseResponseStatus.POST_POSTS_INVALID_CONTENTS);
}
if(postPostsReq.getPostImgUrls().size()<1){
return new BaseResponse<>(BaseResponseStatus.POST_POSTS_EMPTY_IMGURL);
}
PostPostsRes postPostsRes = postService.createPosts(postPostsReq.getUserIdx(),postPostsReq);
return new BaseResponse<>(postPostsRes);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
# Service
public PostPostsRes createPosts(int userIdx, PostPostsReq postPostsReq) throws BaseException{
try{
int postIdx = postDao.insertPosts(userIdx, postPostsReq.getContent());
for(int i=0; i<postPostsReq.getPostImgUrls().size(); i++){
postDao.insertPostImgs(postIdx, postPostsReq.getPostImgUrls().get(i));
}
return new PostPostsRes(postIdx);
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
# Dao
public int insertPosts(int userIdx, String content){
String insertPostQuery = "insert into Post (userIdx, content) VALUES (?,?)";
Object[] insertPostParams = new Object[] {userIdx, content};
this.jdbcTemplate.update(insertPostQuery, insertPostParams);
String lastInsertIdxQuery = "select last_insert_id()";
return this.jdbcTemplate.queryForObject(lastInsertIdxQuery,int.class);
}
public int insertPostImgs(int postIdx, PostImgUrlReq postImgUrlReq){
String insertPostImgsQuery = "insert into PostImgUrl (postIdx, imgUrl) VALUES (?,?)";
Object[] insertPostImgsParams = new Object[]{postIdx, postImgUrlReq.getImgUrl()};
this.jdbcTemplate.update(insertPostImgsQuery, insertPostImgsParams);
String lastInsertIdxQuery = "select last_insert_id()";
return this.jdbcTemplate.queryForObject(lastInsertIdxQuery,int.class);
}
# postman
🤯 트러블 슈팅 > [오류] @NoArgsConstructor / deserialize(역직렬화)


# API 명세서
2. 게시물 수정 API
# model
PatchPostsReq
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PatchPostsReq {
private int userIdx;
private String content;
}
# Controller
@ResponseBody
@PatchMapping("/{postIdx}")
public BaseResponse<String> modifyPost(@PathVariable ("postIdx") int postIdx, @RequestBody PatchPostsReq patchPostsReq) {
if(patchPostsReq.getContent().length()>450){
return new BaseResponse<>(BaseResponseStatus.POST_POSTS_INVALID_CONTENTS);
}
try{
postService.modifyPost(patchPostsReq.getUserIdx(), postIdx, patchPostsReq);
String result = "게시글 수정이 완료되었습니다.";
return new BaseResponse<>(result);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
# Service
public void modifyPost(int userIdx, int postIdx, PatchPostsReq patchPostsReq) throws BaseException{
if(postProvider.checkUserExist(userIdx)==0){
throw new BaseException(USERS_EMPTY_USER_ID);
}
if(postProvider.checkPostExist(postIdx)==0){
throw new BaseException(POSTS_EMPTY_POST_ID);
}
try{
int result = postDao.updatePost(postIdx, patchPostsReq.getContent());
if(result==0){
throw new BaseException(MODIFY_FAIL_POST);
}
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
# Provider
public int checkUserExist(int userIdx) throws BaseException{
try{
return postDao.checkUserExist(userIdx);
} catch (Exception exception){
throw new BaseException(DATABASE_ERROR);
}
}
public int checkPostExist(int postIdx) throws BaseException{
try{
return postDao.checkPostExist(postIdx);
} catch (Exception exception){
throw new BaseException(DATABASE_ERROR);
}
}
# Dao
public int updatePost(int postIdx, String content){
String updatePostQuery = "UPDATE Post SET content=? WHERE postIdx=?;";
Object[] updatePostParams = new Object[] {content,postIdx};
return this.jdbcTemplate.update(updatePostQuery, updatePostParams);
}
public int checkUserExist(int userIdx){
String checkUserExistQuery = "select exists(select userIdx from User where userIdx = ?)";
int checkUserExistParams = userIdx;
return this.jdbcTemplate.queryForObject(checkUserExistQuery,
int.class,
checkUserExistParams);
}
public int checkPostExist(int postIdx){
String checkPostExistQuery = "select exists(select postIdx from Post where postIdx = ?)";
int checkPostExistParams = postIdx;
return this.jdbcTemplate.queryForObject(checkPostExistQuery,
int.class,
checkPostExistParams);
}
# postman


# API 명세서
3. 게시물 삭제 API
# Controller
@ResponseBody
@PatchMapping("/{postIdx}/status")
public BaseResponse<String> deletePost(@PathVariable ("postIdx") int postIdx) {
try{
postService.deletePost(postIdx);
String result = "게시글 삭제가 완료되었습니다.";
return new BaseResponse<>(result);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
# Service
public void deletePost(int postIdx) throws BaseException{
try{
int result = postDao.deletePost(postIdx);
if(result==0){
throw new BaseException(DELETE_FAIL_POST);
}
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
# Dao
public int deletePost(int postIdx){
String deletePostQuery = "UPDATE Post SET status='INACTIVE' WHERE postIdx=?;";
Object[] deletePostParams = new Object[] {postIdx};
return this.jdbcTemplate.update(deletePostQuery, deletePostParams);
}
# postman


# API 명세서
'Server > UMC 2기 Server' 카테고리의 다른 글
[UMC] Server 10주차 *실습* 로그인 API / JWT 발급 / 회원용 API 인증 (0) | 2022.05.27 |
---|---|
[UMC] Server 10주차 Paging / Transaction / 로그인 / 쿠키세션 / jwt / OAuth (0) | 2022.05.26 |
[UMC] Server 8주차 **GET** 유저 피드 조회 API / 게시물 리스트 조회 API (0) | 2022.05.12 |
[UMC] Server 7주차 유저 삭제 API (0) | 2022.05.07 |
[UMC] Server 7주차 Springboot / 유저 조회 API / API 명세서 작성 (0) | 2022.05.05 |