protected override Species CreateEntityFromModel(CrudCreateEditViewModel <Species> model) { var species = new Species(); this.UpdateEntityFromModel(model, ref species); return(species); }
protected override Breed CreateEntityFromModel(CrudCreateEditViewModel <Breed> model) { var breed = new Breed(); this.UpdateEntityFromModel(model, ref breed); return(breed); }
protected override void UpdateEntityFromModel(CrudCreateEditViewModel <Contact> model, ref Contact entity) { // Whitelisting values that the user can provide entity.Name = model.Entity.Name; entity.Email = model.Entity.Email; entity.Phone = model.Entity.Phone; entity.Type = model.Entity.Type; }
protected override Contact CreateEntityFromModel(CrudCreateEditViewModel <Contact> model) { var contact = new Contact(); this.UpdateEntityFromModel(model, ref contact); return(contact); }
protected override Location CreateEntityFromModel(CrudCreateEditViewModel <Location> model) { var location = new Location { Type = model.Entity.Type // Type is only whitelisted during creation. }; this.UpdateEntityFromModel(model, ref location); return(location); }
public virtual async Task <IActionResult> Edit( CrudCreateEditViewModel <EntityT> model, [FromServices] IAuthorizationService auth ) { var isAuthed = await auth.AuthorizeAsync(this.User, this.Config.WritePolicy); if (!isAuthed.Succeeded) { return(this.Forbid()); } if (this.ModelState.IsValid) { var result = await this.Crud.GetByIdAsync(this.GetEntityId(model.Entity)); if (!result.Succeeded) { return(this.RedirectToAction("Index", new { error = result.GatherErrorMessages().FirstOrDefault() })); } var dbEntity = result.Value; this.UpdateEntityFromModel(model, ref dbEntity); using (var workScope = this.UnitOfWork.Begin("Edit Entity")) { var updateResult = this.Crud.Update(dbEntity); if (!updateResult.Succeeded) { workScope.Rollback("CreateAsync failed."); foreach (var error in result.GatherErrorMessages()) { this.ModelState.AddModelError(string.Empty, error); } return(this.View(this.Config.VIEW_CREATE_EDIT, model)); } workScope.Commit(); } this.Logger.LogInformation( "Entity {Entity} updated by {User}", typeof(EntityT).Name, this.User.FindFirstValue(ClaimTypes.Name) ); } return(this.View(this.Config.VIEW_CREATE_EDIT, model)); }
private async Task <HttpResponseMessage> CrudCreateEditAsync <T>(string url, T entity, bool isCreate) where T : class { var model = new CrudCreateEditViewModel <T> { IsCreate = isCreate, Entity = entity }; var codeToCheck = (isCreate) ? HttpStatusCode.Redirect // Test for redirect, since it should be going to an edit page, instead of erroring to the create page. : HttpStatusCode.OK; // Test for going back to the same page. return(await this.PostEnsureStatusAsync(url, model.ToFormEncodedContent(), codeToCheck)); }
protected override void UpdateEntityFromModel(CrudCreateEditViewModel <Location> model, ref Location entity) { entity.Name = model.Entity.Name; // Whitelist changes. if (model.Entity.Holding != null && model.Entity.Type == LocationType.Holding) { var left = entity.Holding ?? new LocationHolding(); var right = model.Entity.Holding; left.Address = right.Address; left.GridReference = right.GridReference; left.HoldingNumber = right.HoldingNumber; left.OwnerId = right.OwnerId; left.Postcode = right.Postcode; left.Timestamp = right.Timestamp; left.Location = entity; entity.Holding = left; } }
/// <summary> /// Updates an instance of <typeparamref name="EntityT"/> from the given <paramref name="model"/> /// </summary> /// <param name="model">The model to update the entity from.</param> /// <param name="entity">The entity to update.</param> protected abstract void UpdateEntityFromModel(CrudCreateEditViewModel <EntityT> model, ref EntityT entity);
/// <summary> /// Creates an instance of <typeparamref name="EntityT"/> from the given <paramref name="model"/> /// </summary> /// <param name="model">The model to create the entity from.</param> /// <returns>The <typeparamref name="EntityT"/> create from <paramref name="model"/>.</returns> protected abstract EntityT CreateEntityFromModel(CrudCreateEditViewModel <EntityT> model);
protected override void UpdateEntityFromModel(CrudCreateEditViewModel <Species> model, ref Species entity) { entity.Name = model.Entity.Name; entity.GestrationPeriod = model.Entity.GestrationPeriod; }
protected override void UpdateEntityFromModel(CrudCreateEditViewModel <Breed> model, ref Breed entity) { entity.Name = model.Entity.Name; entity.SpeciesId = model.Entity.SpeciesId; }