Skip to content
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
@@ -1,15 +1,13 @@
package com.project.core.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.project.core.controller.dto.request.FindCustomerRequest;
import com.project.core.infra.entity.customer.Customer;
import com.project.core.service.CustomerService;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
Expand All @@ -19,8 +17,7 @@ public class CustomerController {

@GetMapping
public Long save(@RequestBody FindCustomerRequest request) {
Customer customer = customerService.loadByContactEnc(request.contactEnc());
return customer.getCustomerId();
Customer customer = customerService.loadByContactEnc(request.contactEnc());
return customer.getCustomerId();
}

}
32 changes: 16 additions & 16 deletions src/main/java/com/project/core/controller/PlanController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
@RequestMapping("/plan")
public class PlanController {

private final PlanService planService;
private final PlanService planService;

@PostMapping("/join")
public ResponseEntity<Void> joinSubscription(@RequestBody SubscriptionJoinRequest request) {
planService.joinSubscription(request.customerId(), request.planId());
return ResponseEntity.ok().build();
}
@PostMapping("/join")
public ResponseEntity<Void> joinSubscription(@RequestBody SubscriptionJoinRequest request) {
planService.joinSubscription(request.customerId(), request.planId());
return ResponseEntity.ok().build();
}

@PostMapping("/change")
public ResponseEntity<Void> changePlan(@RequestBody PlanChangeRequest request) {
planService.changePlan(request.subId(), request.planId());
return ResponseEntity.ok().build();
}
@PostMapping("/change")
public ResponseEntity<Void> changePlan(@RequestBody PlanChangeRequest request) {
planService.changePlan(request.subId(), request.planId());
return ResponseEntity.ok().build();
}

@PostMapping("/{subId}/terminate")
public ResponseEntity<Void> terminateSubscription(@PathVariable Long subId) {
planService.terminateSubscription(subId);
return ResponseEntity.ok().build();
}
@PostMapping("/{subId}/terminate")
public ResponseEntity<Void> terminateSubscription(@PathVariable Long subId) {
planService.terminateSubscription(subId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package com.project.core.controller.dto.request;

public record FindCustomerRequest(
String contactEnc
) {
}

public record FindCustomerRequest(String contactEnc) {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package com.project.core.controller.dto.request;

public record SubscriptionJoinRequest(
Long customerId,
Long planId
) {}
public record SubscriptionJoinRequest(Long customerId, Long planId) {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,73 @@
import com.project.core.infra.entity.plan.SubscriptionPlan;
import com.project.core.infra.entity.subscription.enums.SubscriptionStatus;
import com.project.core.infra.entity.vas.SubscriptionVas;

import jakarta.persistence.*;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "subscription")
public class Subscription {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "sub_id")
private Long subId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;

@Column(name = "phone_number", nullable = false)
private String phoneNumber;

@Column(name = "start_date", nullable = false)
private LocalDateTime startDate;

@Column(name = "end_date")
private LocalDateTime endDate;

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false, length = 10)
private SubscriptionStatus status;

@Column(name = "send_day", nullable = false)
private Integer sendDay;

//---------------------------------------------------------------------

// 요금제 이력 (1:N)
@OneToMany(mappedBy = "subscription", cascade = CascadeType.ALL)
private List<SubscriptionPlan> planHistory = new ArrayList<>();

// 부가서비스 이력 (1:N)
@OneToMany(mappedBy = "subscription", cascade = CascadeType.ALL)
private List<SubscriptionVas> vasHistory = new ArrayList<>();

// 할인 이력 (1:N)

@Builder
public Subscription(Customer customer, String phoneNumber) {
this.customer = customer;
this.phoneNumber = phoneNumber;
this.startDate = LocalDateTime.now();
this.status = SubscriptionStatus.ACTIVE;
this.sendDay = 20;
}

// 서비스 해지 처리 메서드
public void terminate() {
this.status = SubscriptionStatus.TERMINATED;
this.endDate = LocalDateTime.now();
}
private static final int DEFAULT_SEND_DAY = 20;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "sub_id")
private Long subId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;

@Column(name = "phone_number", nullable = false)
private String phoneNumber;

@Column(name = "start_date", nullable = false)
private LocalDateTime startDate;

@Column(name = "end_date")
private LocalDateTime endDate;

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false, length = 10)
private SubscriptionStatus status;

@Column(name = "send_day", nullable = false)
private Integer sendDay;

// ---------------------------------------------------------------------

// 요금제 이력 (1:N)
@OneToMany(mappedBy = "subscription", cascade = CascadeType.ALL)
private List<SubscriptionPlan> planHistory = new ArrayList<>();

// 부가서비스 이력 (1:N)
@OneToMany(mappedBy = "subscription", cascade = CascadeType.ALL)
private List<SubscriptionVas> vasHistory = new ArrayList<>();

// 할인 이력 (1:N)

@Builder
public Subscription(Customer customer, String phoneNumber, Clock clock) {
this.customer = customer;
this.phoneNumber = phoneNumber;
this.startDate = LocalDateTime.now(clock);
this.status = SubscriptionStatus.ACTIVE;
this.sendDay = DEFAULT_SEND_DAY;
}

// 서비스 해지 처리 메서드
public void terminate(Clock clock) {
this.status = SubscriptionStatus.TERMINATED;
this.endDate = LocalDateTime.now(clock);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.project.core.infra.repository.customer;

import com.project.core.infra.entity.customer.Customer;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.project.core.infra.entity.customer.Customer;


public interface CustomerRepository extends JpaRepository<Customer, Long> {
Optional<Customer> findByContactEnc(String contactEnc);
Optional<Customer> findByContactEnc(String contactEnc);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface SubscriptionRepository extends JpaRepository<Subscription, Long> {
long countByCustomerAndStatus(Customer customer, SubscriptionStatus status);
boolean existsByPhoneNumberAndStatus(String phoneNumber, SubscriptionStatus status);
boolean existsByPhoneNumber(String phoneNumber);
long countByCustomerAndStatus(Customer customer, SubscriptionStatus status);

boolean existsByPhoneNumberAndStatus(String phoneNumber, SubscriptionStatus status);

boolean existsByPhoneNumber(String phoneNumber);
}
59 changes: 34 additions & 25 deletions src/main/java/com/project/core/service/CustomerService.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
package com.project.core.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.core.controller.dto.request.ChangeEmailRequest;
import com.project.core.controller.dto.request.ChangeGradeRequest;
import com.project.core.controller.dto.response.ChangeEmailResponse;
import com.project.core.controller.dto.response.ChangeGradeResponse;
import com.project.core.infra.entity.customer.Customer;
import com.project.core.infra.repository.customer.CustomerRepository;

import com.project.global.exception.code.domain.core.CoreErrorCode;
import com.project.global.exception.core.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service @RequiredArgsConstructor
@Service
@RequiredArgsConstructor
public class CustomerService {
private final CustomerRepository customerRepository;

@Transactional
public Customer loadByContactEnc(String contactEnc) {
return customerRepository.findByContactEnc(contactEnc).orElseThrow(()->new IllegalArgumentException("사용자를 찾을 수 없습니다"));
}

@Transactional
public ChangeEmailResponse changeEmailEnc(Long userId, ChangeEmailRequest request) {
Customer customer = customerRepository.findById(userId).orElseThrow(()->new IllegalArgumentException("사용자를 찾을 수 없습니다"));
private final CustomerRepository customerRepository;

@Transactional
public Customer loadByContactEnc(String contactEnc) {
return customerRepository
.findByContactEnc(contactEnc)
.orElseThrow(() -> new EntityNotFoundException(CoreErrorCode.CUSTOMER_NOT_FOUND));
}

@Transactional
public ChangeEmailResponse changeEmailEnc(Long userId, ChangeEmailRequest request) {
Customer customer =
customerRepository
.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(CoreErrorCode.CUSTOMER_NOT_FOUND));

customer.changeEmailEnc(request.emailEnc());
return new ChangeEmailResponse(customer.getEmailEnc());
}

customer.changeEmailEnc(request.emailEnc());
return new ChangeEmailResponse(customer.getEmailEnc());
}

@Transactional
public ChangeGradeResponse changeUserGrade(Long userId, ChangeGradeRequest request) {
Customer customer = customerRepository.findById(userId).orElseThrow(()->new IllegalArgumentException("사용자를 찾을 수 없습니다"));
@Transactional
public ChangeGradeResponse changeUserGrade(Long userId, ChangeGradeRequest request) {
Customer customer =
customerRepository
.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(CoreErrorCode.CUSTOMER_NOT_FOUND));

customer.changeGrade(request.grade());
return new ChangeGradeResponse(customer.getGrade());
}
customer.changeGrade(request.grade());
return new ChangeGradeResponse(customer.getGrade());
}
}
Loading
Loading