Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
bookstore-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Narendar Vakiti
bookstore-api
Commits
adf5d3b8
Commit
adf5d3b8
authored
Jun 11, 2020
by
Narendar Vakiti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added microservice component actuator
parent
6aa87ed6
Changes
23
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
845 additions
and
91 deletions
+845
-91
pom.xml
pom.xml
+22
-1
Address.java
src/main/java/com/bookstore/bean/Address.java
+27
-0
Admin.java
src/main/java/com/bookstore/bean/Admin.java
+9
-0
Author.java
src/main/java/com/bookstore/bean/Author.java
+10
-14
AuthorRequest.java
src/main/java/com/bookstore/bean/AuthorRequest.java
+11
-0
Book.java
src/main/java/com/bookstore/bean/Book.java
+8
-3
BookRequest.java
src/main/java/com/bookstore/bean/BookRequest.java
+6
-4
CustomInfo.java
src/main/java/com/bookstore/bean/custom/CustomInfo.java
+23
-0
AuthorController.java
src/main/java/com/bookstore/controller/AuthorController.java
+76
-0
BookController.java
src/main/java/com/bookstore/controller/BookController.java
+95
-25
CustomEndPoint.java
...ain/java/com/bookstore/customendpoint/CustomEndPoint.java
+29
-0
AddressRepository.java
src/main/java/com/bookstore/dao/AddressRepository.java
+8
-0
AuthorNotExisted.java
...ava/com/bookstore/exceptionhandling/AuthorNotExisted.java
+10
-0
BookNotExisted.java
.../java/com/bookstore/exceptionhandling/BookNotExisted.java
+10
-0
AuthorService.java
src/main/java/com/bookstore/service/AuthorService.java
+15
-0
AuthorServiceImpl.java
src/main/java/com/bookstore/service/AuthorServiceImpl.java
+103
-0
BookService.java
src/main/java/com/bookstore/service/BookService.java
+9
-6
BookServiceImpl.java
src/main/java/com/bookstore/service/BookServiceImpl.java
+84
-38
application.properties
src/main/resources/application.properties
+3
-0
AuthorControllerTest.java
...t/java/com/bookstore/controller/AuthorControllerTest.java
+41
-0
BookControllerTest.java
...est/java/com/bookstore/controller/BookControllerTest.java
+118
-0
AuthorServiceImplTest.java
...est/java/com/bookstore/service/AuthorServiceImplTest.java
+29
-0
BookServiceImplTest.java
src/test/java/com/bookstore/service/BookServiceImplTest.java
+99
-0
No files found.
pom.xml
View file @
adf5d3b8
...
...
@@ -43,7 +43,12 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-jpa
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
...
...
@@ -71,6 +76,22 @@
<version>
3.3.0.603
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-all
</artifactId>
<version>
2.0.2-beta
</version>
<scope>
test
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.13
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
...
...
src/main/java/com/bookstore/bean/Address.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
bean
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.ToString
;
import
javax.persistence.Entity
;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.Id
;
import
javax.persistence.Table
;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table
(
name
=
"address"
)
public
class
Address
{
@Id
@GeneratedValue
private
Integer
addressID
;
private
String
city
;
private
String
country
;
}
src/main/java/com/bookstore/bean/Admin.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
bean
;
public
class
Admin
{
private
int
adminId
;
private
String
name
;
private
String
email
;
private
String
password
;
}
src/main/java/com/bookstore/bean/Author.java
View file @
adf5d3b8
...
...
@@ -3,14 +3,7 @@ package com.bookstore.bean;
import
java.util.Set
;
import
javax.persistence.CascadeType
;
import
javax.persistence.Entity
;
import
javax.persistence.FetchType
;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.GenerationType
;
import
javax.persistence.Id
;
import
javax.persistence.OneToMany
;
import
javax.persistence.Table
;
import
javax.persistence.*
;
import
lombok.AllArgsConstructor
;
...
...
@@ -20,20 +13,23 @@ import lombok.Setter;
import
lombok.ToString
;
@Getter
@Setter
@ToString
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table
(
name
=
"author"
)
public
class
Author
{
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
)
@GeneratedValue
private
int
authorId
;
private
String
authorName
;
private
String
address
;
@OneToMany
(
mappedBy
=
"author"
,
targetEntity
=
BookStore
.
class
,
cascade
=
CascadeType
.
ALL
,
private
String
authorEmail
;
/*@OneToMany(mappedBy="author", targetEntity = Book.class, cascade = CascadeType.ALL,
fetch = FetchType.LAZY)
private
Set
<
BookStore
>
books
;
private Set<Book> books;*/
@OneToOne
@JoinColumn
(
name
=
"address_ID"
)
private
Address
address
;
}
src/main/java/com/bookstore/bean/AuthorRequest.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
bean
;
import
lombok.*
;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public
class
AuthorRequest
{
private
Author
Author
;
}
src/main/java/com/bookstore/bean/Book
Store
.java
→
src/main/java/com/bookstore/bean/Book.java
View file @
adf5d3b8
...
...
@@ -14,20 +14,25 @@ import lombok.NoArgsConstructor;
import
lombok.Setter
;
import
lombok.ToString
;
import
java.time.LocalDate
;
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table
(
name
=
"bookstore"
)
public
class
Book
Store
{
public
class
Book
{
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
)
@GeneratedValue
private
int
bookId
;
private
String
bookName
;
private
double
bookPrice
;
private
boolean
active
;
private
boolean
availability
;
private
String
level
;
private
String
rating
;
private
LocalDate
publishedDate
;
@ManyToOne
@JoinColumn
(
name
=
"author_ID"
)
...
...
src/main/java/com/bookstore/bean/Book
Details
.java
→
src/main/java/com/bookstore/bean/Book
Request
.java
View file @
adf5d3b8
...
...
@@ -3,12 +3,13 @@ package com.bookstore.bean;
import
lombok.*
;
import
java.io.Serializable
;
import
java.time.LocalDate
;
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public
class
Book
Details
implements
Serializable
{
public
class
Book
Request
implements
Serializable
{
/**
*
...
...
@@ -18,9 +19,10 @@ public class BookDetails implements Serializable{
private
int
bookId
;
private
String
bookName
;
private
double
bookPrice
;
private
boolean
active
;
private
int
authorId
;
private
boolean
availability
;
private
String
level
;
private
String
rating
;
private
LocalDate
publishedDate
;
private
String
authorName
;
private
String
address
;
}
src/main/java/com/bookstore/bean/custom/CustomInfo.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
bean
.
custom
;
import
com.fasterxml.jackson.annotation.JsonAnyGetter
;
import
com.fasterxml.jackson.annotation.JsonAnySetter
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
java.util.Map
;
@JsonInclude
(
JsonInclude
.
Include
.
NON_EMPTY
)
public
class
CustomInfo
{
private
Map
<
String
,
String
>
details
;
@JsonAnyGetter
public
Map
<
String
,
String
>
getDetails
()
{
return
details
;
}
@JsonAnySetter
public
void
setDetails
(
Map
<
String
,
String
>
details
)
{
this
.
details
=
details
;
}
}
src/main/java/com/bookstore/controller/AuthorController.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
controller
;
import
com.bookstore.bean.Author
;
import
com.bookstore.bean.AuthorRequest
;
import
com.bookstore.bean.Status
;
import
com.bookstore.service.AuthorService
;
import
com.bookstore.service.BookService
;
import
com.google.gson.Gson
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
@RestController
public
class
AuthorController
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AuthorController
.
class
);
@Autowired
private
AuthorService
authorService
;
@PostMapping
(
"/addauthordetails"
)
public
String
addAuthorDetails
(
@RequestBody
AuthorRequest
authorRequest
)
{
String
message
=
authorService
.
saveAuthorDetails
(
authorRequest
);
return
message
;
}
/**
* To get all book authors details
* @return authors
*/
@GetMapping
(
"/getauthordetails"
)
public
String
getAuthorDetails
(){
String
response
=
null
;
try
{
List
<
Author
>
authors
=
authorService
.
getAuthorDetails
();
response
=
new
Gson
().
toJson
(
authors
);
if
(
authors
.
isEmpty
()){
Status
status
=
new
Status
(
"Authors details not found"
,
false
);
response
=
new
Gson
().
toJson
(
status
);
}
return
response
;
}
catch
(
Exception
e
){
logger
.
error
(
"Exception while fetching the authors details : "
+
e
.
getMessage
());
Status
status
=
new
Status
(
"Something went wrong while fetching the authors details"
,
false
);
response
=
new
Gson
().
toJson
(
status
);
return
response
;
}
}
@DeleteMapping
(
"/deleteauthordetails/{athorId}"
)
public
String
deleteAuthorDetails
(
@PathVariable
Integer
athorId
){
try
{
authorService
.
deleteAuthorDetails
(
athorId
);
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while deleting the author detials :: "
+
e
.
getMessage
());
}
return
"Author Id "
+
athorId
+
" details are deleted successfully"
;
}
@PutMapping
(
"/updateauthordetails"
)
public
Author
updateAuthorDetails
(
@RequestBody
Author
author
){
Author
auth
=
new
Author
();
try
{
auth
=
authorService
.
updateAuthorDetails
(
author
);
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while updating book details :: "
+
e
.
getMessage
());
}
return
auth
;
}
}
src/main/java/com/bookstore/controller/BookController.java
View file @
adf5d3b8
...
...
@@ -2,17 +2,14 @@ package com.bookstore.controller;
import
java.util.List
;
import
com.bookstore.bean.*
;
import
com.bookstore.exceptionhandling.BookNotExisted
;
import
com.google.gson.Gson
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.bookstore.bean.Author
;
import
com.bookstore.bean.BookDetails
;
import
com.bookstore.bean.BookStore
;
import
org.springframework.web.bind.annotation.*
;
import
com.bookstore.service.BookService
;
/**
...
...
@@ -24,7 +21,6 @@ public class BookController {
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
BookController
.
class
);
@Autowired
private
BookService
bookService
;
...
...
@@ -33,30 +29,104 @@ public class BookController {
* @return books
*/
@GetMapping
(
"/getbookdetails"
)
public
List
<
BookStore
>
getBookDetails
(){
List
<
BookStore
>
books
=
bookService
.
getBookDetails
();
return
books
;
}
/**
* To get all book authors details
* @return authors
*/
@GetMapping
(
"/getauthordetails"
)
public
List
<
Author
>
getAuthorDetails
(){
List
<
Author
>
authors
=
bookService
.
getAuthorDetails
();
return
authors
;
public
String
getBookDetails
(){
String
response
=
null
;
try
{
List
<
Book
>
books
=
bookService
.
getBookDetails
();
response
=
new
Gson
().
toJson
(
books
);
if
(
books
.
isEmpty
()){
Status
status
=
new
Status
(
"Books details not found"
,
false
);
response
=
new
Gson
().
toJson
(
status
);
}
return
response
;
}
catch
(
Exception
e
){
logger
.
error
(
"Exception while fetching the book details : "
+
e
.
getMessage
());
Status
status
=
new
Status
(
"Something went wrong while fetching the books details"
,
false
);
response
=
new
Gson
().
toJson
(
status
);
return
response
;
}
}
/**
* Store book and author details
* @param book
Details
* @param book
Request
* @return message
*/
@PostMapping
(
"/addbookdetails"
)
public
String
addBookDetails
(
@RequestBody
Book
Details
bookDetails
)
{
String
message
=
bookService
.
saveBookDetails
(
book
Details
);
public
String
addBookDetails
(
@RequestBody
Book
Request
bookRequest
)
{
String
message
=
bookService
.
saveBookDetails
(
book
Request
);
return
message
;
}
@GetMapping
(
"/findbooksbyauthor/{authorName}"
)
public
String
findBooksByAuthor
(
@PathVariable
String
authorName
){
String
response
=
null
;
try
{
List
<
Book
>
books
=
bookService
.
findBooksByAuthor
(
authorName
);
response
=
new
Gson
().
toJson
(
books
);
if
(
books
.
isEmpty
()){
Status
status
=
new
Status
(
"Books details not found for author :: "
+
authorName
,
false
);
response
=
new
Gson
().
toJson
(
status
);
}
return
response
;
}
catch
(
Exception
e
){
logger
.
error
(
"Exception while fetching the book details : "
+
e
.
getMessage
());
Status
status
=
new
Status
(
"Something went wrong while fetching the book details by author name"
,
false
);
response
=
new
Gson
().
toJson
(
status
);
return
response
;
}
}
@DeleteMapping
(
"/deletebookdetails/{bookId}"
)
public
String
deleteBookDetails
(
@PathVariable
Integer
bookId
){
try
{
boolean
flag
=
bookService
.
existsBookById
(
bookId
);
if
(
flag
){
bookService
.
deleteBookDetails
(
bookId
);
}
else
{
throw
new
BookNotExisted
(
"Book Details not found for Book ID ::"
+
bookId
,
"Book ID :: "
+
bookId
+
" details are not deleted"
);
}
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while deleting the book details :: "
+
e
.
getMessage
());
return
"Exception while deleting the book details"
;
}
return
"Book Id :: "
+
bookId
+
" details are deleted successfully"
;
}
@PutMapping
(
"/updatebookdetails"
)
public
Book
updateBookDetails
(
@RequestBody
Book
updateBook
){
Book
book
=
new
Book
();
try
{
book
=
bookService
.
updateBookDetails
(
updateBook
);
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while updating book details :: "
+
e
.
getMessage
());
}
return
book
;
}
@GetMapping
(
"/findBooksByRating/{rating}"
)
public
String
findBooksByRating
(
@PathVariable
String
rating
){
String
response
=
null
;
try
{
List
<
Book
>
books
=
bookService
.
findBooksByRating
(
rating
);
if
(!
books
.
isEmpty
()){
response
=
new
Gson
().
toJson
(
books
);
}
else
{
logger
.
info
(
"Books Details not found on rating :: "
+
rating
);
Status
status
=
new
Status
(
"Books Details not found on rating :: "
+
rating
,
false
);
response
=
new
Gson
().
toJson
(
status
);
}
}
catch
(
Exception
e
){
logger
.
error
(
"Exception while fetching the books details : "
+
e
.
getMessage
());
Status
status
=
new
Status
(
"Something went wrong while fetching the books details"
,
false
);
response
=
new
Gson
().
toJson
(
status
);
return
response
;
}
return
null
;
}
}
src/main/java/com/bookstore/customendpoint/CustomEndPoint.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
customendpoint
;
import
com.bookstore.bean.custom.CustomInfo
;
import
org.springframework.boot.actuate.endpoint.annotation.Endpoint
;
import
org.springframework.boot.actuate.endpoint.annotation.ReadOperation
;
import
org.springframework.stereotype.Component
;
import
java.lang.annotation.Annotation
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
@Component
@Endpoint
(
id
=
"custom-info"
)
public
class
CustomEndPoint
{
@ReadOperation
public
CustomInfo
getCustomInfo
(){
Map
<
String
,
String
>
details
=
new
LinkedHashMap
<>();
details
.
put
(
"status"
,
"UP"
);
details
.
put
(
"Url"
,
"/custom-info"
);
CustomInfo
customInfo
=
new
CustomInfo
();
customInfo
.
setDetails
(
details
);
return
customInfo
;
}
}
src/main/java/com/bookstore/dao/AddressRepository.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
dao
;
import
com.bookstore.bean.Address
;
import
org.springframework.data.jpa.repository.JpaRepository
;
public
interface
AddressRepository
extends
JpaRepository
<
Address
,
Integer
>
{
}
src/main/java/com/bookstore/exceptionhandling/AuthorNotExisted.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
exceptionhandling
;
public
class
AuthorNotExisted
extends
Exception
{
private
String
message
;
private
String
details
;
AuthorNotExisted
(
String
message
,
String
details
){
this
.
message
=
message
;
this
.
details
=
details
;
}
}
src/main/java/com/bookstore/exceptionhandling/BookNotExisted.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
exceptionhandling
;
public
class
BookNotExisted
extends
Exception
{
private
String
message
;
private
String
details
;
public
BookNotExisted
(
String
message
,
String
details
){
this
.
message
=
message
;
this
.
details
=
details
;
}
}
src/main/java/com/bookstore/service/AuthorService.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
service
;
import
com.bookstore.bean.Author
;
import
com.bookstore.bean.AuthorRequest
;
import
java.util.List
;
public
interface
AuthorService
{
public
List
<
Author
>
getAuthorDetails
();
public
String
saveAuthorDetails
(
AuthorRequest
authorRequest
);
public
Author
findAuthorByName
(
String
authorName
);
public
void
deleteAuthorDetails
(
Integer
authorId
);
public
Author
updateAuthorDetails
(
Author
author
);
}
src/main/java/com/bookstore/service/AuthorServiceImpl.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
service
;
import
com.bookstore.bean.*
;
import
com.bookstore.dao.AddressRepository
;
import
com.bookstore.dao.AuthorRepository
;
import
com.bookstore.dao.BookRepository
;
import
com.google.gson.Gson
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Optional
;
@Service
public
class
AuthorServiceImpl
implements
AuthorService
{
@Autowired
private
AddressRepository
addressRepository
;
@Autowired
private
AuthorRepository
authorRepository
;
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AuthorServiceImpl
.
class
);
/**
* To get all book authors details
* @return authors
*/
@Override
public
List
<
Author
>
getAuthorDetails
()
{
List
<
Author
>
author
=
new
ArrayList
<>();
try
{
author
=
authorRepository
.
findAll
();
logger
.
info
(
"Author Details Response :: "
+
author
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"Exception While Fetching Author Details :: "
+
e
.
getMessage
());
}
return
author
;
}
/**
* Store book and author details
* @param request
* @return message
*/
@Override
public
String
saveAuthorDetails
(
AuthorRequest
request
)
{
String
message
=
null
;
Status
status
=
new
Status
(
"Author details are not saved successfully"
,
false
);
try
{
boolean
bookFlag
=
Optional
.
ofNullable
(
request
).
isPresent
();
if
(
bookFlag
)
{
logger
.
info
(
"Author Details save request :: "
+
request
);
Address
address
=
addressRepository
.
save
(
request
.
getAuthor
().
getAddress
());
Author
authorId
=
authorRepository
.
save
(
request
.
getAuthor
());
status
=
new
Status
(
"Author details are added successfully"
,
true
);
}
message
=
new
Gson
().
toJson
(
status
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"Exception While Saving Author Details :: "
+
e
.
getMessage
());
}
return
message
;
}
@Override
public
Author
findAuthorByName
(
String
authorName
){
Author
author
=
new
Author
();
try
{
author
=
authorRepository
.
findAuthorByName
(
authorName
);
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while fetching Author details by author name :: "
+
e
.
getMessage
());
}
return
author
;
}
@Override
public
void
deleteAuthorDetails
(
Integer
auhtorId
){
try
{
authorRepository
.
deleteById
(
auhtorId
);
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while deleting author details :: "
+
e
.
getMessage
());
}
}
public
Author
updateAuthorDetails
(
Author
author
){
Author
auth
=
new
Author
();
try
{
if
(
author
!=
null
){
auth
=
authorRepository
.
save
(
author
);
}
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while updating author details :: "
+
e
.
getMessage
());
}
return
auth
;
}
}
src/main/java/com/bookstore/service/BookService.java
View file @
adf5d3b8
...
...
@@ -2,14 +2,17 @@ package com.bookstore.service;
import
java.util.List
;
import
com.bookstore.bean.Author
;
import
com.bookstore.bean.BookDetails
;
import
com.bookstore.bean.BookStore
;
import
com.bookstore.bean.BookRequest
;
import
com.bookstore.bean.Book
;
public
interface
BookService
{
public
List
<
BookStore
>
getBookDetails
();
public
List
<
Author
>
getAuthorDetails
();
public
String
saveBookDetails
(
BookDetails
bookDetails
);
public
List
<
Book
>
getBookDetails
();
public
String
saveBookDetails
(
BookRequest
bookRequest
);
public
List
<
Book
>
findBooksByAuthor
(
String
authorName
);
public
void
deleteBookDetails
(
Integer
bookId
);
public
Book
updateBookDetails
(
Book
book
);
public
Boolean
existsBookById
(
Integer
bookId
);
public
List
<
Book
>
findBooksByRating
(
String
rating
);
}
src/main/java/com/bookstore/service/BookServiceImpl.java
View file @
adf5d3b8
package
com
.
bookstore
.
service
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
com.bookstore.exceptionhandling.BookNotExisted
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
com.bookstore.bean.Author
;
import
com.bookstore.bean.Book
Details
;
import
com.bookstore.bean.Book
Store
;
import
com.bookstore.bean.Book
Request
;
import
com.bookstore.bean.Book
;
import
com.bookstore.bean.Status
;
import
com.bookstore.dao.Author
Dao
;
import
com.bookstore.dao.Book
StoreDao
;
import
com.bookstore.dao.Author
Repository
;
import
com.bookstore.dao.Book
Repository
;
import
com.google.gson.Gson
;
@Service
public
class
BookServiceImpl
implements
BookService
{
@Autowired
private
Book
StoreDao
bookStoreDao
;
private
Book
Repository
bookRepository
;
@Autowired
private
Author
Dao
authorDao
;
private
Author
Repository
authorRepository
;
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
BookServiceImpl
.
class
);
...
...
@@ -33,61 +37,50 @@ public class BookServiceImpl implements BookService{
* @return books
*/
@Override
public
List
<
Book
Store
>
getBookDetails
()
{
public
List
<
Book
>
getBookDetails
()
{
List
<
Book
Store
>
listOfBooks
=
null
;
List
<
Book
>
listOfBooks
=
new
ArrayList
<>()
;
try
{
listOfBooks
=
bookStoreDao
.
findAll
();
listOfBooks
=
bookRepository
.
findAll
();
listOfBooks
=
listOfBooks
.
stream
().
sorted
().
collect
(
Collectors
.
toList
());
logger
.
info
(
"Book Details Response :: "
+
listOfBooks
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"Exception While Fetching Book Details :: "
+
e
);
logger
.
error
(
"Exception While Fetching Book Details :: "
+
e
.
getMessage
()
);
}
return
listOfBooks
;
}
/**
* To get all book authors details
* @return authors
*/
@Override
public
List
<
Author
>
getAuthorDetails
()
{
List
<
Author
>
author
=
null
;
try
{
author
=
authorDao
.
findAll
();
logger
.
info
(
"Author Details Response :: "
+
author
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"Exception While Fetching Author Details :: "
+
e
);
}
return
author
;
}
/**
* Store book and author details
* @param book
* @return message
*/
@Override
public
String
saveBookDetails
(
Book
Details
book
)
{
public
String
saveBookDetails
(
Book
Request
book
)
{
String
message
=
null
;
Status
status
=
new
Status
(
"Book details are not saved successfully"
,
false
);
try
{
boolean
bookFlag
=
Optional
.
ofNullable
(
book
).
isPresent
();
if
(
bookFlag
)
{
logger
.
info
(
"Book Details save request :: "
+
book
);
Author
author
=
new
Author
();
author
.
setAuthorName
(
book
.
getAuthorName
());
author
.
setAddress
(
book
.
getAddress
());
Author
authorId
=
authorDao
.
save
(
author
);
BookStore
bookStore
=
new
BookStore
();
Author
author
=
authorRepository
.
findAuthorByName
(
book
.
getAuthorName
());
Book
bookStore
=
new
Book
();
bookStore
.
setBookName
(
book
.
getBookName
());
bookStore
.
setBookPrice
(
book
.
getBookPrice
());
bookStore
.
setAuthor
(
authorId
);
bookStoreDao
.
save
(
bookStore
);
bookStore
.
setAvailability
(
book
.
isAvailability
());
bookStore
.
setLevel
(
book
.
getLevel
());
bookStore
.
setRating
(
book
.
getRating
());
if
(
author
==
null
){
logger
.
info
(
"Author is not existed with name :: "
+
book
.
getAuthorName
());
}
else
{
bookStore
.
setAuthor
(
author
);
}
Book
books
=
bookRepository
.
save
(
bookStore
);
status
=
new
Status
(
"Book details are added successfully"
,
true
);
if
(
books
==
null
){
status
=
new
Status
(
"Book details are not added successfully, try again"
,
true
);
}
}
message
=
new
Gson
().
toJson
(
status
);
...
...
@@ -98,4 +91,57 @@ public class BookServiceImpl implements BookService{
return
message
;
}
@Override
public
List
<
Book
>
findBooksByAuthor
(
String
authorName
)
{
List
<
Book
>
books
=
new
ArrayList
<>();
try
{
books
=
bookRepository
.
findBooksByAuthor
(
authorName
);
}
catch
(
Exception
e
){
logger
.
error
(
"Exception While fetching Books Details by author name :: "
+
e
.
getMessage
());
throw
e
;
}
return
books
;
}
@Override
public
void
deleteBookDetails
(
Integer
bookId
){
try
{
bookRepository
.
deleteById
(
bookId
);
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while deleting book details :: "
+
e
.
getMessage
());
throw
e
;
}
}
@Override
public
Boolean
existsBookById
(
Integer
bookId
){
return
bookRepository
.
existsById
(
bookId
);
}
@Override
public
Book
updateBookDetails
(
Book
book
){
Book
updatedBook
=
new
Book
();
try
{
updatedBook
=
bookRepository
.
save
(
book
);
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while updating book details :: "
+
e
.
getMessage
());
}
return
book
;
}
@Override
public
List
<
Book
>
findBooksByRating
(
String
rating
){
List
<
Book
>
books
=
new
ArrayList
<>();
try
{
books
=
bookRepository
.
findBooksByRating
(
rating
);
//find availability of books
if
(!
books
.
isEmpty
()){
books
=
books
.
stream
().
filter
(
book
->
book
.
isAvailability
()).
collect
(
Collectors
.
toList
());
}
}
catch
(
Exception
e
){
logger
.
info
(
"Exception while updating book details :: "
+
e
.
getMessage
());
}
return
books
;
}
}
src/main/resources/application.properties
View file @
adf5d3b8
server.port
=
8083
spring.application.name
=
bookstoreapi
#management.security.enabled=false
management.endpoints.web.exposure.include
=
*
spring.datasource.url
=
jdbc:mysql://localhost:3306/book_schema
spring.datasource.username
=
narendar
spring.datasource.password
=
narendar
spring.jpa.hibernate.ddl-auto
=
update
spring.jpa.show-sql
=
true
spring.jpa.properties.hibernate.dialect
=
org.hibernate.dialect.MySQL8Dialect
\ No newline at end of file
src/test/java/com/bookstore/controller/AuthorControllerTest.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
controller
;
import
com.bookstore.service.AuthorService
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
class
AuthorControllerTest
{
@Mock
private
AuthorService
AuthorService
;
@InjectMocks
private
AuthorController
authorController
;
private
MockMvc
mockMvc
;
@BeforeEach
void
setUp
()
{
MockitoAnnotations
.
initMocks
(
this
);
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
authorController
).
build
();
}
@Test
void
addAuthorDetails
()
{
}
@Test
void
getAuthorDetails
()
{
}
@Test
void
deleteAuthorDetails
()
{
}
}
\ No newline at end of file
src/test/java/com/bookstore/controller/BookControllerTest.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
controller
;
import
com.bookstore.bean.Author
;
import
com.bookstore.bean.BookRequest
;
import
com.bookstore.bean.Book
;
import
com.bookstore.bean.Status
;
import
com.bookstore.service.BookService
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MvcResult
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
post
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
class
BookControllerTest
{
@Mock
private
BookService
bookService
;
@InjectMocks
private
BookController
bookController
;
private
MockMvc
mockMvc
;
@BeforeEach
void
setUp
()
{
MockitoAnnotations
.
initMocks
(
this
);
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
bookController
).
build
();
}
@Test
void
getBookDetails
()
throws
Exception
{
List
<
Book
>
listOfBooks
=
new
ArrayList
<>();
listOfBooks
.
add
(
bookDetails
());
when
(
bookService
.
getBookDetails
()).
thenReturn
(
listOfBooks
);
MvcResult
mvcResult
=
mockMvc
.
perform
(
get
(
"/getbookdetails"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
assertEquals
(
mvcResult
.
getResponse
().
getStatus
(),
200
);
}
@Test
void
addBookDetails
()
throws
Exception
{
Status
status
=
new
Status
(
"Book details are added successfully"
,
true
);
String
response
=
(
new
ObjectMapper
()).
writeValueAsString
(
status
);
when
(
bookService
.
saveBookDetails
(
bookRequest
())).
thenReturn
(
response
);
String
jsonRequest
=
(
new
ObjectMapper
()).
writeValueAsString
(
bookRequest
());
mockMvc
.
perform
(
post
(
"/addbookdetails"
).
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
jsonRequest
))
.
andExpect
(
status
().
isOk
());
}
@Test
void
findBooksByAuthor
()
throws
Exception
{
List
<
Book
>
listOfBooks
=
new
ArrayList
<>();
listOfBooks
.
add
(
bookDetails
());
when
(
bookService
.
findBooksByAuthor
(
"Kathy Sierra"
)).
thenReturn
(
listOfBooks
);
MvcResult
mvcResult
=
mockMvc
.
perform
(
get
(
"/findbooksbyauthor/authorName"
,
"Kathy Sierra"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
assertEquals
(
mvcResult
.
getResponse
().
getStatus
(),
200
);
}
@Test
void
deleteBookDetails
()
throws
Exception
{
MvcResult
mvcResult
=
mockMvc
.
perform
(
get
(
"/deletebookdetails/bookId"
,
"2"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
assertEquals
(
mvcResult
.
getResponse
().
getStatus
(),
200
);
}
public
Book
bookDetails
(){
Book
books
=
new
Book
();
books
.
setBookId
(
2
);
books
.
setBookName
(
"Head First Java"
);
books
.
setAvailability
(
true
);
books
.
setBookPrice
(
450.00
);
books
.
setRating
(
"4.3"
);
books
.
setPublishedDate
(
LocalDate
.
of
(
2003
,
4
,
12
));
books
.
setLevel
(
"Beginner"
);
Author
author
=
new
Author
();
author
.
setAuthorName
(
"Kathy Sierra"
);
books
.
setAuthor
(
author
);
return
books
;
}
public
BookRequest
bookRequest
(){
BookRequest
books
=
new
BookRequest
();
books
.
setBookId
(
2
);
books
.
setBookName
(
"Head First Java"
);
books
.
setAvailability
(
true
);
books
.
setBookPrice
(
450.00
);
books
.
setRating
(
"4.3"
);
books
.
setPublishedDate
(
LocalDate
.
of
(
2003
,
4
,
12
));
books
.
setLevel
(
"Beginner"
);
books
.
setAuthorName
(
"Kathy Sierra"
);
return
books
;
}
}
\ No newline at end of file
src/test/java/com/bookstore/service/AuthorServiceImplTest.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
service
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
class
AuthorServiceImplTest
{
@BeforeEach
void
setUp
()
{
}
@Test
void
getAuthorDetails
()
{
}
@Test
void
saveAuthorDetails
()
{
}
@Test
void
findAuthorByName
()
{
}
@Test
void
deleteAuthorDetails
()
{
}
}
\ No newline at end of file
src/test/java/com/bookstore/service/BookServiceImplTest.java
0 → 100644
View file @
adf5d3b8
package
com
.
bookstore
.
service
;
import
com.bookstore.bean.Author
;
import
com.bookstore.bean.BookRequest
;
import
com.bookstore.bean.Book
;
import
com.bookstore.dao.BookRepository
;
import
org.junit.Before
;
import
org.junit.jupiter.api.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.test.web.servlet.MockMvc
;
import
java.time.LocalDate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
MockitoJUnitRunner
.
class
)
class
BookServiceImplTest
{
@Mock
private
BookRepository
bookRepository
;
@InjectMocks
private
BookServiceImpl
bookService
;
private
MockMvc
mockMvc
;
@Before
public
void
setUp
()
throws
Exception
{
MockitoAnnotations
.
initMocks
(
this
);
}
@Test
void
getBookDetails
()
{
List
<
Book
>
listOfBooks
=
new
ArrayList
<>();
listOfBooks
.
add
(
bookDetails
());
when
(
bookRepository
.
findAll
()).
thenReturn
(
listOfBooks
);
List
<
Book
>
books
=
bookService
.
getBookDetails
();
assertEquals
(
listOfBooks
,
books
);
}
@Test
void
saveBookDetails
()
{
Book
books
=
bookDetails
();
when
(
bookRepository
.
save
(
books
)).
thenReturn
(
books
);
assertEquals
(
books
,
bookService
.
saveBookDetails
(
bookRequest
()));
}
@Test
void
findBooksByAuthor
()
{
List
<
Book
>
listOfBooks
=
new
ArrayList
<>();
listOfBooks
.
add
(
bookDetails
());
when
(
bookRepository
.
findBooksByAuthor
(
"Kathy Sierra"
)).
thenReturn
(
listOfBooks
);
assertEquals
(
listOfBooks
,
bookService
.
findBooksByAuthor
(
"Kathy Sierra"
));
}
@Test
void
deleteBookDetails
()
{
Book
books
=
bookDetails
();
when
(
bookRepository
.
existsById
(
books
.
getBookId
())).
thenReturn
(
true
);
//assertFalse(bookRepository.exists(books.getBookId()));
}
public
Book
bookDetails
(){
Book
books
=
new
Book
();
books
.
setBookId
(
2
);
books
.
setBookName
(
"Head First Java"
);
books
.
setAvailability
(
true
);
books
.
setBookPrice
(
450.00
);
books
.
setRating
(
"4.3"
);
books
.
setPublishedDate
(
LocalDate
.
of
(
2003
,
4
,
12
));
books
.
setLevel
(
"Beginner"
);
Author
author
=
new
Author
();
author
.
setAuthorName
(
"Kathy Sierra"
);
books
.
setAuthor
(
author
);
return
books
;
}
public
BookRequest
bookRequest
(){
BookRequest
books
=
new
BookRequest
();
books
.
setBookId
(
2
);
books
.
setBookName
(
"Head First Java"
);
books
.
setAvailability
(
true
);
books
.
setBookPrice
(
450.00
);
books
.
setRating
(
"4.3"
);
books
.
setPublishedDate
(
LocalDate
.
of
(
2003
,
4
,
12
));
books
.
setLevel
(
"Beginner"
);
books
.
setAuthorName
(
"Kathy Sierra"
);
return
books
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment