Skip to content

Commit

Permalink
Merge branch 'dev' into hot-flx/#439
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 authored Sep 14, 2023
2 parents 2f05d33 + d5bc766 commit c7544d3
Show file tree
Hide file tree
Showing 29 changed files with 358 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.festago.application;

import com.festago.domain.EntryCode;
import com.festago.domain.EntryCodeExtractor;
import com.festago.domain.EntryCodePayload;
import com.festago.domain.EntryCodeProvider;
import java.util.Date;
import org.springframework.stereotype.Component;

@Component
public class EntryCodeManager {

private static final int MILLISECOND_FACTOR = 1_000;
private static final int DEFAULT_PERIOD = 30;
private static final int DEFAULT_OFFSET = 10;

private final EntryCodeProvider entryCodeProvider;
private final EntryCodeExtractor entryCodeExtractor;

public EntryCodeManager(EntryCodeProvider entryCodeProvider, EntryCodeExtractor entryCodeExtractor) {
this.entryCodeProvider = entryCodeProvider;
this.entryCodeExtractor = entryCodeExtractor;
}

public EntryCode provide(EntryCodePayload entryCodePayload, long currentTimeMillis) {
Date expiredAt = new Date(currentTimeMillis + (DEFAULT_PERIOD + DEFAULT_OFFSET) * MILLISECOND_FACTOR);
String code = entryCodeProvider.provide(entryCodePayload, expiredAt);
return new EntryCode(code, DEFAULT_PERIOD, DEFAULT_OFFSET);
}

public EntryCodePayload extract(String code) {
return entryCodeExtractor.extract(code);
}
}
15 changes: 5 additions & 10 deletions backend/src/main/java/com/festago/application/EntryService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.festago.application;

