Skip to content

Commit

Permalink
Merge pull request #2 from TarasYashchuk/dev
Browse files Browse the repository at this point in the history
implemented dto and service
  • Loading branch information
TarasYashchuk authored Jun 17, 2024
2 parents 7b7fbc9 + d75993d commit 97c301b
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 35 deletions.
86 changes: 86 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<properties>
<jdk.version>17</jdk.version>
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -52,14 +54,98 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<sourceDirectories>src</sourceDirectories>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/mate/academy/BookStoreApplication.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
package mate.academy;

import java.math.BigDecimal;
import mate.academy.model.Book;
import mate.academy.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class BookStoreApplication {
private BookRepository bookRepository;

@Autowired
public BookStoreApplication(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}

public static void main(String[] args) {
SpringApplication.run(BookStoreApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner() {
return args -> {
Book book = new Book();
book.setAuthor("Jack");
book.setId(1L);
book.setPrice(BigDecimal.valueOf(13));
book.setDescription("123");
book.setCoverImage("/images");
book.setIsbn("123");
book.setTitle("title");

bookRepository.save(book);
};
}
}
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.config;

import org.mapstruct.InjectionStrategy;
import org.mapstruct.NullValueCheckStrategy;

@org.mapstruct. MapperConfig(
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
nullValueCheckStrategy = NullValueCheckStrategy. ALWAYS,
implementationPackage = "<PACKAGE_NAME>.impl"
)
public class MapperConfig {
}
35 changes: 35 additions & 0 deletions src/main/java/mate/academy/controller/BookController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mate.academy.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import mate.academy.dto.BookDto;
import mate.academy.dto.CreateBookRequestDto;
import mate.academy.service.BookService;
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 org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/books")
public class BookController {
private final BookService bookService;

@GetMapping
public List<BookDto> getAll() {
return bookService.getAll();
}

@PostMapping
public BookDto save(@RequestBody CreateBookRequestDto requestDto) {
return bookService.save(requestDto);
}

@GetMapping("/{id}")
public BookDto getBookById(@PathVariable Long id) {
return bookService.getBookById(id);
}
}
15 changes: 15 additions & 0 deletions src/main/java/mate/academy/dto/BookDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mate.academy.dto;

import java.math.BigDecimal;
import lombok.Data;

@Data
public class BookDto {
private Long id;
private String title;
private String author;
private String isbn;
private BigDecimal price;
private String description;
private String coverImage;
}
14 changes: 14 additions & 0 deletions src/main/java/mate/academy/dto/CreateBookRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mate.academy.dto;

import java.math.BigDecimal;
import lombok.Data;

@Data
public class CreateBookRequestDto {
private String title;
private String author;
private String isbn;
private BigDecimal price;
private String description;
private String coverImage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mate.academy.exception;

public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message) {
super(message);
}
}
16 changes: 16 additions & 0 deletions src/main/java/mate/academy/mapper/BookMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mate.academy.mapper;

import mate.academy.config.MapperConfig;
import mate.academy.dto.BookDto;
import mate.academy.dto.CreateBookRequestDto;
import mate.academy.model.Book;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(config = MapperConfig.class)
public interface BookMapper {
BookDto toDto(Book book);

@Mapping(target = "id", ignore = true)
Book toModel(CreateBookRequestDto requestDto);
}
28 changes: 20 additions & 8 deletions src/main/java/mate/academy/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package mate.academy.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import mate.academy.dto.BookDto;
import mate.academy.dto.CreateBookRequestDto;
import mate.academy.exception.EntityNotFoundException;
import mate.academy.mapper.BookMapper;
import mate.academy.model.Book;
import mate.academy.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class BookService {
private final BookRepository bookRepository;

@Autowired
public BookService(BookRepository bookRepository) {
this.bookRepository = bookRepository;
private final BookMapper bookMapper;

public BookDto save(CreateBookRequestDto requestDto) {
Book book = bookMapper.toModel(requestDto);
return bookMapper.toDto(bookRepository.save(book));
}

public Book save(Book book) {
return bookRepository.save(book);
public List<BookDto> getAll() {
return bookRepository.findAll().stream()
.map(bookMapper::toDto)
.toList();
}

public List<Book> findAll() {
return bookRepository.findAll();
public BookDto getBookById(Long id) {
Book book = bookRepository.findById(id)
.orElseThrow(
() -> new EntityNotFoundException("Book with id " + id + " not found"));
return bookMapper.toDto(book);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true

server.servlet.context-path=/api

0 comments on commit 97c301b

Please sign in to comment.