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 @@ -47,13 +47,16 @@ public void handlePaymentCompleted(PaymentCompletedEvent event) {
Payment payment = paymentRepository.findById(event.paymentId())
.orElseThrow(PaymentNotFoundException::new);

String description = "[" + payment.getRental().getPost().getTitle() + "] 결제 완료";

PointHistory history = PointHistory.builder()
.user(user)
.rental(payment.getRental())
.payment(payment)
.type(PointHistoryType.EARN)
.amount(rewardPoint)
.finalBalance(user.getPoint())
.description(description)
.build();
pointHistoryRepository.save(history);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,16 @@ public void completePayment(Long paymentId, Long userId, PaymentRequest request)
User user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
user.usePoint(request.getPointAmount());

String description = "[" + post.getTitle() + "] 대여 시 사용";

PointHistory pointHistory = PointHistory.builder()
.user(user)
.rental(rental)
.payment(payment)
.type(PointHistoryType.SPEND)
.amount(request.getPointAmount())
.finalBalance(user.getPoint())
.description(description)
.build();
pointHistoryRepository.save(pointHistory);
}
Expand Down Expand Up @@ -204,27 +207,33 @@ public void cancelPayment(Long paymentId) {

user.usePoint(earnedPoint);

String description = "[" + payment.getRental().getPost().getTitle() + "] 결제 취소";

PointHistory reclaimHistory = PointHistory.builder()
.user(user)
.rental(payment.getRental())
.payment(payment)
.type(PointHistoryType.ADJUST)
.amount(earnedPoint)
.finalBalance(user.getPoint())
.description(description)
.build();
pointHistoryRepository.save(reclaimHistory);
});

if (payment.getUsedPoint() > 0) {
user.addPoint(payment.getUsedPoint());

String description = "[" + payment.getRental().getPost().getTitle() + "] 대여 취소";

PointHistory pointHistory = PointHistory.builder()
.user(user)
.rental(payment.getRental())
.payment(payment)
.type(PointHistoryType.REFUND)
.amount(payment.getUsedPoint())
.finalBalance(user.getPoint())
.description(description)
.build();
pointHistoryRepository.save(pointHistory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.rentify.rentify_api.point.controller;

import com.rentify.rentify_api.point.dto.PointHistoryResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
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 java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;

@Tag(name = "Point API", description = "포인트 관련 API")
public interface PointApiDocs {

@Operation(
summary = "포인트 이력 조회",
description = "로그인한 사용자의 포인트 이력을 전체 조회합니다. 최신순으로 정렬됩니다."
)
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "포인트 이력 조회 성공",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = List.class),
examples = @ExampleObject(
value = """
[
{
"historyId": 1,
"type": "EARN",
"amount": 1000,
"finalBalance": 5000,
"description": "대여 적립",
"createdAt": "2026-04-05T10:30:00"
},
{
"historyId": 2,
"type": "SPEND",
"amount": -500,
"finalBalance": 4000,
"description": "쿠폰 사용",
"createdAt": "2026-04-04T15:20:00"
}
]
"""
)
)
),
@ApiResponse(
responseCode = "401",
description = "인증 실패 (로그인 필요)",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(
value = "{\"success\": false, \"code\": \"401\", \"message\": \"인증이 필요합니다.\", \"data\": null}"
)
)
),
@ApiResponse(
responseCode = "404",
description = "존재하지 않는 사용자",
content = @Content(
mediaType = "application/json",
examples = @ExampleObject(
value = "{\"success\": false, \"code\": \"404\", \"message\": \"존재하지 않는 사용자입니다.\", \"data\": null}"
)
)
)
})
@GetMapping("/history")
ResponseEntity<List<PointHistoryResponse>> getPointHistory(
@Parameter(hidden = true) @AuthenticationPrincipal Long userId
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.rentify.rentify_api.point.controller;

import com.rentify.rentify_api.point.dto.PointHistoryResponse;
import com.rentify.rentify_api.point.service.PointService;
import java.util.List;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/points")
@RequiredArgsConstructor
public class PointController implements PointApiDocs {

private final PointService pointService;

@Override
@GetMapping("/history")
public ResponseEntity<List<PointHistoryResponse>> getPointHistory(
@AuthenticationPrincipal Long userId
) {
List<PointHistoryResponse> histories = pointService.getPointHistories(userId);
return ResponseEntity.ok(histories);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rentify.rentify_api.point.dto;

import com.rentify.rentify_api.point.entity.PointHistory;
import com.rentify.rentify_api.point.entity.PointHistoryType;
import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record PointHistoryResponse(
Long historyId,
PointHistoryType type,
Integer amount,
Integer finalBalance,
String description,
LocalDateTime createdAt
) {
public static PointHistoryResponse from(PointHistory pointHistory) {
return PointHistoryResponse.builder()
.historyId(pointHistory.getId())
.type(pointHistory.getType())
.amount(pointHistory.getAmount())
.finalBalance(pointHistory.getFinalBalance())
.description(pointHistory.getDescription())
.createdAt(pointHistory.getCreateAt())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class PointHistory {
@Column(name = "final_balance", nullable = false)
private Integer finalBalance;

@Column(name = "description")
private String description;

@CreationTimestamp
@Column(name = "created_at", nullable = false)
private LocalDateTime createAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import com.rentify.rentify_api.payment.entity.Payment;
import com.rentify.rentify_api.point.entity.PointHistory;
import com.rentify.rentify_api.point.entity.PointHistoryType;
import com.rentify.rentify_api.user.entity.User;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PointHistoryRepository extends JpaRepository<PointHistory, Long> {

Optional<PointHistory> findByPaymentAndType(Payment payment, PointHistoryType earn);

List<PointHistory> findAllByUserOrderByCreateAtDesc(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.rentify.rentify_api.point.service;

import com.rentify.rentify_api.point.dto.PointHistoryResponse;
import com.rentify.rentify_api.point.repository.PointHistoryRepository;
import com.rentify.rentify_api.user.entity.User;
import com.rentify.rentify_api.user.exception.UserNotFoundException;
import com.rentify.rentify_api.user.repository.UserRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PointService {

private final PointHistoryRepository pointHistoryRepository;
private final UserRepository userRepository;

public List<PointHistoryResponse> getPointHistories(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(UserNotFoundException::new);

return pointHistoryRepository.findAllByUserOrderByCreateAtDesc(user)
.stream()
.map(PointHistoryResponse::from)
.toList();
}
}