Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 44 additions & 34 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,40 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>org.example</groupId>
<artifactId>SpringBoot-BookShop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot-BookShop</name>
<description>SpringBoot-BookShop</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>

<properties>
<java.version>17</java.version>
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand All @@ -64,39 +55,58 @@
</dependency>

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

<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.23.0</version> <!-- Upewnij się, że wersja jest aktualna -->
</dependency>
</dependencies>

<build>
<plugins>
<plugin>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.36</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.6.3</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>4.23.0</version>
<executions>
<execution>
<phase>compile</phase>
<phase>process-resources</phase>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
<goals>
<goal>check</goal>
<goal>update</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
</plugin>
</plugins>
</build>

</project>
12 changes: 0 additions & 12 deletions src/main/java/org/example/SpringBootBookShopApplication.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
package org.example;

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 SpringBootBookShopApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootBookShopApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {

}
};
}

}
13 changes: 13 additions & 0 deletions src/main/java/org/example/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.example.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 {
}
43 changes: 43 additions & 0 deletions src/main/java/org/example/controller/BookController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.example.controller;

import lombok.RequiredArgsConstructor;
import org.example.dto.BookDto;
import org.example.dto.CreateBookRequestDto;
import org.example.service.BookService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@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 createBookRequestDto) {
return bookService.save(createBookRequestDto);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
public void deleteBookById(@PathVariable Long id) {
bookService.deleteById(id);
}

@PutMapping("/{id}")
public void updateBookById(@PathVariable Long id, @RequestBody BookDto bookDto) {
bookService.updateBookById(id, bookDto);
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/example/dto/BookDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.dto;

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

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

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

@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 org.example.exception;

public class BookNotFoundException extends RuntimeException {
public BookNotFoundException(String message) {
super(message);
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/example/exception/BookSaveException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.exception;

public class BookSaveException extends RuntimeException {
public BookSaveException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.exception;

public class BookValidationException extends RuntimeException {
public BookValidationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.exception;

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

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

@Mapper(config = MapperConfig.class)
public interface BookMapper {
BookDto toDto(Book book);
Book toModel(CreateBookRequestDto createBookRequestDto);
}
33 changes: 33 additions & 0 deletions src/main/java/org/example/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
package org.example.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Column;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import java.math.BigDecimal;

@Entity
@Data
@SQLDelete(sql = "UPDATE books SET is_deleted = TRUE WHERE id=?")
@Where(clause = "is_deleted=false")
@Table(name = "books")
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String author;
@Column(nullable = false, unique = true)
private String isbn;
@Column(nullable = false)
private BigDecimal price;
private String description;
private String coverImage;
@Column(nullable = false)
private boolean isDeleted = false;
}
20 changes: 19 additions & 1 deletion src/main/java/org/example/repository/BookRepository.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package org.example.repository;

public interface BookRepository {
import org.example.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;

public interface BookRepository extends JpaRepository<Book, Long> {

@Modifying
@Query("UPDATE Book b SET "
+ " b.title = :title,"
+ " b.author = :author,"
+ " b.isbn = :isbn,"
+ " b.price = :price,"
+ " b.description = :description,"
+ " b.coverImage = :coverImage WHERE b.id = :id AND b.isDeleted = false")
void updateBookById(Long id, String title, String author, String isbn,
BigDecimal price, String description, String coverImage);
}

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/org/example/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package org.example.service;

import org.example.dto.BookDto;
import org.example.dto.CreateBookRequestDto;
import java.util.List;

public interface BookService {
BookDto save(CreateBookRequestDto createBookRequestDto);
List<BookDto> findAll();
BookDto findById(Long id);
void deleteById(Long id);
void updateBookById(Long id, BookDto bookDto);
}
Loading