Skip to content

Commit

Permalink
Refactor: 포인트 도메인 운영코드 개선과 테스트코드 개선 및 추가 (#144)
Browse files Browse the repository at this point in the history
* refactor: 포인트 어드민 컨트롤러에 있던 서비스로직 포인트 어드민 서비스 클래스로 옮김

* test: 포인트 어드민 서비스 테스트

* test: 포인트 서비스 테스트 추가 및 개선

* refactor: 포인트 출금 요청시 로그에는 출금 요청한 포인트 그대로 찍히도록 변경. 단, 관리자페이지에서는 수수료가 적용된 포인트로 보이도록 함.

* refactor: 관리자 페이지 약간의 수정,css 더 깔끔히 수정

* refactor: 관리자 페이지 가독성을 위해 LocalDateTime을 포맷팅해서 String으로 등록 시간을 표현한다.

* fix: 테스트 정상 통과하도록 수정

* chore: 버튼 이름 수정
  • Loading branch information
zzoe2346 authored Nov 3, 2024
1 parent 2a965e7 commit b32d48a
Show file tree
Hide file tree
Showing 11 changed files with 489 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,30 @@
package com.example.sinitto.point.controller;

import com.example.sinitto.common.exception.NotFoundException;
import com.example.sinitto.member.entity.Member;
import com.example.sinitto.member.repository.MemberRepository;
import com.example.sinitto.point.dto.PointLogWithBankInfo;
import com.example.sinitto.point.dto.PointLogWithDepositMessage;
import com.example.sinitto.point.entity.PointLog;
import com.example.sinitto.point.service.PointAdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class PointAdminController {

private final PointAdminService pointAdminService;
private final MemberRepository memberRepository;

public PointAdminController(PointAdminService pointAdminService, MemberRepository memberRepository) {
public PointAdminController(PointAdminService pointAdminService) {
this.pointAdminService = pointAdminService;
this.memberRepository = memberRepository;
}

@GetMapping("/admin/point/charge")
public String showAllChargeRequest(Model model) {

List<PointLog> pointLogs = pointAdminService.readAllNotCompletedPointChargeRequest();
List<PointLogWithDepositMessage> logWithDepositMessages = new ArrayList<>();
List<PointLogWithDepositMessage> logWithDepositMessages = pointAdminService.getPointLogWithDepositMessage();

for (PointLog pointLog : pointLogs) {
Member member = memberRepository.findById(pointLog.getMember().getId())
.orElseThrow(() -> new NotFoundException("멤버를 찾을 수 없습니다"));

PointLogWithDepositMessage pointLogWithDepositMessage = new PointLogWithDepositMessage(
pointLog.getId(),
pointLog.getPrice(),
pointLog.getPostTime(),
pointLog.getStatus(),
member.getDepositMessage()
);

logWithDepositMessages.add(pointLogWithDepositMessage);
}
model.addAttribute("logWithDepositMessages", logWithDepositMessages);

return "point/charge";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.example.sinitto.point.entity.PointLog;

import java.time.LocalDateTime;

public record PointLogWithBankInfo(
Long pointLogId,
int price,
LocalDateTime postTime,
String postTime,
PointLog.Status status,
String bankName,
String bankAccountNumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.example.sinitto.point.entity.PointLog;

import java.time.LocalDateTime;

public record PointLogWithDepositMessage(
Long pointLogId,
int price,
LocalDateTime postTime,
String postTime,
PointLog.Status status,
String depositMessage
) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/example/sinitto/point/entity/PointLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@EntityListeners(AuditingEntityListener.class)
public class PointLog {

public static final double WITHDRAWAL_FEE_RATE = 0.8;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down Expand Up @@ -94,6 +96,10 @@ private void checkStatusChange(Status wantStatus) {
}
}

public int getPointPriceAfterFee() {
return (int) (price * WITHDRAWAL_FEE_RATE);
}

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.sinitto.point.service;

import com.example.sinitto.common.exception.NotFoundException;
import com.example.sinitto.member.entity.Member;
import com.example.sinitto.member.repository.MemberRepository;
import com.example.sinitto.point.dto.PointLogWithBankInfo;
import com.example.sinitto.point.dto.PointLogWithDepositMessage;
import com.example.sinitto.point.entity.Point;
import com.example.sinitto.point.entity.PointLog;
import com.example.sinitto.point.repository.PointLogRepository;
Expand All @@ -11,6 +14,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -20,16 +24,36 @@ public class PointAdminService {
private final PointLogRepository pointLogRepository;
private final PointRepository pointRepository;
private final SinittoBankInfoRepository sinittoBankInfoRepository;
private final MemberRepository memberRepository;

public PointAdminService(PointLogRepository pointLogRepository, PointRepository pointRepository, SinittoBankInfoRepository sinittoBankInfoRepository) {
public PointAdminService(PointLogRepository pointLogRepository, PointRepository pointRepository, SinittoBankInfoRepository sinittoBankInfoRepository, MemberRepository memberRepository) {
this.pointLogRepository = pointLogRepository;
this.pointRepository = pointRepository;
this.sinittoBankInfoRepository = sinittoBankInfoRepository;
this.memberRepository = memberRepository;
}

public List<PointLog> readAllNotCompletedPointChargeRequest() {
@Transactional(readOnly = true)
public List<PointLogWithDepositMessage> getPointLogWithDepositMessage() {

List<PointLog> pointLogs = pointLogRepository.findAllByStatusInOrderByPostTimeDesc(List.of(PointLog.Status.CHARGE_WAITING, PointLog.Status.CHARGE_REQUEST, PointLog.Status.CHARGE_COMPLETE, PointLog.Status.CHARGE_FAIL));
List<PointLogWithDepositMessage> logWithDepositMessages = new ArrayList<>();

for (PointLog pointLog : pointLogs) {
Member member = memberRepository.findById(pointLog.getMember().getId())
.orElse(new Member("미등록 유저", "미등록 유저", "미등록 유저", false));

PointLogWithDepositMessage pointLogWithDepositMessage = new PointLogWithDepositMessage(
pointLog.getId(),
pointLog.getPrice(),
pointLog.getPostTime().format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분")),
pointLog.getStatus(),
member.getDepositMessage()
);

return pointLogRepository.findAllByStatusInOrderByPostTimeDesc(List.of(PointLog.Status.CHARGE_WAITING, PointLog.Status.CHARGE_REQUEST, PointLog.Status.CHARGE_COMPLETE, PointLog.Status.CHARGE_FAIL));
logWithDepositMessages.add(pointLogWithDepositMessage);
}
return logWithDepositMessages;
}

@Transactional
Expand Down Expand Up @@ -108,8 +132,8 @@ public List<PointLogWithBankInfo> getPointLogWithBankInfo() {

PointLogWithBankInfo pointLogWithBankInfo = new PointLogWithBankInfo(
pointLog.getId(),
pointLog.getPrice(),
pointLog.getPostTime(),
pointLog.getPointPriceAfterFee(),
pointLog.getPostTime().format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분")),
pointLog.getStatus(),
sinittoBankInfo.getBankName(),
sinittoBankInfo.getAccountNumber()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
@Service
public class PointService {

public static final double WITHDRAWAL_FEE_RATE = 0.8;
private final MemberRepository memberRepository;
private final PointRepository pointRepository;
private final PointLogRepository pointLogRepository;
Expand All @@ -34,6 +33,7 @@ public PointService(MemberRepository memberRepository, PointRepository pointRepo
this.sinittoBankInfoRepository = sinittoBankInfoRepository;
}

@Transactional(readOnly = true)
public PointResponse getPoint(Long memberId) {

Member member = memberRepository.findById(memberId)
Expand All @@ -45,6 +45,7 @@ public PointResponse getPoint(Long memberId) {
return new PointResponse(point.getPrice());
}

@Transactional(readOnly = true)
public Page<PointLogResponse> getPointLogs(Long memberId, Pageable pageable) {

Member member = memberRepository.findById(memberId)
Expand Down Expand Up @@ -87,14 +88,12 @@ public void savePointWithdrawRequest(Long memberId, int price) {
Point point = pointRepository.findByMember(member)
.orElseThrow(() -> new NotFoundException("요청한 멤버의 포인트를 찾을 수 없습니다"));

int adjustedPrice = (int) (price * WITHDRAWAL_FEE_RATE);

if (!point.isSufficientForDeduction(price)) {
throw new BadRequestException(String.format("보유한 포인트(%d) 보다 더 많은 포인트에 대한 출금요청입니다", point.getPrice()));
}

point.deduct(price);

pointLogRepository.save(new PointLog(PointLog.Content.WITHDRAW_REQUEST.getMessage(), member, adjustedPrice, PointLog.Status.WITHDRAW_REQUEST));
pointLogRepository.save(new PointLog(PointLog.Content.WITHDRAW_REQUEST.getMessage(), member, price, PointLog.Status.WITHDRAW_REQUEST));
}
}
69 changes: 47 additions & 22 deletions src/main/resources/static/css/point.css
Original file line number Diff line number Diff line change
@@ -1,61 +1,86 @@
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
background-color: #e9edf0;
margin: 0;
padding: 20px;
color: #333;
}

h1 {
text-align: center;
color: #333;
font-weight: 600;
margin-bottom: 30px;
}

table {

width: 100%;
border-collapse: collapse;
margin: 20px 0;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
background-color: #fff;
border-radius: 8px;
overflow: hidden;
}

th, td {
padding: 12px;
text-align: left;
border: 1px solid #ddd;
background-color: #fff;
padding: 16px;
text-align: center;
border-bottom: 1px solid #e1e4e8;
}

th {
background-color: #4CAF50;
color: white;
background-color: rgba(52, 87, 217, 0.8);
color: #ffffff;
font-weight: 600;
font-size: 16px;
top: 0;
}

tr:nth-child(even) {
background-color: #f9f9f9;
td {
font-size: 14px;
color: #555;
}

tr:hover {
background-color: #f1f1f1;
tr:nth-child(even) {
background-color: #f7f9fb;
}

button {
background-color: #4CAF50;
color: white;
background-color: rgba(52, 87, 217, 0.8);
color: #ffffff;
border: none;
padding: 10px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
padding: 10px 20px;
font-size: 14px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
transition: background-color 0.3s;
transition: background-color 0.3s, transform 0.2s;
}

button:hover {
background-color: #45a049;
background-color: rgba(52, 87, 217, 0.93);
transform: scale(1.03);
}

button:active {
background-color: #00473c;
}

form {
display: inline;
}
margin: 0;
}

.header {
background-color: #00796b;
padding: 20px;
color: #ffffff;
border-radius: 8px;
margin-bottom: 30px;
}

.header h1 {
margin: 0;
font-size: 24px;
}
7 changes: 6 additions & 1 deletion src/main/resources/templates/point/charge.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<title>충전 페이지</title>
</head>
<body>

<div>
<h1>포인트 충전 관리</h1>
</div>

<div>
<table>
<thead>
Expand Down Expand Up @@ -36,7 +41,7 @@
<form method="post"
th:action="@{/admin/point/charge/complete/{pointLogId}(pointLogId=${logWithDepositMessage.pointLogId})}"
th:if="${logWithDepositMessage.status.toString()} == 'CHARGE_WAITING'">
<button type="submit">포인트 지급 & 완료 상태로 변경</button>
<button type="submit">완료 상태로 변경</button>
</form>

<!-- 상태가 CHARGE_WAITING일 때 -->
Expand Down
11 changes: 8 additions & 3 deletions src/main/resources/templates/point/withdraw.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
<title>출금 페이지</title>
</head>
<body>

<div>
<h1>포인트 출금 관리</h1>
</div>

<div>
<table>
<thead>
<tr>
<th>출금 요청시간</th>
<th>출금 포인트</th>
<th>요청 시간</th>
<th>수수료 적용 된 출금 포인트</th>
<th>은행 이름</th>
<th>계좌번호</th>
<th>상태</th>
Expand All @@ -38,7 +43,7 @@
<form method="post"
th:action="@{/admin/point/withdraw/complete/{pointLogId}(pointLogId=${logWithBankInfo.pointLogId()})}"
th:if="${logWithBankInfo.status().toString()} == 'WITHDRAW_WAITING'">
<button type="submit">입금 완료 & 완료 상태로 변경</button>
<button type="submit">완료 상태로 변경</button>
</form>

<!-- 상태가 CHARGE_WAITING일 때 -->
Expand Down
Loading

0 comments on commit b32d48a

Please sign in to comment.