1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| @RequestMapping(value = "/addBook", method = RequestMethod.POST, produces = "application/json")
@ApiOperation(value = "Add a new Book in the Library", response = BookDTO.class)
@ApiResponses(value = { @ApiResponse(code = 409, message = "Conflict: the book already exist"),
@ApiResponse(code = 201, message = "Created: the book is successfully inserted"),
@ApiResponse(code = 304, message = "Not Modified: the book is unsuccessfully inserted") })
public ResponseEntity<BookDTO> createNewBook(@RequestBody BookDTO bookDTORequest) {
// , UriComponentsBuilder uriComponentBuilder
Book existingBook = bookService.findBookByIsbn(bookDTORequest.getIsbn());
if (existingBook != null) {
return new ResponseEntity<BookDTO>(HttpStatus.CONFLICT);
}
Book bookRequest = mapBookDTOToBook(bookDTORequest);
Book book = bookService.saveBook(bookRequest);
if (book != null && book.getId() != null) {
BookDTO bookDTO = mapBookToBookDTO(book);
return new ResponseEntity<BookDTO>(bookDTO, HttpStatus.CREATED);
}
return new ResponseEntity<BookDTO>(HttpStatus.NOT_MODIFIED);
}
private BookDTO mapBookToBookDTO(Book book) {
ModelMapper mapper = new ModelMapper();
BookDTO bookDTO = mapper.map(book, BookDTO.class);
if (book.getCategory() != null) {
bookDTO.setCategory(new CategoryDTO(book.getCategory().getCode(), book.getCategory().getLabel()));
}
return bookDTO;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(using = BookDTODeserializer.class)
@Data
@AllArgsConstructors
public class BookDTO implements Comparable<BookDTO> {
@ApiModelProperty(value = "Book id")
private Integer id;
@ApiModelProperty(value = "Book title")
private String title;
@ApiModelProperty(value = "Book isbn")
private String isbn;
@ApiModelProperty(value = "Book release date by the editor")
private LocalDate releaseDate;
@ApiModelProperty(value = "Book register date in the library")
private LocalDate registerDate;
@ApiModelProperty(value = "Book total examplaries")
private Integer totalExamplaries;
@ApiModelProperty(value = "Book author")
private String author;
@ApiModelProperty(value = "Book category")
private CategoryDTO category;
@Override
public int compareTo(BookDTO o) {
return title.compareToIgnoreCase(o.getTitle());
}
public BookDTO() {
super();
}
}
@Entity
@Data
@AllArgsConstructors
public class Book {
private static final long serialVersionUID = 425345L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String author;
private String publisher;
private String publicationDate;
private String language;
private String category;
private int numberOfPages;
private String format;
private String isbn;
private double shippingWeight;
private double listPrice;
private double ourPrice;
private boolean active = true;
@Column(columnDefinition = "text")
private String description;
private int inStockNumber;
@Transient
private MultipartFile bookImage;
}
@Data
@AllArgsConstructors
@JsonDeserialize(using = CategoryDTODeserializer.class)
public class CategoryDTO implements Comparable<CategoryDTO> {
public CategoryDTO() {
}
public CategoryDTO(String code, String label) {
super();
this.code = code;
this.label = label;
}
@ApiModelProperty(value = "Category code")
private String code;
@ApiModelProperty(value = "Category label")
private String label;
}
@Entity
@Table(name = "CATEGORY")
public class Category {
public Category() {
}
public Category(String code, String label) {
super();
this.code = code;
this.label = label;
}
private String code;
private String label;
@Id
@Column(name = "CODE")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Column(name = "LABEL", nullable = false)
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
public class BookDTODeserializer extends StdDeserializer<BookDTO> {
@Override
public BookDTO deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
JsonNode node = p.getCodec().readTree(p);
Integer id = (Integer) ((IntNode) node.get("id")).numberValue();
String title = node.get("title").asText();
String isbn = node.get("isbn").asText();
LocalDate releaseDate = LocalDate.parse(node.get("releaseDate").asText());
LocalDate registerDate = LocalDate.parse(node.get("registerDate").asText());
Integer totalExamplaries = (Integer) ((IntNode) node.get("totalExamplaries")).numberValue();
String author = node.get("author").asText();
String codeCategory = node.get("code").asText();
String labelCategory = node.get("label").asText();
return new BookDTO(id, title, isbn, releaseDate, registerDate, totalExamplaries, author,
new CategoryDTO(codeCategory, labelCategory));
// return null;
}
public BookDTODeserializer(Class<?> vc) {
super(vc);
// TODO Auto-generated constructor stub
}
public BookDTODeserializer(JavaType valueType) {
super(valueType);
// TODO Auto-generated constructor stub
}
public BookDTODeserializer(StdDeserializer<?> src) {
super(src);
// TODO Auto-generated constructor stub
}
}
public class CategoryDTODeserializer extends StdDeserializer<CategoryDTO> {
@Override
public CategoryDTO deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = p.getCodec().readTree(p);
String codeCategory = node.get("code").asText();
String labelCategory = node.get("label").asText();
return new CategoryDTO(codeCategory, labelCategory);
}
public CategoryDTODeserializer(Class<?> vc) {
super(vc);
// TODO Auto-generated constructor stub
}
public CategoryDTODeserializer(JavaType valueType) {
super(valueType);
// TODO Auto-generated constructor stub
}
public CategoryDTODeserializer(StdDeserializer<?> src) {
super(src);
// TODO Auto-generated constructor stub
}
} |
Partager