Frapid.WebApi.DataAccess.FormRepository.UpdateAsync C# (CSharp) Method

UpdateAsync() public method

public UpdateAsync ( object>.Dictionary item, object primaryKeyValue, List customFields ) : System.Threading.Tasks.Task
item object>.Dictionary
primaryKeyValue object
customFields List
return System.Threading.Tasks.Task
        public async Task UpdateAsync(Dictionary<string, object> item, object primaryKeyValue,
            List<CustomField> customFields)
        {
            if (string.IsNullOrWhiteSpace(this.Database))
            {
                return;
            }

            if (!this.SkipValidation)
            {
                if (!this.Validated)
                {
                    await this.ValidateAsync(AccessTypeEnum.Edit, this.LoginId, this.Database, false).ConfigureAwait(false);
                }
                if (!this.HasAccess)
                {
                    Log.Information(
                        $"Access to edit entity \"{this.FullyQualifiedObjectName}\" with Primary Key {this.PrimaryKey} was denied to the user with Login ID {this.LoginId}.");
                    throw new UnauthorizedException("Access is denied.");
                }
            }

            item = this.Crypt(item);

            item["audit_user_id"] = this.UserId;
            item["audit_ts"] = DateTimeOffset.UtcNow;
            item["deleted"] = false;

            using (var db = DbProvider.GetDatabase(this.Database))
            {
                var sql = new Sql("UPDATE " + this.FullyQualifiedObjectName + " SET");

                int index = 0;

                foreach (var prop in item.Where(x => !x.Key.Equals(this.PrimaryKey)))
                {
                    if (index > 0)
                    {
                        sql.Append(",");
                    }

                    sql.Append(Sanitizer.SanitizeIdentifierName(prop.Key) + "=@0", prop.Value);
                    index++;
                }


                sql.Where(this.PrimaryKey + "=@0", primaryKeyValue);

                await db.NonQueryAsync(sql).ConfigureAwait(false);
                await this.AddCustomFieldsAsync(primaryKeyValue, customFields).ConfigureAwait(false);
            }
        }

Usage Example

Esempio n. 1
0
        public async Task<object> EditAsync(string schemaName, string tableName, string primaryKey, [FromBody] JArray form)
        {
            var item = form[0].ToObject<Dictionary<string, object>>();
            var customFields = form[1].ToObject<List<CustomField>>(JsonHelper.GetJsonSerializer());

            if(item == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
            }

            try
            {
                var repository = new FormRepository(schemaName, tableName, this.AppUser.Tenant, this.AppUser.LoginId, this.AppUser.UserId);
                await repository.UpdateAsync(item, primaryKey, customFields).ConfigureAwait(false);
                return primaryKey;
            }
            catch(UnauthorizedException)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }
            catch(DataAccessException ex)
            {
                throw new HttpResponseException
                    (
                    new HttpResponseMessage
                    {
                        Content = new StringContent(ex.Message),
                        StatusCode = HttpStatusCode.InternalServerError
                    });
            }
#if !DEBUG
            catch
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }
#endif
        }