Skip to content

Commit

Permalink
Merge pull request #4 from ChabVlad/hw1_dto
Browse files Browse the repository at this point in the history
Hw1 dto
  • Loading branch information
ChabVlad authored Sep 7, 2024
2 parents 5f860e6 + f189a30 commit 27c5ea0
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 18 deletions.
39 changes: 38 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath/>
</parent>
<groupId>project</groupId>
<artifactId>BookStore</artifactId>
Expand All @@ -29,6 +29,8 @@
<properties>
<java.version>17</java.version>
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -66,15 +68,49 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</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>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand All @@ -96,6 +132,7 @@
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<sourceDirectories>src</sourceDirectories>
</configuration>
</plugin>
</plugins>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/project/bookstore/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package project.bookstore.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 {
}
37 changes: 37 additions & 0 deletions src/main/java/project/bookstore/controller/BookController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package project.bookstore.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
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;
import project.bookstore.dto.BookDto;
import project.bookstore.dto.CreateBookRequestDto;
import project.bookstore.mapper.BookMapper;
import project.bookstore.service.BookService;

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

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

@GetMapping("/{id}")
public BookDto getBookById(@PathVariable Long id) {
return bookService.getBookById(id);
}

@PostMapping
public BookDto createBook(@RequestBody CreateBookRequestDto requestBookDto) {
return bookMapper.toDto(bookService.save(requestBookDto));
}
}
16 changes: 16 additions & 0 deletions src/main/java/project/bookstore/dto/BookDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package project.bookstore.dto;

import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BookDto {
private String title;
private String author;
private String isbn;
private BigDecimal price;
private String description;
private String coverImage;
}
16 changes: 16 additions & 0 deletions src/main/java/project/bookstore/dto/CreateBookRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package project.bookstore.dto;

import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
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 project.bookstore.exception;

public class DataProcessingException extends RuntimeException {
public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package project.bookstore.exception;

public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public EntityNotFoundException(String message) {
super(message);
}
}
14 changes: 14 additions & 0 deletions src/main/java/project/bookstore/mapper/BookMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package project.bookstore.mapper;

import org.mapstruct.Mapper;
import project.bookstore.config.MapperConfig;
import project.bookstore.dto.BookDto;
import project.bookstore.dto.CreateBookRequestDto;
import project.bookstore.model.Book;

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

public Book toModel(CreateBookRequestDto requestDto);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package project.bookstore.repository;

import java.util.List;
import java.util.Optional;
import project.bookstore.model.Book;

public interface BookRepository {
Book save(Book book);

List findAll();
List<Book> findAll();

Optional<Book> findBookById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package project.bookstore.repository.impl;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import java.util.List;
import java.util.Optional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import project.bookstore.exception.DataProcessingException;
import project.bookstore.model.Book;
import project.bookstore.repository.BookRepository;

Expand Down Expand Up @@ -38,14 +37,22 @@ public Book save(Book book) {
}

@Override
public List findAll() {
public List<Book> findAll() {
try (Session session = sessionFactory.openSession()) {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Book> criteriaQuery = criteriaBuilder.createQuery(Book.class);
Root<Book> root = criteriaQuery.from(Book.class);
return session.createQuery(criteriaQuery).getResultList();
return session.createQuery("FROM Book", Book.class).getResultList();
} catch (Exception e) {
throw new RuntimeException("Cannot find all books", e);
throw new DataProcessingException("Can't found books ", e);
}
}

@Override
public Optional<Book> findBookById(Long id) {
try (Session session = sessionFactory.openSession()) {
return Optional.ofNullable(session.createQuery(
"FROM Book WHERE id = :id", Book.class)
.setParameter("id", id).uniqueResult());
} catch (Exception e) {
throw new DataProcessingException("Can't found book by id: " + id, e);
}
}
}
8 changes: 6 additions & 2 deletions src/main/java/project/bookstore/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package project.bookstore.service;

import java.util.List;
import project.bookstore.dto.BookDto;
import project.bookstore.dto.CreateBookRequestDto;
import project.bookstore.model.Book;

public interface BookService {
Book save(Book book);
Book save(CreateBookRequestDto requestBookDto);

List findAll();
List<BookDto> findAll();

BookDto getBookById(Long id);
}
22 changes: 17 additions & 5 deletions src/main/java/project/bookstore/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package project.bookstore.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import project.bookstore.dto.BookDto;
import project.bookstore.dto.CreateBookRequestDto;
import project.bookstore.exception.EntityNotFoundException;
import project.bookstore.mapper.BookMapper;
import project.bookstore.model.Book;
import project.bookstore.repository.BookRepository;
import project.bookstore.service.BookService;

@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookRepository bookRepository;
private BookMapper bookMapper;

@Override
public Book save(Book book) {
public Book save(CreateBookRequestDto requestBookDto) {
Book book = bookMapper.toModel(requestBookDto);
return bookRepository.save(book);
}

@Override
public List findAll() {
return bookRepository.findAll();
public List<BookDto> findAll() {
return bookRepository.findAll().stream()
.map(bookMapper::toDto)
.toList();
}

@Override
public BookDto getBookById(Long id) {
return bookMapper.toDto(bookRepository.findBookById(id).orElseThrow(
() -> new EntityNotFoundException("Book not found by id: " + id)));
}
}

0 comments on commit 27c5ea0

Please sign in to comment.