public async Task <ResultModel <Dictionary <string, object> > > Create(Dictionary <string, object> model) { try { IEnumerable <AddedColumn> addedColumns = await _addedColumn.GetAll(); Contact entity = MapToEntity(model, addedColumns); var checkunique = await _contact.GetAll(t => t.Name == entity.Name); if (checkunique.Any()) { return(ResultModel <Dictionary <string, object> > .GetErrorResult(new List <ResultErrorModel> { new ResultErrorModel(ResultErrorTypeEnum.UniqueFieldError, new string[] { "Contact Name" }) })); } entity = await _contact.Add(entity); await _contact.Commit(); model = MapToModel(entity); return(ResultModel <Dictionary <string, object> > .GetSuccessResult(model)); } catch (Exception e) { await _contact.Abort(); ResultErrorModel error = new ResultErrorModel(e); return(ResultModel <Dictionary <string, object> > .GetExceptionResult(error)); } }
public override async Task <IEnumerable <Contact> > GetFiltered(List <ColumnFilterInfo> filters, PaginationInfo paginationInfo) { IEnumerable <AddedColumn> addedColumns = await addedColumn.GetAll(); IEnumerable <AddedColumn> contactAddedCols = addedColumns.Where(t => t.EntityName == typeof(Contact).Name); IEnumerable <AddedColumn> companyAddedCols = addedColumns.Where(t => t.EntityName == typeof(Company).Name); ConfigDbSet(); FilterDefinition <Contact> filterDefinition = Builders <Contact> .Filter.Empty; foreach (ColumnFilterInfo filter in filters) { if (filter.EntityName == typeof(Contact).Name) { switch (filter.ColumnName) { case "ID": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.ColumnName, Converters.ToInt(filter.Value)); break; case "Name": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.ColumnName, Converters.ToString(filter.Value)); break; default: AddedColumn col = contactAddedCols.FirstOrDefault(t => t.ColumnName == filter.ColumnName); if (col != null) { switch (col.ColumnType.ToLower()) { case "text": case "date": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.ColumnName, Converters.ToString(filter.Value)); break; case "number": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.ColumnName, Converters.ToInt(filter.Value)); break; } } break; } } else if (filter.EntityName == typeof(Company).Name) { switch (filter.ColumnName) { case "ID": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.EntityName + "." + filter.ColumnName, Converters.ToInt(filter.Value)); break; case "Name": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.EntityName + "." + filter.ColumnName, Converters.ToString(filter.Value)); break; case "NumberOfEmployees": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.EntityName + "." + filter.ColumnName, Converters.ToInt(filter.Value)); break; default: AddedColumn col = companyAddedCols.FirstOrDefault(t => t.EntityName == filter.EntityName && t.ColumnName == filter.ColumnName); if (col != null) { switch (col.ColumnType.ToLower()) { case "text": case "date": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.EntityName + "." + filter.ColumnName, Converters.ToString(filter.Value)); break; case "number": filterDefinition = filterDefinition & Builders <Contact> .Filter.Eq(filter.EntityName + "." + filter.ColumnName, Converters.ToInt(filter.Value)); break; } } break; } //filterDefinition = filterDefinition & Builders<Contact>.Filter.Eq(filter.EntityName + "." + filter.ColumnName, filter.Value); } } var all = await DbSet.FindAsync(filterDefinition); return(all.ToList()); }