Skip to content

Commit

Permalink
[Feat] 보관함 crud api 생성 #363 (#371)
Browse files Browse the repository at this point in the history
* feat: 모든 보관함 조회 api 추가

* feat:보관함 생성 api

* feat: 보관함 수정 api 완성

* feat: 보관함 삭제 api

* docs: 보관함 crud api 문서화

* fix: signature를 대문자로 비교하기 위해 코드 수정
  • Loading branch information
birdieHyun authored Nov 14, 2023
1 parent 8eb301d commit 8ee8085
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 2 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[]
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;
}
}
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
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();
}
}
Loading

0 comments on commit 8ee8085

Please sign in to comment.