Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 친구 추가/좋아요 API 구현 #35

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.potatocake.everymoment.controller;

import com.potatocake.everymoment.dto.SuccessResponse;
import com.potatocake.everymoment.dto.response.FriendRequestPageRequest;
import com.potatocake.everymoment.exception.ErrorCode;
import com.potatocake.everymoment.exception.GlobalException;
import com.potatocake.everymoment.security.MemberDetails;
import com.potatocake.everymoment.service.FriendRequestService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
public class FriendRequestController {

private final FriendRequestService friendRequestService;

@GetMapping("/api/friend-requests")
public ResponseEntity<SuccessResponse<FriendRequestPageRequest>> getFriendRequests(
@RequestParam(required = false) Long key,
@RequestParam(defaultValue = "10") int size,
@AuthenticationPrincipal MemberDetails memberDetails) {
FriendRequestPageRequest friendRequests = friendRequestService.getFriendRequests(key, size,
memberDetails.getId());

return ResponseEntity.ok()
.body(SuccessResponse.ok(friendRequests));
}

@PostMapping("/api/members/{memberId}/friend-requests")
public ResponseEntity<SuccessResponse> sendFriendRequest(@PathVariable Long memberId,
@AuthenticationPrincipal MemberDetails memberDetails) {
if (memberDetails.getId().equals(memberId)) {
throw new GlobalException(ErrorCode.SELF_FRIEND_REQUEST);
}

friendRequestService.sendFriendRequest(memberDetails.getId(), memberId);

return ResponseEntity.ok()
.body(SuccessResponse.ok());
}

@PostMapping("/api/friend-requests/{requestId}/accept")
public ResponseEntity<SuccessResponse> acceptFriendRequest(@PathVariable Long requestId,
@AuthenticationPrincipal MemberDetails memberDetails) {
friendRequestService.acceptFriendRequest(requestId, memberDetails.getId());

return ResponseEntity.ok()
.body(SuccessResponse.ok());
}

@DeleteMapping("/api/friend-requests/{requestId}/reject")
public ResponseEntity<SuccessResponse> rejectFriendRequest(@PathVariable Long requestId,
@AuthenticationPrincipal MemberDetails memberDetails) {
friendRequestService.rejectFriendRequest(requestId, memberDetails.getId());

return ResponseEntity.ok()
.body(SuccessResponse.ok());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.potatocake.everymoment.controller;

import com.potatocake.everymoment.dto.SuccessResponse;
import com.potatocake.everymoment.dto.response.LikeCountResponse;
import com.potatocake.everymoment.security.MemberDetails;
import com.potatocake.everymoment.service.LikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RequestMapping("/api/diaries/{diaryId}/likes")
@RestController
public class LikeController {

private final LikeService likeService;

@GetMapping
public ResponseEntity<SuccessResponse<LikeCountResponse>> getLikeCount(@PathVariable Long diaryId) {
LikeCountResponse likeCount = likeService.getLikeCount(diaryId);

return ResponseEntity.ok()
.body(SuccessResponse.ok(likeCount));
}

@PostMapping
public ResponseEntity<SuccessResponse> toggleLike(@PathVariable Long diaryId,
@AuthenticationPrincipal MemberDetails memberDetails) {
likeService.toggleLike(memberDetails.getId(), diaryId);

return ResponseEntity.ok()
.body(SuccessResponse.ok());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.potatocake.everymoment.dto.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Builder
@Getter
public class FriendRequestPageRequest {

private List<FriendRequestResponse> friendRequests;
private Long next;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.potatocake.everymoment.dto.response;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class FriendRequestResponse {

private Long id;
private Long senderId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.potatocake.everymoment.dto.response;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class LikeCountResponse {

private Long likeCount;

}
36 changes: 36 additions & 0 deletions src/main/java/com/potatocake/everymoment/entity/FriendRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.potatocake.everymoment.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class FriendRequest {

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
private Member sender;

@ManyToOne(fetch = FetchType.LAZY)
private Member receiver;

@Builder
public FriendRequest(Long id, Member sender, Member receiver) {
this.id = id;
this.sender = sender;
this.receiver = receiver;
}

}
36 changes: 36 additions & 0 deletions src/main/java/com/potatocake/everymoment/entity/Like.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.potatocake.everymoment.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Table(name = "likes")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class Like {

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
private Diary diary;

@Builder
public Like(Long id, Member member, Diary diary) {
this.id = id;
this.member = member;
this.diary = diary;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public enum ErrorCode {
METHOD_NOT_ALLOWED("지원하지 않는 HTTP 메소드입니다.", HttpStatus.METHOD_NOT_ALLOWED),

/* CategoryService */
CATEGORY_NOT_OWNER("본인의 카테고리만 수정할 수 있습니다.", HttpStatus.FORBIDDEN);
CATEGORY_NOT_OWNER("본인의 카테고리만 수정할 수 있습니다.", HttpStatus.FORBIDDEN),

/* FriendRequestController */
SELF_FRIEND_REQUEST("자기 자신에게 친구 요청을 보낼 수 없습니다.", BAD_REQUEST),

/* FriendRequestService */
FRIEND_REQUEST_ALREADY_EXISTS("이미 친구 요청을 보냈습니다.", CONFLICT),
FRIEND_REQUEST_NOT_FOUND("존재하지 않는 친구 요청입니다.", NOT_FOUND);

private final String message;
private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface FriendRepository extends JpaRepository<Friend, Long>, JpaSpecificationExecutor<Friend> {

Optional<Friend> findByMemberIdAndFriendId(Member member, Member friend);

List<Member> findAllFriendIdsByMemberId(Member member);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.potatocake.everymoment.repository;

import com.potatocake.everymoment.entity.FriendRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FriendRequestRepository extends JpaRepository<FriendRequest, Long> {

boolean existsBySenderIdAndReceiverId(Long senderId, Long receiverId);

Window<FriendRequest> findByReceiverId(Long receiverId, ScrollPosition scrollPosition, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.potatocake.everymoment.repository;

import com.potatocake.everymoment.entity.Diary;
import com.potatocake.everymoment.entity.Like;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LikeRepository extends JpaRepository<Like, Long> {

Optional<Like> findByMemberIdAndDiaryId(Long memberId, Long diaryId);

Long countByDiary(Diary diary);

}
Loading
Loading