Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/team-moabam/moabam-BE in…
Browse files Browse the repository at this point in the history
…to feature/#163-ranking-system

# Conflicts:
#	src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java
  • Loading branch information
parksey committed Nov 30, 2023
2 parents 2b8f721 + 245db4c commit 00c5705
Show file tree
Hide file tree
Showing 21 changed files with 178 additions and 81 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public PurchaseProductResponse purchaseBugProduct(Long memberId, Long productId,

@Transactional
public void use(Member member, BugType bugType, int count) {
if (count == 0) {
return;
}

Bug bug = member.getBug();

bug.use(bugType, count);
Expand All @@ -88,6 +92,10 @@ public void use(Member member, BugType bugType, int count) {

@Transactional
public void reward(Member member, BugType bugType, int count) {
if (count == 0) {
return;
}

Bug bug = member.getBug();

bug.increase(bugType, count);
Expand All @@ -104,6 +112,10 @@ public void charge(Long memberId, Product bugProduct) {

@Transactional
public void applyCoupon(Long memberId, BugType bugType, int count) {
if (count == 0) {
return;
}

Bug bug = getByMemberId(memberId);

bug.increase(bugType, count);
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/com/moabam/api/application/image/ImageService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.moabam.api.application.image;

import static com.moabam.global.error.model.ErrorMessage.IMAGE_CONVERT_FAIL;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -10,7 +13,10 @@
import com.moabam.api.domain.image.ImageName;
import com.moabam.api.domain.image.ImageResizer;
import com.moabam.api.domain.image.ImageType;
import com.moabam.api.domain.image.NewImage;
import com.moabam.api.dto.room.CertifyRoomsRequest;
import com.moabam.api.infrastructure.s3.S3Manager;
import com.moabam.global.error.exception.BadRequestException;

import lombok.RequiredArgsConstructor;

Expand All @@ -22,7 +28,7 @@ public class ImageService {
private final S3Manager s3Manager;

@Transactional
public List<String> uploadImages(List<MultipartFile> multipartFiles, ImageType imageType) {
public List<String> uploadImages(List<? extends MultipartFile> multipartFiles, ImageType imageType) {

List<String> result = new ArrayList<>();

Expand All @@ -38,6 +44,24 @@ public List<String> uploadImages(List<MultipartFile> multipartFiles, ImageType i
return result;
}

public List<NewImage> getNewImages(CertifyRoomsRequest request) {
return request.getCertifyRoomsRequest().stream()
.map(certifyRoomRequest -> {
try {
return NewImage.of(String.valueOf(certifyRoomRequest.getRoutineId()),
certifyRoomRequest.getImage().getContentType(), certifyRoomRequest.getImage().getBytes());
} catch (IOException e) {
throw new BadRequestException(IMAGE_CONVERT_FAIL);
}
})
.toList();
}

@Transactional
public void deleteImage(String imageUrl) {
s3Manager.deleteImage(imageUrl);
}

private ImageResizer toImageResizer(MultipartFile multipartFile, ImageType imageType) {
ImageName imageName = ImageName.of(multipartFile, imageType);

Expand All @@ -46,9 +70,4 @@ private ImageResizer toImageResizer(MultipartFile multipartFile, ImageType image
.fileName(imageName.getFileName())
.build();
}

@Transactional
public void deleteImage(String imageUrl) {
s3Manager.deleteImage(imageUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.moabam.api.domain.payment.Order;
import com.moabam.api.domain.payment.Payment;
import com.moabam.api.domain.product.Product;
import com.moabam.api.dto.payment.ConfirmTossPaymentResponse;
import com.moabam.api.dto.payment.PaymentResponse;
import com.moabam.api.dto.payment.RequestConfirmPaymentResponse;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -36,4 +38,12 @@ public static PaymentResponse toPaymentResponse(Payment payment) {
.build())
.orElse(null);
}

public static RequestConfirmPaymentResponse toRequestConfirmPaymentResponse(Payment payment,
ConfirmTossPaymentResponse response) {
return RequestConfirmPaymentResponse.builder()
.payment(payment)
.paymentKey(response.paymentKey())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.moabam.api.dto.payment.ConfirmPaymentRequest;
import com.moabam.api.dto.payment.ConfirmTossPaymentResponse;
import com.moabam.api.dto.payment.PaymentRequest;
import com.moabam.api.dto.payment.RequestConfirmPaymentResponse;
import com.moabam.api.infrastructure.payment.TossPaymentService;
import com.moabam.global.error.exception.NotFoundException;
import com.moabam.global.error.exception.TossPaymentException;

import lombok.RequiredArgsConstructor;

Expand All @@ -36,28 +38,30 @@ public void request(Long memberId, Long paymentId, PaymentRequest request) {
payment.request(request.orderId());
}

public Payment validateInfo(Long memberId, ConfirmPaymentRequest request) {
@Transactional
public RequestConfirmPaymentResponse requestConfirm(Long memberId, ConfirmPaymentRequest request) {
Payment payment = getByOrderId(request.orderId());
payment.validateInfo(memberId, request.amount());

return payment;
try {
ConfirmTossPaymentResponse response = tossPaymentService.confirm(request);
return PaymentMapper.toRequestConfirmPaymentResponse(payment, response);
} catch (TossPaymentException exception) {
payment.fail(request.paymentKey());
throw exception;
}
}

@Transactional
public void confirm(Long memberId, Payment payment, ConfirmTossPaymentResponse response) {
payment.confirm(response.paymentKey());
public void confirm(Long memberId, Payment payment, String paymentKey) {
payment.confirm(paymentKey);

if (payment.isCouponApplied()) {
couponService.discount(payment.getCouponWalletId(), memberId);
}
bugService.charge(memberId, payment.getProduct());
}

@Transactional
public void fail(Payment payment, String paymentKey) {
payment.fail(paymentKey);
}

private Payment getById(Long paymentId) {
return paymentRepository.findById(paymentId)
.orElseThrow(() -> new NotFoundException(PAYMENT_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static RoomDetailsResponse toRoomDetailsResponse(Long memberId, Room room
List<TodayCertificateRankResponse> todayCertificateRankResponses, double completePercentage) {
return RoomDetailsResponse.builder()
.roomId(room.getId())
.roomCreatedAt(room.getCreatedAt())
.myMemberId(memberId)
.title(room.getTitle())
.managerNickName(managerNickname)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/moabam/api/domain/image/ImageResizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private BufferedImage getBufferedImage() {
}
}

private ResizedImage toMultipartFile(byte[] bytes) {
return ResizedImage.of(fileName, image.getContentType(), bytes);
private NewImage toMultipartFile(byte[] bytes) {
return NewImage.of(fileName, image.getContentType(), bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class ResizedImage implements MultipartFile {
public class NewImage implements MultipartFile {

private final String name;
private final String contentType;
private final long size;
private final byte[] bytes;

public static ResizedImage of(String name, String contentType, byte[] bytes) {
return new ResizedImage(name, contentType, bytes.length, bytes);
public static NewImage of(String name, String contentType, byte[] bytes) {
return new NewImage(name, contentType, bytes.length, bytes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.moabam.api.dto.payment;

import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.moabam.api.domain.payment.PaymentStatus;

import lombok.Builder;

Expand All @@ -13,9 +10,7 @@ public record ConfirmTossPaymentResponse(
String paymentKey,
String orderId,
String orderName,
PaymentStatus status,
int totalAmount,
LocalDateTime approvedAt
int totalAmount
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.moabam.api.dto.payment;

import com.moabam.api.domain.payment.Payment;

import lombok.Builder;

@Builder
public record RequestConfirmPaymentResponse(
Payment payment,
String paymentKey
) {

}
14 changes: 14 additions & 0 deletions src/main/java/com/moabam/api/dto/room/CertifyRoomRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.moabam.api.dto.room;

import org.springframework.web.multipart.MultipartFile;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CertifyRoomRequest {

private Long routineId;
private MultipartFile image;
}
13 changes: 13 additions & 0 deletions src/main/java/com/moabam/api/dto/room/CertifyRoomsRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.moabam.api.dto.room;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CertifyRoomsRequest {

private List<CertifyRoomRequest> certifyRoomsRequest;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.moabam.api.dto.room;

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

import com.moabam.api.domain.room.RoomType;
Expand All @@ -10,6 +11,7 @@
@Builder
public record RoomDetailsResponse(
Long roomId,
LocalDateTime roomCreatedAt,
Long myMemberId,
String title,
String managerNickName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

Expand All @@ -21,7 +20,6 @@
import reactor.core.publisher.Mono;

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

Expand All @@ -39,7 +37,6 @@ public void init() {
.build();
}

@Transactional
public ConfirmTossPaymentResponse confirm(ConfirmPaymentRequest request) {
return webClient.post()
.uri("/v1/payments/confirm")
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/com/moabam/api/presentation/PaymentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import org.springframework.web.bind.annotation.RestController;

import com.moabam.api.application.payment.PaymentService;
import com.moabam.api.domain.payment.Payment;
import com.moabam.api.dto.payment.ConfirmPaymentRequest;
import com.moabam.api.dto.payment.ConfirmTossPaymentResponse;
import com.moabam.api.dto.payment.PaymentRequest;
import com.moabam.api.infrastructure.payment.TossPaymentService;
import com.moabam.api.dto.payment.RequestConfirmPaymentResponse;
import com.moabam.global.auth.annotation.Auth;
import com.moabam.global.auth.model.AuthMember;
import com.moabam.global.error.exception.TossPaymentException;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,7 +24,6 @@
public class PaymentController {

private final PaymentService paymentService;
private final TossPaymentService tossPaymentService;

@PostMapping("/{paymentId}")
@ResponseStatus(HttpStatus.OK)
Expand All @@ -39,13 +35,7 @@ public void request(@Auth AuthMember member, @PathVariable Long paymentId,
@PostMapping("/confirm")
@ResponseStatus(HttpStatus.OK)
public void confirm(@Auth AuthMember member, @Valid @RequestBody ConfirmPaymentRequest request) {
Payment payment = paymentService.validateInfo(member.id(), request);

try {
ConfirmTossPaymentResponse response = tossPaymentService.confirm(request);
paymentService.confirm(member.id(), payment, response);
} catch (TossPaymentException exception) {
paymentService.fail(payment, request.paymentKey());
}
RequestConfirmPaymentResponse response = paymentService.requestConfirm(member.id(), request);
paymentService.confirm(member.id(), response.payment(), response.paymentKey());
}
}
Loading

0 comments on commit 00c5705

Please sign in to comment.