Skip to content

Commit

Permalink
Infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
sofieneBK committed Mar 24, 2021
1 parent cc597e2 commit bdf5960
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
67 changes: 67 additions & 0 deletions infrastructure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>ms-library</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>infrastructure</artifactId>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>domain</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.example.adapters;

import org.example.entity.Book;
import org.example.mappers.BookMapper;
import org.example.repository.BookRepository;
import org.exemple.data.BookDto;
import org.exemple.ports.spi.BookPersistencePort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class BookJpaAdapter implements BookPersistencePort {

@Autowired
private BookRepository bookRepository;

@Override
public BookDto addBook(BookDto bookDto) {

Book book = BookMapper.INSTANCE.bookDtoToBook(bookDto);

Book bookSaved = bookRepository.save(book);

return BookMapper.INSTANCE.bookToBookDto(bookSaved);
}

@Override
public void deleteBookById(Long id) {
bookRepository.deleteById(id);
}

@Override
public BookDto updateBook(BookDto bookDto) {
return addBook(bookDto);
}

@Override
public List<BookDto> getBooks() {

List<Book> bookList = bookRepository.findAll();

return BookMapper.INSTANCE.bookListToBookDtoList(bookList);
}

@Override
public BookDto getBookById(Long bookId) {

Optional<Book> book = bookRepository.findById(bookId);

if (book.isPresent()) {
return BookMapper.INSTANCE.bookToBookDto(book.get());
}

return null;
}
}
29 changes: 29 additions & 0 deletions infrastructure/src/main/java/org/example/entity/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;

private String description;

private Double price;
}
22 changes: 22 additions & 0 deletions infrastructure/src/main/java/org/example/mappers/BookMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.example.mappers;

import org.example.entity.Book;
import org.exemple.data.BookDto;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface BookMapper {

BookMapper INSTANCE = Mappers.getMapper(BookMapper.class);

BookDto bookToBookDto(Book book);

Book bookDtoToBook(BookDto bookDto);

List<BookDto> bookListToBookDtoList(List<Book> bookList);

List<Book> BookDtoListTobookList(List<BookDto> BookDtoList);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.example.repository;

import org.example.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {

}

0 comments on commit bdf5960

Please sign in to comment.