public async Task <IActionResult> MyComponent(MyComponentInputModel model) { if (!ModelState.IsValid) { var componentTypes = _componentTypeService.GetAll() .Include(x => x.Details); var multipliers = _unitMultiplierService.GetAll(); model.ComponentTypes = componentTypes; model.UnitMultipliers = multipliers; return(View(model)); } var userComponent = await _userComponentTypeService.GetByIdAsync(model.Id); var userId = _userManager.GetUserId(User); if (userComponent == null) { userComponent = new UserComponentType { ComponentTypeId = model.ComponentTypeId, Quantity = model.Quantity, UnitPrice = model.UnitPrice, CreatedByUserId = userId, CreatedDateTime = DateTime.UtcNow, IsActive = true }; await _userComponentTypeService.SaveComponentAsync(userComponent); var type = await _componentTypeService.GetByIdAsync(model.ComponentTypeId); var details = _componentTypeDetailService.GetByComponentTypeId(type.Id); var userDetails = details.Select(x => new UserComponentTypeDetail { UserComponentTypeId = userComponent.Id, UnitMultiplierId = 1, ComponentTypeDetailId = x.Id, Value = 0, CreatedByUserId = userId, CreatedDateTime = DateTime.UtcNow, IsActive = true }); await _userComponentTypeDetailService.SaveDetailsAsync(userDetails); return(Redirect($"/MyComponents/MyComponent?userComponentTypeId={userComponent.Id}")); } else { userComponent.Quantity = model.Quantity; userComponent.UnitPrice = model.UnitPrice; userComponent.LastUpdatedByUserId = userId; userComponent.LastUpdatedDateTime = DateTime.UtcNow; var details = _userComponentTypeDetailService.GetByUserComponentTypeId(userComponent.Id); foreach (var detail in details) { var detailModel = model.Details.SingleOrDefault(x => x.DetailId == detail.Id); detail.UnitMultiplierId = detailModel.MultiplierId; detail.Value = detailModel.Value; detail.LastUpdatedByUserId = userId; detail.LastUpdatedDateTime = DateTime.UtcNow; } await _userComponentTypeService.UpdateComponentAsync(userComponent); await _userComponentTypeDetailService.UpdateRangeAsync(details); return(Redirect($"/MyComponents/Index")); } }
public async Task SaveDetailsAsync_WhenArgumentIsNull_ShouldReturnFalse() { var result = await _userComponentTypeDetailService.SaveDetailsAsync(null); Assert.IsFalse(result); }