Skip to content

Commit 1a0ca42

Browse files
committed
implementacao do resource de Book e testes
1 parent 39d4525 commit 1a0ca42

File tree

10 files changed

+471
-458
lines changed

10 files changed

+471
-458
lines changed

library-model/src/main/java/com/library/app/author/model/Author.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public Author(final String name) {
2727
this.name = name;
2828
}
2929

30+
public Author(final Long id) {
31+
this.id = id;
32+
}
33+
3034
public Long getId() {
3135
return id;
3236
}

library-model/src/main/java/com/library/app/category/model/Category.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public Category(final String name) {
2828
this.name = name;
2929
}
3030

31+
public Category(final Long id) {
32+
this.id = id;
33+
}
34+
3135
public Long getId() {
3236
return id;
3337
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
package com.library.app.book.resource;
22

3-
import javax.ws.rs.core.UriInfo;
4-
53
import com.library.app.book.model.filter.BookFilter;
64
import com.library.app.common.resource.AbstractFilterExtractorFromUrl;
75

6+
import javax.ws.rs.core.UriInfo;
7+
88
public class BookFilterExtractorFromUrl extends AbstractFilterExtractorFromUrl {
99

10-
public BookFilterExtractorFromUrl(final UriInfo uriInfo) {
11-
super(uriInfo);
12-
}
10+
public BookFilterExtractorFromUrl(final UriInfo uriInfo) {
11+
super(uriInfo);
12+
}
1313

14-
public BookFilter getFilter() {
15-
final BookFilter bookFilter = new BookFilter();
14+
public BookFilter getFilter() {
15+
final BookFilter bookFilter = new BookFilter();
1616

17-
bookFilter.setPaginationData(extractPaginationData());
18-
bookFilter.setTitle(getUriInfo().getQueryParameters().getFirst("title"));
17+
bookFilter.setPaginationData(extractPaginationData());
18+
bookFilter.setTitle(getUriInfo().getQueryParameters().getFirst("title"));
1919

20-
final String categoryIdStr = getUriInfo().getQueryParameters().getFirst("categoryId");
21-
if (categoryIdStr != null) {
22-
bookFilter.setCategoryId(Long.valueOf(categoryIdStr));
23-
}
20+
final String categoryIdStr = getUriInfo().getQueryParameters().getFirst("categoryId");
21+
if (categoryIdStr != null) {
22+
bookFilter.setCategoryId(Long.valueOf(categoryIdStr));
23+
}
2424

25-
return bookFilter;
26-
}
25+
return bookFilter;
26+
}
2727

28-
@Override
29-
protected String getDefaultSortField() {
30-
return "title";
31-
}
28+
@Override
29+
protected String getDefaultSortField() {
30+
return "title";
31+
}
3232

3333
}
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.library.app.book.resource;
22

3-
import javax.enterprise.context.ApplicationScoped;
4-
import javax.inject.Inject;
5-
63
import com.google.gson.JsonArray;
74
import com.google.gson.JsonElement;
85
import com.google.gson.JsonObject;
@@ -14,52 +11,55 @@
1411
import com.library.app.common.json.EntityJsonConverter;
1512
import com.library.app.common.json.JsonReader;
1613

14+
import javax.enterprise.context.ApplicationScoped;
15+
import javax.inject.Inject;
16+
1717
@ApplicationScoped
1818
public class BookJsonConverter implements EntityJsonConverter<Book> {
1919

20-
@Inject
21-
CategoryJsonConverter categoryJsonConverter;
20+
@Inject
21+
CategoryJsonConverter categoryJsonConverter;
2222

23-
@Inject
24-
AuthorJsonConverter authorJsonConverter;
23+
@Inject
24+
AuthorJsonConverter authorJsonConverter;
2525

26-
@Override
27-
public Book convertFrom(final String json) {
28-
final JsonObject jsonObject = JsonReader.readAsJsonObject(json);
26+
@Override
27+
public Book convertFrom(final String json) {
28+
final JsonObject jsonObject = JsonReader.readAsJsonObject(json);
2929

30-
final Book book = new Book();
31-
book.setTitle(JsonReader.getStringOrNull(jsonObject, "title"));
32-
book.setDescription(JsonReader.getStringOrNull(jsonObject, "description"));
30+
final Book book = new Book();
31+
book.setTitle(JsonReader.getStringOrNull(jsonObject, "title"));
32+
book.setDescription(JsonReader.getStringOrNull(jsonObject, "description"));
3333

34-
final Category category = new Category();
35-
category.setId(JsonReader.getLongOrNull(jsonObject, "categoryId"));
36-
book.setCategory(category);
34+
final Category category = new Category();
35+
category.setId(JsonReader.getLongOrNull(jsonObject, "categoryId"));
36+
book.setCategory(category);
3737

38-
final JsonArray authorsIdsJsonArray = jsonObject.getAsJsonArray("authorsIds");
39-
if (authorsIdsJsonArray != null) {
40-
for (final JsonElement authorIdJsonElement : authorsIdsJsonArray) {
41-
final Author author = new Author();
42-
author.setId(authorIdJsonElement.getAsLong());
43-
book.addAuthor(author);
44-
}
45-
}
38+
final JsonArray authorsIdsJsonArray = jsonObject.getAsJsonArray("authorsIds");
39+
if (authorsIdsJsonArray != null) {
40+
for (final JsonElement authorIdJsonElement : authorsIdsJsonArray) {
41+
final Author author = new Author();
42+
author.setId(authorIdJsonElement.getAsLong());
43+
book.addAuthor(author);
44+
}
45+
}
4646

47-
book.setPrice(JsonReader.getDoubeOrNull(jsonObject, "price"));
47+
book.setPrice(JsonReader.getDoubeOrNull(jsonObject, "price"));
4848

49-
return book;
50-
}
49+
return book;
50+
}
5151

52-
@Override
53-
public JsonElement convertToJsonElement(final Book book) {
54-
final JsonObject jsonObject = new JsonObject();
52+
@Override
53+
public JsonElement convertToJsonElement(final Book book) {
54+
final JsonObject jsonObject = new JsonObject();
5555

56-
jsonObject.addProperty("id", book.getId());
57-
jsonObject.addProperty("title", book.getTitle());
58-
jsonObject.addProperty("description", book.getDescription());
59-
jsonObject.add("category", categoryJsonConverter.convertToJsonElement(book.getCategory()));
60-
jsonObject.add("authors", authorJsonConverter.convertToJsonElement(book.getAuthors()));
61-
jsonObject.addProperty("price", book.getPrice());
56+
jsonObject.addProperty("id", book.getId());
57+
jsonObject.addProperty("title", book.getTitle());
58+
jsonObject.addProperty("description", book.getDescription());
59+
jsonObject.add("category", categoryJsonConverter.convertToJsonElement(book.getCategory()));
60+
jsonObject.add("authors", authorJsonConverter.convertToJsonElement(book.getAuthors()));
61+
jsonObject.addProperty("price", book.getPrice());
6262

63-
return jsonObject;
64-
}
63+
return jsonObject;
64+
}
6565
}

0 commit comments

Comments
 (0)