Ejemplo n.º 1
0
        public async Task <IActionResult> Update(TargetUpdateViewModel model)
        {
            var isAuthResult = await _targetService.CheckAuthorization(model.Id, UserId);

            if (!isAuthResult.Success)
            {
                AddModelErrors(isAuthResult);
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var getTargetServiceResult = await _targetService.GetTargetAsync(model.Id);

            var targetDto = getTargetServiceResult.Result;

            _mapper.Map <TargetUpdateViewModel, TargetDto>(model, targetDto);

            targetDto.UserId = UserId;
            var serviceResult = await _targetService.UpdateAsync(targetDto);

            if (!serviceResult.Success)
            {
                AddModelErrors(serviceResult);
                return(View(model));
            }

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdateBatchTargetAsync(int id, [FromBody] TargetUpdateViewModel target)
        {
            try
            {
                if (target == null)
                {
                    return(NoContent());
                }

                var qry = _queryFactory.CreateBatchesQuery();
                var dto = await qry.ExecuteAsync(id).ConfigureAwait(false);

                if (dto == null)
                {
                    return(NotFound());
                }

                if (dto.Target == null)
                {
                    // create new target and link to this batch
                    var newCmd = _commandsFactory.CreateTargetsCommand();
                    dto.Target = await newCmd.AddAsync(new TargetDto()).ConfigureAwait(false);

                    var batchCmd = _commandsFactory.CreateBatchesCommand();
                    dto = await batchCmd.UpdateAsync(dto).ConfigureAwait(false);
                }

                dto.Target.EndSugar   = target.EndingSugar;
                dto.Target.pH         = target.pH;
                dto.Target.StartSugar = target.StartingSugar;
                dto.Target.TA         = target.TA;
                dto.Target.Temp       = target.FermentationTemp;

                if (target.EndSugarUOM.HasValue)
                {
                    dto.Target.EndSugarUom = new UnitOfMeasure {
                        Id = target.EndSugarUOM.Value
                    }
                }
                ;
                if (target.StartSugarUOM.HasValue)
                {
                    dto.Target.StartSugarUom = new UnitOfMeasure {
                        Id = target.StartSugarUOM.Value
                    }
                }
                ;
                if (target.TempUOM.HasValue)
                {
                    dto.Target.TempUom = new UnitOfMeasure {
                        Id = target.TempUOM.Value
                    }
                }
                ;

                var cmd       = _commandsFactory.CreateTargetsCommand();
                var targetDto = await cmd.UpdateAsync(dto.Target).ConfigureAwait(false);

                var model = _modelFactory.CreateTargetViewModel(targetDto);

                return(Ok(model));
            }
            catch (Exception)
            {
                return(StatusCode(500));

                throw;
            }
        }