Skip to content
This repository has been archived by the owner on Dec 7, 2024. It is now read-only.

Commit

Permalink
feat(entities): basic JPA relationship mapping and your respective va…
Browse files Browse the repository at this point in the history
…lidation
  • Loading branch information
alexZ7000 committed Oct 10, 2024
1 parent 04c475b commit 32ec94c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
50 changes: 50 additions & 0 deletions src/main/java/com/example/comerce/core/entities/Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.comerce.core.entities;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Entity
@Table(name = "address")
@Getter
@Setter
public class Address {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "address_id", nullable = false, unique = true, updatable = false, length = 36)
@NotNull(message = "Address ID não pode ser null")
private UUID address_id;

@Column(nullable = false)
@NotBlank(message = "CEP não pode estar em branco")
private String postal_code;

@Column(nullable = false)
@NotBlank(message = "Rua não pode estar em branco")
private String street;

@Column(nullable = false)
@NotBlank(message = "Número não pode estar em branco")
private String number;

@Column(nullable = false)
@NotBlank(message = "Bairro não pode estar em branco")
private String complement;

@Column(nullable = false)
@NotBlank(message = "Bairro não pode estar em branco")
private String neighborhood;

@Column(nullable = false)
@NotBlank(message = "Cidade não pode estar em branco")
private String city;

@Column(nullable = false)
@NotBlank(message = "Estado não pode estar em branco")
private String state;
}
27 changes: 27 additions & 0 deletions src/main/java/com/example/comerce/core/entities/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.comerce.core.entities;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import lombok.Singular;

import java.util.UUID;

@Entity
@Table(name = "category")
@Getter
@Setter
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "category_id", nullable = false, unique = true, updatable = false, length = 36)
@NotNull(message = "Category ID não pode ser null")
private UUID category_id;

@Column
@Size(min = 10, max = 255, message = "A descrição da categoria deve ter entre 10 e 255 caracteres")
private String description;
}
36 changes: 36 additions & 0 deletions src/main/java/com/example/comerce/core/entities/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.comerce.core.entities;

import jakarta.persistence.*;
import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.util.UUID;

@Entity
@Table(name = "orders")
@Getter
@Setter
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id", nullable = false, unique = true, updatable = false, length = 36)
@NotNull(message = "Order ID não pode ser null")
private UUID order_id;

@Column(nullable = false)
@NotNull(message = "Data do pedido não pode ser null")
@FutureOrPresent(message = "Data do pedido deve ser no presente ou no futuro")
private Date date;

@Column
@Positive(message = "O desconto deve ser um valor positivo")
private double discount;

@Column(nullable = false)
@Positive(message = "O valor total deve ser um valor positivo")
private double total_price;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.comerce.core.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Embeddable
@Getter
@Setter
@EqualsAndHashCode
public class ProductCategoryId {
@Column(insertable = false, updatable = false)
private UUID product_id;

@Column(insertable = false, updatable = false)
private UUID category_id;
}
36 changes: 32 additions & 4 deletions src/main/java/com/example/comerce/core/entities/User.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
package com.example.comerce.core.entities;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Entity
@Table(name = "users")
@Getter
@Setter
public class User {

@Id
private UUID id;
}
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id", nullable = false, unique = true, updatable = false, length = 36)
@NotNull(message = "User ID não pode ser null")
private UUID user_id;

@Column(length = 100, nullable = false)
@NotBlank(message = "Nome não pode estar em branco")
@Size(min = 3, max = 100, message = "Nome deve ter no mínimo 3 caracteres e no máximo 100 caracteres")
private String name;

@Column(length = 11, nullable = false, unique = true)
@Size(min = 11, max = 11, message = "Telefone deve ter 11 caracteres")
private String telephone;

@Column(length = 11, nullable = false, unique = true)
@Size(min = 11, max = 11, message = "CPF deve ter 11 caracteres")
private String cpf;

@Column(length = 100, nullable = false, unique = true)
@NotBlank(message = "Email não pode estar em branco")
@Email(message = "E-mail inválido")
private String email;

@Column(length = 255, nullable = false)
private Address address;
}

0 comments on commit 32ec94c

Please sign in to comment.