public DbCustomUserField(CustomUserField model) { Id = model.Id; Name = model.Name; Type = model.Type.ToString(); Required = model.Required; Validation = JsonConvert.SerializeObject(model.GetValidationProperties()); }
public async Task UpdateCustomUserField(CustomUserField field) { try { using (var connection = await GetConnectionAsync()) { var model = new DbCustomUserField(field); await connection.ExecuteAsync(RegistrarQueries.CustomUserFieldUpdate, model); } } catch (Exception) { throw new Exception("Could not delete custom user field"); } }
public SingleCustomUserFieldResponse(CustomUserField field) { Id = field.Id; Name = field.Name; Type = field.Type.ToString(); Required = field.Required; var props = field.GetValidationProperties(); var propTypes = props.GetType().GetProperties(); Validation = new Dictionary <string, object>(); foreach (var prop in propTypes) { var name = char.ToLowerInvariant(prop.Name[0]) + prop.Name.Substring(1); Validation.Add(name, prop.GetValue(props)); } }
public CustomUserField ToModel() { EUserFieldType type; if (!Enum.TryParse(Type, out type)) { return(null); } var field = CustomUserField.GetFieldForType(type); field.Id = Id; field.Name = Name; field.Type = type; field.Required = Required; var props = JsonConvert.DeserializeObject <dynamic>(Validation); field.SetValidationProperties(props); return(field); }
public RegisterCustomFieldRequestModel(CustomUserField model) { Name = model.Name; Value = model.Value; }
public CustomUserFieldViewModel(CustomUserField model) { _model = model; }
public async Task <IHttpActionResult> UpdateCustomFields(IList <CreateCustomUserFieldModel> models) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (models == null) { return(BadRequest()); } // Read existing fields from database var existingFields = await _fieldStore.GetCustomUserFields(); // Store lists for database interraction var update = new List <CustomUserField>(); var create = new List <CustomUserField>(); var names = new List <string>(); var errors = new List <string>(); foreach (var model in models) { var validation = new CustomUserValidation(model.Validation); // Ensure the field names are unique if (names.Contains(model.Name)) { var errMsg = $"Duplicate Field name: {model.Name}"; // Avoid duplicate errors if more than two fields share a name if (!errors.Contains(errMsg)) { errors.Add(errMsg); } } else { names.Add(model.Name); } // Creating a new field if (model.Id == 0) { var field = CustomUserField.GetFieldForType(model.Type); field.Name = model.Name; field.Type = model.Type; field.Required = model.Required; if (field.SetValidationProperties(validation)) { create.Add(field); } else { errors.Add($"Invalid Field Valididation for {model.Name}"); } } // Edit an existing field else { var field = existingFields.SingleOrDefault(f => f.Id == model.Id); if (field == null) { errors.Add($"Cannot find existing field, ID: {model.Id}"); continue; } if (field.Type != model.Type) { errors.Add($"Cannot change type of {field.Name} ({field.Id})"); continue; } // Update editable properties field.Name = model.Name; field.Required = model.Required; if (field.SetValidationProperties(validation)) { update.Add(field); } else { errors.Add($"Invalid Field Valididation for {model.Name}"); } } } // Error reading fields, return invalid if (errors.Any()) { foreach (var error in errors) { ModelState.AddModelError("", error); } return(BadRequest(ModelState)); } // Any fields omitted must have been deleted var delete = existingFields.Where(f => !create.Contains(f)).Where(f => !update.Contains(f)).ToList(); // Sanity checks if (create.Count + update.Count != models.Count) { return(InternalServerError()); } if (update.Count + delete.Count != existingFields.Count) { return(InternalServerError()); } // Run synchronously to avoid any uniqueness conflicts and race conditions foreach (var d in delete) { await _fieldStore.DeleteCustomUserField(d); } foreach (var u in update) { await _fieldStore.UpdateCustomUserField(u); } foreach (var c in create) { await _fieldStore.CreateCustomUserField(c); } // Update User View with custom fields (columns) await _fieldStore.UpdateUserView(); return(Ok()); }