Esempio n. 1
0
        public async Task <IActionResult> ComponentType(int?componentTypeId)
        {
            var type = await _componentTypeService.GetByIdAsync(componentTypeId);

            if (type == null)
            {
                return(View());
            }
            else
            {
                var details      = _componentTypeDetailService.GetByComponentTypeId(type.Id);
                var inputDetails = _mapper.Map <IEnumerable <ComponentTypeDetailInputModel> >(details);

                var model = _mapper.Map <ComponentTypeInputModel>(type);
                model.Details = inputDetails;

                return(View(model));
            }
        }
        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"));
            }
        }
Esempio n. 3
0
        public async Task GetByIdAsync_WhenIdIsNull_ShouldReturnNull()
        {
            var result = await _componentTypeService.GetByIdAsync(null);

            Assert.IsNull(result);
        }