Skip to content

Commit

Permalink
refactor: subscription api 수정사항
Browse files Browse the repository at this point in the history
  • Loading branch information
ez23re committed Oct 3, 2024
1 parent b677434 commit 6002816
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 212 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.splanet.splanet.subscription.controller;

import com.splanet.splanet.subscription.dto.SubscriptionDto;
import com.splanet.splanet.subscription.entity.Subscription;
import com.splanet.splanet.subscription.dto.SubscriptionRequest;
import com.splanet.splanet.subscription.dto.SubscriptionResponse;
import com.splanet.splanet.subscription.service.SubscriptionService;
import com.splanet.splanet.core.exception.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -10,9 +10,7 @@
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.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

Expand All @@ -32,15 +30,15 @@ public SubscriptionController(SubscriptionService subscriptionService) {
@ApiResponses(value = {
@ApiResponse(responseCode = "200",
description = "구독 정보가 성공적으로 조회되었습니다.",
content = @Content(schema = @Schema(implementation = SubscriptionDto.class))),
content = @Content(schema = @Schema(implementation = SubscriptionResponse.class))),
@ApiResponse(responseCode = "401",
description = "인증되지 않은 사용자입니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404",
description = "활성화된 구독을 찾을 수 없습니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
public ResponseEntity<SubscriptionDto> getSubscription(@AuthenticationPrincipal Long userId) {
public ResponseEntity<SubscriptionResponse> getSubscription(@AuthenticationPrincipal Long userId) {
return subscriptionService.getSubscription(userId);
}

Expand All @@ -58,11 +56,12 @@ public ResponseEntity<String> cancelSubscription(@AuthenticationPrincipal Long u
@PostMapping("/payment")
@Operation(summary = "구독", description = "사용자가 구독을 구매합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "구독이 성공적으로 구매되었습니다.")
@ApiResponse(responseCode = "200", description = "구독이 성공적으로 구매되었습니다.",
content = @Content(schema = @Schema(implementation = SubscriptionResponse.class)))
})
public ResponseEntity<SubscriptionDto> subscribe(
public ResponseEntity<SubscriptionResponse> subscribe(
@AuthenticationPrincipal Long userId,
@RequestParam Subscription.Type type) {
return subscriptionService.subscribe(userId, type);
@RequestBody SubscriptionRequest request) {
return subscriptionService.subscribe(userId, request);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.splanet.splanet.subscription.dto;

import lombok.Getter;
import lombok.Setter;
import com.splanet.splanet.subscription.entity.Subscription;

@Getter
@Setter
public class SubscriptionRequest {
private Subscription.Type type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.splanet.splanet.subscription.dto;

import lombok.Getter;
import lombok.Setter;
import com.splanet.splanet.subscription.entity.Subscription;

@Getter
@Setter
public class SubscriptionResponse {
private String message;
private SubscriptionDetails subscription;

public SubscriptionResponse(String message, Subscription subscription) {
this.message = message;
this.subscription = new SubscriptionDetails(subscription);
}

@Getter
@Setter
public static class SubscriptionDetails {
private Long id;
private String startDate;
private String endDate;

public SubscriptionDetails(Subscription subscription) {
this.id = subscription.getId();
this.startDate = subscription.getStartDate().toString();
this.endDate = subscription.getEndDate().toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.splanet.splanet.subscription.service;

import com.splanet.splanet.subscription.dao.SubscriptionDao;
import com.splanet.splanet.subscription.dto.SubscriptionDto;
import com.splanet.splanet.subscription.dto.SubscriptionRequest;
import com.splanet.splanet.subscription.dto.SubscriptionResponse;
import com.splanet.splanet.subscription.entity.Subscription;
import com.splanet.splanet.subscription.repository.SubscriptionRepository;
import com.splanet.splanet.core.exception.BusinessException;
import com.splanet.splanet.core.exception.ErrorCode;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,25 +12,24 @@
@Service
public class SubscriptionService {

private final SubscriptionDao subscriptionDao;
private final SubscriptionRepository subscriptionRepository;

public SubscriptionService(SubscriptionDao subscriptionDao) {
this.subscriptionDao = subscriptionDao;
public SubscriptionService(SubscriptionRepository subscriptionRepository) {
this.subscriptionRepository = subscriptionRepository;
}

public ResponseEntity<SubscriptionDto> getSubscription(Long userId) {
// 구독 조회
public ResponseEntity<SubscriptionResponse> getSubscription(Long userId) {
if (userId == null) {
throw new BusinessException(ErrorCode.UNAUTHORIZED);
}

// 가장 최근의 ACTIVE 구독 조회
Subscription subscription = subscriptionDao.findLatestActiveSubscription(userId)
// 가장 최근 ACTIVE 구독
Subscription subscription = subscriptionRepository.findTopByUserIdAndStatusOrderByStartDateDesc(userId, Subscription.Status.ACTIVE)
.orElseThrow(() -> new BusinessException(ErrorCode.SUBSCRIPSTION_NOT_FOUND));

SubscriptionDto responseDto = SubscriptionDto.fromSubscription(subscription);
return ResponseEntity.ok()
.header("Message", "구독 정보가 성공적으로 조회되었습니다.")
.body(responseDto);
SubscriptionResponse response = new SubscriptionResponse("구독 정보가 성공적으로 조회되었습니다.", subscription);
return ResponseEntity.ok(response);
}

// 구독 취소
Expand All @@ -38,34 +38,38 @@ public ResponseEntity<String> cancelSubscription(Long userId) {
throw new BusinessException(ErrorCode.UNAUTHORIZED);
}

Subscription subscription = subscriptionDao.findLatestActiveSubscription(userId)
Subscription subscription = subscriptionRepository.findTopByUserIdAndStatusOrderByStartDateDesc(userId, Subscription.Status.ACTIVE)
.orElseThrow(() -> new BusinessException(ErrorCode.SUBSCRIPSTION_NOT_FOUND));

if (subscription.getStatus() == Subscription.Status.CANCELED) {
throw new BusinessException(ErrorCode.ALREADY_CANCELED);
}

subscriptionDao.cancelSubscription(subscription);
return ResponseEntity.ok("구독이 성공적으로 취소되었습니다.");
subscription.cancel();
subscriptionRepository.save(subscription);
return ResponseEntity.ok("{\"message\": \"구독이 성공적으로 취소되었습니다.\"}");
}

// 구독하기
public ResponseEntity<SubscriptionDto> subscribe(Long userId, Subscription.Type type) {
public ResponseEntity<SubscriptionResponse> subscribe(Long userId, SubscriptionRequest request) {
// 구독 객체 생성
Subscription subscription = Subscription.builder()
Subscription subscription = createSubscription(userId, request.getType());
Subscription savedSubscription = subscriptionRepository.save(subscription);
return createSubscriptionResponse(savedSubscription);
}

private Subscription createSubscription(Long userId, Subscription.Type type) {
return Subscription.builder()
.userId(userId)
.type(type)
.status(Subscription.Status.ACTIVE)
.startDate(java.time.LocalDateTime.now())
.endDate(java.time.LocalDateTime.now().plusMonths(type == Subscription.Type.MONTHLY ? 1 : 12))
.build();
}

Subscription savedSubscription = subscriptionDao.saveSubscription(subscription);

SubscriptionDto responseDto = SubscriptionDto.fromSubscription(savedSubscription);

return ResponseEntity.ok()
.header("Message", "구독이 성공적으로 구매되었습니다.")
.body(responseDto);
private ResponseEntity<SubscriptionResponse> createSubscriptionResponse(Subscription savedSubscription) {
SubscriptionResponse response = new SubscriptionResponse("구독이 성공적으로 구매되었습니다.", savedSubscription);
return ResponseEntity.ok(response);
}
}
Loading

0 comments on commit 6002816

Please sign in to comment.