Skip to content

Commit 729bbf1

Browse files
committed
added get endpoints to loan controller, one for users and one for admin
1 parent a3c197a commit 729bbf1

5 files changed

Lines changed: 118 additions & 12 deletions

File tree

API_DOCUMENTATION.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,36 @@ Retrieves a specific genre by its ID.
280280

281281
| Method | Endpoint | Description | Access |
282282
| :--- | :--------------- | :------------------------------ | :------ |
283+
| GET | `/loans` | Get all loans for the user | User |
284+
| GET | `/admin/loans` | Get all loans | Admin |
283285
| POST | `/loan/{bookId}` | Loan a book | User |
284286
| POST | `/return/{loanId}`| Return a book | User |
285287

288+
### GET /loans
289+
Returns a paginated and sorted list of all loans for the authenticated user.
290+
291+
**Query Parameters:**
292+
* `page` (optional, integer): The page number to retrieve.
293+
* `size` (optional, integer): The number of loans per page.
294+
* `sortField` (optional, string, default: `id`): The field to sort by.
295+
* `sortDirection` (optional, string, default: `ASC`): The sort direction (`ASC` or `DESC`).
296+
297+
**Note:** Pagination is only enabled when both `page` and `size` parameters are provided.
298+
299+
### GET /admin/loans
300+
Returns a paginated and sorted list of all loans.
301+
302+
**Query Parameters:**
303+
* `loanStatus` (optional, string): Filter by loan status (`ACTIVE`, `RETURNED`).
304+
* `userId` (optional, integer): Filter by user ID.
305+
* `bookId` (optional, integer): Filter by book ID.
306+
* `page` (optional, integer): The page number to retrieve.
307+
* `size` (optional, integer): The number of loans per page.
308+
* `sortField` (optional, string, default: `id`): The field to sort by.
309+
* `sortDirection` (optional, string, default: `ASC`): The sort direction (`ASC` or `DESC`).
310+
311+
**Note:** Pagination is only enabled when both `page` and `size` parameters are provided.
312+
286313
### POST /loan/{bookId}
287314
Loans a book to the authenticated user.
288315

src/main/java/com/themetalstorm/bibliothekssystem/controller/LoanController.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.themetalstorm.bibliothekssystem.controller;
22

33
import com.themetalstorm.bibliothekssystem.dto.LoanDTO;
4+
import com.themetalstorm.bibliothekssystem.model.LoanStatus;
45
import com.themetalstorm.bibliothekssystem.model.User;
56
import com.themetalstorm.bibliothekssystem.service.LoanService;
67
import jakarta.websocket.server.PathParam;
8+
import org.springframework.data.domain.Page;
79
import org.springframework.security.access.prepost.PreAuthorize;
810
import org.springframework.web.bind.annotation.*;
911

@@ -17,8 +19,30 @@ public LoanController(LoanService loanService) {
1719
this.loanService = loanService;
1820
}
1921

20-
// TODO: Get User Loans (user)
21-
// TODO: get all loans (admin)
22+
@PreAuthorize("hasRole('ROLE_USER')")
23+
@GetMapping("/loans")
24+
public ResponseEntity<Page<LoanDTO>> getUserLoans(@RequestHeader(name="Authorization") String token,
25+
@RequestParam(required = false) Integer page,
26+
@RequestParam(required = false) Integer size,
27+
@RequestParam(defaultValue = "id") String sortField,
28+
@RequestParam(defaultValue = "ASC") String sortDirection) {
29+
return new ResponseEntity<>(loanService.getUserLoans(token, page, size, sortField, sortDirection), HttpStatus.OK);
30+
}
31+
@PreAuthorize("hasRole('ROLE_ADMIN')")
32+
@GetMapping("/admin/loans")
33+
public ResponseEntity<Page<LoanDTO>> getAllLoans(@RequestHeader(name="Authorization") String token,
34+
@RequestParam(required = false) LoanStatus loanStatus,
35+
@RequestParam(required = false) Integer userId,
36+
@RequestParam(required = false) Integer bookId,
37+
@RequestParam(required = false) Integer page,
38+
@RequestParam(required = false) Integer size,
39+
@RequestParam(defaultValue = "id") String sortField,
40+
@RequestParam(defaultValue = "ASC") String sortDirection) {
41+
return new ResponseEntity<>(loanService.getAllLoans(token, page, size, sortField, sortDirection, userId, bookId, loanStatus), HttpStatus.OK);
42+
}
43+
44+
45+
2246
// TODO: Add loan (admin)
2347
// TODO: Edit loan (admin)
2448
// TODO: Remove loan (admin)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package com.themetalstorm.bibliothekssystem.repository;
22

33
import com.themetalstorm.bibliothekssystem.model.Loan;
4+
import com.themetalstorm.bibliothekssystem.model.LoanStatus;
5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
47
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
59
import org.springframework.stereotype.Repository;
610

711
import java.util.List;
812

