Skip to content

Commit

Permalink
Merge pull request #372 from UPbrella/dev
Browse files Browse the repository at this point in the history
0.5.0 release-dev
  • Loading branch information
birdieHyun authored Nov 14, 2023
2 parents bd4733e + 8ee8085 commit 2aa6b46
Show file tree
Hide file tree
Showing 15 changed files with 446 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/docs/asciidoc/api/store/store.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,27 @@ include::{snippets}/store-find-store-introduction-doc/http-request.adoc[]
==== HTTP Response
include::{snippets}/store-find-store-introduction-doc/http-response.adoc[]
include::{snippets}/store-find-store-introduction-doc/response-fields-data.adoc[]

=== 보관함 전체 조회
==== HTTP Request
include::{snippets}/find-all-lockers/http-request.adoc[]

==== HTTP Response
include::{snippets}/find-all-lockers/http-response.adoc[]
include::{snippets}/find-all-lockers/response-fields-data.adoc[]

=== 보관함 생성
==== HTTP Request
include::{snippets}/create-locker/http-request.adoc[]
include::{snippets}/create-locker/request-fields.adoc[]

=== 보관함 수정
==== HTTP Request
include::{snippets}/update-locker/http-request.adoc[]
include::{snippets}/update-locker/path-parameters.adoc[]
include::{snippets}/update-locker/request-fields.adoc[]

=== 보관함 삭제
==== HTTP Request
include::{snippets}/delete-locker/http-request.adoc[]
include::{snippets}/delete-locker/path-parameters.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public ResponseEntity<CustomResponse<LockerPasswordResponse>> rentUmbrellaByUser
SessionUser user = (SessionUser) httpSession.getAttribute("user");
User userToRent = userService.findUserById(user.getId());

LockerPasswordResponse lockerPasswordResponse = lockerService.findLockerPassword(rentUmbrellaByUserRequest);

rentService.addRental(rentUmbrellaByUserRequest, userToRent);

LockerPasswordResponse lockerPasswordResponse = lockerService.findLockerPassword(rentUmbrellaByUserRequest);
log.info("UBU 우산 대여 성공");

return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,16 @@ public ResponseEntity<CustomErrorResponse> missingUmbrellaException(MissingUmbre
e.getMessage()
));
}

