diff --git a/hexagonal-example/pom.xml b/hexagonal-example/pom.xml new file mode 100644 index 0000000000..47bd8e7489 --- /dev/null +++ b/hexagonal-example/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + com.baeldung + HexagonalLibrary + 0.0.1-SNAPSHOT + HexagonalLibrary + HexagonalLibrary + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.HexagonalLibrary + + + + + + \ No newline at end of file diff --git a/hexagonal-example/src/com/baeldung/HexagonalLibrary.java b/hexagonal-example/src/com/baeldung/HexagonalLibrary.java new file mode 100644 index 0000000000..b730fa1244 --- /dev/null +++ b/hexagonal-example/src/com/baeldung/HexagonalLibrary.java @@ -0,0 +1,13 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HexagonalLibrary { + + public static void main(String[] args) { + SpringApplication.run(HexagonalLibrary.class, args); + } + +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRepoImpl.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRepoImpl.java new file mode 100644 index 0000000000..1b678a4e80 --- /dev/null +++ b/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRepoImpl.java @@ -0,0 +1,39 @@ +package com.baeldung.hexagonalPattern.adapter; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.springframework.stereotype.Repository; + +import com.baeldung.hexagonalPattern.core.domain.Book; +import com.baeldung.hexagonalPattern.ports.LibraryRepo; + +@Repository +public class LibraryRepoImpl implements LibraryRepo { + // This class is the actual implementation of the out bound port/adapter. + + private HashMap books = new HashMap(); + + @Override + public void insertBook(Book book) { + // Mock Database call here. + books.put("mock", new Book("mock", "mock", "mock")); + } + + @Override + public Book searchBook(String name) { + // TODO Auto-generated method stub + Book b = new Book(); + // Some code for retrieval of book from db + return b; + } + + @Override + public List getAllBooks() { + // Fetch all books from db + return books.values().stream().collect(Collectors.toList()); + } + +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRestController.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRestController.java new file mode 100644 index 0000000000..176b8471b5 --- /dev/null +++ b/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRestController.java @@ -0,0 +1,37 @@ +package com.baeldung.hexagonalPattern.adapter; + +import java.util.List; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.hexagonalPattern.core.domain.Book; +import com.baeldung.hexagonalPattern.web.LibraryRestUI; + +@RestController +public class LibraryRestController implements LibraryRestUI { + + @Override + @RequestMapping("/library") + public void insertBook(Book book) { + // TODO Auto-generated method stub + + } + + @Override + @GetMapping("/searchBook") + public Book searchBook(@PathVariable String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + @GetMapping("/listBooks") + public List listAllBooks() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/core/domain/Book.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/core/domain/Book.java new file mode 100644 index 0000000000..38e761ba50 --- /dev/null +++ b/hexagonal-example/src/com/baeldung/hexagonalPattern/core/domain/Book.java @@ -0,0 +1,33 @@ +package com.baeldung.hexagonalPattern.core.domain; + +public class Book { + + private String name; + private String author_list; + private String isbn13; + + /* constructors and getter and setters */ + public Book(String string, String string2, String string3) { + // TODO Auto-generated constructor stub + } + + public Book() { + // TODO Auto-generated constructor stub + } + + /* + * public String getName() { return name; } + * + * public void setName(String name) { this.name = name; } + * + * public String getAuthor_list() { return author_list; } + * + * public void setAuthor_list(String author_list) { this.author_list = + * author_list; } + * + * public String getIsbn13() { return isbn13; } + * + * public void setIsbn13(String isbn13) { this.isbn13 = isbn13; } + */ + +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java new file mode 100644 index 0000000000..1bde7c8d0e --- /dev/null +++ b/hexagonal-example/src/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java @@ -0,0 +1,38 @@ +package com.baeldung.hexagonalPattern.core.impl; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.baeldung.hexagonalPattern.core.domain.Book; +import com.baeldung.hexagonalPattern.ports.LibraryRepo; +import com.baeldung.hexagonalPattern.ports.LibraryService; + +@Service +public class LibraryServiceImpl implements LibraryService { + + // This is the class which actually implements the methods from the ports. + // The ports are just for exposing the methods to the outside. + + @Autowired + private LibraryRepo bookRepo; + + @Override + public void insertBook(Book book) { + // TODO some implementation to insert record in the db or similar + bookRepo.insertBook(book); + } + + @Override + public Book lendBook(String name) { + // TODO Auto-generated method stub + return bookRepo.searchBook(name); + } + + @Override + public List getAllBooks() { + // TODO Auto-generated method + return null; + } + +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryRepo.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryRepo.java new file mode 100644 index 0000000000..83858403ef --- /dev/null +++ b/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryRepo.java @@ -0,0 +1,14 @@ +package com.baeldung.hexagonalPattern.ports; + +import java.util.List; +import com.baeldung.hexagonalPattern.core.domain.Book; + +public interface LibraryRepo { + // Outbound Port. + + public void insertBook(Book book); + + public Book searchBook(String name); + + public List getAllBooks(); +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryService.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryService.java new file mode 100644 index 0000000000..7391b4795d --- /dev/null +++ b/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryService.java @@ -0,0 +1,15 @@ +package com.baeldung.hexagonalPattern.ports; + +import java.util.List; +import com.baeldung.hexagonalPattern.core.domain.Book; + +public interface LibraryService { +//This is the in bound port.exposes the application to the world. + + public void insertBook(Book book); + + public Book lendBook(String name); + + List getAllBooks(); + +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/web/LibraryRestUI.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/web/LibraryRestUI.java new file mode 100644 index 0000000000..16d7beac87 --- /dev/null +++ b/hexagonal-example/src/com/baeldung/hexagonalPattern/web/LibraryRestUI.java @@ -0,0 +1,24 @@ +package com.baeldung.hexagonalPattern.web; + +import java.util.List; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import com.baeldung.hexagonalPattern.core.domain.Book; + +public interface LibraryRestUI { + // This is the in bound Adapter + + @PostMapping + void insertBook(@RequestBody Book book); + + @GetMapping("/{name}") + public Book searchBook(@PathVariable String name); + + @GetMapping + public List listAllBooks(); + +}