Skip to content

Commit

Permalink
Merge pull request #218 from Review-zip/dev
Browse files Browse the repository at this point in the history
[Dev] main merge
  • Loading branch information
hsuush authored Feb 15, 2024
2 parents b7c89ac + c16780b commit 7970626
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.ReviewZIP.domain.follow;

import com.example.ReviewZIP.domain.user.UsersService;
import com.example.ReviewZIP.global.response.ApiResponse;
import com.example.ReviewZIP.global.response.code.resultCode.SuccessStatus;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -9,6 +10,8 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -20,6 +23,7 @@
@RequestMapping("/v1/follows")
public class FollowsController {
private final FollowsService followsService;
private final UsersService usersService;

@PostMapping("/users/{userId}")
@Operation(summary = "์œ ์ € ํŒ”๋กœ์šฐ ํ•˜๊ธฐ API",description = "์œ ์ €์˜ id๋ฅผ ๋ฐ›์•„ ํ•ด๋‹น ์œ ์ € ํŒ”๋กœ์šฐ")
Expand All @@ -30,9 +34,9 @@ public class FollowsController {
@Parameters({
@Parameter(name = "userId", description = "ํŒ”๋กœ์šฐํ•  ์œ ์ €์˜ ์•„์ด๋””"),
})
public ApiResponse<SuccessStatus> follow(@PathVariable(name="userId") Long userId) {
public ApiResponse<SuccessStatus> follow(@AuthenticationPrincipal UserDetails user, @PathVariable(name="userId") Long userId) {

return followsService.createFollowing(userId);
return followsService.createFollowing(usersService.getUserId(user), userId);
}

@DeleteMapping("/users/{userId}")
Expand All @@ -44,8 +48,8 @@ public ApiResponse<SuccessStatus> follow(@PathVariable(name="userId") Long userI
@Parameters({
@Parameter(name = "userId", description = "ํŒ”๋กœ์šฐ ์ทจ์†Œํ•  ์œ ์ €์˜ ์•„์ด๋””"),
})
public ApiResponse<SuccessStatus> unfollowUser(@PathVariable(name="userId")Long userId){
public ApiResponse<SuccessStatus> unfollowUser(@AuthenticationPrincipal UserDetails user, @PathVariable(name="userId")Long userId){

return followsService.unfollowUser(userId);
return followsService.unfollowUser(usersService.getUserId(user), userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class FollowsService {


@Transactional
public ApiResponse<SuccessStatus> createFollowing(Long userId) {
Users sender = usersRepository.getById(1L);
public ApiResponse<SuccessStatus> createFollowing(Long myId, Long userId) {
Users sender = usersRepository.getById(myId);
Users receiver = usersRepository.findById(userId).orElseThrow(() -> new UsersHandler(ErrorStatus.USER_NOT_FOUND));
boolean alreadyFollow = followsRepository.existsBySenderAndReceiver(sender, receiver);
if(alreadyFollow)
Expand All @@ -32,8 +32,8 @@ public ApiResponse<SuccessStatus> createFollowing(Long userId) {
}

@Transactional
public ApiResponse<SuccessStatus> unfollowUser(Long userId){
Users sender = usersRepository.findById(1L).orElseThrow(()->new UsersHandler(ErrorStatus.USER_NOT_FOUND));
public ApiResponse<SuccessStatus> unfollowUser(Long myId, Long userId){
Users sender = usersRepository.getById(myId);
Users receiver = usersRepository.findById(userId).orElseThrow(()->new UsersHandler(ErrorStatus.USER_NOT_FOUND));
Follows unfollow = followsRepository.getBySenderAndReceiver(sender, receiver);
followsRepository.delete(unfollow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.ReviewZIP.domain.post.dto.response.PostResponseDto;
import com.example.ReviewZIP.domain.postHashtag.PostHashtags;
import com.example.ReviewZIP.domain.user.Users;
import com.example.ReviewZIP.domain.user.UsersService;
import com.example.ReviewZIP.global.response.ApiResponse;
import com.example.ReviewZIP.global.response.code.resultCode.SuccessStatus;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -13,6 +14,8 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -23,6 +26,7 @@
public class PostsController {

private final PostsService postsService;
private final UsersService usersService;

@GetMapping("/{postId}/hashtags")
@Operation(summary = "๊ฒŒ์‹œ๋ฌผ์— ํ•ด์‹œํƒœ๊ทธ ์ถ”๊ฐ€ API", description = "๊ฒŒ์‹œ๋ฌผ์— ํ•ด์‹œํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€")
Expand All @@ -48,9 +52,9 @@ public ApiResponse<SuccessStatus> addHashtags(@RequestParam String query, @PathV
@Parameters({
@Parameter(name = "hashtagId", description = "ํ•ด์‹œํƒœ๊ทธ ์•„์ด๋””"),
})
public ApiResponse<List<PostResponseDto.PostInfoDto>> searchPostsByHashtagId(@PathVariable Long hashtagId) {
public ApiResponse<List<PostResponseDto.PostInfoDto>> searchPostsByHashtagId(@AuthenticationPrincipal UserDetails user, @PathVariable Long hashtagId) {
List<PostHashtags> postList = postsService.searchPostByHashtag(hashtagId);
List<PostResponseDto.PostInfoDto> getPostInfoDtoList = postsService.getPostInfoDtoList(postList);
List<PostResponseDto.PostInfoDto> getPostInfoDtoList = postsService.getPostInfoDtoList(usersService.getUserId(user), postList);
return ApiResponse.onSuccess(getPostInfoDtoList);
}

Expand All @@ -64,9 +68,9 @@ public ApiResponse<List<PostResponseDto.PostInfoDto>> searchPostsByHashtagId(@Pa
@Parameters({
@Parameter(name = "postId", description = "๊ฒŒ์‹œ๊ธ€์˜ ์•„์ด๋””"),
})
public ApiResponse<PostResponseDto.PostInfoDto> getPostInfo(@PathVariable(name = "postId") Long postId){
public ApiResponse<PostResponseDto.PostInfoDto> getPostInfo(@AuthenticationPrincipal UserDetails user, @PathVariable(name = "postId") Long postId){

PostResponseDto.PostInfoDto postInfoDto = postsService.getPostInfoDto(postId);
PostResponseDto.PostInfoDto postInfoDto = postsService.getPostInfoDto(usersService.getUserId(user), postId);

return ApiResponse.onSuccess(postInfoDto);
}
Expand All @@ -88,8 +92,8 @@ public ApiResponse<PostResponseDto.CreatedPostResponseDto> createPost(@RequestBo
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, ์„ฑ๊ณต"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "POST405", description = "์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•˜์ง€ ์•Š์€ ๊ฒŒ์‹œ๊ธ€์ด ํ•„์š”ํ•จ.",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
})
public ApiResponse<PostResponseDto.PostInfoDto> getRandomPost() {
PostResponseDto.PostInfoDto randomPostInfoDto = postsService.getOneRandomPostInfoDto(1L);
public ApiResponse<PostResponseDto.PostInfoDto> getRandomPost(@AuthenticationPrincipal UserDetails user) {
PostResponseDto.PostInfoDto randomPostInfoDto = postsService.getOneRandomPostInfoDto(usersService.getUserId(user));

return ApiResponse.onSuccess(randomPostInfoDto);
}
Expand All @@ -100,8 +104,8 @@ public ApiResponse<PostResponseDto.PostInfoDto> getRandomPost() {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, ์„ฑ๊ณต"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "POST405", description = "์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•˜์ง€ ์•Š์€ ๊ฒŒ์‹œ๊ธ€์ด ํ•„์š”ํ•จ.",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
})
public ApiResponse<List<PostResponseDto.PostInfoDto>> getRandomPosts() {
List<PostResponseDto.PostInfoDto> randomPostInfoDtos = postsService.getThreeRandomPostsInfo(1L);
public ApiResponse<List<PostResponseDto.PostInfoDto>> getRandomPosts(@AuthenticationPrincipal UserDetails user) {
List<PostResponseDto.PostInfoDto> randomPostInfoDtos = postsService.getThreeRandomPostsInfo(usersService.getUserId(user));

return ApiResponse.onSuccess(randomPostInfoDtos);
}
Expand All @@ -114,9 +118,9 @@ public ApiResponse<List<PostResponseDto.PostInfoDto>> getRandomPosts() {
@Parameters({
@Parameter(name = "postId", description = "๊ฒŒ์‹œ๋ฌผ ์•„์ด๋””"),
})
public ApiResponse<List<PostResponseDto.PostUserLikeDto>> getPostLikeUserList(@PathVariable Long postId) {
public ApiResponse<List<PostResponseDto.PostUserLikeDto>> getPostLikeUserList(@AuthenticationPrincipal UserDetails user, @PathVariable Long postId) {
List<Users> postLikeUserList = postsService.getPostLikeUserList(postId);
List<Long> userFollowingList = postsService.getFollowigIdList();
List<Long> userFollowingList = postsService.getFollowigIdList(usersService.getUserId(user));

List<PostResponseDto.PostUserLikeDto> postLikeList = PostsConverter.toPostUserLikeListDto(postLikeUserList, userFollowingList);

Expand All @@ -132,8 +136,8 @@ public ApiResponse<List<PostResponseDto.PostUserLikeDto>> getPostLikeUserList(@P
@Parameters({
@Parameter(name = "postId", description = "๊ฒŒ์‹œ๊ธ€์˜ ์•„์ด๋””"),
})
public ApiResponse<SuccessStatus> deletePost(@PathVariable(name = "postId") Long postId){
postsService.deletePost(postId);
public ApiResponse<SuccessStatus> deletePost(@AuthenticationPrincipal UserDetails user, @PathVariable(name = "postId") Long postId){
postsService.deletePost(usersService.getUserId(user),postId);
return ApiResponse.onSuccess(SuccessStatus._OK);
}

Expand Down Expand Up @@ -168,8 +172,8 @@ public ApiResponse<SuccessStatus> removePostLike(@PathVariable Long postId) {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, ์„ฑ๊ณต"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "POST401", description = "ํ•ด๋‹นํ•˜๋Š” ๊ฒŒ์‹œ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
})
public ApiResponse<SuccessStatus> createScrabs(@PathVariable(name = "postid")Long postId){
return postsService.createScrabs(postId);
public ApiResponse<SuccessStatus> createScrabs(@AuthenticationPrincipal UserDetails user, @PathVariable(name = "postid")Long postId){
return postsService.createScrabs(usersService.getUserId(user),postId);
}

@DeleteMapping("/{postid}/scrabs")
Expand All @@ -178,8 +182,8 @@ public ApiResponse<SuccessStatus> createScrabs(@PathVariable(name = "postid")Lon
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, ์„ฑ๊ณต"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "POST401", description = "ํ•ด๋‹นํ•˜๋Š” ๊ฒŒ์‹œ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
})
public ApiResponse<SuccessStatus> deleteScrabs(@PathVariable(name = "postid")Long postId){
postsService.deleteScrabs(postId);
public ApiResponse<SuccessStatus> deleteScrabs(@AuthenticationPrincipal UserDetails user, @PathVariable(name = "postid")Long postId){
postsService.deleteScrabs(usersService.getUserId(user), postId);
return ApiResponse.onSuccess(SuccessStatus._OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

@Repository
public interface PostsRepository extends JpaRepository<Posts, Long> {

Optional<Posts> findByUserIdAndId(Long userId, Long postId);
@Query("SELECT count(p) FROM Posts p WHERE p.user != :user")
long countByUserNot(@Param("user") Users user);

Expand Down
32 changes: 14 additions & 18 deletions src/main/java/com/example/ReviewZIP/domain/post/PostsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Posts createPost(PostRequestDto postRequestDto) {

public static final int NUM_OF_RANDOM_POST = 1;
public PostResponseDto.PostInfoDto getOneRandomPostInfoDto(Long userId) {
Users user = usersRepository.getById(1L);
Users user = usersRepository.getById(userId);

long totalPostCount = postsRepository.countByUserNot(user);

Expand Down Expand Up @@ -106,7 +106,7 @@ public PostResponseDto.PostInfoDto getOneRandomPostInfoDto(Long userId) {

public static final int NUM_OF_RANDOM_POSTS = 3;
public List<PostResponseDto.PostInfoDto> getThreeRandomPostsInfo(Long userId) {
Users user = usersRepository.getById(1L);
Users user = usersRepository.getById(userId);

long totalPostCount = postsRepository.countByUserNot(user);

Expand Down Expand Up @@ -142,9 +142,8 @@ public List<PostResponseDto.PostInfoDto> getThreeRandomPostsInfo(Long userId) {
}

// ํŠน์ • ๊ฒŒ์‹œ๋ฌผ์˜ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
public PostResponseDto.PostInfoDto getPostInfoDto(Long postId){
// ์ข‹์•„์š”์™€ ์Šคํฌ๋žฉ ํ‘œ์‹œ๋ฅผ ์œ„ํ•˜์—ฌ 1L๋กœ ํ•ด๋‹น ์œ ์ €๋ฅผ ๋Œ€์ฒด
Users user = usersRepository.getById(1L);
public PostResponseDto.PostInfoDto getPostInfoDto(Long myId, Long postId){
Users user = usersRepository.getById(myId);
Posts post = postsRepository.findById(postId).orElseThrow(()->new PostsHandler(ErrorStatus.POST_NOT_FOUND));
boolean checkLike = postLikesRepository.existsByUserAndPost(user, post);
boolean checkScrab = scrabsRepository.existsByUserAndPost(user, post);
Expand All @@ -154,9 +153,9 @@ public PostResponseDto.PostInfoDto getPostInfoDto(Long postId){
return PostsConverter.toPostInfoResultDto(post, user, checkLike, checkScrab, createdAt);
}

List<PostResponseDto.PostInfoDto> getPostInfoDtoList(List<PostHashtags> postHashtagList){
List<PostResponseDto.PostInfoDto> getPostInfoDtoList(Long userId, List<PostHashtags> postHashtagList){
return postHashtagList.stream()
.map(postHashtag -> getPostInfoDto(postHashtag.getPost().getId()))
.map(postHashtag -> getPostInfoDto(userId, postHashtag.getPost().getId()))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -185,8 +184,8 @@ public String getCreatedAt(LocalDateTime createdAt){
}

@Transactional
public void deletePost(Long postId){
Posts post = postsRepository.findById(postId).orElseThrow(()-> new PostsHandler(ErrorStatus.POST_NOT_FOUND));
public void deletePost(Long userId, Long postId){
Posts post = postsRepository.findByUserIdAndId(userId, postId).orElseThrow(()-> new PostsHandler(ErrorStatus.POST_NOT_FOUND));

postsRepository.deleteById(post.getId());

Expand Down Expand Up @@ -221,9 +220,8 @@ public ApiResponse<SuccessStatus> addLike(Long postId) {
}


public List<Long> getFollowigIdList(){
// ์ผ๋‹จ 1L๋กœ ๋‚˜๋ฅผ ๋Œ€์ฒด
Users me = usersRepository.getById(1L);
public List<Long> getFollowigIdList(Long userId){
Users me = usersRepository.getById(userId);
List<Follows> followingList = me.getFollowingList();
List<Long> followingIdList = new ArrayList<>();

Expand All @@ -239,9 +237,8 @@ public List<Users> getPostLikeUserList(Long postId){
}

@Transactional
public ApiResponse<SuccessStatus> createScrabs(Long postId){
// userId๋Š” 1๋กœ ๋Œ€์ฒด
Users me = usersRepository.getById(1L);
public ApiResponse<SuccessStatus> createScrabs(Long userId, Long postId){
Users me = usersRepository.getById(userId);
Posts post = postsRepository.findById(postId).orElseThrow(()->new PostsHandler(ErrorStatus.POST_NOT_FOUND));
boolean alreadyScrab = scrabsRepository.existsByUserAndPost(me, post);
if(alreadyScrab){
Expand All @@ -257,9 +254,8 @@ public ApiResponse<SuccessStatus> createScrabs(Long postId){
}

@Transactional
public void deleteScrabs(Long postId){
// userId๋Š” 1๋กœ ๋Œ€์ฒด
Users me = usersRepository.getById(1L);
public void deleteScrabs(Long userId, Long postId){
Users me = usersRepository.getById(userId);
Posts post = postsRepository.findById(postId).orElseThrow(()->new PostsHandler(ErrorStatus.POST_NOT_FOUND));

Scrabs scrabs = scrabsRepository.findByUserAndPost(me, post);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.ReviewZIP.domain.searchHistory;

import com.example.ReviewZIP.domain.user.UsersService;
import com.example.ReviewZIP.global.response.ApiResponse;
import com.example.ReviewZIP.global.response.code.resultCode.SuccessStatus;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -9,13 +10,17 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/history")
public class SearchHistoriesController {
private final SearchHistoriesService searchHistoriesService;
private final UsersService usersService;

@PostMapping("/users/{userId}")
@GetMapping("/search/name")
@Operation(summary = "์œ ์ € ๊ฒ€์ƒ‰ ์ €์žฅํ•˜๊ธฐ API",description = "์œ ์ €์˜ id๋ฅผ ๋ฐ›์•„ ํ•ด๋‹น ์œ ์ € ๊ฒ€์ƒ‰ ๊ธฐ๋ก์„ ์ €์žฅ")
Expand All @@ -26,9 +31,8 @@ public class SearchHistoriesController {
@Parameters({
@Parameter(name = "userId", description = "์œ ์ €์˜ ์•„์ด๋””"),
})
public ApiResponse<SuccessStatus> saveUserSearchHistory(@PathVariable(name = "userId")Long userId){
// ๋‚˜๋Š” 1L๋กœ ์„ค์ •
searchHistoriesService.saveUserSearchHistory(1L, userId);
public ApiResponse<SuccessStatus> saveUserSearchHistory(@AuthenticationPrincipal UserDetails user, @PathVariable(name = "userId")Long userId){
searchHistoriesService.saveUserSearchHistory(usersService.getUserId(user), userId);

return ApiResponse.onSuccess(SuccessStatus._OK);
}
Expand All @@ -41,9 +45,8 @@ public ApiResponse<SuccessStatus> saveUserSearchHistory(@PathVariable(name = "us
@Parameters({
@Parameter(name = "hashtag", description = "ํ•ด์‹œํƒœ๊ทธ"),
})
public ApiResponse<SuccessStatus> saveHashtagSearchHistory(@RequestParam(name = "hashtag")String hashtag){
// ๋‚˜๋Š” 1L๋กœ ์„ค์ •
searchHistoriesService.saveHashtagSearchHistory(1L, hashtag);
public ApiResponse<SuccessStatus> saveHashtagSearchHistory(@AuthenticationPrincipal UserDetails user, @RequestParam(name = "hashtag")String hashtag){
searchHistoriesService.saveHashtagSearchHistory(usersService.getUserId(user), hashtag);

return ApiResponse.onSuccess(SuccessStatus._OK);
}
Expand All @@ -54,8 +57,8 @@ public ApiResponse<SuccessStatus> saveHashtagSearchHistory(@RequestParam(name =
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, ์„ฑ๊ณต"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "HISTORY401", description = "ํ•ด๋‹น ๊ฒ€์ƒ‰๊ธฐ๋ก์„ ์ฐพ์„ ์ˆ˜ ์—†์Œ",content = @Content(schema = @Schema(implementation = ApiResponse.class))),
})
public ApiResponse<SuccessStatus> deleteHistory(@PathVariable(name = "historyId")Long historyId){
searchHistoriesService.deleteUserSearchHistory(historyId);
public ApiResponse<SuccessStatus> deleteHistory(@AuthenticationPrincipal UserDetails user,@PathVariable(name = "historyId")Long historyId){
searchHistoriesService.deleteUserSearchHistory(usersService.getUserId(user), historyId);

return ApiResponse.onSuccess(SuccessStatus._OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

@Repository
public interface SearchHistoriesRepository extends JpaRepository<SearchHistories, Long> {
Optional<SearchHistories> findByIdAndUserId(Long hashtagId, Long userId);
}
Loading

0 comments on commit 7970626

Please sign in to comment.