Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: 쿠폰 및 노션 메서드, 변수, 클래스명 변경 #122

Merged
merged 2 commits into from
Nov 21, 2023
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
22 changes: 11 additions & 11 deletions src/main/java/com/moabam/api/application/coupon/CouponMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class CouponMapper {

public static Coupon toEntity(Long adminId, CreateCouponRequest request) {
public static Coupon toEntity(Long adminId, CreateCouponRequest coupon) {
return Coupon.builder()
.name(request.name())
.description(request.description())
.type(CouponType.from(request.couponType()))
.point(request.point())
.stock(request.stock())
.startAt(request.startAt())
.endAt(request.endAt())
.name(coupon.name())
.description(coupon.description())
.type(CouponType.from(coupon.type()))
.point(coupon.point())
.stock(coupon.stock())
.startAt(coupon.startAt())
.endAt(coupon.endAt())
.adminId(adminId)
.build();
}

// TODO : Admin Table 생성 시, 관리자 명 추가할 예정
public static CouponResponse toDto(Coupon coupon) {
return CouponResponse.builder()
.couponId(coupon.getId())
.couponAdminName(coupon.getAdminId() + "admin")
.id(coupon.getId())
.adminName(coupon.getAdminId() + "admin")
.name(coupon.getName())
.description(coupon.getDescription())
.point(coupon.getPoint())
.stock(coupon.getStock())
.couponType(coupon.getType())
.type(coupon.getType())
.startAt(coupon.getStartAt())
.endAt(coupon.getEndAt())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public void register(AuthorizationMember member, String couponName) {
return;
}

couponQueueRepository.addQueue(couponName, member.nickname(), registerTime);
couponQueueRepository.addIfAbsent(couponName, member.nickname(), registerTime);
}

private boolean canRegister(String couponName) {
Coupon coupon = couponService.validateCouponPeriod(couponName);
Coupon coupon = couponService.validatePeriod(couponName);

return coupon.getStock() <= couponQueueRepository.queueSize(coupon.getName());
return coupon.getStock() <= couponQueueRepository.size(coupon.getName());
}
}
22 changes: 11 additions & 11 deletions src/main/java/com/moabam/api/application/coupon/CouponService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.moabam.api.domain.coupon.repository.CouponSearchRepository;
import com.moabam.api.domain.member.Role;
import com.moabam.api.dto.coupon.CouponResponse;
import com.moabam.api.dto.coupon.CouponSearchRequest;
import com.moabam.api.dto.coupon.CouponStatusRequest;
import com.moabam.api.dto.coupon.CreateCouponRequest;
import com.moabam.global.auth.model.AuthorizationMember;
import com.moabam.global.common.util.ClockHolder;
Expand All @@ -32,31 +32,31 @@ public class CouponService {
private final ClockHolder clockHolder;

@Transactional
public void createCoupon(AuthorizationMember admin, CreateCouponRequest request) {
public void create(AuthorizationMember admin, CreateCouponRequest request) {
validateAdminRole(admin);
validateConflictCouponName(request.name());
validateCouponPeriod(request.startAt(), request.endAt());
validateConflictName(request.name());
validatePeriod(request.startAt(), request.endAt());

Coupon coupon = CouponMapper.toEntity(admin.id(), request);
couponRepository.save(coupon);
}

@Transactional
public void deleteCoupon(AuthorizationMember admin, Long couponId) {
public void delete(AuthorizationMember admin, Long couponId) {
validateAdminRole(admin);
Coupon coupon = couponRepository.findById(couponId)
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_COUPON));
couponRepository.delete(coupon);
}

public CouponResponse getCouponById(Long couponId) {
public CouponResponse getById(Long couponId) {
Coupon coupon = couponRepository.findById(couponId)
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_COUPON));

return CouponMapper.toDto(coupon);
}

public List<CouponResponse> getCoupons(CouponSearchRequest request) {
public List<CouponResponse> getAllByStatus(CouponStatusRequest request) {
LocalDateTime now = clockHolder.times();
List<Coupon> coupons = couponSearchRepository.findAllByStatus(now, request);

Expand All @@ -65,7 +65,7 @@ public List<CouponResponse> getCoupons(CouponSearchRequest request) {
.toList();
}

public Coupon validateCouponPeriod(String couponName) {
public Coupon validatePeriod(String couponName) {
LocalDateTime now = clockHolder.times();
Coupon coupon = couponRepository.findByName(couponName)
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_COUPON));
Expand All @@ -77,7 +77,7 @@ public Coupon validateCouponPeriod(String couponName) {
throw new BadRequestException(ErrorMessage.INVALID_COUPON_PERIOD_END);
}

private void validateCouponPeriod(LocalDateTime startAt, LocalDateTime endAt) {
private void validatePeriod(LocalDateTime startAt, LocalDateTime endAt) {
if (startAt.isAfter(endAt)) {
throw new BadRequestException(ErrorMessage.INVALID_COUPON_PERIOD);
}
Expand All @@ -89,8 +89,8 @@ private void validateAdminRole(AuthorizationMember admin) {
}
}

private void validateConflictCouponName(String name) {
if (couponRepository.existsByName(name)) {
private void validateConflictName(String couponName) {
if (couponRepository.existsByName(couponName)) {
throw new ConflictException(ErrorMessage.CONFLICT_COUPON_NAME);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,50 +40,49 @@ public class NotificationService {
private final ClockHolder clockHolder;

@Transactional
public void sendKnockNotification(AuthorizationMember member, Long targetId, Long roomId) {
public void sendKnock(AuthorizationMember member, Long targetId, Long roomId) {
roomService.validateRoomById(roomId);

String knockKey = generateKnockKey(member.id(), targetId, roomId);
validateConflictKnockNotification(knockKey);
validateConflictKnock(knockKey);
validateFcmToken(targetId);

String fcmToken = notificationRepository.findFcmTokenByMemberId(targetId);
String notificationBody = String.format(KNOCK_BODY, member.nickname());
fcmService.sendAsyncFcm(fcmToken, notificationBody);
notificationRepository.saveKnockNotification(knockKey);
fcmService.sendAsync(fcmToken, notificationBody);
notificationRepository.saveKnock(knockKey);
}

@Scheduled(cron = "0 50 * * * *")
public void sendCertificationTimeNotification() {
public void sendCertificationTime() {
int certificationTime = (clockHolder.times().getHour() + ONE_HOUR) % HOURS_IN_A_DAY;
List<Participant> participants = participantSearchRepository.findAllByRoomCertifyTime(certificationTime);

participants.parallelStream().forEach(participant -> {
String roomTitle = participant.getRoom().getTitle();
String fcmToken = notificationRepository.findFcmTokenByMemberId(participant.getMemberId());
String notificationBody = String.format(CERTIFY_TIME_BODY, roomTitle);
fcmService.sendAsyncFcm(fcmToken, notificationBody);
fcmService.sendAsync(fcmToken, notificationBody);
});
}

public List<Long> getMyKnockedNotificationStatusInRoom(Long memberId, Long roomId,
List<Participant> participants) {
public List<Long> getMyKnockStatusInRoom(Long memberId, Long roomId, List<Participant> participants) {
List<Participant> filteredParticipants = participants.stream()
.filter(participant -> !participant.getMemberId().equals(memberId))
.toList();

Predicate<Long> knockPredicate = targetId ->
notificationRepository.existsByKey(generateKnockKey(memberId, targetId, roomId));
notificationRepository.existsKnockByKnockKey(generateKnockKey(memberId, targetId, roomId));

Map<Boolean, List<Long>> knockNotificationStatus = filteredParticipants.stream()
Map<Boolean, List<Long>> knockStatus = filteredParticipants.stream()
.map(Participant::getMemberId)
.collect(Collectors.partitioningBy(knockPredicate));

return knockNotificationStatus.get(true);
return knockStatus.get(true);
}

private void validateConflictKnockNotification(String knockKey) {
if (notificationRepository.existsByKey(knockKey)) {
private void validateConflictKnock(String knockKey) {
if (notificationRepository.existsKnockByKnockKey(knockKey)) {
throw new ConflictException(ErrorMessage.CONFLICT_KNOCK);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private List<TodayCertificateRankResponse> getTodayCertificateRankResponses(Long
.map(Participant::getMemberId)
.toList());

List<Long> myKnockedNotificationStatusInRoom = notificationService.getMyKnockedNotificationStatusInRoom(
List<Long> myKnockedNotificationStatusInRoom = notificationService.getMyKnockStatusInRoom(
memberId, roomId, participants);

addCompletedMembers(responses, dailyMemberCertifications, members, certifications, participants, date,
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/moabam/api/domain/coupon/CouponType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public enum CouponType {
GOLDEN_COUPON("황금"),
DISCOUNT_COUPON("할인");

private final String typeName;
private final String name;
private static final Map<String, CouponType> COUPON_TYPE_MAP;

static {
COUPON_TYPE_MAP = Collections.unmodifiableMap(Arrays.stream(values())
.collect(Collectors.toMap(CouponType::getTypeName, Function.identity())));
.collect(Collectors.toMap(CouponType::getName, Function.identity())));
}

public static CouponType from(String typeName) {
return Optional.ofNullable(COUPON_TYPE_MAP.get(typeName))
public static CouponType from(String name) {
return Optional.ofNullable(COUPON_TYPE_MAP.get(name))
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_COUPON_TYPE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class CouponQueueRepository {

private final ZSetRedisRepository zSetRedisRepository;

public void addQueue(String couponName, String memberNickname, double score) {
public void addIfAbsent(String couponName, String memberNickname, double score) {
zSetRedisRepository.addIfAbsent(requireNonNull(couponName), requireNonNull(memberNickname), score);
}

public Long queueSize(String couponName) {
public Long size(String couponName) {
return zSetRedisRepository.size(requireNonNull(couponName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.stereotype.Repository;

import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.dto.coupon.CouponSearchRequest;
import com.moabam.api.dto.coupon.CouponStatusRequest;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -21,48 +21,48 @@ public class CouponSearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<Coupon> findAllByStatus(LocalDateTime now, CouponSearchRequest request) {
public List<Coupon> findAllByStatus(LocalDateTime now, CouponStatusRequest couponStatus) {
return jpaQueryFactory.selectFrom(coupon)
.where(filterCouponStatus(now, request))
.where(filterStatus(now, couponStatus))
.fetch();
}

private BooleanExpression filterCouponStatus(LocalDateTime now, CouponSearchRequest request) {
if (request.couponOngoing() && request.couponNotStarted() && request.couponEnded()) {
private BooleanExpression filterStatus(LocalDateTime now, CouponStatusRequest couponStatus) {
if (couponStatus.ongoing() && couponStatus.notStarted() && couponStatus.ended()) {
return null;
}

// 시작 전이거나 진행 중인 쿠폰들을 조회하고 싶은 경우
if (request.couponOngoing() && request.couponNotStarted()) {
if (couponStatus.ongoing() && couponStatus.notStarted()) {
return (coupon.startAt.gt(now))
.or(coupon.startAt.loe(now).and(coupon.endAt.goe(now)));
}

// 종료 됐거나 진행 중인 쿠폰들을 조회하고 싶은 경우
if (request.couponOngoing() && request.couponEnded()) {
if (couponStatus.ongoing() && couponStatus.ended()) {
return (coupon.endAt.lt(now))
.or(coupon.startAt.loe(now).and(coupon.endAt.goe(now)));
}

// 진행 중이 아니고, 시작 전이거나, 종료된 쿠폰들을 조회하고 싶은 경우
if (request.couponNotStarted() && request.couponEnded()) {
if (couponStatus.notStarted() && couponStatus.ended()) {
return coupon.startAt.gt(now)
.or(coupon.endAt.lt(now));
}

// 진행 중인 쿠폰들을 조회하고 싶은 경우
if (request.couponOngoing()) {
if (couponStatus.ongoing()) {
return coupon.startAt.loe(now)
.and(coupon.endAt.goe(now));
}

// 시작 적인 쿠폰들을 조회하고 싶은 경우
if (request.couponNotStarted()) {
if (couponStatus.notStarted()) {
return coupon.startAt.gt(now);
}

// 종료된 쿠폰들을 조회하고 싶은 경우
if (request.couponEnded()) {
if (couponStatus.ended()) {
return coupon.endAt.lt(now);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void saveFcmToken(Long memberId, String fcmToken) {
);
}

public void saveKnockNotification(String knockKey) {
public void saveKnock(String knockKey) {
stringRedisRepository.save(
requireNonNull(knockKey),
BLANK,
Expand All @@ -46,8 +46,8 @@ public String findFcmTokenByMemberId(Long memberId) {
return stringRedisRepository.get(String.valueOf(requireNonNull(memberId)));
}

public boolean existsByKey(String key) {
return stringRedisRepository.hasKey(requireNonNull(key));
public boolean existsKnockByKnockKey(String knockKey) {
return stringRedisRepository.hasKey(requireNonNull(knockKey));
}

public boolean existsFcmTokenByMemberId(Long memberId) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/moabam/api/dto/coupon/CouponResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

@Builder
public record CouponResponse(
Long couponId,
String couponAdminName,
Long id,
String adminName,
String name,
String description,
int point,
int stock,
CouponType couponType,
CouponType type,
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm")
LocalDateTime startAt,
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm")
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/moabam/api/dto/coupon/CouponSearchRequest.java

This file was deleted.

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

import lombok.Builder;

@Builder
public record CouponStatusRequest(
boolean ongoing,
boolean notStarted,
boolean ended
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public record CreateCouponRequest(
@NotBlank(message = "쿠폰명이 입력되지 않았거나 20자를 넘었습니다.") @Length(max = 20) String name,
@Length(max = 50, message = "쿠폰 간단 소개는 최대 50자까지 가능합니다.") String description,
@NotBlank(message = "쿠폰 종류를 입력해주세요.") String couponType,
@NotBlank(message = "쿠폰 종류를 입력해주세요.") String type,
@Min(value = 1, message = "벌레 수 혹은 할인 금액은 1 이상이어야 합니다.") int point,
@Min(value = 1, message = "쿠폰 재고는 1 이상이어야 합니다.") int stock,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm")
Expand Down
Loading