Skip to content

Commit 455717a

Browse files
Borrow created, now can search (all, by student ID and by book ID)
1 parent c961aee commit 455717a

File tree

7 files changed

+141
-23
lines changed

7 files changed

+141
-23
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.library.LibraryAPI.domain.repository;
2+
3+
import com.example.library.LibraryAPI.persistence.entity.*;
4+
import org.springframework.stereotype.*;
5+
6+
import java.util.*;
7+
8+
@Repository
9+
public interface BorrowRepository {
10+
List<Borrow> getAll();
11+
Optional<List<Borrow>> getBorrowByStudentId(int studentId);
12+
Optional<List<Borrow>> getBorrowByBookId(int bookId);
13+
Borrow save (Borrow borrow);
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.library.LibraryAPI.domain.service;
2+
import com.example.library.LibraryAPI.persistence.entity.*;
3+
import org.springframework.beans.factory.annotation.*;
4+
import com.example.library.LibraryAPI.domain.repository.BorrowRepository;
5+
import org.springframework.stereotype.*;
6+
import java.util.*;
7+
8+
@Service
9+
public class BorrowService {
10+
11+
@Autowired
12+
private BorrowRepository borrowRepository;
13+
14+
public List<Borrow> getAll(){
15+
return borrowRepository.getAll();
16+
}
17+
18+
public Optional<List<Borrow>> getBorrowByStudentId(int studentId){
19+
return borrowRepository.getBorrowByStudentId(studentId);
20+
}
21+
22+
public Optional<List<Borrow>> getBorrowByBookId(int bookId){
23+
return borrowRepository.getBorrowByBookId(bookId);
24+
}
25+
26+
public Borrow save(Borrow borrow){
27+
return borrowRepository.save(borrow);
28+
}
29+
30+
}
31+
32+
33+
34+
35+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.library.LibraryAPI.persistence;
2+
3+
import com.example.library.LibraryAPI.persistence.crud.*;
4+
import com.example.library.LibraryAPI.persistence.entity.*;
5+
import org.springframework.beans.factory.annotation.*;
6+
import org.springframework.boot.autoconfigure.*;
7+
import org.springframework.stereotype.*;
8+
9+
import java.security.*;
10+
import java.util.*;
11+
12+
@EnableAutoConfiguration
13+
@Repository
14+
public class BorrowRepository implements com.example.library.LibraryAPI.domain.repository.BorrowRepository {
15+
@Autowired
16+
private BorrowCrudRepository borrowCrudRepository;
17+
18+
@Override
19+
public List<Borrow> getAll() {
20+
List<Borrow> borrows = (List<Borrow>) borrowCrudRepository.findAll();
21+
return borrows;
22+
}
23+
24+
@Override
25+
public Optional<List<Borrow>> getBorrowByStudentId(int studentId) {
26+
List<Borrow> borrows = borrowCrudRepository.findBorrowByStudentId(studentId);
27+
return Optional.of(borrows);
28+
29+
}
30+
31+
@Override
32+
public Optional<List<Borrow>> getBorrowByBookId(int bookId) {
33+
List<Borrow> borrows = borrowCrudRepository.findBorrowByBookId(bookId);
34+
return Optional.of(borrows);
35+
}
36+
@Override
37+
public Borrow save(Borrow borrow) {
38+
return borrowCrudRepository.save(borrow);
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package com.example.library.LibraryAPI.persistence.crud;
22

33
import com.example.library.LibraryAPI.persistence.entity.*;
4+
import org.springframework.data.jpa.repository.*;
45
import org.springframework.data.repository.*;
56

7+
import java.util.*;
8+
69
public interface BorrowCrudRepository extends CrudRepository<Borrow, Integer> {
10+
11+
@Query(value = "SELECT * FROM borrows WHERE student_id = ?", nativeQuery = true)
12+
List<Borrow > findBorrowByStudentId(int studentId);
13+
14+
@Query(value = "SELECT * FROM borrows WHERE book_id = ?", nativeQuery = true)
15+
List<Borrow> findBorrowByBookId(int bookId);
716
}

src/main/java/com/example/library/LibraryAPI/persistence/entity/Borrow.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ public class Borrow {
1717
private Date broughtDate;
1818

1919

20-
@OneToMany(mappedBy = "borrow")
21-
private List<Student> students;
22-
2320
public Integer getBorrowId() {
2421
return borrowId;
2522
}
@@ -59,12 +56,4 @@ public Date getBroughtDate() {
5956
public void setBroughtDate(Date broughtDate) {
6057
this.broughtDate = broughtDate;
6158
}
62-
63-
public List<Student> getStudents() {
64-
return students;
65-
}
66-
67-
public void setStudents(List<Student> students) {
68-
this.students = students;
69-
}
7059
}

src/main/java/com/example/library/LibraryAPI/persistence/entity/Student.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public class Student {
1717
private String clasS;
1818
private Integer point;
1919

20-
21-
@ManyToOne
22-
@JoinColumn(name = "borrowId", insertable = false, updatable = false) //Mediante esto, no se actualizan ni
23-
private Borrow borrow;
24-
2520
public Integer getStudentId() {
2621
return studentId;
2722
}
@@ -78,11 +73,4 @@ public void setPoint(Integer point) {
7873
this.point = point;
7974
}
8075

81-
public Borrow getBorrow() {
82-
return borrow;
83-
}
84-
85-
public void setBorrow(Borrow borrow) {
86-
this.borrow = borrow;
87-
}
8876
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.library.LibraryAPI.web.config.controller;
2+
3+
import com.example.library.LibraryAPI.domain.service.*;
4+
import com.example.library.LibraryAPI.persistence.entity.*;
5+
import org.springframework.beans.factory.annotation.*;
6+
import org.springframework.http.*;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.*;
10+
11+
@RestController
12+
@RequestMapping("/borrows")
13+
public class BorrowController {
14+
15+
@Autowired
16+
private BorrowService borrowService;
17+
18+
@GetMapping("/all")
19+
public ResponseEntity<List<Borrow>> getAll(){
20+
return new ResponseEntity<>(borrowService.getAll(), HttpStatus.OK);
21+
}
22+
23+
@GetMapping("/student/{studentId}")
24+
public ResponseEntity<List<Borrow>> getBorrowByStudentId(@PathVariable int studentId){
25+
return borrowService.getBorrowByStudentId(studentId).map(
26+
borrows -> new ResponseEntity<>(borrows, HttpStatus.OK)
27+
28+
).orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
29+
}
30+
@GetMapping("/book/{bookId}")
31+
public ResponseEntity<List<Borrow>> getBorrowByBookId(@PathVariable int bookId){
32+
return borrowService.getBorrowByBookId(bookId).map(
33+
borrows -> new ResponseEntity<>(borrows, HttpStatus.OK)
34+
35+
).orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
36+
}
37+
@PostMapping("/save")
38+
public ResponseEntity<Borrow> save(@RequestBody Borrow borrow){
39+
return new ResponseEntity<>(borrowService.save(borrow), HttpStatus.CREATED);
40+
}
41+
}
42+
43+

0 commit comments

Comments
 (0)