From a9d5d86b32b9fbd26b41bdedf54822d059d45f23 Mon Sep 17 00:00:00 2001 From: Artur Kmieckowiak Date: Sat, 11 Jan 2020 10:03:37 +0100 Subject: [PATCH] Added book controller --- .../study/bookapi/api/BookController.java | 34 +++++++++++++++++++ .../study/bookapi/model/BookDocument.java | 12 ++++++- .../study/bookapi/model/UserDocument.java | 34 +++++++++++++++++++ .../bookapi/repository/BookRepository.java | 12 +++++++ .../study/bookapi/service/BookService.java | 24 +++++++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/fnecan/study/bookapi/model/UserDocument.java create mode 100644 src/main/java/com/fnecan/study/bookapi/repository/BookRepository.java create mode 100644 src/main/java/com/fnecan/study/bookapi/service/BookService.java diff --git a/src/main/java/com/fnecan/study/bookapi/api/BookController.java b/src/main/java/com/fnecan/study/bookapi/api/BookController.java index c47d43f..842dbff 100644 --- a/src/main/java/com/fnecan/study/bookapi/api/BookController.java +++ b/src/main/java/com/fnecan/study/bookapi/api/BookController.java @@ -1,4 +1,38 @@ 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 { + + @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) + ); + } } diff --git a/src/main/java/com/fnecan/study/bookapi/model/BookDocument.java b/src/main/java/com/fnecan/study/bookapi/model/BookDocument.java index acae3ce..297765d 100644 --- a/src/main/java/com/fnecan/study/bookapi/model/BookDocument.java +++ b/src/main/java/com/fnecan/study/bookapi/model/BookDocument.java @@ -11,11 +11,13 @@ public class BookDocument { private String author; private String isbn; 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.isbn = isbn; this.genre = genre; + this.userId = userId; } public String getAuthor() { @@ -41,4 +43,12 @@ public class BookDocument { public void setGenre(Genre genre) { this.genre = genre; } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } } diff --git a/src/main/java/com/fnecan/study/bookapi/model/UserDocument.java b/src/main/java/com/fnecan/study/bookapi/model/UserDocument.java new file mode 100644 index 0000000..5070fd9 --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/model/UserDocument.java @@ -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; + } +} diff --git a/src/main/java/com/fnecan/study/bookapi/repository/BookRepository.java b/src/main/java/com/fnecan/study/bookapi/repository/BookRepository.java new file mode 100644 index 0000000..3859c7c --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/repository/BookRepository.java @@ -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 { + List findByUserId(String userId); + Optional findByUserIdAndId(String userId, String bookId); +} diff --git a/src/main/java/com/fnecan/study/bookapi/service/BookService.java b/src/main/java/com/fnecan/study/bookapi/service/BookService.java new file mode 100644 index 0000000..14be183 --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/service/BookService.java @@ -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 getBooksForUser(String userId) { + return bookRepository.findByUserId(userId); + } + + public Optional getBookDetails(String userId, String bookId) { + return bookRepository.findByUserIdAndId(userId, bookId); + } +}