This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
366 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/com/example/comerce/core/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.example.comerce.core.controller; | ||
|
||
public class CategoryController { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/example/comerce/core/controller/ProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.example.comerce.core.controller; | ||
|
||
public class ProductController { | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/example/comerce/core/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
src/main/java/com/example/comerce/core/dto/AddressDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.example.comerce.core.dtos; | ||
|
||
public class CategoryDTO { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.example.comerce.core.dtos; | ||
|
||
public class OrderDTO { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.example.comerce.core.dtos; | ||
|
||
public class ProductDTO { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/comerce/core/entities/OrderItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/main/java/com/example/comerce/core/entities/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
Oops, something went wrong.