@ExceptionHandler(CannotBeRentedException.class)
public ResponseEntity<CustomErrorResponse> cannotBeRentedException(CannotBeRentedException e) {

return ResponseEntity
.badRequest()
.body(new CustomErrorResponse(
"fail",
400,
e.getMessage()
));
}
}
5 changes: 5 additions & 0 deletions src/main/java/upbrella/be/rent/entity/Locker.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public void updateCount() {
public void updateLastAccess(LocalDateTime now) {
this.lastAccess = now;
}

public void updateLocker(StoreMeta storeMeta, String secretKey) {
this.storeMeta = storeMeta;
this.secretKey = secretKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package upbrella.be.rent.exception;

public class CannotBeRentedException extends RuntimeException {

public CannotBeRentedException(String message) {

super(message);
}

}
61 changes: 59 additions & 2 deletions src/main/java/upbrella/be/rent/service/LockerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,69 @@
import upbrella.be.rent.exception.LockerSignatureErrorException;
import upbrella.be.rent.exception.NoSignatureException;
import upbrella.be.rent.repository.LockerRepository;
import upbrella.be.store.dto.request.CreateLockerRequest;
import upbrella.be.store.dto.request.UpdateLockerRequest;
import upbrella.be.store.dto.response.AllLockerResponse;
import upbrella.be.store.dto.response.SingleLockerResponse;
import upbrella.be.store.entity.StoreMeta;
import upbrella.be.store.service.StoreMetaService;
import upbrella.be.util.HotpGenerator;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class LockerService {

private final LockerRepository lockerRepository;
private final StoreMetaService storeMetaService;

public AllLockerResponse findAll() {
List<Locker> all = lockerRepository.findAll();

return new AllLockerResponse(all.stream()
.map(SingleLockerResponse::fromLocker)
.collect(Collectors.toList()));

}

@Transactional
public void createLocker(CreateLockerRequest request) {

isMultipleLockers(request.getStoreId());

StoreMeta storeMeta = storeMetaService.findStoreMetaById(request.getStoreId());
Locker locker = Locker.builder()
.storeMeta(storeMeta)
.secretKey(request.getSecretKey())
.build();

lockerRepository.save(locker);
}

@Transactional
public void updateLocker(Long lockerId, UpdateLockerRequest request) {
isMultipleLockers(request.getStoreId());

StoreMeta storeMeta = storeMetaService.findStoreMetaById(request.getStoreId());

lockerRepository.findById(lockerId)
.ifPresent(locker -> locker.updateLocker(storeMeta, request.getSecretKey()));
}

public void deleteLocker(Long lockerId) {

if(!lockerRepository.existsById(lockerId)) {
throw new IllegalArgumentException("해당 보관함이 존재하지 않습니다.");
}
lockerRepository.deleteById(lockerId);
}

@Transactional
public LockerPasswordResponse findLockerPassword(RentUmbrellaByUserRequest rentUmbrellaByUserRequest) {
Expand Down Expand Up @@ -82,7 +132,7 @@ private String encodeHash(String lockerSecretKey, String salt) {
throw new RuntimeException(e);
}

byte[] encodedhash = digest.digest((lockerSecretKey + "." + salt).getBytes(StandardCharsets.UTF_8));
byte[] encodedhash = digest.digest((lockerSecretKey.toUpperCase() + "." + salt.toUpperCase()).getBytes(StandardCharsets.UTF_8));

// 바이트 배열을 16진수 문자열로 변환
StringBuilder hexString = new StringBuilder(2 * encodedhash.length);
Expand All @@ -94,7 +144,14 @@ private String encodeHash(String lockerSecretKey, String salt) {
}
hexString.append(hex);
}
return hexString.toString();
return hexString.toString().toUpperCase();
}

private void isMultipleLockers(Long storeId) {
Optional<Locker> byStoreMetaId = lockerRepository.findByStoreMetaId(storeId);
if(byStoreMetaId.isPresent()) {
throw new IllegalArgumentException("이미 보관함이 존재합니다.");
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/upbrella/be/rent/service/RentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public RentFormResponse findRentForm(long umbrellaId) {

Umbrella umbrella = umbrellaService.findUmbrellaById(umbrellaId);

if (umbrella.validateCannotBeRented()) {
throw new CannotBeRentedException("[ERROR] 해당 우산은 대여 불가능한 우산입니다.");
}

return RentFormResponse.of(umbrella);
}

Expand Down
73 changes: 73 additions & 0 deletions src/main/java/upbrella/be/store/controller/LockerController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package upbrella.be.store.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import upbrella.be.rent.service.LockerService;
import upbrella.be.store.dto.request.CreateLockerRequest;
import upbrella.be.store.dto.request.UpdateLockerRequest;
import upbrella.be.store.dto.response.AllLockerResponse;
import upbrella.be.util.CustomResponse;

@RestController
@RequiredArgsConstructor
public class LockerController {

private final LockerService lockerService;

@GetMapping("/admin/lockers")
public ResponseEntity<CustomResponse<AllLockerResponse>> getLockers() {

AllLockerResponse lockers = lockerService.findAll();

return ResponseEntity
.ok()
.body(new CustomResponse<>(
"success",
200,
"보관함 조회 성공",
lockers));
}

@PostMapping("/admin/lockers")
public ResponseEntity<CustomResponse<Void>> createLocker(@RequestBody CreateLockerRequest request) {

lockerService.createLocker(request);

return ResponseEntity
.ok()
.body(new CustomResponse<>(
"success",
200,
"보관함 생성 성공",
null));
}

@PatchMapping("/admin/lockers/{lockerId}")
public ResponseEntity<CustomResponse<Void>> updateLocker(@PathVariable Long lockerId, @RequestBody UpdateLockerRequest request) {

lockerService.updateLocker(lockerId, request);

return ResponseEntity
.ok()
.body(new CustomResponse<>(
"success",
200,
"보관함 업데이트 성공",
null));
}

@DeleteMapping("/admin/lockers/{lockerId}")
public ResponseEntity<CustomResponse<Void>> deleteLocker(@PathVariable Long lockerId) {

lockerService.deleteLocker(lockerId);

return ResponseEntity
.ok()
.body(new CustomResponse<>(
"success",
200,
"보관함 삭제 성공",
null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package upbrella.be.store.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import upbrella.be.util.CustomErrorResponse;

@RestControllerAdvice
public class LockerExceptionHandler {

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<CustomErrorResponse> nonExistingStoreDetail(IllegalArgumentException ex) {

return ResponseEntity
.badRequest()
.body(new CustomErrorResponse(
"bad request",
400,
ex.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package upbrella.be.store.dto.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CreateLockerRequest {

private long storeId;
private String secretKey;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package upbrella.be.store.dto.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UpdateLockerRequest {

private long storeId;
private String secretKey;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package upbrella.be.store.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AllLockerResponse {

private List<SingleLockerResponse> lockers;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package upbrella.be.store.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import upbrella.be.rent.entity.Locker;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SingleLockerResponse {

private long id;
private long storeMetaId;
private String secretKey;

public static SingleLockerResponse fromLocker(Locker locker) {

return SingleLockerResponse.builder()
.id(locker.getId())
.storeMetaId(locker.getStoreMeta().getId())
.secretKey(locker.getSecretKey())
.build();
}
}
7 changes: 7 additions & 0 deletions src/main/java/upbrella/be/umbrella/entity/Umbrella.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Umbrella {
private boolean missed;

public void delete() {

this.deleted = true;
}

Expand Down Expand Up @@ -73,6 +74,12 @@ public void rentUmbrella() {
}

public void returnUmbrella() {

this.rentable = true;
}

public boolean validateCannotBeRented() {

return missed || deleted || !rentable;
}
}
Loading

0 comments on commit 2aa6b46

Please sign in to comment.