From 19bbf2f76d16f98024580d0ff175bc04143e6b1f Mon Sep 17 00:00:00 2001 From: albertboo9 Date: Thu, 14 Dec 2023 11:35:50 +0100 Subject: [PATCH] =?UTF-8?q?Suivant=20la=20structure=20des=20autres=20featu?= =?UTF-8?q?res=20j'ai=20cr=C3=A9=C3=A9=20les=20features=20AddProduct=20et?= =?UTF-8?q?=20DeleteProduct=20qui=20permettent=20respectivement=20d'ajoute?= =?UTF-8?q?r=20un=20produit=20et=20de=20supprimer=20un=20produit.=20j'ai?= =?UTF-8?q?=20ajouter=20la=20d=C3=A9finition=20de=20la=20m=C3=A9thode=20Ad?= =?UTF-8?q?d=20et=20Delete=20dans=20IProductRepositories.=20Puis=20dans=20?= =?UTF-8?q?les=20app.services=20et=20les=20lib.Repositories=20=20j'ai=20j'?= =?UTF-8?q?ai=20accompli=20les=20t=C3=A2ches=20demand=C3=A9=20par=20les=20?= =?UTF-8?q?commentaires=20TODO=20puis=20j'ai=20les=20exceptions=20NotImple?= =?UTF-8?q?mentationException=20en=20commentaire.=20Ensuite=20dans=20le=20?= =?UTF-8?q?mod=C3=A8le=20Order=20j'ai=20=C3=A9crit=20les=20m=C3=A9thodes?= =?UTF-8?q?=20AddProduct=20et=20RemoveProduct=20pour=20respectivement=20aj?= =?UTF-8?q?outer=20un=20produit=20=C3=A0=20la=20commande=20et=20retirer=20?= =?UTF-8?q?un=20produit=20de=20la=20commande.=20pour=20finir=20j'ai=20rajo?= =?UTF-8?q?uter=20mes=20deux=20features=20que=20j'ai=20cr=C3=A9er=20au=20d?= =?UTF-8?q?=C3=A9part=20dans=20le=20dictionnaires=20=5Ffeatures=20pour=20q?= =?UTF-8?q?u'ils=20apparaissent=20dans=20le=20menu=20en=20ligne=20de=20com?= =?UTF-8?q?mande.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Maintenant pour ameliorer le code on pourrait rajouter une méthodes dans le feature commande pour spécifier la quantité d'un produit d'un produit dans une commande, du genre pour un produit donné quelle quantité souhaite t'on commandé... Et pour le service des produits lors de l'ajout d'un nouveau produit on peut aussi ajouter sa quantité en stock, comme ça à chaque fois que ce produit est commandé la quantité commandéé est reduite à la quantité en stock. --- src/app/Repositories/IProductsRepository.cs | 2 + src/app/Services/OrderService.cs | 52 +++++++++++++--- src/app/Services/ProductService.cs | 19 +++++- src/lib/Repositories/OrderRepository.cs | 11 ++-- src/lib/Repositories/ProductsRepository.cs | 22 ++++++- src/models/Order.cs | 67 ++++++++++++++------- src/ui/Features/AddProduct.cs | 35 +++++++++++ src/ui/Features/DeleteProduct.cs | 37 ++++++++++++ src/ui/Program.cs | 4 +- 9 files changed, 209 insertions(+), 40 deletions(-) create mode 100644 src/ui/Features/AddProduct.cs create mode 100644 src/ui/Features/DeleteProduct.cs diff --git a/src/app/Repositories/IProductsRepository.cs b/src/app/Repositories/IProductsRepository.cs index 88c4869..06febfc 100644 --- a/src/app/Repositories/IProductsRepository.cs +++ b/src/app/Repositories/IProductsRepository.cs @@ -6,4 +6,6 @@ public interface IProductsRepository { IReadOnlyCollection GetAll(); Product? GetById(int Id); + void Add(Product product); + void Delete(int id); } diff --git a/src/app/Services/OrderService.cs b/src/app/Services/OrderService.cs index b707607..ddb8c6f 100644 --- a/src/app/Services/OrderService.cs +++ b/src/app/Services/OrderService.cs @@ -1,6 +1,7 @@ using app.Repositories; using models; + namespace app.Services; public sealed class OrderService @@ -26,20 +27,53 @@ public IReadOnlyCollection GetAll() return _orderRepository.GetAll(); } - public Task ProcessOrderAsync(int orderId) + public async Task ProcessOrderAsync(int orderId) + { + try { // Asynchronously process the order - // Simulates a long CPU bound calculation - // TODO: Retrieve the order with the corresponding Id from the repository - Thread.Sleep(1400); - // TODO: Change the status of this order - throw new NotImplementedException(); + await Task.Delay(1400); // Simule un traitement asynchrone + + // TODO: Retrieve the order with the corresponding Id from the repository +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. + Order order = _orderRepository.GetById(orderId); +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + + if (order != null) + { + // TODO: Change the status of this order + order.ChangeOrderStatus(true); + + // Notify that the order has been processed + // OrderProcessed?.Invoke(order); + } + else + { + Console.WriteLine($"Order with Id {orderId} not found."); + } + } + catch (Exception ex) + { + // Handle exceptions appropriately + Console.WriteLine($"Error processing order: {ex.Message}"); + } + //throw new NotImplementedException(); } private void HandleOrderProcessed(Order order) { // Notify the user that an order has been processed - // TODO: Write the order informations to the console - throw new NotImplementedException(); - } + Console.WriteLine($"Order {order.Id} has been processed successfully!"); + Console.WriteLine($"Total Price: {order.TotalPrice:C}"); + Console.WriteLine("Ordered Products:"); + + foreach (var product in order.Products) + { + Console.WriteLine($" - {product.Name} ({product.Price:C})"); + } + + Console.WriteLine("___________________________________________"); + // TODO: Write the order informations to the console + //throw new NotImplementedException(); + } } diff --git a/src/app/Services/ProductService.cs b/src/app/Services/ProductService.cs index 91b239c..35e8328 100644 --- a/src/app/Services/ProductService.cs +++ b/src/app/Services/ProductService.cs @@ -3,9 +3,15 @@ namespace app.Services; -public class ProductService(IProductsRepository productsRepository) +public class ProductService { - private readonly IProductsRepository _productsRepository = productsRepository; + private readonly IProductsRepository _productsRepository; + + // Constructeur pour injecter le repository nécessaire lors de la création d'une instance de ProductService. + public ProductService(IProductsRepository productsRepository) + { + _productsRepository = productsRepository; + } public IReadOnlyCollection GetAll() { @@ -15,4 +21,13 @@ public IReadOnlyCollection GetAll() { return _productsRepository.GetById(Id); } + public void AddProduct(Product product) + { + _productsRepository.Add(product); + } + + public void DeleteProduct(int id) + { + _productsRepository.Delete(id); + } } diff --git a/src/lib/Repositories/OrderRepository.cs b/src/lib/Repositories/OrderRepository.cs index bf98970..340f121 100644 --- a/src/lib/Repositories/OrderRepository.cs +++ b/src/lib/Repositories/OrderRepository.cs @@ -3,7 +3,7 @@ namespace lib.Repositories; -internal class OrderRepository : IOrderRepository +public class OrderRepository : IOrderRepository { private readonly List _orders = []; public event Action OrderProcessed; @@ -22,18 +22,21 @@ public OrderRepository(IEnumerable orders) public IReadOnlyCollection GetAll() { // TODO: Implement the logic that returns all the registered orders - throw new NotImplementedException(); + return _orders.AsReadOnly(); + //throw new NotImplementedException(); } public Order? GetById(int id) { // TODO: Return the first or default order that matches the id - throw new NotImplementedException(); + return _orders.Find(p => p.Id == id); + // throw new NotImplementedException(); } public void Add(Order order) { - // TODO: Add the logic to processed the order and save it into the list of orders + _orders.Add(order); + OrderProcessed?.Invoke(order); } } diff --git a/src/lib/Repositories/ProductsRepository.cs b/src/lib/Repositories/ProductsRepository.cs index 2e75bee..6f3f891 100644 --- a/src/lib/Repositories/ProductsRepository.cs +++ b/src/lib/Repositories/ProductsRepository.cs @@ -3,7 +3,7 @@ namespace lib.Repositories.Concrete; -internal class ProductsRepository(IEnumerable products) +public class ProductsRepository(IEnumerable products) : IProductsRepository { private readonly List _products = products.ToList(); @@ -11,12 +11,28 @@ internal class ProductsRepository(IEnumerable products) public IReadOnlyCollection GetAll() { // TODO: Return all the products from the list - throw new NotImplementedException(); + return _products.AsReadOnly(); + // throw new NotImplementedException(); } public Product? GetById(int Id) { // TODO: Retrieve a single product or default from the _products database by its Id - throw new NotImplementedException(); + return _products.Find(p => p.Id == Id); + // throw new NotImplementedException(); } + + // Implémentation de la méthode Add + public void Add(Product product) + { + _products.Add(product); + } + + // Implémentation de la méthode Delete + public void Delete(int id) + { + var productToDelete = _products.Find(p => p.Id == id); + if (productToDelete != null) + _products.Remove(productToDelete); + } } diff --git a/src/models/Order.cs b/src/models/Order.cs index 946a3ca..4f98809 100644 --- a/src/models/Order.cs +++ b/src/models/Order.cs @@ -1,28 +1,53 @@ -namespace models; +using System; +using System.Collections.Generic; +using System.Linq; -public sealed class Order + +namespace models { - private static int _count = 0; - private readonly List _products = []; - public int Id { get; private init; } = ++_count; - public bool Processed { get; private set; } - public IReadOnlyList Products => _products.ToArray(); - public decimal TotalPrice => Products.Sum(p => p.Price); - public void AddProduct(Product product) + public sealed class Order { - // TODO: Implement the method that adds a product to this order - throw new NotImplementedException(); - } + private static int _count = 0; + private readonly List _products = new List(); + // private readonly List _products = []; + public int Id { get; private init; } = ++_count; + public bool Processed { get; private set; } + public IReadOnlyList Products => _products.ToArray(); + public decimal TotalPrice => Products.Sum(p => p.Price); - public void RemoveProduct(int id) - { - // TODO: Implement the method that removes a specific product from the order based on its Id - throw new NotImplementedException(); - } + public void AddProduct(Product product) + { + if (product == null) + { + throw new ArgumentNullException(nameof(product), "Product cannot be null"); + } + _products.Add(product); + // throw new NotImplementedException(); + } - public void ChangeOrderStatus(bool status) - { - Processed = status; + public void RemoveProduct(int id) + { +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. + Product productToRemove = _products.FirstOrDefault(p => p.Id == id); +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + + if (productToRemove != null) + { + _products.Remove(productToRemove); + } + else + { + throw new ArgumentException($"Product with Id {id} not found in the order.", nameof(id)); + } + + + // throw new NotImplementedException(); + } + + public void ChangeOrderStatus(bool status) + { + Processed = status; + } } -} +} \ No newline at end of file diff --git a/src/ui/Features/AddProduct.cs b/src/ui/Features/AddProduct.cs new file mode 100644 index 0000000..f8ac074 --- /dev/null +++ b/src/ui/Features/AddProduct.cs @@ -0,0 +1,35 @@ +using app.Services; +using Microsoft.Extensions.DependencyInjection; +using models; +using ui.Utilities; + +namespace ui.Features +{ + internal static partial class Feature + { + // Méthode pour créer un nouveau produit. + public static Task CreateProduct(IServiceProvider sp) + { + var productService = sp.GetRequiredService(); + + Console.Write("Enter the name of the new product: "); + var name = Console.ReadLine(); + + Console.Write("Enter the price of the new product: "); + if (!decimal.TryParse(Console.ReadLine(), out decimal price)) + { + Display.WriteError("Invalid price. Retry after 1s ..."); + Thread.Sleep(1000); + return Task.FromResult(false); + } + +#pragma warning disable CS8604 // Possible null reference argument. + var newProduct = new Product(name, price); +#pragma warning restore CS8604 // Possible null reference argument. + productService.AddProduct(newProduct); + + Console.WriteLine($"New product created successfully. Id: {newProduct.Id}"); + return Task.FromResult(false); + } + } +} diff --git a/src/ui/Features/DeleteProduct.cs b/src/ui/Features/DeleteProduct.cs new file mode 100644 index 0000000..38ee3ea --- /dev/null +++ b/src/ui/Features/DeleteProduct.cs @@ -0,0 +1,37 @@ +using app.Services; +using Microsoft.Extensions.DependencyInjection; +using ui.Utilities; + +namespace ui.Features +{ + internal static partial class Feature + { + // Méthode pour supprimer un produit existant. + public static Task DeleteProduct(IServiceProvider sp) + { + var productService = sp.GetRequiredService(); + + Console.Write("Enter the Id of the product you want to delete: "); + if (!int.TryParse(Console.ReadLine(), out int productId)) + { + Display.WriteError("Invalid Id. Retry after 1s ..."); + Thread.Sleep(1000); + return Task.FromResult(false); + } + + var existingProduct = productService.GetById(productId); + if (existingProduct is null) + { + Display.WriteError($"Product with Id {productId} does not exist. Retry after 1s ..."); + Thread.Sleep(1000); + return Task.FromResult(false); + } + + productService.DeleteProduct(existingProduct.Id); + + Console.WriteLine($"Product with Id {productId} deleted successfully."); + + return Task.FromResult(false); + } + } +} diff --git a/src/ui/Program.cs b/src/ui/Program.cs index ffe4689..e137b5d 100644 --- a/src/ui/Program.cs +++ b/src/ui/Program.cs @@ -17,7 +17,9 @@ private static readonly Dictionary< { 1, ("See all products", Feature.ViewProducts) }, { 2, ("Make an order", Feature.MakeOrder) }, { 3, ("See all orders", Feature.ViewOrders) }, - { 4, ("Quit", Feature.Exit) }, + { 4, ("CreateProduct", Feature.CreateProduct) }, + { 5, ("DeleteProduct", Feature.DeleteProduct) }, + { 6, ("Quit", Feature.Exit) }, }; private static readonly (int, string)[] _options = _features