1package com.student_loan.service;
3import java.util.ArrayList;
5import java.util.Optional;
6import java.util.stream.Collectors;
8import org.springframework.beans.factory.annotation.Autowired;
9import org.springframework.stereotype.Service;
11import com.student_loan.model.Item;
12import com.student_loan.model.Item.ItemStatus;
13import com.student_loan.model.Loan;
14import com.student_loan.repository.ItemRepository;
15import com.student_loan.repository.LoanRepository;
16import com.student_loan.repository.UserRepository;
17import com.student_loan.dtos.LoanAndItemDto;
26 private ItemRepository itemRepository;
28 private UserRepository userRepository;
30 private LoanRepository loanRepository;
38 return itemRepository.findAll();
47 return itemRepository.findByStatus(ItemStatus.AVAILABLE);
57 return itemRepository.findById(
id);
67 List<Item> items =
new ArrayList<>();
68 for (Long
id : itemsId) {
70 optionalItem.ifPresent(items::add);
84 if(userRepository.findById(
id).isPresent()) {
85 return itemRepository.findByOwner(
id);
87 throw new RuntimeException(
"Failed to get items! User not found with id: " +
id);
99 return itemRepository.findByStatus(status);
102 if(!userRepository.findById(item.getOwner()).isPresent()) {
103 throw new RuntimeException(
"Failed to save item with id "+ item.getId()+
": Owner not found with id: " + item.getOwner());
105 return itemRepository.save(item);
110 List<Loan> loans = loanRepository.findByLenderAndLoanStatus(userId, Loan.Status.IN_USE);
112 return loans.stream()
114 Item item = itemRepository.findById(loan.getItem())
115 .orElseThrow(() ->
new RuntimeException(
"Item no encontrado"));
116 LoanAndItemDto loanItemDto =
new LoanAndItemDto();
117 loanItemDto.setLoanId(loan.getId());
118 loanItemDto.setBorrowerId(loan.getBorrower());
119 loanItemDto.setLenderId(loan.getLender());
120 loanItemDto.setStartDate(loan.getLoanDate());
121 loanItemDto.setEndDate(loan.getEstimatedReturnDate());
122 loanItemDto.setItemId(item.getId());
123 loanItemDto.setItemName(item.getName());
124 loanItemDto.setItemDescription(item.getDescription());
127 .collect(Collectors.toList());
131 List<Loan> loans = loanRepository.findByBorrowerAndLoanStatus(userId, Loan.Status.IN_USE);
133 return loans.stream()
135 Item item = itemRepository.findById(loan.getItem())
136 .orElseThrow(() ->
new RuntimeException(
"Item no encontrado"));
137 LoanAndItemDto loanItemDto =
new LoanAndItemDto();
138 loanItemDto.setLoanId(loan.getId());
139 loanItemDto.setBorrowerId(loan.getBorrower());
140 loanItemDto.setLenderId(loan.getLender());
141 loanItemDto.setStartDate(loan.getLoanDate());
142 loanItemDto.setEndDate(loan.getEstimatedReturnDate());
143 loanItemDto.setItemId(item.getId());
144 loanItemDto.setItemName(item.getName());
145 loanItemDto.setItemDescription(item.getDescription());
148 .collect(Collectors.toList());
161 Optional<Item> optionalItem = itemRepository.findById(item.getId());
162 if (optionalItem.isPresent()) {
163 throw new RuntimeException(
164 "Failed to create item with id " + item.getId() +
": Item already exists with id: " + item.getId());
165 }
else if(!userRepository.findById(item.getOwner()).isPresent()) {
166 throw new RuntimeException(
"Failed to save item with id "+ item.getId()+
": Owner not found with id: " + item.getOwner());
168 return itemRepository.save(item);
178 itemRepository.deleteById(
id);
List< Item > getItemsByUser(Long id)
List< Item > getItemsByAvailability(ItemStatus status)
Item createItem(Item item)
List< Item > getAllItems()
List< Item > getAvailableItems()
List< Item > getItemsById(List< Long > itemsId)
List< LoanAndItemDto > getItemsLentByUserWithActiveLoans(Long userId)
List< LoanAndItemDto > getItemsBorrowedByUserWithActiveLoans(Long userId)
Optional< Item > getItemById(Long id)