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:
parent
6300f61b7e
commit
fb597316b0
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue