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.
feat: RESTful API finished, some adjutments needed
- Loading branch information
Showing
11 changed files
with
258 additions
and
21 deletions.
There are no files selected for viewing
44 changes: 42 additions & 2 deletions
44
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 |
---|---|---|
@@ -1,10 +1,50 @@ | ||
package com.example.comerce.core.controller; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
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; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@RestController | ||
@RequestMapping("/api/category") | ||
public class CategoryController { | ||
@Autowired | ||
private CategoryService categoryService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<Category>> getAllCategories() { | ||
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)); | ||
return category.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<Category> createCategory(@RequestBody CategoryDTO categoryDTO) { | ||
Category savedCategory = categoryService.save(categoryDTO.toEntity()); | ||
return ResponseEntity.ok(savedCategory); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteCategory(@PathVariable 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()); | ||
return ResponseEntity.ok(updatedCategory); | ||
} | ||
} |
51 changes: 48 additions & 3 deletions
51
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 |
---|---|---|
@@ -1,10 +1,55 @@ | ||
package com.example.comerce.core.controller; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import com.example.comerce.core.dto.ProductDTO; | ||
import com.example.comerce.core.entities.Product; | ||
import com.example.comerce.core.services.ProductService; | ||
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/product") | ||
@RequestMapping("/api/products") | ||
public class ProductController { | ||
@Autowired | ||
private ProductService productService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<Product>> getAllProducts() { | ||
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); | ||
return product.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<Product> createProduct(@RequestBody ProductDTO productDTO) { | ||
Product savedProduct = productService.save(productDTO); | ||
return ResponseEntity.ok(savedProduct); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteProduct(@PathVariable 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); | ||
return ResponseEntity.ok(updatedProduct); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/example/comerce/core/dto/CategoryDTO.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,10 +1,26 @@ | ||
package com.example.comerce.core.dto; | ||
|
||
import com.example.comerce.core.entities.Category; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public 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(); | ||
category.setDescription(this.description); | ||
return category; | ||
} | ||
|
||
public static CategoryDTO toDTO(CategoryDTO category) { | ||
CategoryDTO categoryDTO = new CategoryDTO(); | ||
categoryDTO.setDescription(category.getDescription()); | ||
return 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 |
---|---|---|
@@ -1,10 +1,41 @@ | ||
package com.example.comerce.core.dto; | ||
|
||
import com.example.comerce.core.entities.Order; | ||
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; | ||
|
||
@Getter | ||
@Setter | ||
public class OrderDTO { | ||
|
||
@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; | ||
|
||
@Positive(message = "O desconto deve ser um valor positivo") | ||
private double discount; | ||
|
||
@Positive(message = "O valor total deve ser um valor positivo") | ||
private double total_price; | ||
|
||
public Order toEntity() { | ||
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(); | ||
orderDTO.setDate(order.getDate()); | ||
orderDTO.setDiscount(order.getDiscount()); | ||
orderDTO.setTotal_price(order.getDiscount()); | ||
return orderDTO; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/example/comerce/core/dto/ProductDTO.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,10 +1,53 @@ | ||
package com.example.comerce.core.dto; | ||
|
||
import com.example.comerce.core.entities.Product; | ||
import jakarta.validation.constraints.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.Date; | ||
|
||
@Getter | ||
@Setter | ||
public class ProductDTO { | ||
|
||
@NotBlank(message = "O nome do produto não pode estar em branco") | ||
private String name; | ||
|
||
@Positive(message = "A quantidade em estoque deve ser positiva") | ||
private int stock_quantity; | ||
|
||
@PositiveOrZero(message = "O preço de custo do produto deve ser zero ou positivo") | ||
private double cost_price; | ||
|
||
@PositiveOrZero(message = "O preço de venda do produto deve ser zero ou positivo") | ||
private double sell_price; | ||
|
||
@FutureOrPresent(message = "A data de criação do produto deve ser no presente ou no futuro") | ||
private Date created_at; | ||
|
||
@PositiveOrZero(message = "O preço do produto deve ser zero ou positivo") | ||
private double price; | ||
|
||
public Product toEntity() { | ||
Product product = new Product(); | ||
product.setName(this.name); | ||
product.setStock_quantity(this.stock_quantity); | ||
product.setCost_price(this.cost_price); | ||
product.setSell_price(this.sell_price); | ||
product.setCreated_at(this.created_at); | ||
product.setPrice(this.price); | ||
return product; | ||
} | ||
|
||
public static ProductDTO toDTO(ProductDTO product) { | ||
ProductDTO productDTO = new ProductDTO(); | ||
productDTO.setName(product.getName()); | ||
productDTO.setStock_quantity(product.getStock_quantity()); | ||
productDTO.setCost_price(product.getCost_price()); | ||
productDTO.setSell_price(product.getSell_price()); | ||
productDTO.setCreated_at(product.getCreated_at()); | ||
productDTO.setPrice(product.getPrice()); | ||
return 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
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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/example/comerce/core/services/CategoryService.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,8 +1,36 @@ | ||
package com.example.comerce.core.services; | ||
|
||
import com.example.comerce.core.entities.Category; | ||
import com.example.comerce.core.repository.CategoryRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
public class CategoryService { | ||
@Autowired | ||
private CategoryRepository categoryRepository; | ||
|
||
public List<Category> findAll() { | ||
return categoryRepository.findAll(); | ||
} | ||
|
||
public Category save(Category category) { | ||
return categoryRepository.save(category); | ||
} | ||
|
||
public void delete(UUID categoryId) { | ||
categoryRepository.deleteById(categoryId); | ||
} | ||
|
||
public Category update(UUID categoryId, Category category) { | ||
category.setCategory_id(categoryId); | ||
return categoryRepository.save(category); | ||
} | ||
|
||
public Category findById(UUID categoryId) { | ||
return categoryRepository.findById(categoryId).orElse(null); | ||
} | ||
} |
Oops, something went wrong.