913
@Repository
1014
public interface LoanRepository extends JpaRepository<Loan, Integer> {
1115
List<Loan> findLoansByUserId(Integer userId);
16+
Page<Loan> findLoansByUserId(Integer userId, Pageable pageable);
17+
1218
List<Loan> findLoansByBookId(Integer bookId);
19+
20+
@Query("SELECT l FROM Loan l " +
21+
"WHERE (:loanStatus IS NULL OR l.status = :loanStatus) AND " +
22+
"(:userId IS NULL OR l.userId = :userId) AND " +
23+
"(:bookId IS NULL OR l.bookId = :bookId)")
24+
25+
Page<Loan> findBySearch(Integer page, Integer size, String sortField, String sortDirection, Integer userId, Integer bookId, LoanStatus loanStatus, Pageable unpaged);
1326
}

src/main/java/com/themetalstorm/bibliothekssystem/service/BookService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public Page<BookDTO> getBookBySearch(String search, Integer genreId, Integer aut
137137

138138
Page<Book> all;
139139
if (page == null || size == null ) {
140-
System.out.println("\"NULL\" = " + "NULL");
141140
all = bookRepository.findBySearch(search, genreId, authorId, Pageable.unpaged(sort));
142141
}
143142
else{

src/main/java/com/themetalstorm/bibliothekssystem/service/LoanService.java

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package com.themetalstorm.bibliothekssystem.service;
22

3-
import com.themetalstorm.bibliothekssystem.dto.BookDTO;
43
import com.themetalstorm.bibliothekssystem.dto.LoanDTO;
5-
import com.themetalstorm.bibliothekssystem.model.Book;
6-
import com.themetalstorm.bibliothekssystem.model.Loan;
7-
import com.themetalstorm.bibliothekssystem.model.LoanStatus;
8-
import com.themetalstorm.bibliothekssystem.model.User;
4+
import com.themetalstorm.bibliothekssystem.model.*;
95
import com.themetalstorm.bibliothekssystem.repository.BookRepository;
106
import com.themetalstorm.bibliothekssystem.repository.LoanRepository;
11-
import org.springframework.beans.factory.annotation.Autowired;
12-
import org.springframework.http.HttpStatus;
13-
import org.springframework.security.core.userdetails.UserDetails;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.PageRequest;
9+
import org.springframework.data.domain.Pageable;
10+
import org.springframework.data.domain.Sort;
11+
import org.springframework.security.access.AccessDeniedException;
1412
import org.springframework.stereotype.Service;
1513
import org.springframework.transaction.annotation.Transactional;
1614
import com.themetalstorm.bibliothekssystem.exceptions.ResourceNotFoundException;
1715

1816
import java.time.LocalDateTime;
1917
import java.util.List;
2018
import java.util.Objects;
21-
import java.util.Optional;
2219

2320
@Service
2421
public class LoanService {
@@ -93,4 +90,50 @@ public LoanDTO removeLoan(Integer loanId, String token) {
9390
Loan toReturn = loanRepository.saveAndFlush(loan);
9491
return new LoanDTO(toReturn);
9592
}
93+
94+
public Page<LoanDTO> getUserLoans(String token, Integer page, Integer size, String sortField, String sortDirection) {
95+
String jwtToken = token.substring(7);
96+
97+
String username = jwtService.extractUserName(jwtToken);
98+
User user = myUserService.loadWholeUserByUsername(username);
99+
int userId = user.getId();
100+
101+
Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), sortField);
102+
103+
Page<Loan> all;
104+
if (page == null || size == null) {
105+
all = loanRepository.findLoansByUserId(userId, Pageable.unpaged(sort));
106+
} else {
107+
Pageable pageable = PageRequest.of(page, size, sort);
108+
all = loanRepository.findLoansByUserId(userId, pageable);
109+
110+
}
111+
112+
return all.map(LoanDTO::new);
113+
}
114+
115+
public Page<LoanDTO> getAllLoans(String token, Integer page, Integer size, String sortField, String sortDirection, Integer userId, Integer bookId, LoanStatus loanStatus) {
116+
117+
String jwtToken = token.substring(7);
118+
119+
String username = jwtService.extractUserName(jwtToken);
120+
User user = myUserService.loadWholeUserByUsername(username);
121+
if(user.getRole() != Role.ROLE_ADMIN){
122+
throw new AccessDeniedException("Only Admin Users are allowed to view all loans");
123+
}
124+
125+
Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), sortField);
126+
Page<Loan> all;
127+
128+
if (page == null || size == null) {
129+
130+
all = loanRepository.findBySearch(page, size, sortField, sortDirection, userId,bookId, loanStatus, Pageable.unpaged(sort));
131+
} else {
132+
Pageable pageable = PageRequest.of(page, size, sort);
133+
134+
all = loanRepository.findBySearch(page, size, sortField, sortDirection, userId, bookId, loanStatus, pageable);
135+
}
136+
137+
return all.map(LoanDTO::new);
138+
}
96139
}

0 commit comments

Comments
 (0)