1+ package com .library .app .book .resource ;
2+
3+ import static com .library .app .common .model .StandardsOperationResults .*;
4+
5+ import javax .annotation .security .PermitAll ;
6+ import javax .annotation .security .RolesAllowed ;
7+ import javax .inject .Inject ;
8+ import javax .ws .rs .Consumes ;
9+ import javax .ws .rs .GET ;
10+ import javax .ws .rs .POST ;
11+ import javax .ws .rs .PUT ;
12+ import javax .ws .rs .Path ;
13+ import javax .ws .rs .PathParam ;
14+ import javax .ws .rs .Produces ;
15+ import javax .ws .rs .core .Context ;
16+ import javax .ws .rs .core .MediaType ;
17+ import javax .ws .rs .core .Response ;
18+ import javax .ws .rs .core .Response .ResponseBuilder ;
19+ import javax .ws .rs .core .UriInfo ;
20+
21+ import org .slf4j .Logger ;
22+ import org .slf4j .LoggerFactory ;
23+
24+ import com .google .gson .JsonElement ;
25+ import com .library .app .author .exception .AuthorNotFoundException ;
26+ import com .library .app .book .exception .BookNotFoundException ;
27+ import com .library .app .book .model .Book ;
28+ import com .library .app .book .model .filter .BookFilter ;
29+ import com .library .app .book .services .BookServices ;
30+ import com .library .app .category .exception .CategoryNotFoundException ;
31+ import com .library .app .common .exception .FieldNotValidException ;
32+ import com .library .app .common .json .JsonUtils ;
33+ import com .library .app .common .json .JsonWriter ;
34+ import com .library .app .common .json .OperationResultJsonWriter ;
35+ import com .library .app .common .model .HttpCode ;
36+ import com .library .app .common .model .OperationResult ;
37+ import com .library .app .common .model .PaginatedData ;
38+ import com .library .app .common .model .ResourceMessage ;
39+
40+ @ Path ("/books" )
41+ @ Produces (MediaType .APPLICATION_JSON )
42+ @ Consumes (MediaType .APPLICATION_JSON )
43+ @ RolesAllowed ({ "EMPLOYEE" })
44+ public class BookResource {
45+
46+ private Logger logger = LoggerFactory .getLogger (getClass ());
47+
48+ private static final ResourceMessage RESOURCE_MESSAGE = new ResourceMessage ("book" );
49+
50+ @ Inject
51+ BookServices bookServices ;
52+
53+ @ Inject
54+ BookJsonConverter bookJsonConverter ;
55+
56+ @ Context
57+ UriInfo uriInfo ;
58+
59+ @ POST
60+ public Response add (final String body ) {
61+ logger .debug ("Adding a new book with body {}" , body );
62+ Book book = bookJsonConverter .convertFrom (body );
63+
64+ HttpCode httpCode = HttpCode .CREATED ;
65+ OperationResult result ;
66+ try {
67+ book = bookServices .add (book );
68+ result = OperationResult .success (JsonUtils .getJsonElementWithId (book .getId ()));
69+ } catch (final FieldNotValidException e ) {
70+ httpCode = HttpCode .VALIDATION_ERROR ;
71+ logger .error ("One of the fields of the book is not valid" , e );
72+ result = getOperationResultInvalidField (RESOURCE_MESSAGE , e );
73+ } catch (final CategoryNotFoundException e ) {
74+ httpCode = HttpCode .VALIDATION_ERROR ;
75+ logger .error ("Category not found for book" , e );
76+ result = getOperationResultDependencyNotFound (RESOURCE_MESSAGE , "category" );
77+ } catch (final AuthorNotFoundException e ) {
78+ httpCode = HttpCode .VALIDATION_ERROR ;
79+ logger .error ("Author not found for book" , e );
80+ result = getOperationResultDependencyNotFound (RESOURCE_MESSAGE , "author" );
81+ }
82+
83+ logger .debug ("Returning the operation result after adding book: {}" , result );
84+ return Response .status (httpCode .getCode ()).entity (OperationResultJsonWriter .toJson (result )).build ();
85+ }
86+
87+ @ PUT
88+ @ Path ("/{id}" )
89+ public Response update (@ PathParam ("id" ) final Long id , final String body ) {
90+ logger .debug ("Updating the book {} with body {}" , id , body );
91+ final Book book = bookJsonConverter .convertFrom (body );
92+ book .setId (id );
93+
94+ HttpCode httpCode = HttpCode .OK ;
95+ OperationResult result ;
96+ try {
97+ bookServices .update (book );
98+ result = OperationResult .success ();
99+ } catch (final FieldNotValidException e ) {
100+ httpCode = HttpCode .VALIDATION_ERROR ;
101+ logger .error ("One of the fields of the book is not valid" , e );
102+ result = getOperationResultInvalidField (RESOURCE_MESSAGE , e );
103+ } catch (final CategoryNotFoundException e ) {
104+ httpCode = HttpCode .VALIDATION_ERROR ;
105+ logger .error ("Category not found for book" , e );
106+ result = getOperationResultDependencyNotFound (RESOURCE_MESSAGE , "category" );
107+ } catch (final AuthorNotFoundException e ) {
108+ httpCode = HttpCode .VALIDATION_ERROR ;
109+ logger .error ("Author not found for book" , e );
110+ result = getOperationResultDependencyNotFound (RESOURCE_MESSAGE , "author" );
111+ } catch (final BookNotFoundException e ) {
112+ httpCode = HttpCode .NOT_FOUND ;
113+ logger .error ("No book found for the given id" , e );
114+ result = getOperationResultNotFound (RESOURCE_MESSAGE );
115+ }
116+
117+ logger .debug ("Returning the operation result after updating book: {}" , result );
118+ return Response .status (httpCode .getCode ()).entity (OperationResultJsonWriter .toJson (result )).build ();
119+ }
120+
121+ @ GET
122+ @ Path ("/{id}" )
123+ public Response findById (@ PathParam ("id" ) final Long id ) {
124+ logger .debug ("Find book: {}" , id );
125+ ResponseBuilder responseBuilder ;
126+ try {
127+ final Book book = bookServices .findById (id );
128+ final OperationResult result = OperationResult .success (bookJsonConverter .convertToJsonElement (book ));
129+ responseBuilder = Response .status (HttpCode .OK .getCode ()).entity (OperationResultJsonWriter .toJson (result ));
130+ logger .debug ("Book found: {}" , book );
131+ } catch (final BookNotFoundException e ) {
132+ logger .error ("No book found for id" , id );
133+ responseBuilder = Response .status (HttpCode .NOT_FOUND .getCode ());
134+ }
135+
136+ return responseBuilder .build ();
137+ }
138+
139+ @ GET
140+ @ PermitAll
141+ public Response findByFilter () {
142+
143+ final BookFilter bookFilter = new BookFilterExtractorFromUrl (uriInfo ).getFilter ();
144+ logger .debug ("Finding books using filter: {}" , bookFilter );
145+
146+ final PaginatedData <Book > books = bookServices .findByFilter (bookFilter );
147+
148+ logger .debug ("Found {} books" , books .getNumberOfRows ());
149+
150+ final JsonElement jsonWithPagingAndEntries = JsonUtils .getJsonElementWithPagingAndEntries (books ,
151+ bookJsonConverter );
152+ return Response .status (HttpCode .OK .getCode ()).entity (JsonWriter .writeToString (jsonWithPagingAndEntries ))
153+ .build ();
154+ }
155+
156+ }
0 commit comments