Beispiel #1
0
        public async Task SaveComponentAsync_WhenArgumentIsIInvalid_ShouldReturnFalse()
        {
            var type   = new UserComponentType();
            var result = await userComponentTypeService.SaveComponentAsync(type);

            Assert.IsFalse(result);
        }
        public async Task <bool> UpdateComponentAsync(UserComponentType type)
        {
            try
            {
                _context.UserComponentTypes.Update(type);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        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"));
            }
        }