Skip to content

Commit

Permalink
Merge pull request #52 from kaveenexe/Dew
Browse files Browse the repository at this point in the history
inventory delete
  • Loading branch information
IT21016820 authored Oct 18, 2024
2 parents 9cd062f + e3c865e commit eb6da99
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
16 changes: 16 additions & 0 deletions ColletteAPI/Controllers/InventoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,21 @@ public async Task<IActionResult> GetAllProducts()
var products = await _inventoryService.GetAllProductsAsync();
return Ok(products);
}

// DELETE: api/inventory/{productId}
[HttpDelete("{productId}")]
public async Task<IActionResult> DeleteInventoryItem(string productId)
{
// Attempt to delete the inventory item
var success = await _inventoryService.DeleteInventoryItemAsync(productId);

// If the deletion was blocked, return a 403 Forbidden response
if (!success)
{
return Forbid("Deletion not allowed for Delivered or Cancelled orders.");
}

return NoContent(); // Return 204 No Content if successful
}
}
}
3 changes: 3 additions & 0 deletions ColletteAPI/Repositories/IInventoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public interface IInventoryRepository
Task CreateInventoryAsync(Inventory inventory); // Add a new inventory item
Task UpdateInventoryAsync(Inventory inventory); // Update an inventory item
Task<IEnumerable<Inventory>> GetAllInventoriesAsync(); // Get all inventories

Task<bool> DeleteInventoryItemAsync(string productId); // Add method to delete inventory item

}
}
30 changes: 29 additions & 1 deletion ColletteAPI/Repositories/InventoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ namespace ColletteAPI.Repositories
public class InventoryRepository : IInventoryRepository
{
private readonly IMongoCollection<Inventory> _inventoryCollection;
private readonly IOrderRepository _orderRepository; // Dependency on OrderRepository

public InventoryRepository(IMongoDatabase database)
public InventoryRepository(IMongoDatabase database, IOrderRepository orderRepository)
{
_inventoryCollection = database.GetCollection<Inventory>("Inventories");
_orderRepository = orderRepository; // Inject OrderRepository

}

// Retrieves inventory based on productId
Expand All @@ -38,5 +41,30 @@ public async Task<IEnumerable<Inventory>> GetAllInventoriesAsync()
{
return await _inventoryCollection.Find(_ => true).ToListAsync();
}



// Delete inventory item if conditions are met based on order status
public async Task<bool> DeleteInventoryItemAsync(string productId)
{
// Fetch all orders related to the product
var orders = await _orderRepository.GetOrdersByProductId(productId);

// Check the order status for all related orders
foreach (var order in orders)
{
// If any order is in a non-deletable state, block the deletion
if (order.Status == OrderStatus.Delivered || order.Status == OrderStatus.Cancelled || order.Status == OrderStatus.Pending)
{
return false; // Block deletion
}
}

// If no conflicting statuses (Delivered/Cancelled), proceed to delete the inventory item
var result = await _inventoryCollection.DeleteOneAsync(i => i.ProductId == productId);

// Return true if the deletion was successful
return result.DeletedCount > 0;
}
}
}
41 changes: 40 additions & 1 deletion ColletteAPI/Repositories/ProductRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ namespace ColletteAPI.Repositories
public class ProductRepository : IProductRepository
{
private readonly IMongoCollection<Product> _products;
private readonly IInventoryRepository _inventoryRepository; // Dependency on InventoryRepository

public ProductRepository(IMongoClient client, IConfiguration configuration)
public ProductRepository(IMongoClient client, IConfiguration configuration, IInventoryRepository inventoryRepository)
{
var database = client.GetDatabase(configuration.GetSection("MongoDB:DatabaseName").Value);
_products = database.GetCollection<Product>("Products");
_inventoryRepository = inventoryRepository; // Injecting InventoryRepository

}

// Retrieves all products for a specific vendor.
Expand Down Expand Up @@ -72,5 +75,41 @@ public async Task<List<Product>> GetAllProductsAsync()
{
return await _products.Find(_ => true).ToListAsync();
}


//// Updates an existing product and automatically updates the inventory stock
//public async Task UpdateProductAsync(string id, Product updatedProduct)
//{
// // Update the product in the products collection
// await _products.ReplaceOneAsync(p => p.Id == id && p.VendorId == updatedProduct.VendorId, updatedProduct);

// // Update the stock quantity in the inventory
// var inventoryItem = await _inventoryRepository.GetInventoryByProductIdAsync(updatedProduct.UniqueProductId);
// if (inventoryItem != null)
// {
// inventoryItem.StockQuantity = updatedProduct.StockQuantity; // Update stock quantity
// await _inventoryRepository.UpdateInventoryAsync(inventoryItem); // Update inventory record
// }
//}

//// Optionally: If you need to update only the stock quantity
//public async Task UpdateProductQuantityAsync(string productId, int newQuantity)
//{
// // Retrieve the product by its UniqueProductId
// var product = await GetProductById(productId);
// if (product != null)
// {
// product.StockQuantity = newQuantity; // Update the product's stock quantity
// await _products.ReplaceOneAsync(p => p.UniqueProductId == productId, product);

// // Update the corresponding inventory item
// var inventoryItem = await _inventoryRepository.GetInventoryByProductIdAsync(product.UniqueProductId);
// if (inventoryItem != null)
// {
// inventoryItem.StockQuantity = newQuantity; // Sync the inventory stock
// await _inventoryRepository.UpdateInventoryAsync(inventoryItem);
// }
// }
//}
}
}
3 changes: 3 additions & 0 deletions ColletteAPI/Services/IInventoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public interface IInventoryService
{
Task SyncProductsToInventoryAsync(); // Sync all products to the inventory
Task<IEnumerable<InventoryDto>> GetAllProductsAsync(); // Get all products with product details

Task<bool> DeleteInventoryItemAsync(string productId); // Add delete method signature

}
}
13 changes: 12 additions & 1 deletion ColletteAPI/Services/InventoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ public class InventoryService : IInventoryService
{
private readonly IInventoryRepository _inventoryRepository;
private readonly IProductRepository _productRepository; // Using ProductRepository
private readonly IOrderRepository _orderRepository; // Assuming you have an OrderRepository to handle orders

public InventoryService(IInventoryRepository inventoryRepository, IProductRepository productRepository)

public InventoryService(IInventoryRepository inventoryRepository, IProductRepository productRepository, IOrderRepository orderRepository)
{
_inventoryRepository = inventoryRepository;
_productRepository = productRepository;
_orderRepository = orderRepository; // Injecting OrderRepository

}

// Syncs all products to inventories
Expand Down Expand Up @@ -73,5 +77,12 @@ public async Task<IEnumerable<InventoryDto>> GetAllProductsAsync()

return productDetails;
}

// Delete inventory item with order status check
public async Task<bool> DeleteInventoryItemAsync(string productId)
{
// Call the repository method to delete the item, with order status check
return await _inventoryRepository.DeleteInventoryItemAsync(productId);
}
}
}

0 comments on commit eb6da99

Please sign in to comment.