-
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.
- Loading branch information
Showing
19 changed files
with
1,045 additions
and
8 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
50 changes: 50 additions & 0 deletions
50
src/main/java/com/splanet/splanet/friend/controller/FriendApi.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,50 @@ | ||
package com.splanet.splanet.friend.controller; | ||
|
||
import com.splanet.splanet.friend.dto.FriendResponse; | ||
import com.splanet.splanet.plan.dto.PlanResponseDto; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
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.RequestMapping; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RequestMapping("/api/friends") | ||
@Tag(name = "Friend", description = "μΉκ΅¬ κ΄λ ¨ API") | ||
public interface FriendApi { | ||
|
||
@GetMapping | ||
@Operation(summary = "μΉκ΅¬ λͺ©λ‘ μ‘°ν", description = "μ¬μ©μμ μΉκ΅¬ λͺ©λ‘μ μ‘°νν©λλ€.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "μΉκ΅¬ λͺ©λ‘μ΄ μ±κ³΅μ μΌλ‘ μ‘°νλμμ΅λλ€."), | ||
@ApiResponse(responseCode = "401", description = "μΈμ¦λμ§ μμ μ¬μ©μμ λλ€.") | ||
}) | ||
ResponseEntity<List<FriendResponse>> getFriends( | ||
@Parameter(description = "JWT μΈμ¦μΌλ‘ μ λ¬λ μ¬μ©μ ID", required = true) @AuthenticationPrincipal Long userId); | ||
|
||
@GetMapping("/{friendId}/plans") | ||
@Operation(summary = "μΉκ΅¬ νλ μ‘°ν", description = "μΉκ΅¬μ 곡κ°λ νλ λͺ©λ‘μ μ‘°νν©λλ€.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "μΉκ΅¬μ 곡κ°λ κ³νμ΄ μ±κ³΅μ μΌλ‘ μ‘°νλμμ΅λλ€."), | ||
@ApiResponse(responseCode = "400", description = "μλͺ»λ μμ²μ λλ€ (μ ν¨νμ§ μμ μΉκ΅¬ ID)."), | ||
@ApiResponse(responseCode = "401", description = "μΈμ¦λμ§ μμ μ¬μ©μμ λλ€."), | ||
@ApiResponse(responseCode = "404", description = "μΉκ΅¬λ₯Ό μ°Ύμ μ μμ΅λλ€.") | ||
}) | ||
ResponseEntity<List<PlanResponseDto>> getFriendPlan( | ||
@Parameter(description = "μ‘°νν μΉκ΅¬ ID", required = true) @PathVariable("friendId") Long friendId, | ||
@Parameter(description = "JWT μΈμ¦μΌλ‘ μ λ¬λ μ¬μ©μ ID", required = true) @AuthenticationPrincipal Long userId); | ||
|
||
@DeleteMapping("/{friendId}") | ||
@Operation(summary = "μΉκ΅¬ μμ νκΈ°", description = "μΉκ΅¬ λͺ©λ‘μμ μμ ν©λλ€.") | ||
ResponseEntity<Map<String, String>> unfriend( | ||
@Parameter(description = "μμ ν μΉκ΅¬ ID", required = true) @PathVariable("friendId") Long friendId, | ||
@Parameter(description = "JWT μΈμ¦μΌλ‘ μ λ¬λ μ¬μ©μ ID", required = true) @AuthenticationPrincipal Long userId); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/splanet/splanet/friend/controller/FriendController.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,45 @@ | ||
package com.splanet.splanet.friend.controller; | ||
|
||
import com.splanet.splanet.friend.dto.FriendResponse; | ||
import com.splanet.splanet.friend.service.FriendService; | ||
import com.splanet.splanet.plan.dto.PlanResponseDto; | ||
import com.splanet.splanet.plan.repository.PlanRepository; | ||
import com.splanet.splanet.user.repository.UserRepository; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
public class FriendController implements FriendApi { | ||
|
||
private final FriendService friendService; | ||
|
||
public FriendController(FriendService friendService) { | ||
this.friendService = friendService;; | ||
} | ||
|
||
@Override | ||
public ResponseEntity<List<FriendResponse>> getFriends(Long userId) { | ||
List<FriendResponse> friends = friendService.getFriends(userId); | ||
return ResponseEntity.ok(friends); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<List<PlanResponseDto>> getFriendPlan( | ||
@PathVariable Long friendId, | ||
@AuthenticationPrincipal Long userId) { | ||
return friendService.getFriendPlan(friendId, userId); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<Map<String, String>> unfriend( | ||
@PathVariable Long friendId, | ||
@AuthenticationPrincipal Long userId) { | ||
ResponseEntity<Map<String, String>> responseEntity = friendService.unfriend(friendId, userId); | ||
return responseEntity; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/splanet/splanet/friend/dto/FriendResponse.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,4 @@ | ||
package com.splanet.splanet.friend.dto; | ||
|
||
public record FriendResponse(Long userId, String nickname, String profileImage) { | ||
} |
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
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
103 changes: 103 additions & 0 deletions
103
src/main/java/com/splanet/splanet/friend/service/FriendService.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,103 @@ | ||
package com.splanet.splanet.friend.service; | ||
|
||
import com.splanet.splanet.core.exception.BusinessException; | ||
import com.splanet.splanet.core.exception.ErrorCode; | ||
import com.splanet.splanet.friend.dto.FriendResponse; | ||
import com.splanet.splanet.friend.entity.Friend; | ||
import com.splanet.splanet.friend.repository.FriendRepository; | ||
import com.splanet.splanet.friendRequest.entity.FriendRequest; | ||
import com.splanet.splanet.friendRequest.repository.FriendRequestRepository; | ||
import com.splanet.splanet.plan.dto.PlanResponseDto; | ||
import com.splanet.splanet.plan.entity.Plan; | ||
import com.splanet.splanet.plan.repository.PlanRepository; | ||
import com.splanet.splanet.user.entity.User; | ||
import com.splanet.splanet.user.repository.UserRepository; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class FriendService { | ||
|
||
private final FriendRepository friendRepository; | ||
private final PlanRepository planRepository; | ||
private final FriendRequestRepository friendRequestRepository; | ||
|
||
public FriendService(FriendRepository friendRepository, PlanRepository planRepository, FriendRequestRepository friendRequestRepository) { | ||
this.friendRepository = friendRepository; | ||
this.planRepository = planRepository; | ||
this.friendRequestRepository = friendRequestRepository; | ||
} | ||
|
||
// μΉκ΅¬ λͺ©λ‘ μ‘°ν | ||
public List<FriendResponse> getFriends(Long userId) { | ||
List<Friend> friends = friendRepository.findByUserId(userId); | ||
|
||
// Friend μν°ν°λ₯Ό FriendResponse DTOλ‘ λ³ν | ||
return friends.stream() | ||
.map(friend -> { | ||
User friendUser = friend.getFriend(); | ||
return new FriendResponse( | ||
friendUser.getId(), | ||
friendUser.getNickname(), | ||
friendUser.getProfileImage() | ||
); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// μΉκ΅¬μ κ³΅κ° νλ μ‘°ν | ||
public ResponseEntity<List<PlanResponseDto>> getFriendPlan(Long friendId, Long userId) { | ||
// μΉκ΅¬ λͺ©λ‘μ friendIdκ° μλμ§ νμΈ | ||
boolean isFriend = friendRepository.existsByUserIdAndFriendId(userId, friendId); | ||
|
||
if (!isFriend) { | ||
throw new BusinessException(ErrorCode.FRIEND_NOT_FOUND); | ||
} | ||
|
||
List<Plan> publicPlans = planRepository.findAllByUserIdAndAccessibility(friendId, true); | ||
|
||
// 곡κ°λ νλμ΄ μμ κ²½μ°, λΉ λͺ©λ‘ λ°ν | ||
if (publicPlans.isEmpty()) { | ||
return ResponseEntity.ok(Collections.emptyList()); | ||
} | ||
|
||
List<PlanResponseDto> planResponseDtos = publicPlans.stream() | ||
.map(plan -> PlanResponseDto.builder() | ||
.id(plan.getId()) | ||
.title(plan.getTitle()) | ||
.description(plan.getDescription()) | ||
.startDate(plan.getStartDate()) | ||
.endDate(plan.getEndDate()) | ||
.accessibility(plan.getAccessibility()) | ||
.isCompleted(plan.getIsCompleted()) | ||
.createdAt(plan.getCreatedAt()) | ||
.updatedAt(plan.getUpdatedAt()) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
|
||
return ResponseEntity.ok(planResponseDtos); | ||
} | ||
|
||
// μΉκ΅¬ μμ (μ·¨μ)νκΈ° | ||
@Transactional | ||
public ResponseEntity<Map<String, String>> unfriend(Long friendId, Long userId) { | ||
if (!friendRepository.existsByUserIdAndFriendId(userId, friendId)) { | ||
throw new BusinessException(ErrorCode.FRIEND_NOT_FOUND); | ||
} | ||
|
||
friendRepository.deleteByRequesterIdAndReceiverId(userId, friendId); | ||
|
||
List<FriendRequest> pendingRequests = friendRequestRepository.findPendingRequestsByReceiverId(userId, friendId, FriendRequest.Status.PENDING); | ||
for (FriendRequest request : pendingRequests) { | ||
friendRequestRepository.delete(request); | ||
} | ||
|
||
return ResponseEntity.ok(Map.of("message", "μΉκ΅¬ λ§ΊκΈ° μ·¨μλμμ΅λλ€!")); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/splanet/splanet/friendRequest/controller/FriendRequestApi.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,72 @@ | ||
package com.splanet.splanet.friendRequest.controller; | ||
|
||
import com.splanet.splanet.friendRequest.dto.FriendRequestCreateRequest; | ||
import com.splanet.splanet.friendRequest.dto.ReceivedFriendRequestResponse; | ||
import com.splanet.splanet.friendRequest.dto.SentFriendRequestResponse; | ||
import com.splanet.splanet.friendRequest.dto.SuccessResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RequestMapping("/api/friends/requests") | ||
@Tag(name = "FriendRequest", description = "μΉκ΅¬ μμ² κ΄λ ¨ API") | ||
public interface FriendRequestApi { | ||
|
||
@PostMapping | ||
@Operation(summary = "μΉκ΅¬ μμ²", description = "νΉμ μ¬μ©μμκ² μΉκ΅¬ μμ²μ 보λ λλ€.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "μΉκ΅¬ μμ²μ΄ μ±κ³΅μ μΌλ‘ μ μ‘λμμ΅λλ€."), | ||
@ApiResponse(responseCode = "400", description = "μλͺ»λ μμ²μ λλ€ (μ ν¨νμ§ μμ μ μ ID)."), | ||
@ApiResponse(responseCode = "401", description = "μΈμ¦λμ§ μμ μ¬μ©μμ λλ€.") | ||
}) | ||
ResponseEntity<SuccessResponse> sendFriendRequest( | ||
@Parameter(description = "μΉκ΅¬ μμ²μ λ³΄λΌ μ¬μ©μ ID", required = true) @AuthenticationPrincipal Long userId, | ||
@RequestBody FriendRequestCreateRequest request); | ||
|
||
@PostMapping("/{requestId}/accept") | ||
@Operation(summary = "μΉκ΅¬ μμ² μλ½", description = "μΉκ΅¬ μμ² μλ½") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "μΉκ΅¬ μμ² μ±κ³΅μ μΌλ‘ μλ½λμμ΅λλ€."), | ||
@ApiResponse(responseCode = "400", description = "μλͺ»λ μμ²μ λλ€ (μ ν¨νμ§ μμ μ μ ID)."), | ||
@ApiResponse(responseCode = "401", description = "μΈμ¦λμ§ μμ μ¬μ©μμ λλ€."), | ||
@ApiResponse(responseCode = "404", description = "μΉκ΅¬ μμ²μ μ°Ύμ μ μμ΅λλ€.") | ||
}) | ||
ResponseEntity<ReceivedFriendRequestResponse> acceptFriendRequest(@AuthenticationPrincipal Long userId, | ||
@Parameter(description = "μΉκ΅¬ μμ² ID", required = true) @PathVariable Long requestId); | ||
|
||
@PostMapping("/{requestId}/reject") | ||
@Operation(summary = "μΉκ΅¬ μμ² κ±°μ ", description = "μΉκ΅¬ μμ² κ±°μ ") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "μΉκ΅¬ μμ²μ΄ μ±κ³΅μ μΌλ‘ κ±°μ λμμ΅λλ€."), | ||
@ApiResponse(responseCode = "401", description = "μΈμ¦λμ§ μμ μ¬μ©μμ λλ€."), | ||
@ApiResponse(responseCode = "404", description = "μΉκ΅¬ μμ²μ μ°Ύμ μ μμ΅λλ€."), | ||
@ApiResponse(responseCode = "404", description = "μΉκ΅¬ μμ²μ μ°Ύμ μ μμ΅λλ€.") | ||
}) | ||
ResponseEntity<ReceivedFriendRequestResponse> rejectFriendRequest( | ||
@Parameter(description = "μΉκ΅¬ μμ² ID", required = true) @PathVariable Long requestId, @AuthenticationPrincipal Long userId); | ||
|
||
@GetMapping("/received") | ||
@Operation(summary = "μΉκ΅¬ μμ² λͺ©λ‘ μ‘°ν (λ°μ μμ²)", description = "μ¬μ©μκ° λ°μ μΉκ΅¬ μμ² λͺ©λ‘μ μ‘°νν©λλ€.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "λ°μ μΉκ΅¬ μμ² λͺ©λ‘μ΄ μ±κ³΅μ μΌλ‘ μ‘°νλμμ΅λλ€."), | ||
@ApiResponse(responseCode = "401", description = "μΈμ¦λμ§ μμ μ¬μ©μμ λλ€.") | ||
}) | ||
ResponseEntity<List<ReceivedFriendRequestResponse>> getReceivedRequests( | ||
@Parameter(description = "JWT μΈμ¦μΌλ‘ μ λ¬λ μ¬μ©μ ID", required = true) @AuthenticationPrincipal Long userId); | ||
|
||
@GetMapping("/sent") | ||
@Operation(summary = "μΉκ΅¬ μμ² λͺ©λ‘ μ‘°ν (λ³΄λΈ μμ²)", description = "μ¬μ©μκ° λ³΄λΈ μΉκ΅¬ μμ² λͺ©λ‘μ μ‘°νν©λλ€.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "λ³΄λΈ μΉκ΅¬ μμ² λͺ©λ‘μ΄ μ±κ³΅μ μΌλ‘ μ‘°νλμμ΅λλ€."), | ||
@ApiResponse(responseCode = "401", description = "μΈμ¦λμ§ μμ μ¬μ©μμ λλ€.") | ||
}) | ||
ResponseEntity<List<SentFriendRequestResponse>> getSentRequests( | ||
@Parameter(description = "JWT μΈμ¦μΌλ‘ μ λ¬λ μ¬μ©μ ID", required = true) @AuthenticationPrincipal Long userId); | ||
} |
Oops, something went wrong.