Skip to content

Commit

Permalink
Merge pull request #339 from UPbrella/dev
Browse files Browse the repository at this point in the history
v 1.0.0 release
  • Loading branch information
birdieHyun authored Oct 13, 2023
2 parents f4f59cf + fc3aa7a commit 4fe2d57
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/upbrella/be/rent/service/RentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public ReturnFormResponse findReturnForm(long storeId, User userToReturn) {

@Transactional
public void addRental(RentUmbrellaByUserRequest rentUmbrellaByUserRequest, User userToRent) {
userService.checkBlackList(userToRent.getId());

rentRepository.findByUserIdAndReturnedAtIsNull(userToRent.getId())
.ifPresent(history -> {
Expand Down Expand Up @@ -93,6 +94,7 @@ public void addRental(RentUmbrellaByUserRequest rentUmbrellaByUserRequest, User
@Transactional
public void returnUmbrellaByUser(User userToReturn, ReturnUmbrellaByUserRequest request) {

userService.checkBlackList(userToReturn.getId());
History history = rentRepository.findByUserIdAndReturnedAtIsNull(userToReturn.getId())
.orElseThrow(() -> new NonExistingUmbrellaForRentException("[ERROR] 해당 유저가 대여 중인 우산이 없습니다."));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,17 @@ public UmbrellaWithHistory(long id, StoreMeta storeMeta, long uuid, boolean rent
this.missed = missed;
this.historyId = historyId;
}

@QueryProjection
public UmbrellaWithHistory(long id, StoreMeta storeMeta, long uuid, boolean rentable, boolean deleted, LocalDateTime createdAt, String etc, boolean missed) {
this.id = id;
this.storeMeta = storeMeta;
this.uuid = uuid;
this.rentable = rentable;
this.deleted = deleted;
this.createdAt = createdAt;
this.etc = etc;
this.missed = missed;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public long countMissingUmbrellasByStore(long storeId) {
.fetchCount();
}

// TODO : history와 연동
@Override
public List<UmbrellaWithHistory> findUmbrellaAndHistoryOrderedByUmbrellaId(Pageable pageable) {

Expand All @@ -105,16 +106,13 @@ public List<UmbrellaWithHistory> findUmbrellaAndHistoryOrderedByUmbrellaId(Pagea
umbrella.deleted,
umbrella.createdAt,
umbrella.etc,
umbrella.missed,
history.id
umbrella.missed
);

return queryFactory.select(umbrellaWithHistory)
.from(history)
.rightJoin(history.umbrella, umbrella)
.from(umbrella)
.join(umbrella.storeMeta)
.where(umbrella.deleted.eq(false)
.and(history.returnedAt.isNull()))
.where(umbrella.deleted.eq(false))
.orderBy(umbrella.id.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/upbrella/be/user/dto/request/JoinRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package upbrella.be.user.dto.request;

import lombok.*;
import upbrella.be.user.validation.OnlyNumbers;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
Expand All @@ -22,5 +23,6 @@ public class JoinRequest {
@Size(max = 40)
private String bank;
@Size(max = 40)
@OnlyNumbers
private String accountNumber;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.*;
import org.hibernate.validator.constraints.Range;
import upbrella.be.user.validation.OnlyNumbers;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
Expand All @@ -17,5 +18,6 @@ public class UpdateBankAccountRequest {
private String bank;
@Size(min = 1, max = 45)
@NotBlank
@OnlyNumbers
private String accountNumber;
}
9 changes: 9 additions & 0 deletions src/main/java/upbrella/be/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ public void updateAdminStatus(Long id) {

foundUser.updateAdminStatus();
}

@Transactional(readOnly = true)
public void checkBlackList(Long userId) {

blackListRepository.findById(userId)
.ifPresent(blackList -> {
throw new BlackListUserException("[ERROR] 정지된 회원입니다. 정지된 회원은 이용이 불가능합니다.");
});
}
}
19 changes: 19 additions & 0 deletions src/main/java/upbrella/be/user/validation/OnlyNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package upbrella.be.user.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = OnlyNumbersValidator.class)
public @interface OnlyNumbers {
String message() default "The field should contain only numbers";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package upbrella.be.user.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class OnlyNumbersValidator implements ConstraintValidator<OnlyNumbers, String> {

@Override
public void initialize(OnlyNumbers constraintAnnotation) {
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {

if (value == null) return true;
return value.matches("^\\d+$");
}
}
2 changes: 0 additions & 2 deletions src/test/java/upbrella/be/config/FixtureBuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ private static String pickAccountNumberString() {

StringBuilder sb = new StringBuilder();
sb.append(Arbitraries.integers().between(100, 999).sample())
.append("-")
.append(Arbitraries.integers().between(100, 999).sample())
.append("-")
.append(Arbitraries.integers().between(100000, 999999).sample());

return sb.toString();
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/upbrella/be/rent/service/RentServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import upbrella.be.user.dto.response.SessionUser;
import upbrella.be.user.dto.response.SingleHistoryResponse;
import upbrella.be.user.entity.User;
import upbrella.be.user.exception.BlackListUserException;
import upbrella.be.user.exception.NonExistingMemberException;
import upbrella.be.user.service.UserService;
import upbrella.be.util.AesEncryptor;
Expand Down Expand Up @@ -576,4 +577,19 @@ void notAvailableUmbrellaTest() {
assertThatThrownBy(() -> rentService.addRental(rentUmbrellaByUserRequest, userToRent))
.isInstanceOf(NotAvailableUmbrellaException.class);
}

@Test
@DisplayName("블랙리스트에 등록된 유저가 우산을 대여할 경우 예외가 발생한다.")
void test() {
// given
User user = FixtureBuilderFactory.builderUser(aesEncryptor).sample();
RentUmbrellaByUserRequest request = RentUmbrellaByUserRequest.builder().build();

doThrow(BlackListUserException.class).when(userService).checkBlackList(user.getId());
// when

// then
assertThatThrownBy(() -> rentService.addRental(request, user))
.isInstanceOf(BlackListUserException.class);
}
}
34 changes: 34 additions & 0 deletions src/test/java/upbrella/be/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
Expand Down Expand Up @@ -483,4 +484,37 @@ void notLoginException() {
assertThatThrownBy(() -> userService.findDecryptedUserById(user))
.isInstanceOf(NonExistingMemberException.class);
}

@Test
@DisplayName("사용자가 블랙리스트에 등록되어 있으면 예외가 발생한다.")
void checkBlackListThrowTest() {
// given
BlackList blackList = BlackList.builder()
.id(1L)
.socialId(1L)
.blockedAt(LocalDateTime.now())
.build();

given(blackListRepository.findById(1L)).willReturn(Optional.of(blackList));
// when

// then
assertThatThrownBy(() -> userService.checkBlackList(1L))
.isInstanceOf(BlackListUserException.class);
}

@Test
@DisplayName("사용자가 블랙리스트에 없으면 예외가 발생하지 않는다.")
void checkBlackListNotThrowTest() {
// given

given(blackListRepository.findById(1L)).willReturn(Optional.empty());

// when


// then
assertThatCode(() -> userService.checkBlackList(1L))
.doesNotThrowAnyException();
}
}

0 comments on commit 4fe2d57

Please sign in to comment.