Added book controller
This commit is contained in:
parent
ced78faf6b
commit
a9d5d86b32
@ -1,4 +1,38 @@
|
|||||||
package com.fnecan.study.bookapi.api;
|
package com.fnecan.study.bookapi.api;
|
||||||
|
|
||||||
|
import com.fnecan.study.bookapi.model.BookDocument;
|
||||||
|
import com.fnecan.study.bookapi.service.BookService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/books")
|
||||||
public class BookController {
|
public class BookController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
BookService bookService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity getBooksForUser(@RequestHeader("userId") String userId){
|
||||||
|
return ResponseEntity.ok(
|
||||||
|
bookService.getBooksForUser(userId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{bookId}")
|
||||||
|
public ResponseEntity getBookDetails(
|
||||||
|
@RequestHeader("userId") String userId,
|
||||||
|
@PathVariable("bookId") String bookId
|
||||||
|
){
|
||||||
|
return ResponseEntity.ok(
|
||||||
|
bookService.getBookDetails(userId, bookId)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,11 +11,13 @@ public class BookDocument {
|
|||||||
private String author;
|
private String author;
|
||||||
private String isbn;
|
private String isbn;
|
||||||
private Genre genre;
|
private Genre genre;
|
||||||
|
private String userId;
|
||||||
|
|
||||||
public BookDocument(String author, String isbn, Genre genre) {
|
public BookDocument(String author, String isbn, Genre genre, String userId) {
|
||||||
this.author = author;
|
this.author = author;
|
||||||
this.isbn = isbn;
|
this.isbn = isbn;
|
||||||
this.genre = genre;
|
this.genre = genre;
|
||||||
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAuthor() {
|
public String getAuthor() {
|
||||||
@ -41,4 +43,12 @@ public class BookDocument {
|
|||||||
public void setGenre(Genre genre) {
|
public void setGenre(Genre genre) {
|
||||||
this.genre = genre;
|
this.genre = genre;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(String userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.fnecan.study.bookapi.model;
|
||||||
|
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
|
|
||||||
|
@Document
|
||||||
|
public class UserDocument {
|
||||||
|
@Id
|
||||||
|
private String userId;
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
private String location;
|
||||||
|
|
||||||
|
public UserDocument(String username, String location) {
|
||||||
|
this.username = username;
|
||||||
|
this.location = location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocation(String location) {
|
||||||
|
this.location = location;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.fnecan.study.bookapi.repository;
|
||||||
|
|
||||||
|
import com.fnecan.study.bookapi.model.BookDocument;
|
||||||
|
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface BookRepository extends MongoRepository<BookDocument, String> {
|
||||||
|
List<BookDocument> findByUserId(String userId);
|
||||||
|
Optional<BookDocument> findByUserIdAndId(String userId, String bookId);
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.fnecan.study.bookapi.service;
|
||||||
|
|
||||||
|
import com.fnecan.study.bookapi.model.BookDocument;
|
||||||
|
import com.fnecan.study.bookapi.repository.BookRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BookService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
BookRepository bookRepository;
|
||||||
|
|
||||||
|
public List<BookDocument> getBooksForUser(String userId) {
|
||||||
|
return bookRepository.findByUserId(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<BookDocument> getBookDetails(String userId, String bookId) {
|
||||||
|
return bookRepository.findByUserIdAndId(userId, bookId);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user