Skip to content

Commit

Permalink
Merge pull request #40 from Team-KeepGoing/feature/device
Browse files Browse the repository at this point in the history
Refactor :: add rental info to /userinfo return value [2/2]
  • Loading branch information
miraexhoi authored Jun 11, 2024
2 parents db682bc + 904943e commit 3f788cc
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.keepgoing.keepserver.domain.book.entity.dto.BookResponseDto;
import org.springframework.stereotype.Component;

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

@Component
public class BookMapper {
public BookResponseDto entityToDto(Book entity) {
Expand All @@ -14,4 +17,10 @@ public BookResponseDto entityToDto(Book entity) {
.state(entity.getState())
.build();
}

public List<BookResponseDto> convertBooksToDtos(List<Book> books) {
return books.stream()
.map(this::entityToDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.keepgoing.keepserver.domain.book.repository;

import com.keepgoing.keepserver.domain.book.entity.Book;
import com.keepgoing.keepserver.domain.user.entity.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -12,4 +13,5 @@ public interface BookRepository extends JpaRepository<Book, Long> {
Book findBookByNfcCode(String NfcCode);
List<Book> findByBookNameContaining(String bookName);
Optional<Book> findByBookName(String bookName);
List<Book> findByBorrower(User borrower);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.user.entity.user.User;
import com.keepgoing.keepserver.domain.user.repository.user.UserRepository;
import com.keepgoing.keepserver.global.common.BaseResponse;
import com.keepgoing.keepserver.global.common.S3.S3Uploader;
Expand Down Expand Up @@ -93,8 +94,8 @@ public BaseResponse editBook(String nfcCode, BookRequestDTO bookRequest) {
bookRepository.save(book);
return new BaseResponse(HttpStatus.OK, "책 정보 수정 성공");
}
}




public List<Book> findBooksBorrowedByUser(User user) {
return bookRepository.findByBorrower(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Component
public class DeviceMapper {
public DeviceResponseDto entityToDto(Device entity) {
public static DeviceResponseDto entityToDto(Device entity) {
return DeviceResponseDto.builder()
.id(entity.getId())
.deviceName(entity.getDeviceName())
Expand All @@ -20,17 +20,17 @@ public DeviceResponseDto entityToDto(Device entity) {
.build();
}

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

public List<DeviceResponseDto> convertDevicesToDtos(List<Device> devices) {
public static List<DeviceResponseDto> convertDevicesToDtos(List<Device> devices) {
return devices.stream()
.map(this::entityToDto)
.map(DeviceMapper::entityToDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ResponseEntity<?> registerAndAuthenticateUser(@RequestBody SignupRequest
return ResponseEntity.ok().body(response);
}

@Operation(summary = "프로필", description = "토큰을 이용하여 유저 정보와 대여한 기자재 목록을 조회합니다.")
@Operation(summary = "프로필", description = "토큰을 이용하여 유저 정보와 대여한 기자재 및 도서 목록을 조회합니다.")
@GetMapping("/userinfo")
public UserProfileDto provideUserInfo(Authentication authentication) {
String userEmail = authentication.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.keepgoing.keepserver.domain.user.payload.request;

import com.keepgoing.keepserver.domain.book.entity.dto.BookResponseDto;
import com.keepgoing.keepserver.domain.device.payload.response.DeviceResponseDto;
import com.keepgoing.keepserver.domain.user.entity.user.User;
import lombok.AllArgsConstructor;
Expand All @@ -14,4 +15,5 @@
public class UserProfileDto {
private User user;
private List<DeviceResponseDto> borrowedDevices;
private List<BookResponseDto> brrowedBooks;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.keepgoing.keepserver.domain.user.service.user;

import com.keepgoing.keepserver.domain.book.entity.Book;
import com.keepgoing.keepserver.domain.book.entity.dto.BookResponseDto;
import com.keepgoing.keepserver.domain.book.mapper.BookMapper;
import com.keepgoing.keepserver.domain.book.service.BookServiceImpl;
import com.keepgoing.keepserver.domain.device.entity.Device;
import com.keepgoing.keepserver.domain.device.mapper.DeviceMapper;
import com.keepgoing.keepserver.domain.device.payload.response.DeviceResponseDto;
Expand Down Expand Up @@ -36,6 +40,8 @@ public class UserServiceImpl implements UserService {
private final JwtUtils jwtUtils;
private final DeviceServiceImpl deviceService;
private final DeviceMapper deviceMapper;
private final BookServiceImpl bookService;
private final BookMapper bookMapper;

@Override
@Transactional
Expand Down Expand Up @@ -65,9 +71,10 @@ public void updateUserData(UserInfoRequest request, String email) {
public UserProfileDto provideUserInfo(String userEmail) {
User user = userRepository.findByEmailEquals(userEmail).orElseThrow(DeviceException::userNotFound);
List<DeviceResponseDto> borrowedDevicesDto = getBorrowedDevicesForUser(user);
List<BookResponseDto> borrowedBooksDto = getBorrowedBooksForUser(user);
hideUserPassword(user);

return new UserProfileDto(user, borrowedDevicesDto);
return new UserProfileDto(user, borrowedDevicesDto, borrowedBooksDto);
}

/* 인증 및 JWT 토큰 생성 */
Expand All @@ -87,6 +94,11 @@ private List<DeviceResponseDto> getBorrowedDevicesForUser(User user) {
return deviceMapper.convertDevicesToDtos(borrowedDevices);
}

private List<BookResponseDto> getBorrowedBooksForUser(User user) {
List<Book> borrowedBooks = bookService.findBooksBorrowedByUser(user);
return bookMapper.convertBooksToDtos(borrowedBooks);
}

private void hideUserPassword(User user) {
user.hidePassword("");
}
Expand Down

0 comments on commit 3f788cc

Please sign in to comment.