Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolution des TODOs #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/app/Services/OrderService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using app.Repositories;
using Microsoft.VisualBasic;
using models;
using System.Diagnostics;

namespace app.Services;

Expand All @@ -26,20 +28,28 @@
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);

Check warning on line 45 in src/app/Services/OrderService.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 45 in src/app/Services/OrderService.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
}

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}");
}
}
14 changes: 12 additions & 2 deletions src/lib/Repositories/OrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,28 @@ public OrderRepository(IEnumerable<Order> orders)
public IReadOnlyCollection<Order> 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);
}
}
12 changes: 10 additions & 2 deletions src/lib/Repositories/ProductsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ internal class ProductsRepository(IEnumerable<Product> products)
public IReadOnlyCollection<Product> GetAll()
{
// TODO: Return all the products from the list
throw new NotImplementedException();
// ANSWER
if (_products.Count == 0) { return new List<Product>().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;
}
}
15 changes: 13 additions & 2 deletions src/models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading