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
Expand Up @@ -2,125 +2,94 @@

import com.sesac.boheommong.domain.tosspayment.dto.AutoPaymentDto;
import com.sesac.boheommong.domain.tosspayment.service.AutoPaymentService;
import com.sesac.boheommong.global.response.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:3000")
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@RequestMapping("/api/autoPayments")
public class AutoPaymentController {

private final AutoPaymentService autoPaymentService;

public AutoPaymentController(AutoPaymentService autoPaymentService) {
this.autoPaymentService = autoPaymentService;

@Operation(summary = "자동결제 생성", description = "유저 ID, 상품 ID, 날짜, 시간을 받아 자동결제를 생성합니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "생성 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "서버 오류")
})
@PostMapping
public AutoPaymentDto createAutoPayment(@RequestBody AutoPaymentDto dto) {
return autoPaymentService.createAutoPayment(dto);
}

/**
* 자동결제 생성
*
* <p>JSON 예시:
* <pre>
* {
* "userId": 10,
* "dayOfMonth": 26,
* "time": "11:00",
* "productId": 999
* }
* </pre>
*
* <p>productId가 넘어오면, 해당 보험상품과 연동하여 DB에 product_id 저장
* 전체 목록 조회
*/
@Operation(summary = "자동 결제 생성", description = "사용자의 자동 결제 정보를 생성합니다.")
@Operation(summary = "전체 자동결제 목록 조회", description = "DB에 있는 모든 자동결제 정보를 조회합니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "생성 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "서버 오류")
@ApiResponse(responseCode = "200", description = "조회 성공")
})
@PostMapping("/autoPayment")
public Response<AutoPaymentDto> createAutoPayment(@RequestBody AutoPaymentDto dto) {
AutoPaymentDto saved = autoPaymentService.createAutoPayment(dto);
return Response.success(saved);
@GetMapping
public List<AutoPaymentDto> getAllAutoPayments() {
return autoPaymentService.getAllAutoPayments();
}

/**
* 자동결제 목록 조회
* 특정 단건 상세조회
*/
@Operation(summary = "자동 결제 목록 조회", description = "등록된 자동 결제 정보들을 모두 조회합니다.")
@Operation(summary = "자동결제 상세조회", description = "autoPaymentId로 특정 자동결제 정보를 상세 조회합니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "401", description = "인증 실패"),
@ApiResponse(responseCode = "500", description = "서버 오류")
@ApiResponse(responseCode = "404", description = "해당 정보가 존재하지 않음")
})
@GetMapping("/autoPayment")
public Response<List<AutoPaymentDto>> getAutoPayments() {
List<AutoPaymentDto> list = autoPaymentService.getAllAutoPayments();
return Response.success(list);
@GetMapping("/{id}")
public AutoPaymentDto getAutoPaymentDetail(@PathVariable Long id) {
return autoPaymentService.getAutoPaymentDetail(id);
}

/**
* 자동결제 단건 상세조회
* 사용자별 자동결제 목록 조회
*/
@Operation(summary = "자동 결제 상세조회", description = "주어진 id로 자동 결제 정보를 상세 조회합니다.")
@Operation(summary = "사용자별 자동결제 목록 조회", description = "특정 사용자의 자동결제 정보를 조회합니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "404", description = "해당 정보가 존재하지 않음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
@ApiResponse(responseCode = "404", description = "해당 유저가 존재하지 않음")
})
@GetMapping("/autoPayment/{id}")
public Response<AutoPaymentDto> getAutoPaymentDetail(@PathVariable Long id) {
AutoPaymentDto detail = autoPaymentService.getAutoPaymentDetail(id);
return Response.success(detail);
@GetMapping("/user/{userId}")
public List<AutoPaymentDto> getAutoPaymentsByUser(@PathVariable Long userId) {
return autoPaymentService.getAutoPaymentsByUser(userId);
}

/**
* 자동결제 수정
*
* <p>JSON 예시:
* <pre>
* {
* "dayOfMonth": 15,
* "time": "09:30",
* "productId": 123
* }
* </pre>
*
* <p>productId를 지정하면 해당 보험상품으로 수정하여 연관관계를 설정
* <p>productId가 null이면 기존 연관관계 해제
* dayOfMonth, time만 가능
*/
@Operation(summary = "자동 결제 수정", description = "주어진 id의 자동 결제 정보를 수정합니다.")
@Operation(summary = "자동결제 수정", description = "autoPaymentId로 특정 자동결제를 수정합니다. (날짜, 시간만 변경 가능)")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "수정 성공"),
@ApiResponse(responseCode = "404", description = "해당 정보가 존재하지 않음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
@ApiResponse(responseCode = "404", description = "해당 정보가 존재하지 않음")
})
@PutMapping("/autoPayment/{id}")
public Response<AutoPaymentDto> updateAutoPayment(
@PathVariable Long id,
@RequestBody AutoPaymentDto dto
) {
AutoPaymentDto updated = autoPaymentService.updateAutoPayment(id, dto);
return Response.success(updated);
@PutMapping("/{id}")
public AutoPaymentDto updateAutoPayment(@PathVariable Long id, @RequestBody AutoPaymentDto dto) {
return autoPaymentService.updateAutoPayment(id, dto);
}

/**
* 자동결제 삭제
*/
@Operation(summary = "자동 결제 삭제", description = "주어진 id의 자동 결제 정보를 삭제합니다.")
@Operation(summary = "자동결제 삭제", description = "autoPaymentId로 특정 자동결제 정보를 삭제합니다.")
@ApiResponses({
@ApiResponse(responseCode = "204", description = "삭제 성공 (No Content)"),
@ApiResponse(responseCode = "404", description = "해당 정보가 존재하지 않음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
@ApiResponse(responseCode = "404", description = "해당 정보가 존재하지 않음")
})
@DeleteMapping("/autoPayment/{id}")
public Response<Void> deleteAutoPayment(@PathVariable Long id) {
@DeleteMapping("/{id}")
public void deleteAutoPayment(@PathVariable Long id) {
autoPaymentService.deleteAutoPayment(id);
// 응답 바디가 필요 없다면 null 을 넘기거나, 별도 메시지를 넣어도 됨
return Response.success(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package com.sesac.boheommong.domain.tosspayment.dto;

import com.sesac.boheommong.domain.tosspayment.entity.AutoPaymentEntity;
import com.sesac.boheommong.domain.tosspayment.entity.AutoPayment;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* AutoPayment DTO
*/
@Getter
@Setter
@NoArgsConstructor
public class AutoPaymentDto {
private Long id;
private Long userId;

private Long id; // AutoPayment 엔티티의 PK
private Long userId; // FK (User PK)
private Long productId; // FK (InsuranceProduct PK)
private Integer dayOfMonth;
private String time;

// [중요] 어떤 보험 상품과 연결할지 식별하기 위해 productId 추가
private Long productId;
public static AutoPaymentDto fromEntity(AutoPayment entity) {
if (entity == null) return null;

public static AutoPaymentDto fromEntity(AutoPaymentEntity entity) {
if (entity == null) {
return null;
}
AutoPaymentDto dto = new AutoPaymentDto();
dto.setId(entity.getId());
dto.setUserId(entity.getUserId());
dto.setDayOfMonth(entity.getDayOfMonth());
dto.setTime(entity.getTime());

// 보험상품이 연결되어 있다면 productId 세팅
if (entity.getInsuranceProduct() != null) {
dto.setProductId(entity.getInsuranceProduct().getProductId());
if (entity.getUser() != null) {
dto.setUserId(entity.getUser().getUserId());
}
if (entity.getProduct() != null) {
dto.setProductId(entity.getProduct().getProductId());
}

return dto;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.sesac.boheommong.domain.tosspayment.entity;

import com.sesac.boheommong.domain.insurance.entity.InsuranceProduct;
import com.sesac.boheommong.domain.user.entity.User;
import com.sesac.boheommong.global.entity.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Getter
@Table(
name = "auto_payments",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"user_id", "product_id"})
}
)
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AutoPayment {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long Id;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false, updatable = false)
private User user;

@ManyToOne
@JoinColumn(name = "product_id", nullable = false, updatable = false)
private InsuranceProduct product;

@Column(nullable = false)
private Integer dayOfMonth;

@Column(nullable = false)
private String time;

/**
* private 생성자를 사용해 외부에서 직접 인스턴스를 만들 수 없게 막고,
* 정적 팩토리 메서드로만 만들도록 유도
*/
private AutoPayment(User user, InsuranceProduct product, Integer dayOfMonth, String time) {
this.user = user;
this.product = product;
this.dayOfMonth = dayOfMonth;
this.time = time;
}

/**
* 정적 팩토리 메서드
*/
public static AutoPayment create(User user, InsuranceProduct product, Integer dayOfMonth, String time) {
return new AutoPayment(user, product, dayOfMonth, time);
}

public void updateDayOfMonth(Integer dayOfMonth) {
this.dayOfMonth = dayOfMonth;
}
public void updateTime(String time) {
this.time = time;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.sesac.boheommong.domain.tosspayment.repository;

import com.sesac.boheommong.domain.tosspayment.entity.AutoPaymentEntity;
import com.sesac.boheommong.domain.tosspayment.entity.AutoPayment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AutoPaymentRepository extends JpaRepository<AutoPaymentEntity, Long> {
import java.util.List;

@Repository
public interface AutoPaymentRepository extends JpaRepository<AutoPayment, Long> {
/**
* 특정 사용자의 자동결제 목록 조회
* AutoPayment 엔티티 내부의 user 필드(연관관계) → user.userId
*/
List<AutoPayment> findByUser_UserId(Long userId);
}

Loading