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

AddOrEditAsync() public method

public AddOrEditAsync ( object>.Dictionary item, List customFields ) : Task
item object>.Dictionary
customFields List
return Task
        public async Task<object> AddOrEditAsync(Dictionary<string, object> item, List<CustomField> customFields)
        {
            if (string.IsNullOrWhiteSpace(this.Database))
            {
                return null;
            }

            var primaryKeyValue = item.FirstOrDefault(x => x.Key.ToPascalCase().Equals(this.PrimaryKey.ToPascalCase())).Value;

            if (primaryKeyValue != null)
            {
                await this.UpdateAsync(item, primaryKeyValue, customFields).ConfigureAwait(false);
            }
            else
            {
                primaryKeyValue = await this.AddAsync(item, customFields, true).ConfigureAwait(false);
            }

            return primaryKeyValue;
        }

Usage Example

Esempio n. 1
0
        public async Task<object> AddOrEditAsync(string schemaName, string tableName, [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);
                return await repository.AddOrEditAsync(item, customFields).ConfigureAwait(false);
            }
            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
        }