From dbac124d1bc18508cac7ccd79853b21842e69c0a Mon Sep 17 00:00:00 2001 From: Dipeua Berthold Date: Thu, 14 Dec 2023 10:44:59 +0100 Subject: [PATCH] Resolution des TODOs --- src/app/Services/OrderService.cs | 16 +++++++++++++--- src/lib/Repositories/OrderRepository.cs | 14 ++++++++++++-- src/lib/Repositories/ProductsRepository.cs | 12 ++++++++++-- src/models/Order.cs | 15 +++++++++++++-- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/app/Services/OrderService.cs b/src/app/Services/OrderService.cs index b707607..8a21271 100644 --- a/src/app/Services/OrderService.cs +++ b/src/app/Services/OrderService.cs @@ -1,5 +1,7 @@ using app.Repositories; +using Microsoft.VisualBasic; using models; +using System.Diagnostics; namespace app.Services; @@ -26,20 +28,28 @@ public IReadOnlyCollection GetAll() return _orderRepository.GetAll(); } - public Task ProcessOrderAsync(int orderId) + public async Task ProcessOrderAsync(int orderId) { // Asynchronously process the order // Simulates a long CPU bound calculation // TODO: Retrieve the order with the corresponding Id from the repository + // ANSWER + var searchOrder = _orderRepository.GetById(orderId); + if(searchOrder != null) + { + await Task.Delay(1400); + } Thread.Sleep(1400); // TODO: Change the status of this order - throw new NotImplementedException(); + // ANSWER + searchOrder.ChangeOrderStatus(true); } 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(); + // ANSWER + Console.WriteLine($"Order has been processed: {order}"); } } diff --git a/src/lib/Repositories/OrderRepository.cs b/src/lib/Repositories/OrderRepository.cs index bf98970..eaee73f 100644 --- a/src/lib/Repositories/OrderRepository.cs +++ b/src/lib/Repositories/OrderRepository.cs @@ -22,18 +22,28 @@ public OrderRepository(IEnumerable orders) public IReadOnlyCollection GetAll() { // TODO: Implement the logic that returns all the registered orders - throw new NotImplementedException(); + // ANSWER + return _orders.AsReadOnly(); } public Order? GetById(int id) { // TODO: Return the first or default order that matches the id - throw new NotImplementedException(); + // ANSWER + if (id <= 0) { return null; } + + var orderToReTurn = _orders.FirstOrDefault(o => o.Id == id); + if(orderToReTurn == null) { return null; } else { return orderToReTurn; } } public void Add(Order order) { // TODO: Add the logic to processed the order and save it into the list of orders + // ANSWER + if (_orders.Any(o => o.Id == order.Id)) { return; } + _orders.Add(order); + Console.WriteLine("Order ajouté avec succès !"); + OrderProcessed?.Invoke(order); } } diff --git a/src/lib/Repositories/ProductsRepository.cs b/src/lib/Repositories/ProductsRepository.cs index 2e75bee..eb786e7 100644 --- a/src/lib/Repositories/ProductsRepository.cs +++ b/src/lib/Repositories/ProductsRepository.cs @@ -11,12 +11,20 @@ internal class ProductsRepository(IEnumerable products) public IReadOnlyCollection GetAll() { // TODO: Return all the products from the list - throw new NotImplementedException(); + // ANSWER + if (_products.Count == 0) { return new List().AsReadOnly(); } + return _products.AsReadOnly(); } public Product? GetById(int Id) { // TODO: Retrieve a single product or default from the _products database by its Id - throw new NotImplementedException(); + // ANSWER + if (Id <= 0) { return null; } + + var productToReturn = _products.FirstOrDefault(p => p.Id == Id); + if (productToReturn == null) { return null; } + + return productToReturn; } } diff --git a/src/models/Order.cs b/src/models/Order.cs index 946a3ca..f000c89 100644 --- a/src/models/Order.cs +++ b/src/models/Order.cs @@ -12,13 +12,24 @@ public sealed class Order public void AddProduct(Product product) { // TODO: Implement the method that adds a product to this order - throw new NotImplementedException(); + // ANSWER + if (_products.Any(p => p.Id == product.Id || p.Name == product.Name)) { return; } + + _products.Add(product); } public void RemoveProduct(int id) { // TODO: Implement the method that removes a specific product from the order based on its Id - throw new NotImplementedException(); + // ANSWER + if (Id <= 0) { return; } + + var productToRemove = _products.FirstOrDefault(p => p.Id == Id); + if(productToRemove != null) + { + _products.Remove(productToRemove); + } + return; } public void ChangeOrderStatus(bool status)