public async Task <IActionResult> CreateSupplier([FromBody] CreateUpdateSupplierRequest request) { var duplicateSupplierFilter = Builders <Supplier> .Filter.Where(x => x.Email.Equals(request.Email.Trim().ToLower()) || (x.DocumentType.Equals(request.DocumentType.Trim().ToLower()) && x.DocumentNumber.Equals(request.DocumentNumber))); var supplierAlreadyExists = await _mongoDbService.GetCollection <Supplier>(Collections.Suppliers) .Find(duplicateSupplierFilter).AnyAsync(); if (supplierAlreadyExists) { throw new CustomException("Duplicate email or document number."); } var session = await _mongoDbService.StartTransactionAsync(); var newSupplier = new Supplier() { FullName = request.FullName.Trim(), Address = request.Address.Trim(), PhoneNumber = request.PhoneNumber.Trim(), Email = request.Email.Trim().Normalize().ToLowerInvariant(), DocumentType = request.DocumentType.Trim().ToLowerInvariant(), DocumentNumber = request.DocumentNumber, Active = true }; await _mongoDbService.GetCollection <Supplier>(Collections.Suppliers).InsertOneAsync(session, newSupplier); await _mongoDbService.CommitTransactionAsync(); return(new SuccessResult()); }
public async Task <IActionResult> CreateProduct([FromBody] CreateUpdateProductRequest request) { var categoryFilter = Builders <Category> .Filter.Where(x => x.Id.Equals(request.CategoryId)); var validCategory = await _mongoDbService.GetCollection <Category>(Collections.Categories) .Find(categoryFilter).AnyAsync(); if (!validCategory) { throw new CustomException("Invalid category specified."); } var session = await _mongoDbService.StartTransactionAsync(); var newProduct = new Product() { CategoryId = request.CategoryId, Name = request.Name.Trim(), Description = request.Description.Trim(), Stock = request.Stock, UnitPrice = request.UnitPrice, Base64Image = request.Base64Image, Active = true }; await _mongoDbService.GetCollection <Product>(Collections.Products).InsertOneAsync(session, newProduct); await _mongoDbService.CommitTransactionAsync(); return(new SuccessResult()); }
public async Task <IActionResult> CreatePurchase([FromBody] CreatePurchaseRequest request) { var supplierFilter = Builders <Supplier> .Filter.Where(x => x.Id.Equals(request.SupplierId)); var validSupplier = await _mongoDbService.GetCollection <Supplier>(Collections.Suppliers) .Find(supplierFilter).AnyAsync(); if (!validSupplier) { throw new CustomException("Invalid supplier specified."); } var productFilter = Builders <Product> .Filter.In(x => x.Id, request.Details.Select(d => d.ProductId)); var products = await _mongoDbService.GetCollection <Product>(Collections.Products) .Find(productFilter).ToListAsync(); var validProducts = request.Details .All(d => products.Any(p => p.Id.Equals(d.ProductId))); if (!validProducts) { throw new CustomException("Invalid products specified."); } var session = await _mongoDbService.StartTransactionAsync(); var newPurchase = new Purchase() { SupplierId = request.SupplierId, ReceiptType = request.ReceiptType.Trim().ToLowerInvariant(), Tax = request.Tax, Total = Math.Round(request.Details.Sum(d => d.Quantity * d.UnitPrice) * (1 + (request.Tax / 100)), 2), Date = DateTime.Now, Approved = true, Details = request.Details.Select(d => new PurchaseDetail() { ProductId = d.ProductId, Quantity = d.Quantity, UnitPrice = d.UnitPrice }) .ToList() }; await _mongoDbService.GetCollection <Purchase>(Collections.Purchases).InsertOneAsync(session, newPurchase); await request.Details.ForEachAsync(async item => { var productFilter = Builders <Product> .Filter.Where(p => p.Id.Equals(item.ProductId)); var stockUpdateDefinition = new UpdateDefinitionBuilder <Product>() .Inc(x => x.Stock, item.Quantity); await _mongoDbService.GetCollection <Product>(Collections.Products) .UpdateOneAsync(session, productFilter, stockUpdateDefinition); }); await _mongoDbService.CommitTransactionAsync(); return(new SuccessResult()); }
public async Task <IActionResult> CreateCategory([FromBody] CreateUpdateCategoryRequest request) { var session = await _mongoDbService.StartTransactionAsync(); var newCategory = new Category() { Name = request.Name.Trim(), Description = request.Description.Trim(), Active = true }; await _mongoDbService.GetCollection <Category>(Collections.Categories).InsertOneAsync(session, newCategory); await _mongoDbService.CommitTransactionAsync(); return(new SuccessResult()); }