Commit 2fd6dd55 authored by Matej Bumbera's avatar Matej Bumbera
Browse files

feat: created Book entity

parent 875a0bc4
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+3 −0
Original line number Diff line number Diff line
.idea
data/
target/
 No newline at end of file
+94 −0
Original line number Diff line number Diff line
package cz.muni.bookstore.book;

import cz.muni.bookstore.book.enums.Category;
import jakarta.persistence.*;

import java.math.BigDecimal;

/**
 *  Book in a bookstore
 */
@Entity
@Table(name = "book")
public class Book {

    /**
     * Unique identifier for a book
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * Title of the book
     */
    private String title;

    /**
     * Author of the book
     */
    private String author;

    /**
     * Price of the book
     */
    private BigDecimal price;

    /**
     * Category of the book
     */
    private Category category;


    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getAuthor() {
        return author;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Category getCategory() {
        return category;
    }

    @Override
    public final boolean equals(Object o) {
        if (!(o instanceof Book book)) return false;

        return id.equals(book.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}
+5 −0
Original line number Diff line number Diff line
package cz.muni.bookstore.book.enums;

public enum Category {
    FANTASY, SCIFI, ROMANCE, ADVENTURE, HISTORY, POLITICS, SCIENCE, PSYCHOLOGY, TRAVEL, EDUCATION
}
+28 −0
Original line number Diff line number Diff line
package cz.muni.bookstore.book.enums;

import jakarta.persistence.*;

import java.util.stream.Stream;

@Converter(autoApply = true)
public class CategoryConverter implements AttributeConverter<Category, String> {
    @Override
    public String convertToDatabaseColumn(Category category) {
        if (category == null) {
            return null;
        }
        return category.name();
    }

    @Override
    public Category convertToEntityAttribute(String code) {
        if (code == null) {
            return null;
        }

        return Stream.of(Category.values())
                .filter(c -> c.name().equals(code))
                .findFirst()
                .orElseThrow(IllegalArgumentException::new);
    }
}