Skip to content

Commit

Permalink
Merge pull request #2 from yaaariiik/wip-book-entity
Browse files Browse the repository at this point in the history
Was added: BookDto classes, Mapper with config class, BookController, new exception. Was updated: BookRepository, BookService
  • Loading branch information
yaaariiik authored Oct 6, 2023
2 parents a654cd0 + 85dc949 commit 993fc94
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 35 deletions.
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<properties>
<java.version>17</java.version>
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
</properties>

<dependencies>
Expand All @@ -26,6 +28,11 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>

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

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand All @@ -42,6 +49,12 @@
<artifactId>lombok</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -75,6 +88,32 @@
<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>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/book/store/OnlineBookStoreApplication.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
package book.store;

import book.store.model.Book;
import book.store.service.BookService;
import java.math.BigDecimal;
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 OnlineBookStoreApplication {
@Autowired
private BookService bookService;

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

@Bean
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
Book theFountainhead = new Book();
theFountainhead.setTitle("Джерело");
theFountainhead.setAuthor("Айн Ренд");
theFountainhead.setIsbn("9786177279555");
theFountainhead.setPrice(BigDecimal.valueOf(396));

bookService.save(theFountainhead);
System.out.println(bookService.findAll());
}
};
}
}
13 changes: 13 additions & 0 deletions src/main/java/book/store/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package book.store.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/book/store/controller/BookController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package book.store.controller;

import book.store.dto.book.BookDto;
import book.store.dto.book.CreateBookRequestDto;
import book.store.service.BookService;
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;

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

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

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

@PostMapping
public BookDto createBook(@RequestBody CreateBookRequestDto bookDto) {
return bookService.save(bookDto);
}
}
15 changes: 15 additions & 0 deletions src/main/java/book/store/dto/book/BookDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package book.store.dto.book;

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/book/store/dto/book/CreateBookRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package book.store.dto.book;

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 book.store.exception;

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

import book.store.config.MapperConfig;
import book.store.dto.book.BookDto;
import book.store.dto.book.CreateBookRequestDto;
import book.store.model.Book;
import org.mapstruct.Mapper;

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

Book toModel(CreateBookRequestDto requestDto);
}
3 changes: 3 additions & 0 deletions src/main/java/book/store/repository/BookRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import book.store.model.Book;
import java.util.List;
import java.util.Optional;

public interface BookRepository {
Book save(Book book);

List<Book> findAll();

Optional<Book> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import book.store.repository.BookRepository;
import jakarta.persistence.criteria.CriteriaQuery;
import java.util.List;
import java.util.Optional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
Expand Down Expand Up @@ -53,4 +54,12 @@ public List<Book> findAll() {
throw new DataProcessingException("Can't get all books", e);
}
}

@Override
public Optional<Book> findById(Long id) {
try (Session session = sessionFactory.openSession()) {
Book book = session.find(Book.class, id);
return Optional.ofNullable(book);
}
}
}
9 changes: 6 additions & 3 deletions src/main/java/book/store/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package book.store.service;

import book.store.model.Book;
import book.store.dto.book.BookDto;
import book.store.dto.book.CreateBookRequestDto;
import java.util.List;

public interface BookService {
Book save(Book book);
BookDto save(CreateBookRequestDto requestDto);

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

BookDto findById(Long id);
}
27 changes: 22 additions & 5 deletions src/main/java/book/store/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package book.store.service.impl;

import book.store.dto.book.BookDto;
import book.store.dto.book.CreateBookRequestDto;
import book.store.exception.EntityNotFoundException;
import book.store.mapper.BookMapper;
import book.store.model.Book;
import book.store.repository.BookRepository;
import book.store.service.BookService;
Expand All @@ -10,19 +14,32 @@
@Service
public class BookServiceImpl implements BookService {
private final BookRepository bookRepository;
private final BookMapper bookMapper;

@Autowired
public BookServiceImpl(BookRepository bookRepository) {
public BookServiceImpl(BookRepository bookRepository, BookMapper bookMapper) {
this.bookRepository = bookRepository;
this.bookMapper = bookMapper;
}

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

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

@Override
public BookDto findById(Long id) {
Book book = bookRepository.findById(id).orElseThrow(
() -> new EntityNotFoundException("Can't find a book by id" + id)
);
return bookMapper.toDto(book);
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ spring.datasource.password=password

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jackson.deserialization.fail-on-unknown-properties=true
6 changes: 3 additions & 3 deletions src/test/java/book/store/OnlineBookStoreApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootTest
class OnlineBookStoreApplicationTests {

@Test
void contextLoads() {
}
@Test
void contextLoads() {
}

}

0 comments on commit 993fc94

Please sign in to comment.