The code has been reformatted using the intelliJ-formatter.xml from https://github.com/eugenp/tutorials/blob/master/intelliJ/intelliJ-formatter.xml

This commit is contained in:
K. Naveen Kumar 2021-06-02 18:29:36 +05:30
parent 6300f61b7e
commit fb597316b0
12 changed files with 49 additions and 42 deletions

View File

@ -3,7 +3,8 @@ package com.baeldung.hexarch.boostrore.actions;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@Getter @Setter @Getter
@Setter
public class BookReadActions { public class BookReadActions {
private String bookId; private String bookId;
private String bookName; private String bookName;

View File

@ -13,8 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
public class AuthorsController { public class AuthorsController {
private final AuthorsRepository authorsRepository; private final AuthorsRepository authorsRepository;
@PutMapping(value = "/api/v1/authors", @PutMapping(value = "/api/v1/authors", consumes = MediaType.APPLICATION_JSON_VALUE)
consumes = MediaType.APPLICATION_JSON_VALUE)
private void createAuthor(@RequestBody final Author author) { private void createAuthor(@RequestBody final Author author) {
com.baeldung.hexarch.boostrore.model.Author author1 = new com.baeldung.hexarch.boostrore.model.Author(); com.baeldung.hexarch.boostrore.model.Author author1 = new com.baeldung.hexarch.boostrore.model.Author();
author1.setLastName(author.getLastName()); author1.setLastName(author.getLastName());

View File

@ -20,21 +20,14 @@ import java.util.stream.Collectors;
public class BookController { public class BookController {
private final BookOperations bookOperations; private final BookOperations bookOperations;
@PutMapping(value = "/api/v1/books", @PutMapping(value = "/api/v1/books", consumes = MediaType.APPLICATION_JSON_VALUE)
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody @ResponseBody
private BookResponse createBook( private BookResponse createBook(@RequestBody final CreateBookRequest createBookRequest) {
@RequestBody final CreateBookRequest createBookRequest) { Set<String> authorEmailSet = createBookRequest.getAuthors().stream().map(Author::getEmailId).collect(Collectors.toSet());
Set<String> authorEmailSet = createBookRequest.getAuthors()
.stream().map(Author::getEmailId)
.collect(Collectors.toSet());
Book book = bookOperations.create(createBookRequest.getIsbn(), Book book = bookOperations.create(createBookRequest.getIsbn(), createBookRequest.getTitle(), authorEmailSet);
createBookRequest.getTitle(), authorEmailSet);
// book.getAuthors().stream().map() // book.getAuthors().stream().map()
return BookResponse.builder() return BookResponse.builder().isbn(book.getIsbn()).title(book.getName()).build();
.isbn(book.getIsbn()).title(book.getName())
.build();
} }
} }

View File

@ -1,9 +1,8 @@
package com.baeldung.hexarch.boostrore.controller.dto; package com.baeldung.hexarch.boostrore.controller.dto;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import lombok.Builder;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import lombok.EqualsAndHashCode;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import lombok.Value;
import lombok.*;
@Value @Value
@Builder @Builder

View File

@ -4,8 +4,12 @@ import com.baeldung.hexarch.boostrore.model.Author;
public interface AuthorsOperation { public interface AuthorsOperation {
Author create(String firstName, String lastName, String emailId); Author create(String firstName, String lastName, String emailId);
Author getAuthorByEmailId(final String emailId); Author getAuthorByEmailId(final String emailId);
Author get(final long id); Author get(final long id);
void delete(final String emailId); void delete(final String emailId);
void delete(final long id); void delete(final long id);
} }

View File

@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
@AllArgsConstructor @AllArgsConstructor
public class AuthorsOperationsImpl implements AuthorsOperation { public class AuthorsOperationsImpl implements AuthorsOperation {
private final AuthorsRepository authorsRepository; private final AuthorsRepository authorsRepository;
@Override @Override
public Author create(String firstName, String lastName, String emailId) { public Author create(String firstName, String lastName, String emailId) {
Author author = new Author(); Author author = new Author();

View File

@ -5,11 +5,12 @@ import com.baeldung.hexarch.boostrore.model.Book;
import java.util.Set; import java.util.Set;
public interface BookOperations { public interface BookOperations {
Book create(final String isbn, final String bookName, Book create(final String isbn, final String bookName, Set<String> authorEmails);
Set<String> authorEmails);
Book getBookById(final long id); Book getBookById(final long id);
Book getBookByIsbn(final String isbn); Book getBookByIsbn(final String isbn);
Set<Author> getAuthorsOfBook(final long id); Set<Author> getAuthorsOfBook(final long id);
} }

View File

@ -22,13 +22,10 @@ public class BookOperationsImpl implements BookOperations {
// if the book with given isbn already exists then let us // if the book with given isbn already exists then let us
// throw an error // throw an error
if (booksRepository.findByIsbn(isbn).isPresent()) { if (booksRepository.findByIsbn(isbn).isPresent()) {
throw new RuntimeException("A Book with ISBN " + isbn throw new RuntimeException("A Book with ISBN " + isbn + " already exists");
+ " already exists");
} }
//Ask for the Author objects //Ask for the Author objects
Set<Author> authorSet = authors.stream() Set<Author> authorSet = authors.stream().map(this::upsetAuthor).collect(Collectors.toSet());
.map(this::upsetAuthor)
.collect(Collectors.toSet());
// create the book object and associate the authors // create the book object and associate the authors
Book book = new Book(); Book book = new Book();
@ -62,8 +59,7 @@ public class BookOperationsImpl implements BookOperations {
// Check if a author already exists with given email id. if not create the Author // Check if a author already exists with given email id. if not create the Author
// else return the author we found // else return the author we found
if (byEmailId.isEmpty()) { if (byEmailId.isEmpty()) {
throw new RuntimeException("Author not found: email id " throw new RuntimeException("Author not found: email id " + email);
+ email);
} }
return byEmailId.get(); return byEmailId.get();
} }

View File

@ -9,11 +9,13 @@ import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import java.util.Objects; import java.util.Objects;
@Getter @Setter @Getter
@Setter
@Entity(name = "authors") @Entity(name = "authors")
@Table(name = "authors") @Table(name = "authors")
public class Author { public class Author {
@Id @GeneratedValue @Id
@GeneratedValue
private long id; private long id;
private String firstName; private String firstName;
private String lastName; private String lastName;
@ -21,8 +23,10 @@ public class Author {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
Author author = (Author) o; Author author = (Author) o;
return id == author.id; return id == author.id;
} }

View File

@ -3,12 +3,18 @@ package com.baeldung.hexarch.boostrore.model;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import javax.persistence.*; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@Getter @Setter @Getter
@Entity(name = "books") @Table(name = "books") @Setter
@Entity(name = "books")
@Table(name = "books")
public class Book { public class Book {
@Id @Id
@GeneratedValue @GeneratedValue
@ -20,8 +26,10 @@ public class Book {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
Book book = (Book) o; Book book = (Book) o;
return id == book.id; return id == book.id;
} }

View File

@ -9,5 +9,6 @@ import java.util.Optional;
@Repository @Repository
public interface AuthorsRepository extends JpaRepository<Author, Long> { public interface AuthorsRepository extends JpaRepository<Author, Long> {
Optional<Author> findByEmailId(final String emailId); Optional<Author> findByEmailId(final String emailId);
void deleteByEmailId(final String emailId); void deleteByEmailId(final String emailId);
} }