Esempio n. 1
0
        public async Task <IActionResult> UpdateValue(UpdateValueModel model)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }
            if (String.IsNullOrEmpty(model?.Value))
            {
                return(Json("Empty String"));
            }

            var succeeded = await _appService.UpdateValueOfVariableAsync(currentUser, model);

            return(Json(new { success = succeeded }));
        }
        public async Task <bool> UpdateValueOfVariableAsync(IdentityUser user, UpdateValueModel model)
        {
            //Add user identity check here

            var variable = await _context.Variables.SingleOrDefaultAsync(x => x.Id == model.Id);

            if (variable == null)
            {
                return(false);
            }
            var app = await _context.Applications.SingleOrDefaultAsync(x => x.AppId == variable.AppId);

            if (app == null || app.UserId != user.Id)
            {
                return(false);
            }
            variable.VariableValue = model.Value;
            return((await _context.SaveChangesAsync()) == 1);
        }
        public IActionResult UpdateValue([FromBody] UpdateValueModel updateValueModel)
        {
            try
            {
                var userPermission = HttpContext
                                     .User
                                     .Claims
                                     .First(claim => claim.Type == "Permission")
                                     .Value;

                if (userPermission == "1")
                {
                    throw new Exception("You do not have a permission to change laboratory book");
                }

                var dataId    = updateValueModel.dataId;
                var dataType  = updateValueModel.dataType;
                var dataValue = updateValueModel.dataValue;

                var connectonString = _configuration.GetConnectionString("LaboratoryBookConnectionString");

                var laboratoryBook = HttpContext.User.Claims.First(claim => claim.Type == "LaboratoryBook").Value;

                var changeDatumResult = LaboratoryBookHelper.ChangeDatum(
                    connectonString,
                    laboratoryBook,
                    dataType,
                    dataValue,
                    dataId);

                if (!changeDatumResult)
                {
                    return(Json(new Tuple <bool, string>(false, "datum was no changed")));
                }

                return(Json(new Tuple <bool, string>(true, "success!")));
            }
            catch (Exception exception)
            {
                return(Json(new Tuple <bool, string>(false, exception.Message)));
            }
        }