1. 유저 피드 조회 API

model에서 GetUserRes를 GetUserPeedRes로 수정


피드는 내 정보와 게시글로 이루어져 있다.
//GetUserFeedRes
package com.example.demo.src.user.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class GetUserFeedRes {
private boolean _isMyFeed;
private GetUserInfoRes getUserInfo;
private List<GetUserPostsRes> getUserPosts;
}
그래서 GetUserFeedRes는 크게 GetUserInfoRes와 GetUserPostsRes의 리스트로 구성될 것이다.
여기에 내 게시글을 보는것과 다른 사람의 게시글을 보는 것은 화면 구성이 다르기 때문에 boolean type의 _isMyFeed도 추가해줬따.
// GetUserInfoRes
package com.example.demo.src.user.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class GetUserInfoRes {
private int userIdx;
private String nickName;
private String name;
private String profileImgUrl;
private String website;
private String introduce;
private int followerCount;
private int followingCount;
private int postCount;
}
// GetUserPostsRes
package com.example.demo.src.user.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class GetUserPostsRes {
private int postIdx;
private String postImgUrl;
}
여기까지 모델 코드였다.
Controller
// Controller
@ResponseBody
@GetMapping("/{userIdx}")
public BaseResponse<GetUserFeedRes> getUserFeed(@PathVariable("userIdx")int userIdx) {
try{
GetUserFeedRes getUserFeedRes = userProvider.retrieveUserFeed(userIdx,userIdx);
return new BaseResponse<>(getUserFeedRes);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
Provider
// Provider
// userIdxMyJwt : 로그인된 userIdx
// userIdx : pathvariable로 받아온 userIdx
public GetUserFeedRes retrieveUserFeed(int userIdxByJwt, int userIdx) throws BaseException {
Boolean isMyFeed = true;
if (checkUserExist(userIdx) == 0) {
throw new BaseException(USERS_EMPTY_USER_ID);
}
try {
if (userIdxByJwt != userIdx) isMyFeed = false;
GetUserInfoRes getUserInfo = userDao.selectUserInfo(userIdx); // 유저의 정보를 받아오는 객체
List<GetUserPostsRes> getUserPosts = userDao.selectUserPosts(userIdx); // 유저의 게시물 리스트를 받아오는 객체
GetUserFeedRes getUsersRes = new GetUserFeedRes(isMyFeed, getUserInfo, getUserPosts);
return getUsersRes;
} catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
Dao
// dao
// 유저 정보 출력
public GetUserInfoRes selectUserInfo(int userIdx){
String selectUserInfoQuery = "SELECT u.userIdx as userIdx, u.nickName as nickName, u.name as name, u.profileImgUrl as profileImgUrl, u.website as website, u.introduce as introduce,\n" +
" If(postCount is null, 0, postCount) as postCount,\n" +
" If(followerCount is null, 0, followerCount) as followerCount,\n" +
" If(followingCount is null, 0, followingCount) as followingCount\n" +
"FROM User as u\n" +
" left join (select userIdx, count(postIdx) as postCount\n" +
" from Post Where status = 'ACTIVE' group by userIdx) p on p.userIdx = u.userIdx\n" +
" left join (select followerIdx, count(followerIdx) as followerCount\n" +
" from Follow Where status = 'ACTIVE' group by followerIdx) fc on fc.followerIdx = u.userIdx\n" +
" left join (select followeeIdx, count(followeeIdx) as followingCount\n" +
" from Follow Where status = 'ACTIVE' group by followeeIdx) f on f.followeeIdx = u.userIdx\n" +
"WHERE u.userIdx = ? and u.status = 'ACTIVE'";
int selectUserInfoParam = userIdx;
return this.jdbcTemplate.queryForObject(selectUserInfoQuery,
(rs,rowNum) -> new GetUserInfoRes(
rs.getInt("userIdx"),
rs.getString("nickName"),
rs.getString("name"),
rs.getString("profileImgUrl"),
rs.getString("website"),
rs.getString("introduce"),
rs.getInt("followerCount"),
rs.getInt("followingCount"),
rs.getInt("postCount")
),selectUserInfoParam);
}
// 유저 게시글
public List<GetUserPostsRes> selectUserPosts(int userIdx){
String selectUserPostsQuery = "SELECT p.postIdx as postIdx, pi.imgUrl as postImgUrl\n" +
"FROM Post as p\n" +
" join PostImgUrl as pi on pi.postidx = p.postIdx and pi.status = 'ACTIVE'\n" +
" join User as u on u.userIdx = p.userIdx\n" +
"WHERE p.status = 'ACTIVE' and u.userIdx = ?\n" +
"group by p.postIdx\n" +
"HAVING min(pi.postImgUrlIdx)\n" +
"order by p.postIdx";
int selectUserPostsParam = userIdx;
return this.jdbcTemplate.query(selectUserPostsQuery,
(rs,rowNum) -> new GetUserPostsRes(
rs.getInt("postIdx"),
rs.getString("postImgUrl")
),selectUserPostsParam);
}
리스트가 아니면 jdbcTemplate.queryForObject
리스트면 jdbcTemplate.query

postman으로 응답을 확인해보았다. ~성공~
>> api 명세서 작성



2. 게시물 리스트 조회 API


api 명세서를 확인해보면 게시물 리스트 조회 api의 도메인은 post이므로 post도메인 폴더를 만들어줘야한다.

model을 먼저 만들어보겠다.
// GetPostImgRes
// 사진 객체
package com.example.demo.src.post.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class GetPostImgRes {
private int postImgIdx;
private String imgUrl;
}
// GetPostsRes
// 게시글 객체
package com.example.demo.src.post.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class GetPostsRes {
private int postIdx;
private int userIdx;
private String nickname;
private String profileImgUrl;
private String content;
private int postLikeCount;
private int commentCount;
private String updateAt;
private String likeOrNot; // 좋아요 여부
private List<GetPostImgRes> imgs;
}
Controller
package com.example.demo.src.post;
import com.example.demo.config.BaseException;
import com.example.demo.config.BaseResponse;
import com.example.demo.src.post.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;
@RestController
@RequestMapping("/posts")
public class PostController {
final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private final PostProvider postProvider;
@Autowired
private final PostService postService;
@Autowired
private final JwtService jwtService;
public PostController(PostProvider postProvider, PostService postService, JwtService jwtService) {
this.postProvider = postProvider;
this.postService = postService;
this.jwtService = jwtService;
}
@ResponseBody
@GetMapping("")
public BaseResponse<List<GetPostsRes>> getPosts(@RequestParam int userIdx) {
try{
List<GetPostsRes> getPostsRes = postProvider.retrievePosts(userIdx);
return new BaseResponse<>(getPostsRes);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
}
Provider
package com.example.demo.src.post;
import com.example.demo.config.BaseException;
import com.example.demo.src.post.model.GetPostsRes;
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;
import static com.example.demo.config.BaseResponseStatus.USERS_EMPTY_USER_ID;
@Service
public class PostProvider {
private final PostDao postDao;
private final JwtService jwtService;
final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
public PostProvider(PostDao postDao, JwtService jwtService) {
this.postDao = postDao;
this.jwtService = jwtService;
}
public List<GetPostsRes> retrievePosts(int userIdx) throws BaseException{
if(checkUserExist(userIdx)==0)
{
throw new BaseException(USERS_EMPTY_USER_ID);
}
try{
List<GetPostsRes> getPosts = postDao.selectPosts(userIdx);
return getPosts;
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
public int checkUserExist(int userIdx) throws BaseException{
try{
return postDao.checkUserExist(userIdx);
} catch (Exception exception){
throw new BaseException(DATABASE_ERROR);
}
}
}
Dao
package com.example.demo.src.post;
import com.example.demo.src.post.model.*;
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 PostDao {
private JdbcTemplate jdbcTemplate;
private List<GetPostImgRes> getPostImgRes;
@Autowired
public void setDataSource(DataSource dataSource){
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public List<GetPostsRes> selectPosts(int userIdx){
String selectPostsQuery = "select p.postIdx as postIdx, u.userIdx as userIdx, u.nickName as nickName, u.profileImgUrl as profileImgUrl, p.content as content,\n" +
" if(postLikeCount is null, 0, postLikeCount) as postLikeCount,\n" +
" if(commentCount is null, 0, commentCount) as commentCount,\n" +
" case\n" +
" when timestampdiff(second, p.updatedAt, current_timestamp) < 60 then concat(timestampdiff(second,p.updatedAt, current_timestamp), '초 전')\n" +
" when timestampdiff(minute, p.updatedAt, current_timestamp) < 60 then concat(timestampdiff(minute,p.updatedAt, current_timestamp), '분 전')\n" +
" when timestampdiff(hour, p.updatedAt, current_timestamp) < 24 then concat(timestampdiff(hour,p.updatedAt, current_timestamp), '시간 전')\n" +
" when timestampdiff(day, p.updatedAt, current_timestamp) < 365 then concat(timestampdiff(day,p.updatedAt, current_timestamp), '일 전')\n" +
" else timestampdiff(year, p.updatedAt, current_timestamp)\n" +
" end as updatedAt,\n" +
" if(h.status = 'ACTIVE','Y','N') as likeOrNot\n" +
"from Post as p\n" +
" join User as u on u.userIdx = p.userIdx\n" +
" left join (select heartIdx, postIdx, status, count(heartIdx) as postLikeCount\n" +
" from Heart\n" +
" where status = 'ACTIVE'\n" +
" group by postIdx) as h on p.postIdx = h.postIdx\n" +
" left join (select postIdx, count(commendIdx) as commentCount\n" +
" from Comment\n" +
" where status = 'ACTIVE' ) as c on c.postIdx = p.postIdx\n" +
"where p.status = 'ACTIVE' and p.postIdx = ?";
int selectPostsParam = userIdx;
return this.jdbcTemplate.query(selectPostsQuery,
(rs,rowNum) -> new GetPostsRes(
rs.getInt("postIdx"),
rs.getInt("userIdx"),
rs.getString("nickname"),
rs.getString("profileImgUrl"),
rs.getString("content"),
rs.getInt("postLikeCount"),
rs.getInt("commentCount"),
rs.getString("updatedAt"),
rs.getString("likeOrNot"),
getPostImgRes = this.jdbcTemplate.query("select pi.postImgUrlidx, pi.imgUrl\n" +
"from PostImgUrl as pi\n" +
"join Post as p on p.postIdx = pi.postIdx\n" +
"where pi.status = 'ACTIVE' and p.postIdx = ?",
(rk, rownum) -> new GetPostImgRes(
rk.getInt("postImgUrlIdx"),
rk.getString("imgUrl")
), rs.getInt("postIdx")
)
),selectPostsParam);
}
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);
}
}
postman 응답 확인

>> api 명세서




'Server > UMC 2기 Server' 카테고리의 다른 글
[UMC] Server 10주차 Paging / Transaction / 로그인 / 쿠키세션 / jwt / OAuth (0) | 2022.05.26 |
---|---|
[UMC] Server 9주차 *POST*PATCH*DELETE* 게시물 생성 API / 게시물 수정 API / 게시물 삭제 API (0) | 2022.05.18 |
[UMC] Server 7주차 유저 삭제 API (0) | 2022.05.07 |
[UMC] Server 7주차 Springboot / 유저 조회 API / API 명세서 작성 (0) | 2022.05.05 |
[UMC] Server 7주차 Springboot 개발환경 구축하기 (0) | 2022.05.02 |
1. 유저 피드 조회 API

model에서 GetUserRes를 GetUserPeedRes로 수정


피드는 내 정보와 게시글로 이루어져 있다.
//GetUserFeedRes
package com.example.demo.src.user.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class GetUserFeedRes {
private boolean _isMyFeed;
private GetUserInfoRes getUserInfo;
private List<GetUserPostsRes> getUserPosts;
}
그래서 GetUserFeedRes는 크게 GetUserInfoRes와 GetUserPostsRes의 리스트로 구성될 것이다.
여기에 내 게시글을 보는것과 다른 사람의 게시글을 보는 것은 화면 구성이 다르기 때문에 boolean type의 _isMyFeed도 추가해줬따.
// GetUserInfoRes
package com.example.demo.src.user.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class GetUserInfoRes {
private int userIdx;
private String nickName;
private String name;
private String profileImgUrl;
private String website;
private String introduce;
private int followerCount;
private int followingCount;
private int postCount;
}
// GetUserPostsRes
package com.example.demo.src.user.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class GetUserPostsRes {
private int postIdx;
private String postImgUrl;
}
여기까지 모델 코드였다.
Controller
// Controller
@ResponseBody
@GetMapping("/{userIdx}")
public BaseResponse<GetUserFeedRes> getUserFeed(@PathVariable("userIdx")int userIdx) {
try{
GetUserFeedRes getUserFeedRes = userProvider.retrieveUserFeed(userIdx,userIdx);
return new BaseResponse<>(getUserFeedRes);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
Provider
// Provider
// userIdxMyJwt : 로그인된 userIdx
// userIdx : pathvariable로 받아온 userIdx
public GetUserFeedRes retrieveUserFeed(int userIdxByJwt, int userIdx) throws BaseException {
Boolean isMyFeed = true;
if (checkUserExist(userIdx) == 0) {
throw new BaseException(USERS_EMPTY_USER_ID);
}
try {
if (userIdxByJwt != userIdx) isMyFeed = false;
GetUserInfoRes getUserInfo = userDao.selectUserInfo(userIdx); // 유저의 정보를 받아오는 객체
List<GetUserPostsRes> getUserPosts = userDao.selectUserPosts(userIdx); // 유저의 게시물 리스트를 받아오는 객체
GetUserFeedRes getUsersRes = new GetUserFeedRes(isMyFeed, getUserInfo, getUserPosts);
return getUsersRes;
} catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
Dao
// dao
// 유저 정보 출력
public GetUserInfoRes selectUserInfo(int userIdx){
String selectUserInfoQuery = "SELECT u.userIdx as userIdx, u.nickName as nickName, u.name as name, u.profileImgUrl as profileImgUrl, u.website as website, u.introduce as introduce,\n" +
" If(postCount is null, 0, postCount) as postCount,\n" +
" If(followerCount is null, 0, followerCount) as followerCount,\n" +
" If(followingCount is null, 0, followingCount) as followingCount\n" +
"FROM User as u\n" +
" left join (select userIdx, count(postIdx) as postCount\n" +
" from Post Where status = 'ACTIVE' group by userIdx) p on p.userIdx = u.userIdx\n" +
" left join (select followerIdx, count(followerIdx) as followerCount\n" +
" from Follow Where status = 'ACTIVE' group by followerIdx) fc on fc.followerIdx = u.userIdx\n" +
" left join (select followeeIdx, count(followeeIdx) as followingCount\n" +
" from Follow Where status = 'ACTIVE' group by followeeIdx) f on f.followeeIdx = u.userIdx\n" +
"WHERE u.userIdx = ? and u.status = 'ACTIVE'";
int selectUserInfoParam = userIdx;
return this.jdbcTemplate.queryForObject(selectUserInfoQuery,
(rs,rowNum) -> new GetUserInfoRes(
rs.getInt("userIdx"),
rs.getString("nickName"),
rs.getString("name"),
rs.getString("profileImgUrl"),
rs.getString("website"),
rs.getString("introduce"),
rs.getInt("followerCount"),
rs.getInt("followingCount"),
rs.getInt("postCount")
),selectUserInfoParam);
}
// 유저 게시글
public List<GetUserPostsRes> selectUserPosts(int userIdx){
String selectUserPostsQuery = "SELECT p.postIdx as postIdx, pi.imgUrl as postImgUrl\n" +
"FROM Post as p\n" +
" join PostImgUrl as pi on pi.postidx = p.postIdx and pi.status = 'ACTIVE'\n" +
" join User as u on u.userIdx = p.userIdx\n" +
"WHERE p.status = 'ACTIVE' and u.userIdx = ?\n" +
"group by p.postIdx\n" +
"HAVING min(pi.postImgUrlIdx)\n" +
"order by p.postIdx";
int selectUserPostsParam = userIdx;
return this.jdbcTemplate.query(selectUserPostsQuery,
(rs,rowNum) -> new GetUserPostsRes(
rs.getInt("postIdx"),
rs.getString("postImgUrl")
),selectUserPostsParam);
}
리스트가 아니면 jdbcTemplate.queryForObject
리스트면 jdbcTemplate.query

postman으로 응답을 확인해보았다. ~성공~
>> api 명세서 작성



2. 게시물 리스트 조회 API


api 명세서를 확인해보면 게시물 리스트 조회 api의 도메인은 post이므로 post도메인 폴더를 만들어줘야한다.

model을 먼저 만들어보겠다.
// GetPostImgRes
// 사진 객체
package com.example.demo.src.post.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class GetPostImgRes {
private int postImgIdx;
private String imgUrl;
}
// GetPostsRes
// 게시글 객체
package com.example.demo.src.post.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@AllArgsConstructor
public class GetPostsRes {
private int postIdx;
private int userIdx;
private String nickname;
private String profileImgUrl;
private String content;
private int postLikeCount;
private int commentCount;
private String updateAt;
private String likeOrNot; // 좋아요 여부
private List<GetPostImgRes> imgs;
}
Controller
package com.example.demo.src.post;
import com.example.demo.config.BaseException;
import com.example.demo.config.BaseResponse;
import com.example.demo.src.post.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;
@RestController
@RequestMapping("/posts")
public class PostController {
final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private final PostProvider postProvider;
@Autowired
private final PostService postService;
@Autowired
private final JwtService jwtService;
public PostController(PostProvider postProvider, PostService postService, JwtService jwtService) {
this.postProvider = postProvider;
this.postService = postService;
this.jwtService = jwtService;
}
@ResponseBody
@GetMapping("")
public BaseResponse<List<GetPostsRes>> getPosts(@RequestParam int userIdx) {
try{
List<GetPostsRes> getPostsRes = postProvider.retrievePosts(userIdx);
return new BaseResponse<>(getPostsRes);
} catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
}
}
Provider
package com.example.demo.src.post;
import com.example.demo.config.BaseException;
import com.example.demo.src.post.model.GetPostsRes;
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;
import static com.example.demo.config.BaseResponseStatus.USERS_EMPTY_USER_ID;
@Service
public class PostProvider {
private final PostDao postDao;
private final JwtService jwtService;
final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
public PostProvider(PostDao postDao, JwtService jwtService) {
this.postDao = postDao;
this.jwtService = jwtService;
}
public List<GetPostsRes> retrievePosts(int userIdx) throws BaseException{
if(checkUserExist(userIdx)==0)
{
throw new BaseException(USERS_EMPTY_USER_ID);
}
try{
List<GetPostsRes> getPosts = postDao.selectPosts(userIdx);
return getPosts;
}
catch (Exception exception) {
throw new BaseException(DATABASE_ERROR);
}
}
public int checkUserExist(int userIdx) throws BaseException{
try{
return postDao.checkUserExist(userIdx);
} catch (Exception exception){
throw new BaseException(DATABASE_ERROR);
}
}
}
Dao
package com.example.demo.src.post;
import com.example.demo.src.post.model.*;
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 PostDao {
private JdbcTemplate jdbcTemplate;
private List<GetPostImgRes> getPostImgRes;
@Autowired
public void setDataSource(DataSource dataSource){
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public List<GetPostsRes> selectPosts(int userIdx){
String selectPostsQuery = "select p.postIdx as postIdx, u.userIdx as userIdx, u.nickName as nickName, u.profileImgUrl as profileImgUrl, p.content as content,\n" +
" if(postLikeCount is null, 0, postLikeCount) as postLikeCount,\n" +
" if(commentCount is null, 0, commentCount) as commentCount,\n" +
" case\n" +
" when timestampdiff(second, p.updatedAt, current_timestamp) < 60 then concat(timestampdiff(second,p.updatedAt, current_timestamp), '초 전')\n" +
" when timestampdiff(minute, p.updatedAt, current_timestamp) < 60 then concat(timestampdiff(minute,p.updatedAt, current_timestamp), '분 전')\n" +
" when timestampdiff(hour, p.updatedAt, current_timestamp) < 24 then concat(timestampdiff(hour,p.updatedAt, current_timestamp), '시간 전')\n" +
" when timestampdiff(day, p.updatedAt, current_timestamp) < 365 then concat(timestampdiff(day,p.updatedAt, current_timestamp), '일 전')\n" +
" else timestampdiff(year, p.updatedAt, current_timestamp)\n" +
" end as updatedAt,\n" +
" if(h.status = 'ACTIVE','Y','N') as likeOrNot\n" +
"from Post as p\n" +
" join User as u on u.userIdx = p.userIdx\n" +
" left join (select heartIdx, postIdx, status, count(heartIdx) as postLikeCount\n" +
" from Heart\n" +
" where status = 'ACTIVE'\n" +
" group by postIdx) as h on p.postIdx = h.postIdx\n" +
" left join (select postIdx, count(commendIdx) as commentCount\n" +
" from Comment\n" +
" where status = 'ACTIVE' ) as c on c.postIdx = p.postIdx\n" +
"where p.status = 'ACTIVE' and p.postIdx = ?";
int selectPostsParam = userIdx;
return this.jdbcTemplate.query(selectPostsQuery,
(rs,rowNum) -> new GetPostsRes(
rs.getInt("postIdx"),
rs.getInt("userIdx"),
rs.getString("nickname"),
rs.getString("profileImgUrl"),
rs.getString("content"),
rs.getInt("postLikeCount"),
rs.getInt("commentCount"),
rs.getString("updatedAt"),
rs.getString("likeOrNot"),
getPostImgRes = this.jdbcTemplate.query("select pi.postImgUrlidx, pi.imgUrl\n" +
"from PostImgUrl as pi\n" +
"join Post as p on p.postIdx = pi.postIdx\n" +
"where pi.status = 'ACTIVE' and p.postIdx = ?",
(rk, rownum) -> new GetPostImgRes(
rk.getInt("postImgUrlIdx"),
rk.getString("imgUrl")
), rs.getInt("postIdx")
)
),selectPostsParam);
}
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);
}
}
postman 응답 확인

>> api 명세서




'Server > UMC 2기 Server' 카테고리의 다른 글
[UMC] Server 10주차 Paging / Transaction / 로그인 / 쿠키세션 / jwt / OAuth (0) | 2022.05.26 |
---|---|
[UMC] Server 9주차 *POST*PATCH*DELETE* 게시물 생성 API / 게시물 수정 API / 게시물 삭제 API (0) | 2022.05.18 |
[UMC] Server 7주차 유저 삭제 API (0) | 2022.05.07 |
[UMC] Server 7주차 Springboot / 유저 조회 API / API 명세서 작성 (0) | 2022.05.05 |
[UMC] Server 7주차 Springboot 개발환경 구축하기 (0) | 2022.05.02 |