gupta.aashishrules@gmail.com - Hexagonal
This commit is contained in:
gupta-ashu01 2020-07-03 17:18:39 +05:30 committed by GitHub
parent c946569125
commit 6182cdf09f
8 changed files with 211 additions and 0 deletions

23
bookstore/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hexagonal</groupId>
<artifactId>bookstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
package com.hexagonal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}

View File

@ -0,0 +1,37 @@
package com.hexagonal.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.hexagonal.domain.Book;
import com.hexagonal.service.BookService;
@RestController
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/book/add")
@PostMapping(produces = { MediaType.TEXT_PLAIN_VALUE })
public void addBook(@RequestBody Book book) {
bookService.addBook(book);
}
public Book buyBook(@PathVariable String isbn) {
return bookService.buyBook(isbn);
}
public List<Book> listBooks() {
return bookService.listBooks();
}
}

View File

@ -0,0 +1,38 @@
package com.hexagonal.domain;
public class Book {
private String name;
private String isbn;
private String author;
@Override
public String toString() {
return "Book [name=" + name + ", ISBN=" + isbn + ", author=" + author + "]";
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

View File

@ -0,0 +1,15 @@
package com.hexagonal.repository;
import java.util.List;
import com.hexagonal.domain.Book;
public interface BookRepository {
public void add(Book book);
public Book buy(String isbn);
public List<Book> list();
}

View File

@ -0,0 +1,35 @@
package com.hexagonal.repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Repository;
import com.hexagonal.domain.Book;
@Repository
public class BookRepositoryImpl implements BookRepository {
private Map<String, Book> bookMap = new HashMap<>();
@Override
public void add(Book book) {
bookMap.put(book.getIsbn(), book);
System.out.println("Book Added " + book);
}
@Override
public Book buy(String isbn) {
return bookMap.get(isbn);
}
@Override
public List<Book> list() {
return bookMap.values()
.stream()
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,15 @@
package com.hexagonal.service;
import java.util.List;
import com.hexagonal.domain.Book;
public interface BookService {
public void addBook(Book book);
public Book buyBook(String isbn);
public List<Book> listBooks();
}

View File

@ -0,0 +1,35 @@
package com.hexagonal.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hexagonal.domain.Book;
import com.hexagonal.repository.BookRepository;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookRepository bookRepository;
@Override
public void addBook(Book book) {
bookRepository.add(book);
}
@Override
public Book buyBook(String isbn) {
return bookRepository.buy(isbn);
}
@Override
public List<Book> listBooks() {
return bookRepository.list();
}
}