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

Commit

Permalink
feat(User): Client route finished
Browse files Browse the repository at this point in the history
  • Loading branch information
alexZ7000 committed Oct 10, 2024
1 parent 32ec94c commit abd810c
Show file tree
Hide file tree
Showing 23 changed files with 366 additions and 27 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.comerce.core.controller;

public class CategoryController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.comerce.core.controller;

public class ProductController {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
package com.example.comerce.core.controller;

import com.example.comerce.core.dtos.UserDTO;
import com.example.comerce.core.entities.User;
import com.example.comerce.core.services.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@RestController
@RequestMapping("/api/users")
public class UserController {

@Autowired
private UserService userService;

@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.findAll();
return ResponseEntity.ok(users);
}

@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable UUID id) {
Optional<User> user = userService.findById(id);
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@PostMapping
public ResponseEntity<User> createUser(@Valid @RequestBody UserDTO userDTO) {
User savedUser = userService.save(userDTO);
return ResponseEntity.ok(savedUser);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable UUID id) {
userService.delete(id);
return ResponseEntity.noContent().build();
}
}

50 changes: 50 additions & 0 deletions src/main/java/com/example/comerce/core/dto/AddressDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.comerce.core.dtos;

import com.example.comerce.core.entities.Address;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class AddressDTO {
@NotBlank(message = "CEP não pode estar em branco")
private String postal_code;

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

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

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

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

@NotBlank(message = "Estado não pode estar em branco")
private String state;

public Address toEntity() {
Address address = new Address();
address.setPostal_code(this.postal_code);
address.setStreet(this.street);
address.setNumber(this.number);
address.setNeighborhood(this.neighborhood);
address.setCity(this.city);
address.setState(this.state);
return address;
}

public static AddressDTO toDTO(Address address) {
AddressDTO addressDTO = new AddressDTO();
addressDTO.setPostal_code(address.getPostal_code());
addressDTO.setStreet(address.getStreet());
addressDTO.setNumber(address.getNumber());
addressDTO.setNeighborhood(address.getNeighborhood());
addressDTO.setCity(address.getCity());
addressDTO.setState(address.getState());
return addressDTO;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/example/comerce/core/dto/CategoryDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.comerce.core.dtos;

public class CategoryDTO {
}
4 changes: 4 additions & 0 deletions src/main/java/com/example/comerce/core/dto/OrderDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.comerce.core.dtos;

public class OrderDTO {
}
4 changes: 4 additions & 0 deletions src/main/java/com/example/comerce/core/dto/ProductDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.comerce.core.dtos;

public class ProductDTO {
}
50 changes: 49 additions & 1 deletion src/main/java/com/example/comerce/core/dto/UserDTO.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
package com.example.comerce.core.dto;
package com.example.comerce.core.dtos;

import com.example.comerce.core.entities.User;
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;

@Getter
@Setter
public class UserDTO {
@NotBlank(message = "Nome não pode estar em branco")
@Size(min = 3, max = 100, message = "Nome deve ter no mínimo 3 e no máximo 100 caracteres")
private String name;

@NotBlank(message = "Telefone não pode estar em branco")
@Size(min = 11, max = 11, message = "Telefone deve ter 11 caracteres")
private String telephone;

@NotBlank(message = "CPF não pode estar em branco")
@Size(min = 11, max = 11, message = "CPF deve ter 11 caracteres")
private String cpf;

@NotBlank(message = "Email não pode estar em branco")
@Email(message = "E-mail inválido")
private String email;

@NotNull(message = "Endereço não pode ser null")
private AddressDTO address;

public User toEntity() {
User user = new User();
user.setName(this.name);
user.setTelephone(this.telephone);
user.setCpf(this.cpf);
user.setEmail(this.email);
user.setAddress(this.address.toEntity());
return user;
}

public static UserDTO toDTO(User user) {
UserDTO userDTO = new UserDTO();
userDTO.setName(user.getName());
userDTO.setTelephone(user.getTelephone());
userDTO.setCpf(user.getCpf());
userDTO.setEmail(user.getEmail());
userDTO.setAddress(AddressDTO.toDTO(user.getAddress()));
return userDTO;
}
}
10 changes: 0 additions & 10 deletions src/main/java/com/example/comerce/core/entities/Address.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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;

Expand All @@ -17,34 +15,26 @@ 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;
}
4 changes: 4 additions & 0 deletions src/main/java/com/example/comerce/core/entities/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Setter;
import lombok.Singular;

import java.util.Set;
import java.util.UUID;

@Entity
Expand All @@ -24,4 +25,7 @@ public class Category {
@Column
@Size(min = 10, max = 255, message = "A descrição da categoria deve ter entre 10 e 255 caracteres")
private String description;

@ManyToMany(mappedBy = "categories")
private Set<Product> products;
}
8 changes: 8 additions & 0 deletions src/main/java/com/example/comerce/core/entities/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Setter;

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

@Entity
Expand All @@ -21,6 +22,10 @@ public class Order {
@NotNull(message = "Order ID não pode ser null")
private UUID order_id;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

@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")
Expand All @@ -33,4 +38,7 @@ public class Order {
@Column(nullable = false)
@Positive(message = "O valor total deve ser um valor positivo")
private double total_price;

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private List<OrderItem> items;
}
39 changes: 39 additions & 0 deletions src/main/java/com/example/comerce/core/entities/OrderItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.comerce.core.entities;

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

import java.util.UUID;

@Entity
@Table(name = "order_items")
@Getter
@Setter
public class OrderItem {

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

@ManyToOne
@JoinColumn(name = "order_id", nullable = false)
private Order order;

@ManyToOne
@JoinColumn(name = "product_id", nullable = false)
private Product product;

@Column(nullable = false)
@Positive(message = "A quantidade deve ser positiva")
private int quantity;

@Column(nullable = false)
@Positive(message = "O preço do item deve ser positivo")
private double price;
}

44 changes: 44 additions & 0 deletions src/main/java/com/example/comerce/core/entities/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.comerce.core.entities;

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

import java.util.List;
import java.util.Set;
import java.util.UUID;

@Entity
@Table(name = "product")
@Getter
@Setter
public class Product {

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

@Column(nullable = false)
@NotNull(message = "O nome do produto não pode ser null")
private String name;

@Column(nullable = false)
@Positive(message = "O preço do produto deve ser positivo")
private double price;

@ManyToMany
@JoinTable(
name = "product_category",
joinColumns = @JoinColumn(name = "product_id"),
inverseJoinColumns = @JoinColumn(name = "category_id")
)
private Set<Category> categories;

@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
private List<OrderItem> orderItems;
}

Loading

0 comments on commit abd810c

Please sign in to comment.