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

Commit

Permalink
project finished
Browse files Browse the repository at this point in the history
  • Loading branch information
alexZ7000 committed Oct 10, 2024
1 parent 7f9c439 commit 3f84fc8
Show file tree
Hide file tree
Showing 39 changed files with 541 additions and 110 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.comerce.core.dto.CategoryDTO;
import com.example.comerce.core.entities.Category;
import com.example.comerce.core.entities.Product;
import com.example.comerce.core.services.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
Expand All @@ -14,37 +13,42 @@

@RestController
@RequestMapping("/api/category")
public class CategoryController {
public final class CategoryController {

private final CategoryService categoryService;

@Autowired
private CategoryService categoryService;
public CategoryController(final CategoryService categoryService) {
this.categoryService = categoryService;
}

@GetMapping
public ResponseEntity<List<Category>> getAllCategories() {
List<Category> categories = categoryService.findAll();
final List<Category> categories = categoryService.findAll();
return ResponseEntity.ok(categories);
}

@GetMapping("/{id}")
public ResponseEntity<Category> getCategoryById(@PathVariable UUID id) {
Optional<Category> category = Optional.ofNullable(categoryService.findById(id));
public ResponseEntity<Category> getCategoryById(@PathVariable final UUID id) {
final Optional<Category> category = Optional.ofNullable(categoryService.findById(id));
return category.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@PostMapping
public ResponseEntity<Category> createCategory(@RequestBody CategoryDTO categoryDTO) {
Category savedCategory = categoryService.save(categoryDTO.toEntity());
public ResponseEntity<Category> createCategory(@RequestBody final CategoryDTO categoryDTO) {
final Category savedCategory = categoryService.save(categoryDTO.toEntity());
return ResponseEntity.ok(savedCategory);
}

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

@PutMapping("/{id}")
public ResponseEntity<Category> updateCategory(@PathVariable UUID id, @RequestBody CategoryDTO categoryDTO) {
Category updatedCategory = categoryService.update(id, categoryDTO.toEntity());
public ResponseEntity<Category> updateCategory(@PathVariable final UUID id, @RequestBody final CategoryDTO categoryDTO) {
final Category updatedCategory = categoryService.update(id, categoryDTO.toEntity());
return ResponseEntity.ok(updatedCategory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.comerce.core.controller;

import com.example.comerce.core.dto.OrderDTO;
import com.example.comerce.core.entities.Order;
import com.example.comerce.core.services.OrderService;
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/orders")
public final class OrderController {

private final OrderService orderService;

@Autowired
public OrderController(final OrderService orderService) {
this.orderService = orderService;
}

@GetMapping
public ResponseEntity<List<Order>> getAllOrders() {
final List<Order> orders = orderService.findAll();
return ResponseEntity.ok(orders);
}

@GetMapping("/{id}")
public ResponseEntity<Order> getOrderById(@PathVariable final UUID id) {
final Optional<Order> order = orderService.findById(id);
return order.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@PostMapping
public ResponseEntity<Order> createOrder(@RequestBody final OrderDTO orderDTO) {
final Order savedOrder = orderService.save(orderDTO);
return ResponseEntity.ok(savedOrder);
}

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

@PutMapping("/{id}")
public ResponseEntity<Order> updateOrder(@PathVariable final UUID id, @RequestBody final OrderDTO orderDTO) {
final Order updatedOrder = orderService.update(id, orderDTO);
return ResponseEntity.ok(updatedOrder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,42 @@

@RestController
@RequestMapping("/api/products")
public class ProductController {
public final class ProductController {

private final ProductService productService;

@Autowired
private ProductService productService;
public ProductController(final ProductService productService) {
this.productService = productService;
}

@GetMapping
public ResponseEntity<List<Product>> getAllProducts() {
List<Product> products = productService.findAll();
final List<Product> products = productService.findAll();
return ResponseEntity.ok(products);
}

@GetMapping("/{id}")
public ResponseEntity<Product> getProductById(@PathVariable UUID id) {
Optional<Product> product = productService.findById(id);
return product.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@GetMapping("/products/{category}/{id}")
public ResponseEntity<Product> getProductByCategoryAndId(@PathVariable String category, @PathVariable UUID id) {
Optional<Product> product = productService.findById(id);
public ResponseEntity<Product> getProductById(@PathVariable final UUID id) {
final Optional<Product> product = productService.findById(id);
return product.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@PostMapping
public ResponseEntity<Product> createProduct(@RequestBody ProductDTO productDTO) {
Product savedProduct = productService.save(productDTO);
public ResponseEntity<Product> createProduct(@RequestBody final ProductDTO productDTO) {
final Product savedProduct = productService.save(productDTO);
return ResponseEntity.ok(savedProduct);
}

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

@PutMapping("/{id}")
public ResponseEntity<Product> updateProduct(@PathVariable UUID id, @RequestBody ProductDTO productDTO) {
Product updatedProduct = productService.update(id, productDTO);
public ResponseEntity<Product> updateProduct(@PathVariable final UUID id, @RequestBody final ProductDTO productDTO) {
final Product updatedProduct = productService.update(id, productDTO);
return ResponseEntity.ok(updatedProduct);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.comerce.core.controller;

import com.example.comerce.core.dto.UserDTO;
import com.example.comerce.core.entities.LoginRequest;
import com.example.comerce.core.entities.LoginResponse;
import com.example.comerce.core.entities.User;
import com.example.comerce.core.services.UserService;
import jakarta.validation.Valid;
Expand All @@ -14,31 +16,40 @@

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

private final UserService userService;

@Autowired
private UserService userService;
public UserController(final UserService userService) {
this.userService = userService;
}

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

@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable UUID id) {
Optional<User> user = userService.findById(id);
public ResponseEntity<User> getUserById(@PathVariable final UUID id) {
final 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);
public ResponseEntity<User> createUser(@Valid @RequestBody final UserDTO userDTO) {
final User savedUser = userService.save(userDTO);
return ResponseEntity.ok(savedUser);
}

@PostMapping("/login")
public LoginResponse login(@RequestBody final LoginRequest request) throws Exception {
return userService.login(request.getEmail(), request.getPassword());
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable UUID id) {
public ResponseEntity<Void> deleteUser(@PathVariable final UUID id) {
userService.delete(id);
return ResponseEntity.noContent().build();
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/example/comerce/core/dto/AddressDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

Expand All @@ -30,7 +30,7 @@ public class AddressDTO {
private String state;

public Address toEntity() {
Address address = new Address();
final Address address = new Address();
address.setPostal_code(this.postal_code);
address.setStreet(this.street);
address.setNumber(this.number);
Expand All @@ -41,8 +41,8 @@ public Address toEntity() {
return address;
}

public static AddressDTO toDTO(Address address) {
AddressDTO addressDTO = new AddressDTO();
public static AddressDTO toDTO(final Address address) {
final AddressDTO addressDTO = new AddressDTO();
addressDTO.setPostal_code(address.getPostal_code());
addressDTO.setStreet(address.getStreet());
addressDTO.setNumber(address.getNumber());
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/example/comerce/core/dto/CategoryDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

@Getter
@Setter
public class CategoryDTO {
public final class CategoryDTO {

@Size(min = 10, max = 255, message = "A descrição da categoria deve ter entre 10 e 255 caracteres")
private String description;

public Category toEntity() {
Category category = new Category();
final Category category = new Category();
category.setDescription(this.description);
return category;
}

public static CategoryDTO toDTO(CategoryDTO category) {
CategoryDTO categoryDTO = new CategoryDTO();
public static CategoryDTO toDTO(final CategoryDTO category) {
final CategoryDTO categoryDTO = new CategoryDTO();
categoryDTO.setDescription(category.getDescription());
return categoryDTO;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/example/comerce/core/dto/OrderDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Getter
@Setter
public class OrderDTO {
public final class OrderDTO {

@NotNull(message = "Data do pedido não pode ser null")
@FutureOrPresent(message = "Data do pedido deve ser no presente ou no futuro")
Expand All @@ -24,15 +24,15 @@ public class OrderDTO {
private double total_price;

public Order toEntity() {
Order order = new Order();
final Order order = new Order();
order.setDate(this.date);
order.setDiscount(this.discount);
order.setDiscount(this.total_price);
return order;
}

public static OrderDTO toDTO(OrderDTO order) {
OrderDTO orderDTO = new OrderDTO();
public static OrderDTO toDTO(final OrderDTO order) {
final OrderDTO orderDTO = new OrderDTO();
orderDTO.setDate(order.getDate());
orderDTO.setDiscount(order.getDiscount());
orderDTO.setTotal_price(order.getDiscount());
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/example/comerce/core/dto/ProductDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Getter
@Setter
public class ProductDTO {
public final class ProductDTO {

@NotBlank(message = "O nome do produto não pode estar em branco")
private String name;
Expand All @@ -30,7 +30,7 @@ public class ProductDTO {
private double price;

public Product toEntity() {
Product product = new Product();
final Product product = new Product();
product.setName(this.name);
product.setStock_quantity(this.stock_quantity);
product.setCost_price(this.cost_price);
Expand All @@ -40,8 +40,8 @@ public Product toEntity() {
return product;
}

public static ProductDTO toDTO(ProductDTO product) {
ProductDTO productDTO = new ProductDTO();
public static ProductDTO toDTO(final ProductDTO product) {
final ProductDTO productDTO = new ProductDTO();
productDTO.setName(product.getName());
productDTO.setStock_quantity(product.getStock_quantity());
productDTO.setCost_price(product.getCost_price());
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/example/comerce/core/dto/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Getter
@Setter
public class UserDTO {
public final 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;
Expand All @@ -27,21 +27,26 @@ public class UserDTO {
@Email(message = "E-mail inválido")
private String email;

@NotBlank(message = "Senha não pode estar em branco")
@Size(min = 6, max = 255, message = "Senha deve ter no mínimo 6 e no máximo 255 caracteres")
private String password;

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

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

public static UserDTO toDTO(User user) {
UserDTO userDTO = new UserDTO();
final UserDTO userDTO = new UserDTO();
userDTO.setName(user.getName());
userDTO.setTelephone(user.getTelephone());
userDTO.setCpf(user.getCpf());
Expand Down
Loading

0 comments on commit 3f84fc8

Please sign in to comment.