public async Task <Result <int> > Handle(EditDepartmentCommand command, CancellationToken token) { using (var repository = _unitOfWork.GetRepository <Department>()) { var department = await repository.FindAsync(command.Id, true, token).ConfigureAwait(false); if (department == null) { return(Result.Fail($"{nameof(EditDepartmentHandler)} failed on edit {nameof(Department)} '{command.Id}'.", StatusCodes.Status404NotFound)); // We could perform a upserting but such operations will require to have guids as primary keys. } dynamic data = command.Operations.Aggregate(new ExpandoObject() as IDictionary <string, object>, (a, p) => { a.Add(p.Key.Replace("/", ""), p.Value); return(a); }); // Use an expando object to build such as and "anonymous" object. _mapper.Map(data, department); // (*) Update entity with expando properties and his projections, using auto mapper Map(source, destination) overload. ValidateModel(department, out var results); if (results.Count != 0) { return(Result.Fail($"{nameof(EditDepartmentHandler)} failed on edit {nameof(Department)} '{command.Id}' '{results.First().ErrorMessage}'.", StatusCodes.Status400BadRequest)); } var success = await repository.UpdateAsync(department, token : token).ConfigureAwait(false) && // Since the entity has been tracked by the context when was issued FindAsync await _unitOfWork.SaveChangesAsync().ConfigureAwait(false) >= 0; // now any changes projected by auto mapper will be persisted by SaveChangesAsync. return(success ? Result.Ok(StatusCodes.Status204NoContent) : Result.Fail <int>($"{nameof(EditDepartmentHandler)} failed on edit {nameof(Department)} '{command.Id}'.")); } }
public async Task <IActionResult> Edit(int id, [Bind("DepartmentID,DepartmentName")] Department department) { if (id != department.DepartmentID) { return(NotFound()); } if (ModelState.IsValid) { try { var command = new EditDepartmentCommand(department); var handler = CommandHandlerFactory.Build(command); handler.Execute(_context); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DepartmentExists(department.DepartmentID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(department)); }
public async Task <ApiResponse> EditDepartment([FromBody] EditDepartmentCommand model) { var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; model.ModifiedById = userId; model.ModifiedDate = DateTime.UtcNow; return(await _mediator.Send(model)); }
public Department Update(EditDepartmentCommand command) { var depart = _repository.Get(command.Id); depart.UpdateData(command.Name, command.Description, command.Active); if (depart.IsValid()) { _repository.Update(depart); } return(depart); }
public static ICommandHandler <EditDepartmentCommand> Build(EditDepartmentCommand command) { return(new EditDepartmentCommandHandler(command)); }
public async Task <IActionResult> Update([FromBody] EditDepartmentCommand command) { var result = _service.Update(command); return(await Response(result, result.Notifications)); }