Skip to content

Commit c26ed9e

Browse files
committed
Library services updates
1 parent b4c40c4 commit c26ed9e

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

src/main/java/com/learn/SpringBootRESTService/GreetingController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class GreetingController {
1919

2020
@GetMapping("/greeting")
2121
public Greeting greeting(@RequestParam(value="name")String name) {
22-
greeting.setContent("Hello "+name+", "+"Thanks for hitting suman API");
22+
greeting.setContent("Hello "+name+", "+"Thanks for hitting suman's API");
2323
greeting.setId(counter.incrementAndGet());
2424
return greeting;
2525
}

src/main/java/com/learn/SpringBootRESTService/LibraryController.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package com.learn.SpringBootRESTService;
22

3+
import java.util.List;
4+
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.http.HttpHeaders;
57
import org.springframework.http.HttpStatus;
68
import org.springframework.http.HttpStatusCode;
79
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
812
import org.springframework.web.bind.annotation.PostMapping;
913
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RequestParam;
1015
import org.springframework.web.bind.annotation.RestController;
16+
import org.springframework.web.server.ResponseStatusException;
1117

1218
import com.learn.SpringBootRESTService.Service.LibraryService;
1319
import com.learn.SpringBootRESTService.repository.LibraryRepository;
@@ -48,6 +54,22 @@ public ResponseEntity addBookImplementation(@RequestBody Library library) {
4854

4955
}
5056

57+
@GetMapping("/getBooks/{id}")
58+
public Library getBookById(@PathVariable(value = "id")String id) {
59+
try{
60+
Library lib = repository.findById(id).get();
61+
62+
return lib;
63+
}
64+
catch (Exception e) {
65+
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
66+
}
67+
68+
}
5169

70+
@GetMapping("/getBooks/author")
71+
public List<Library> getBookByAuthorName(@RequestParam(value = "authorname")String authorname) {
72+
return repository.findAllByAuthor(authorname);
73+
}
5274

5375
}

src/main/java/com/learn/SpringBootRESTService/repository/LibraryRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77

88

9-
public interface LibraryRepository extends JpaRepository<Library,String> {
9+
public interface LibraryRepository extends JpaRepository<Library,String>,LibraryRepositoryCustom {
1010

1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.learn.SpringBootRESTService.repository;
2+
3+
import java.util.List;
4+
5+
import com.learn.SpringBootRESTService.Library;
6+
7+
public interface LibraryRepositoryCustom {
8+
List<Library> findAllByAuthor(String authorName);
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.learn.SpringBootRESTService.repository;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.annotation.Lazy;
8+
9+
import com.learn.SpringBootRESTService.Library;
10+
11+
public class LibraryRepositoryImpl implements LibraryRepositoryCustom {
12+
@Lazy
13+
@Autowired
14+
LibraryRepository repository;
15+
16+
@Override
17+
public List<Library> findAllByAuthor(String authorName) {
18+
List<Library> booksWithAuthor= new ArrayList<Library>();
19+
List<Library> books= repository.findAll();
20+
21+
for(Library book: books) {
22+
if (book.getAuthor().equalsIgnoreCase(authorName)) {
23+
booksWithAuthor.add(book);
24+
}
25+
}
26+
return booksWithAuthor;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)