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

@ -7,7 +7,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories
@SpringBootApplication
public class BookStoreUsingHexagonalArchitectureApplication {
public static void main(String[] args) {
SpringApplication.run(BookStoreUsingHexagonalArchitectureApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(BookStoreUsingHexagonalArchitectureApplication.class, args);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,11 +5,12 @@ import com.baeldung.hexarch.boostrore.model.Book;
import java.util.Set;
public interface BookOperations {
Book create(final String isbn, final String bookName,
Set<String> authorEmails);
Book create(final String isbn, final String bookName, Set<String> authorEmails);
Book getBookById(final long id);
Book getBookByIsbn(final String isbn);
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
// throw an error
if (booksRepository.findByIsbn(isbn).isPresent()) {
throw new RuntimeException("A Book with ISBN " + isbn
+ " already exists");
throw new RuntimeException("A Book with ISBN " + isbn + " already exists");
}
//Ask for the Author objects
Set<Author> authorSet = authors.stream()
.map(this::upsetAuthor)
.collect(Collectors.toSet());
Set<Author> authorSet = authors.stream().map(this::upsetAuthor).collect(Collectors.toSet());
// create the book object and associate the authors
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
// else return the author we found
if (byEmailId.isEmpty()) {
throw new RuntimeException("Author not found: email id "
+ email);
throw new RuntimeException("Author not found: email id " + email);
}
return byEmailId.get();
}

View File

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

View File

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

View File

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