Skip to content

Commit

Permalink
feat: 행사 조회시 비회원에 의해 만들어진 행사인지 여부 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee committed Nov 19, 2024
1 parent 13bff01 commit 4f8e31e
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum HaengdongErrorCode {

IMAGE_NOT_FOUND("존재하지 않는 이미지 입니다."),
IMAGE_COUNT_INVALID("이미지 수량은 %d개 까지 업로드할 수 있습니다."),
USER_NICK_NAME_LENGTH_INVALID("유저의 닉네임은 %d자 이상 %d자 이하만 입력 가능합니다."),

/* Authentication */

Expand Down
38 changes: 16 additions & 22 deletions server/src/main/java/haengdong/event/application/EventService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package haengdong.event.application;

import java.time.Instant;
import java.util.List;
import java.util.Map.Entry;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import haengdong.common.exception.AuthenticationException;
import haengdong.common.exception.HaengdongErrorCode;
import haengdong.common.exception.HaengdongException;
import haengdong.event.application.request.EventAppRequest;
import haengdong.event.application.request.EventGuestAppRequest;
import haengdong.event.application.request.EventLoginAppRequest;
Expand All @@ -16,20 +12,24 @@
import haengdong.event.application.response.EventImageAppResponse;
import haengdong.event.application.response.EventImageSaveAppResponse;
import haengdong.event.application.response.MemberBillReportAppResponse;
import haengdong.event.application.response.UserAppResponse;
import haengdong.event.domain.RandomValueProvider;
import haengdong.event.domain.bill.Bill;
import haengdong.event.domain.bill.BillRepository;
import haengdong.event.domain.bill.MemberBillReport;
import haengdong.event.domain.event.Event;
import haengdong.event.domain.event.EventRepository;
import haengdong.event.domain.event.image.EventImage;
import haengdong.event.domain.event.image.EventImageRepository;
import haengdong.event.domain.event.EventRepository;
import haengdong.event.domain.event.member.EventMember;
import haengdong.event.domain.event.member.EventMemberRepository;
import haengdong.common.exception.AuthenticationException;
import haengdong.common.exception.HaengdongErrorCode;
import haengdong.common.exception.HaengdongException;
import haengdong.user.application.UserService;
import java.time.Instant;
import java.util.List;
import java.util.Map.Entry;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -45,9 +45,6 @@ public class EventService {
private final EventMemberRepository eventMemberRepository;
private final UserService userService;

@Value("${image.base-url}")
private String baseUrl;

@Transactional
public EventAppResponse saveEventGuest(EventGuestAppRequest request) {
Long userId = userService.joinGuest(request.toUserRequest());
Expand All @@ -73,8 +70,10 @@ public EventAppResponse saveEvent(EventAppRequest request) {

public EventDetailAppResponse findEvent(String token) {
Event event = getEvent(token);
Long userId = event.getUserId();
UserAppResponse user = userService.findById(userId);

return EventDetailAppResponse.of(event);
return EventDetailAppResponse.of(event, user);
}

public EventAppResponse findByGuestPassword(EventLoginAppRequest request) {
Expand Down Expand Up @@ -107,14 +106,9 @@ private MemberBillReportAppResponse createMemberBillReportResponse(Entry<EventMe
}

@Transactional
public void updateEvent(String token, EventUpdateAppRequest request) {
public void updateEventName(String token, EventUpdateAppRequest request) {
Event event = getEvent(token);
if (request.isEventNameExist()) {
event.rename(request.eventName());
}
if (request.isAccountExist()) {
event.changeAccount(request.bankName(), request.accountNumber());
}
event.rename(request.eventName());
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
package haengdong.event.application.request;

public record EventUpdateAppRequest(
String eventName,
String bankName,
String accountNumber
String eventName
) {

public boolean isEventNameExist() {
return eventName != null && !eventName.trim().isEmpty();
}

public boolean isAccountExist() {
return bankName != null && !bankName.trim().isEmpty()
&& accountNumber != null && !accountNumber.trim().isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
public record EventDetailAppResponse(
String eventName,
String bankName,
String accountNumber
String accountNumber,
Boolean createdByGuest
) {

public static EventDetailAppResponse of(Event event) {
return new EventDetailAppResponse(event.getName(), event.getBankName(), event.getAccountNumber());
public static EventDetailAppResponse of(Event event, UserAppResponse user) {
return new EventDetailAppResponse(event.getName(), user.bankName(), user.accountNumber(), user.isGuest());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package haengdong.event.application.response;

import haengdong.user.domain.User;

public record UserAppResponse(
String bankName,
String accountNumber,
Boolean isGuest
) {
public static UserAppResponse of(User user) {
return new UserAppResponse(user.getBank(), user.getAccountNumber(), user.isGuest());
}
}
50 changes: 4 additions & 46 deletions server/src/main/java/haengdong/event/domain/event/Event.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package haengdong.event.domain.event;

import haengdong.user.domain.Bank;
import haengdong.common.domain.BaseEntity;
import haengdong.common.exception.HaengdongErrorCode;
import haengdong.common.exception.HaengdongException;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.util.Arrays;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import haengdong.common.domain.BaseEntity;
import haengdong.common.exception.HaengdongErrorCode;
import haengdong.common.exception.HaengdongException;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -23,8 +21,6 @@ public class Event extends BaseEntity {

private static final int MIN_NAME_LENGTH = 1;
private static final int MAX_NAME_LENGTH = 20;
private static final int MIN_ACCOUNT_NUMBER_LENGTH = 8;
private static final int MAX_ACCOUNT_NUMBER_LENGTH = 30;
private static final String SPACES = " ";

@Id
Expand All @@ -41,18 +37,14 @@ public class Event extends BaseEntity {
@Column(nullable = false, unique = true)
private String token;

@Column(length = MAX_ACCOUNT_NUMBER_LENGTH)
private String account;

@Column(nullable = false)
private Long userId;

public Event(String name, String password, String token) {
private Event(String name, String password, String token) {
validateName(name);
this.name = name;
this.password = new Password(password);
this.token = token;
this.account = "";
this.userId = 0L;
}

Expand Down Expand Up @@ -90,38 +82,4 @@ public void rename(String name) {
validateName(name);
this.name = name;
}

public void changeAccount(String bankName, String accountNumber) {
validateBankName(bankName);
validateAccountNumber(accountNumber);
this.account = bankName + " " + accountNumber;
}

private void validateBankName(String bankName) {
Bank.isExists(bankName);
}

private void validateAccountNumber(String accountNumber) {
int accountLength = accountNumber.trim().length();
if (accountLength < MIN_ACCOUNT_NUMBER_LENGTH || MAX_ACCOUNT_NUMBER_LENGTH < accountLength) {
throw new HaengdongException(
HaengdongErrorCode.ACCOUNT_LENGTH_INVALID, MIN_ACCOUNT_NUMBER_LENGTH, MAX_ACCOUNT_NUMBER_LENGTH);
}
}

public String getBankName() {
String[] bankNameAndAccountNumber = account.split(" ");
if (bankNameAndAccountNumber.length > 0) {
return bankNameAndAccountNumber[0];
}
return "";
}

public String getAccountNumber() {
String[] bankNameAndAccountNumber = account.split(" ");
if (bankNameAndAccountNumber.length > 1) {
return String.join(" ", Arrays.copyOfRange(bankNameAndAccountNumber, 1, bankNameAndAccountNumber.length));
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public ResponseEntity<Void> updateEvent(
@PathVariable("eventId") String token,
@Valid @RequestBody EventUpdateRequest request
) {
eventService.updateEvent(token, request.toAppRequest());

eventService.updateEventName(token, request.toAppRequest());
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package haengdong.event.presentation.request;

import haengdong.event.application.request.EventUpdateAppRequest;
import jakarta.validation.constraints.NotBlank;

public record EventUpdateRequest(
String eventName,
String bankName,
String accountNumber
@NotBlank
String eventName
) {

public EventUpdateAppRequest toAppRequest() {
return new EventUpdateAppRequest(eventName, bankName, accountNumber);
return new EventUpdateAppRequest(eventName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
public record EventDetailResponse(
String eventName,
String bankName,
String accountNumber
String accountNumber,
Boolean createdByGuest
) {

public static EventDetailResponse of(EventDetailAppResponse response) {
return new EventDetailResponse(response.eventName(), response.bankName(), response.accountNumber());
return new EventDetailResponse(response.eventName(), response.bankName(), response.accountNumber(), response.createdByGuest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import haengdong.common.exception.AuthenticationException;
import haengdong.common.exception.HaengdongErrorCode;
import haengdong.common.exception.HaengdongException;
import haengdong.event.application.response.UserAppResponse;
import haengdong.user.application.request.UserGuestSaveAppRequest;
import haengdong.user.application.request.UserUpdateAppRequest;
import haengdong.user.domain.User;
Expand All @@ -14,8 +15,8 @@
@RequiredArgsConstructor
@Service
public class UserService {

private final UserRepository userRepository;

private final KakaoClient kakaoClient;

@Transactional
Expand Down Expand Up @@ -66,6 +67,12 @@ public void withdraw(Long id) {
userRepository.deleteById(id);
}

public UserAppResponse findById(Long id) {
User user = getUser(id);

return UserAppResponse.of(user);
}

private User getUser(Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.PASSWORD_INVALID));
Expand Down
30 changes: 26 additions & 4 deletions server/src/main/java/haengdong/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package haengdong.user.domain;

import haengdong.event.domain.event.Password;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -21,6 +24,8 @@ public class User extends BaseEntity {

private static final int MIN_ACCOUNT_NUMBER_LENGTH = 8;
private static final int MAX_ACCOUNT_NUMBER_LENGTH = 30;
private static final int MIN_NICK_NAME_LENGTH = 1;
private static final int MAX_NICK_NAME_LENGTH = 8;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -29,7 +34,9 @@ public class User extends BaseEntity {
@Column(nullable = false)
private String nickname;

private String password;
@Embedded
@AttributeOverride(name = "value", column = @Column(name = "password"))
private Password password;

private String bank;

Expand All @@ -41,27 +48,38 @@ public class User extends BaseEntity {

private User(String nickname, String password, String bank, String accountNumber, String memberNumber) {
this.nickname = nickname;
this.password = password;
this.password = new Password(password);
this.bank = bank;
this.accountNumber = accountNumber;
this.memberNumber = memberNumber;
}

public static User createGuest(String nickName, String password) {
return new User(nickName, password, null, null, null);
return new User(nickName, password, "", "", "");
}

public static User createMember(String nickName, String memberNumber) {
return new User(nickName, null, null, null, memberNumber);
return new User(nickName, "0000", "", "", memberNumber);
}

public void changeNickname(String nickname) {
validateNickname(nickname);
this.nickname = nickname;
}

private void validateNickname(String nickname) {
int nicknameLength = nickname.trim().length();
if (nicknameLength < MIN_NICK_NAME_LENGTH || MAX_NICK_NAME_LENGTH < nicknameLength) {
throw new HaengdongException(
HaengdongErrorCode.USER_NICK_NAME_LENGTH_INVALID, MIN_NICK_NAME_LENGTH, MAX_NICK_NAME_LENGTH);
}
}

public void changeAccount(String bankName, String accountNumber) {
validateBankName(bankName);
validateAccountNumber(accountNumber);
this.bank = bankName;
this.accountNumber = accountNumber;
}

private void validateBankName(String bankName) {
Expand All @@ -79,4 +97,8 @@ private void validateAccountNumber(String accountNumber) {
public boolean isPasswordMismatch(String rawPassword) {
return !password.matches(rawPassword);
}

public boolean isGuest() {
return memberNumber == null || memberNumber.isBlank();
}
}
Loading

0 comments on commit 4f8e31e

Please sign in to comment.