public bool PurchaseMaterial(PurchaseMaterialCommand purchaseMaterialCommand) { bool isSuccess = true; var material = FetchMaterial(purchaseMaterialCommand.MaterialId); if (material == null) { throw new ValidationException("Material doesn't exist"); } using (var transaction = _supplyContext.Database.BeginTransaction()) { var createMementoCommand = _mapper.Map <CreateMaterialMementoCommand>(material); var mementoId = _supplyCommandRepository.CreateMaterialMemento(createMementoCommand); var purchased = _supplyCommandRepository.CreateTransaction(UserContext.CurrentUser.Id, mementoId, purchaseMaterialCommand); isSuccess = purchased; if (purchased) { var storeCommand = _mapper.Map <StoreMaterialCommand>(purchaseMaterialCommand); var result = _storeService.StoreMaterial(storeCommand).ThrowIfError(); isSuccess = result.Data; } if (isSuccess) { transaction.Commit(); } } return(isSuccess); }
public bool CreateTransaction(int responsibleUserId, int materialMementoId, PurchaseMaterialCommand purchaseMaterialCommand) { var model = _mapper.Map <PurchaseTransaction>(purchaseMaterialCommand); model.ResponsibleSupplierId = responsibleUserId; model.Date = DateTime.Now; model.MaterialMementoId = materialMementoId; _supplyContext.Add(model); _supplyContext.SaveChanges(); return(true); }
public IHttpActionResult Purchase([FromBody] PurchaseMaterialCommand purchaseMaterialCommand) { return(ApiOk(_supplyService.PurchaseMaterial(purchaseMaterialCommand))); }