Skip to content

Commit

Permalink
Merge pull request #39 from Team-KeepGoing/feature/device
Browse files Browse the repository at this point in the history
Feat :: book rental logic
  • Loading branch information
miraexhoi authored Jun 10, 2024
2 parents 3e4b868 + 1349404 commit db682bc
Show file tree
Hide file tree
Showing 29 changed files with 302 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.keepgoing.keepserver.domain.book.consts;

public enum BookState {
AVAILABLE, UNAVAILABLE
AVAILABLE,
UNAVAILABLE,
RENTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.keepgoing.keepserver.domain.book.consts.BookState;
import com.keepgoing.keepserver.domain.user.entity.user.User;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -20,7 +21,7 @@ public class Book {
private long id;

@Column(nullable = false)
private String name;
private String bookName;

@Column(updatable = false, nullable = false)
private String nfcCode;
Expand All @@ -37,13 +38,17 @@ public class Book {

private String imageUrl; //책 이미지 링크

@ManyToOne
@JoinColumn(name = "borrower_id")
private User borrower;

@Builder
public Book(String name, String nfcCode, String writer, Date registrationDate, BookState state, String image) {
this.name = name;
public Book(String bookName, String nfcCode, String writer, Date registrationDate, BookState state, String imageUrl) {
this.bookName = bookName;
this.nfcCode = nfcCode;
this.writer = writer;
this.registrationDate = registrationDate;
this.state = state;
this.imageUrl = image;
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.keepgoing.keepserver.domain.book.entity.dto;

import com.keepgoing.keepserver.domain.book.consts.BookState;
import lombok.Getter;

@Getter
public class BookDTO {
private long id;
private String bookName;
private String imageUrl;
private BookState state;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.keepgoing.keepserver.domain.book.repository.dto;
package com.keepgoing.keepserver.domain.book.entity.dto;

import com.keepgoing.keepserver.domain.book.consts.BookState;
import lombok.Getter;


@Getter
public class BookRequestDTO {
private String name;
private String nfcCode;
private String imageUrl;
private BookState state;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.keepgoing.keepserver.domain.book.entity.dto;

import com.keepgoing.keepserver.domain.book.consts.BookState;
import lombok.Builder;

@Builder
public record BookResponseDto(
Long id,
String bookName,
String imageUrl,
BookState state
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.keepgoing.keepserver.domain.book.mapper;

import com.keepgoing.keepserver.domain.book.entity.Book;
import com.keepgoing.keepserver.domain.book.entity.dto.BookResponseDto;
import org.springframework.stereotype.Component;

@Component
public class BookMapper {
public BookResponseDto entityToDto(Book entity) {
return BookResponseDto.builder()
.id(entity.getId())
.bookName(entity.getBookName())
.imageUrl(entity.getImageUrl())
.state(entity.getState())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.keepgoing.keepserver.domain.book.presentation;

import com.keepgoing.keepserver.domain.book.entity.Book;
import com.keepgoing.keepserver.domain.book.repository.dto.BookRequestDTO;
import com.keepgoing.keepserver.domain.book.entity.dto.BookRequestDTO;
import com.keepgoing.keepserver.global.common.BaseResponse;
import com.keepgoing.keepserver.domain.book.service.BookService;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
Book findBookByNfcCode(String NfcCode);
List<Book> findByNameContaining(String name);
List<Book> findByBookNameContaining(String bookName);
Optional<Book> findByBookName(String bookName);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.keepgoing.keepserver.domain.book.service;

import com.keepgoing.keepserver.domain.book.entity.Book;
import com.keepgoing.keepserver.domain.book.repository.dto.BookRequestDTO;
import com.keepgoing.keepserver.domain.book.entity.dto.BookRequestDTO;
import com.keepgoing.keepserver.global.common.BaseResponse;
import jakarta.transaction.Transactional;
import org.springframework.security.core.Authentication;
Expand All @@ -14,7 +14,7 @@ public interface BookService {
BaseResponse selectAllBook();
BaseResponse deleteBook(String nfcCode, Authentication auth);
BaseResponse selectMyBook(Authentication auth);
public String createNfcCode();
String createNfcCode();
@Transactional
BaseResponse editBook(String nfcCode, BookRequestDTO bookRequest) throws IOException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

import com.keepgoing.keepserver.domain.book.consts.BookState;
import com.keepgoing.keepserver.domain.book.entity.Book;
import com.keepgoing.keepserver.domain.book.entity.dto.BookRequestDTO;
import com.keepgoing.keepserver.domain.book.repository.BookRepository;
import com.keepgoing.keepserver.domain.book.repository.dto.BookRequestDTO;
import com.keepgoing.keepserver.global.common.S3.S3Uploader;
import com.keepgoing.keepserver.global.util.GenerateCertCharacter;
import com.keepgoing.keepserver.domain.user.repository.user.UserRepository;
import com.keepgoing.keepserver.global.common.BaseResponse;
import com.keepgoing.keepserver.global.common.S3.S3Uploader;
import com.keepgoing.keepserver.global.util.GenerateCertCharacter;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -48,7 +46,7 @@ public BaseResponse bookRegister(Book book, MultipartFile multipartFile) {

@Override
public BaseResponse selectAllBook() {
return new BaseResponse(HttpStatus.OK, "책 불러오기 성공", (ArrayList<Book>) bookRepository.findAll());
return new BaseResponse(HttpStatus.OK, "책 불러오기 성공", bookRepository.findAll());
}

@Override
Expand All @@ -70,7 +68,7 @@ public BaseResponse deleteBook(String nfcCode, Authentication auth) {
public BaseResponse selectMyBook(Authentication auth) {
//noinspection OptionalGetWithoutIsPresent
String user = userRepository.findByEmail(auth.getName()).get().getEmail();
List<Book> books = bookRepository.findByNameContaining(user);
List<Book> books = bookRepository.findByBookNameContaining(user);

return new BaseResponse(HttpStatus.OK, "책 가져오기 성공", books.toString());
}
Expand All @@ -91,7 +89,7 @@ public BaseResponse editBook(String nfcCode, BookRequestDTO bookRequest) {

if (bookRequest.getState() != null) book.setState(bookRequest.getState());
if (bookRequest.getImageUrl() != null) book.setImageUrl(bookRequest.getImageUrl());
if (bookRequest.getName() != null) book.setName(bookRequest.getName());
if (bookRequest.getName() != null) book.setBookName(bookRequest.getName());
bookRepository.save(book);
return new BaseResponse(HttpStatus.OK, "책 정보 수정 성공");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public class Device {
/*
기기 대여 상태
*/
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private boolean status;
private DeviceStatus status;

/*
대여자 id
Expand All @@ -43,7 +44,7 @@ public class Device {
private User borrower;

@Builder
public Device(String deviceName, boolean status, String imgUrl) {
public Device(String deviceName, DeviceStatus status, String imgUrl) {
this.deviceName = deviceName;
this.status = status;
this.imgUrl = imgUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.keepgoing.keepserver.domain.device.entity;

public enum DeviceStatus {
AVAILABLE,
UNAVAILABLE,
RENTED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.keepgoing.keepserver.domain.device.mapper;

import com.keepgoing.keepserver.domain.device.entity.Device;
import com.keepgoing.keepserver.domain.device.entity.DeviceStatus;
import com.keepgoing.keepserver.domain.device.payload.response.DeviceResponseDto;
import com.keepgoing.keepserver.domain.device.payload.request.DeviceDto;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class DeviceMapper {
public DeviceResponseDto entityToDto(Device entity) {
return DeviceResponseDto.builder()
.id(entity.getId())
.deviceName(entity.getDeviceName())
.imgUrl(entity.getImgUrl())
.status(entity.getStatus())
.build();
}

public Device dtoToEntity(DeviceDto dto) {
return Device.builder()
.deviceName(dto.deviceName())
.imgUrl(dto.imgUrl())
.status(DeviceStatus.AVAILABLE)
.build();
}

public List<DeviceResponseDto> convertDevicesToDtos(List<Device> devices) {
return devices.stream()
.map(this::entityToDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.keepgoing.keepserver.domain.device.payload.request;

import com.keepgoing.keepserver.domain.device.entity.DeviceStatus;
import lombok.Builder;

@Builder
public record DeviceDto (
Long id,
String deviceName,
String imgUrl,
boolean status
){
DeviceStatus status
) {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.keepgoing.keepserver.domain.device.payload.response;

import com.keepgoing.keepserver.domain.device.entity.DeviceStatus;
import lombok.Builder;

@Builder
public record DeviceResponseDto(
public record DeviceResponseDto (
Long id,
String deviceName,
String imgUrl,
boolean status) {
DeviceStatus status
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
@Builder
public record MyDevicesDto (
Long id,
String deviceName ) {
String deviceName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public BaseResponse allDevices(){
}

@Operation(summary = "기자재 생성", description = "기자재를 생성합니다.")
@GetMapping("/create")
public BaseResponse deviceCreate(DeviceDto deviceDto){
@PostMapping("/create")
public BaseResponse deviceCreate(@RequestBody DeviceDto deviceDto){
return deviceService.deviceCreate(deviceDto);
}

Expand All @@ -47,10 +47,4 @@ public BaseResponse myDevices(Authentication authentication){
public BaseResponse deleteDevice(@PathVariable Long id, Authentication authentication){
return deviceService.deleteDevice(id, authentication);
}

@Operation(summary = "기자재 대여", description = "기자재를 대여합니다.")
@PostMapping("/rent")
public BaseResponse rentDevice(@RequestParam String deviceName, @RequestParam String email) {
return deviceService.rentDevice(deviceName, email);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.keepgoing.keepserver.domain.device.service;

import com.keepgoing.keepserver.domain.device.entity.Device;
import com.keepgoing.keepserver.domain.device.payload.request.DeviceDto;
import com.keepgoing.keepserver.domain.device.payload.response.DeviceResponseDto;
import com.keepgoing.keepserver.domain.device.payload.response.MyDevicesDto;
import com.keepgoing.keepserver.global.common.BaseResponse;
import org.springframework.security.core.Authentication;

Expand All @@ -18,22 +15,4 @@ public interface DeviceService {
BaseResponse deleteDevice(Long id, Authentication authentication);

BaseResponse findAll();
BaseResponse rentDevice(String deviceName, String email);

default DeviceResponseDto entityToDto(Device entity) {
return DeviceResponseDto.builder()
.id(entity.getId())
.deviceName((entity.getDeviceName()))
.imgUrl(entity.getImgUrl())
.status(entity.isStatus())
.build();
}

default Device dtoToEntity(DeviceDto dto) {
return Device.builder()
.deviceName((dto.deviceName()))
.imgUrl(dto.imgUrl())
.status(dto.status())
.build();
}
}
Loading

0 comments on commit db682bc

Please sign in to comment.