import com.festago.domain.EntryCode;
import com.festago.domain.EntryCodeExtractor;
import com.festago.domain.EntryCodePayload;
import com.festago.domain.EntryCodeProvider;
import com.festago.domain.MemberTicket;
import com.festago.domain.MemberTicketRepository;
import com.festago.dto.EntryCodeResponse;
Expand All @@ -21,15 +19,12 @@
@Transactional
public class EntryService {

private final EntryCodeProvider entryCodeProvider;
private final EntryCodeExtractor entryCodeExtractor;
private final EntryCodeManager entryCodeManager;
private final MemberTicketRepository memberTicketRepository;
private final Clock clock;

public EntryService(EntryCodeProvider entryCodeProvider, EntryCodeExtractor entryCodeExtractor,
MemberTicketRepository memberTicketRepository, Clock clock) {
this.entryCodeProvider = entryCodeProvider;
this.entryCodeExtractor = entryCodeExtractor;
public EntryService(EntryCodeManager entryCodeManager, MemberTicketRepository memberTicketRepository, Clock clock) {
this.entryCodeManager = entryCodeManager;
this.memberTicketRepository = memberTicketRepository;
this.clock = clock;
}
Expand All @@ -42,7 +37,7 @@ public EntryCodeResponse createEntryCode(Long memberId, Long memberTicketId) {
if (!memberTicket.canEntry(LocalDateTime.now(clock))) {
throw new BadRequestException(ErrorCode.NOT_ENTRY_TIME);
}
EntryCode entryCode = EntryCode.create(entryCodeProvider, memberTicket);
EntryCode entryCode = entryCodeManager.provide(EntryCodePayload.from(memberTicket), clock.millis());
return EntryCodeResponse.of(entryCode);
}

Expand All @@ -52,7 +47,7 @@ private MemberTicket findMemberTicket(Long memberTicketId) {
}

public TicketValidationResponse validate(TicketValidationRequest request) {
EntryCodePayload entryCodePayload = entryCodeExtractor.extract(request.code());
EntryCodePayload entryCodePayload = entryCodeManager.extract(request.code());
MemberTicket memberTicket = findMemberTicket(entryCodePayload.getMemberTicketId());
memberTicket.changeState(entryCodePayload.getEntryState());
return TicketValidationResponse.from(memberTicket);
Expand Down
5 changes: 4 additions & 1 deletion backend/src/main/java/com/festago/auth/dto/LoginRequest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.festago.auth.dto;

import com.festago.auth.domain.SocialType;
import jakarta.validation.constraints.NotNull;

public record LoginRequest(SocialType socialType, String accessToken) {
public record LoginRequest(
@NotNull(message = "socialType 은 null 일 수 μ—†μŠ΅λ‹ˆλ‹€.") SocialType socialType,
@NotNull(message = "acessToken 은 null 일 수 μ—†μŠ΅λ‹ˆλ‹€.") String accessToken) {

}
10 changes: 0 additions & 10 deletions backend/src/main/java/com/festago/domain/EntryCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import com.festago.exception.ErrorCode;
import com.festago.exception.InternalServerException;
import java.util.Date;

public class EntryCode {

private static final long DEFAULT_PERIOD = 30;
private static final long DEFAULT_OFFSET = 10;
private static final int MILLISECOND_FACTOR = 1000;
private static final int MINIMUM_PERIOD = 0;
private static final int MINIMUM_OFFSET = 0;

Expand Down Expand Up @@ -36,12 +32,6 @@ private boolean isNegative(long offset) {
return offset < MINIMUM_OFFSET;
}

public static EntryCode create(EntryCodeProvider entryCodeProvider, MemberTicket memberTicket) {
Date expiredAt = new Date(new Date().getTime() + (DEFAULT_PERIOD + DEFAULT_OFFSET) * MILLISECOND_FACTOR);
String code = entryCodeProvider.provide(EntryCodePayload.from(memberTicket), expiredAt);
return new EntryCode(code, DEFAULT_PERIOD, DEFAULT_OFFSET);
}

public String getCode() {
return code;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public EntryCodePayload(Long memberTicketId, EntryState entryState) {
this.entryState = entryState;
}

public static EntryCodePayload from(MemberTicket memberTicket) {
return new EntryCodePayload(memberTicket.getId(), memberTicket.getEntryState());
}

private void validate(Long memberTicketId, EntryState entryState) {
if (memberTicketId == null || entryState == null) {
throw new InternalServerException(ErrorCode.INVALID_ENTRY_CODE_PAYLOAD);
}
}

public static EntryCodePayload from(MemberTicket memberTicket) {
return new EntryCodePayload(memberTicket.getId(), memberTicket.getEntryState());
}

public Long getMemberTicketId() {
return memberTicketId;
}
Expand Down
41 changes: 39 additions & 2 deletions backend/src/main/java/com/festago/domain/Festival.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDate;
import java.time.LocalDateTime;

Expand All @@ -18,12 +20,18 @@ public class Festival extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Size(max = 50)
private String name;

@NotNull
private LocalDate startDate;

@NotNull
private LocalDate endDate;

@NotNull
@Size(max = 255)
private String thumbnail;

protected Festival() {
Expand All @@ -38,15 +46,44 @@ public Festival(String name, LocalDate startDate, LocalDate endDate, String thum
}

public Festival(Long id, String name, LocalDate startDate, LocalDate endDate, String thumbnail) {
validate(startDate, endDate);
validate(name, startDate, endDate, thumbnail);
this.id = id;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
this.thumbnail = thumbnail;
}

private void validate(LocalDate startDate, LocalDate endDate) {
private void validate(String name, LocalDate startDate, LocalDate endDate, String thumbnail) {
checkNotNull(name, startDate, endDate, thumbnail);
checkLength(name, thumbnail);
validateDate(startDate, endDate);
}

private void checkNotNull(String name, LocalDate startDate, LocalDate endDate, String thumbnail) {
if (name == null ||
startDate == null ||
endDate == null ||
thumbnail == null) {
throw new IllegalArgumentException("Festival 은 ν—ˆμš©λ˜μ§€ μ•Šμ€ null κ°’μœΌλ‘œ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
}

private void checkLength(String name, String thumbnail) {
if (overLength(name, 50) ||
overLength(thumbnail, 255)) {
throw new IllegalArgumentException("Festival 의 ν•„λ“œλ‘œ ν—ˆμš©λœ 길이λ₯Ό λ„˜μ€ column 을 넣을 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
}

private boolean overLength(String target, int maxLength) {
if (target == null) {
return false;
}
return target.length() > maxLength;
}

private void validateDate(LocalDate startDate, LocalDate endDate) {
if (startDate.isAfter(endDate)) {
throw new BadRequestException(ErrorCode.INVALID_FESTIVAL_DURATION);
}
Expand Down
42 changes: 40 additions & 2 deletions backend/src/main/java/com/festago/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
Expand All @@ -17,18 +19,25 @@
public class Member extends BaseTimeEntity {

private static final String DEFAULT_IMAGE_URL = "https://festa-go.site/images/default-profile.png";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Size(max = 255)
private String socialId;

@Enumerated(EnumType.STRING)
@NotNull
@Enumerated(value = EnumType.STRING)
private SocialType socialType;

@NotNull
@Size(max = 30)
private String nickname;

@NotNull
@Size(max = 255)
private String profileImage;

private LocalDateTime deletedAt = null;
Expand All @@ -45,13 +54,42 @@ public Member(String socialId, SocialType socialType, String nickname, String pr
}

public Member(Long id, String socialId, SocialType socialType, String nickname, String profileImage) {
validate(socialId, socialType, nickname, profileImage);
this.id = id;
this.socialId = socialId;
this.socialType = socialType;
this.nickname = nickname;
this.profileImage = (profileImage != null) ? profileImage : DEFAULT_IMAGE_URL;
}

private void validate(String socialId, SocialType socialType, String nickname, String profileImage) {
checkNotNull(socialId, socialType, nickname);
checkLength(socialId, nickname, profileImage);
}

private void checkNotNull(String socialId, SocialType socialType, String nickname) {
if (socialId == null ||
socialType == null ||
nickname == null) {
throw new IllegalArgumentException("Member λŠ” ν—ˆμš©λ˜μ§€ μ•Šμ€ null κ°’μœΌλ‘œ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
}

private void checkLength(String socialId, String nickname, String profileImage) {
if (overLength(socialId, 255) ||
overLength(nickname, 30) ||
overLength(profileImage, 255)) {
throw new IllegalArgumentException("Member 의 ν•„λ“œλ‘œ ν—ˆμš©λœ 길이λ₯Ό λ„˜μ€ column 을 넣을 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
}

private boolean overLength(String target, int maxLength) {
if (target == null) {
return false;
}
return target.length() > maxLength;
}

public Long getId() {
return id;
}
Expand Down
29 changes: 29 additions & 0 deletions backend/src/main/java/com/festago/domain/MemberTicket.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Objects;

Expand All @@ -20,19 +22,25 @@ public class MemberTicket extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Enumerated(EnumType.STRING)
private EntryState entryState = EntryState.BEFORE_ENTRY;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
private Member owner;

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
private Stage stage;

@Min(value = 0)
private int number;

@NotNull
private LocalDateTime entryTime;

@NotNull
@Enumerated(EnumType.STRING)
private TicketType ticketType;

Expand All @@ -45,6 +53,7 @@ public MemberTicket(Member owner, Stage stage, int number, LocalDateTime entryTi

public MemberTicket(Long id, Member owner, Stage stage, int number, LocalDateTime entryTime,
TicketType ticketType) {
validate(owner, stage, number, entryTime, ticketType);
this.id = id;
this.owner = owner;
this.stage = stage;
Expand All @@ -53,6 +62,26 @@ public MemberTicket(Long id, Member owner, Stage stage, int number, LocalDateTim
this.ticketType = ticketType;
}

private void validate(Member owner, Stage stage, int number, LocalDateTime entryTime, TicketType ticketType) {
checkNotNull(owner, stage, entryTime, ticketType);
checkScope(number);
}

private void checkNotNull(Member owner, Stage stage, LocalDateTime entryTime, TicketType ticketType) {
if (owner == null ||
stage == null ||
entryTime == null ||
ticketType == null) {
throw new IllegalArgumentException("MemberTicket 은 ν—ˆμš©λ˜μ§€ μ•Šμ€ null κ°’μœΌλ‘œ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
}

private void checkScope(int number) {
if (number < 0) {
throw new IllegalArgumentException("MemberTicket 의 ν•„λ“œλ‘œ ν—ˆμš©λœ λ²”μœ„λ₯Ό λ„˜μ€ column 을 넣을 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
}

public void changeState(EntryState originState) {
if (originState != this.entryState) {
return;
Expand Down
Loading

0 comments on commit c7544d3

Please sign in to comment.