public async Task <SaveResult <ExampleEntityDto> > Update(ExampleEntityDto dto) { var saveResult = new SaveResult <ExampleEntityDto>(); var existingEntity = await this._context.ExampleEntities.SingleOrDefaultAsync(m => m.Id == dto.Id); if (existingEntity != null) { var validator = new ExampleEntityValidator(); var validationResult = await validator.ValidateAsync(dto); if (validationResult.IsValid) { existingEntity.Update(dto, this._context, null, DateTime.Now); await this._context.SaveChangesAsync(); existingEntity = await this._context.ExampleEntities.SingleOrDefaultAsync(m => m.Id == dto.Id); saveResult.Succeeded = true; saveResult.Data = existingEntity.ToDto(); } else { saveResult.Errors = validationResult.Errors.Select(m => new ServiceError(m.PropertyName, m.ErrorMessage)); } } else { saveResult.Errors = saveResult.Errors.Append(new ServiceError("NonExistentEntity", $"Entity Not Found (ID: {dto.Id}).")); } return(saveResult); }
public async Task <SaveResult <ExampleEntityDto> > Create(ExampleEntityDto dto) { var saveResult = new SaveResult <ExampleEntityDto>(); var validator = new ExampleEntityValidator(); var validationResult = await validator.ValidateAsync(dto); if (validationResult.IsValid) { var newEntity = dto.ToEntity(); newEntity = this._context.ExampleEntities.Add(newEntity); await this._context.SaveChangesAsync(); saveResult.Succeeded = true; saveResult.Data = newEntity.ToDto(); } else { saveResult.Errors = validationResult.Errors.Select(m => new ServiceError(m.PropertyName, m.ErrorMessage)); } return(saveResult); }
internal static void Update(this ExampleEntity entity, ExampleEntityDto dto, DbContext context, string modifiedBy, DateTime modifiedDate) { if (entity != null && dto != null) { entity.ExampleProperty = dto.ExampleProperty; entity.ModifiedDate = modifiedDate; entity.ModifiedBy = modifiedBy; } }
public async Task ExampleEntity_Create_Test() { var exampleEntity = new ExampleEntityDto { ExampleProperty = "Brand New ExampleEntity" }; var result = await this._exampleEntitiesController.Post(exampleEntity) as OkNegotiatedContentResult <SaveResult <ExampleEntityDto> >; Assert.IsNotNull(result); Assert.IsNotNull(result.Content); Assert.IsNotNull(result.Content.Data); }
internal static ExampleEntity ToEntity(this ExampleEntityDto dto) { if (dto == null) { return(null); } return(new ExampleEntity { // Do not modify changes to system-generated values like Id, CreateDate, CreatedBy, ModifiedDate, and ModifiedBy ExampleProperty = dto.ExampleProperty }); }
public async Task <IHttpActionResult> Put(int id, [FromBody] ExampleEntityDto divisionDto) { try { var result = await this._exampleEntityService.Update(divisionDto); if (result.Errors != null && result.Errors.Any()) { foreach (var serviceError in result.Errors) { this.ModelState.AddModelError(serviceError.Code, serviceError.Description); } } return(this.Ok(result)); } catch (Exception e) { return(this.HandleException(e)); } }