public async Task <IActionResult> Put(Guid id, [FromBody] DepartmentDTO value) { var cmd = new UpdateDepartmentCommand(id, value); var result = await _mediator.Send(cmd); return(Ok(result)); }
public void Handle(UpdateDepartmentCommand message) { if (!message.IsValid()) { NotifyValidationErrors(message); return; } var department = Department.Create(message.Id, message.DepartmentGroup, message.DepartmentType, message.Description, message.ApplyTax, message.Amount, message.Percentage); var existingDepartment = _departmentRepository.GetByDescription(department.Description); if (existingDepartment != null) { if (!existingDepartment.Equals(department)) { Bus.RaiseEvent(new DomainNotification(message.MessageType, "The Department description has already been taken.")); return; } } _departmentRepository.Update(department); if (Commit()) { Bus.RaiseEvent(new DepartmentUpdatedEvent(department.DepartmentGroup, department.DepartmentType, department.Description, department.ApplyTax, department.Amount, department.Percentage)); } }
public async Task UpdateDepartmentAsync(UpdateDepartmentCommand command) { var department = await _departmentRepository.GetDepartmentAsync(command.Id); department.UpdateName(command.Name); _departmentRepository.Update(department); await _unitOfWork.CommitAsync(); }
public async Task <ActionResult> Edit(UpdateDepartmentCommand command) { if (await Mediator.Send(command) == null) { return(BadRequest()); } return(Ok()); }
public async Task <ActionResult> Update(int id, UpdateDepartmentCommand command) { if (id != command.Id) { return(BadRequest()); } await Mediator.Send(command); return(NoContent()); }
public async Task <IActionResult> UpdateDepartment(UpdateDepartmentCommand updateDepartment) { var result = await Mediator.Send(updateDepartment); if (result.Success == false) { return(result.ApiResult); } return(NoContent()); }
public override async Task <Empty> UpdateDepartment(UpdateDepartmentRequest request, ServerCallContext context) { var command = new UpdateDepartmentCommand { Id = request.Id, Name = request.Name, IsEnabled = request.IsEnabled, }; await _organizationApp.UpdateDepartmentAsync(command); return(new Empty()); }
public IEnumerable<IEvent> Handle(UpdateDepartmentCommand cmd) { if (cmd == null) throw new ArgumentNullException(nameof(cmd)); if (cmd.AggregateId == Guid.Empty) throw new ArgumentException(nameof(cmd.AggregateId)); if (string.IsNullOrEmpty(nameof(cmd.Name))) throw new ArgumentException(nameof(cmd.AggregateId)); var events = new List<IEvent>(); events.Add(new UpdateDepartmentEvent(cmd.AggregateId, cmd.Name)); return events; }
public async Task <ActionResult> UpdateDepartmentAsync(UpdateDepartmentCommand updateDepartmentCommand) { bool commandResult = false; _logger.LogInformation( "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", updateDepartmentCommand.GetType().Name, nameof(updateDepartmentCommand.Id), updateDepartmentCommand.Id, updateDepartmentCommand); commandResult = await _mediator.Send(updateDepartmentCommand); if (!commandResult) { return(BadRequest()); } return(Ok()); }
public IEnumerable <IEvent> Handle(UpdateDepartmentCommand cmd) { if (cmd == null) { throw new ArgumentNullException(nameof(cmd)); } if (cmd.AggregateId == Guid.Empty) { throw new ArgumentException(nameof(cmd.AggregateId)); } if (string.IsNullOrEmpty(nameof(cmd.Name))) { throw new ArgumentException(nameof(cmd.AggregateId)); } var events = new List <IEvent>(); events.Add(new UpdateDepartmentEvent(cmd.AggregateId, cmd.Name)); return(events); }
public async Task <IActionResult> UpdateDepartment([FromBody] UpdateDepartmentCommand department) { var command = await _mediator.Send(department); return(Ok(command)); }
public async Task <IActionResult> UpdateDepartment([FromBody] UpdateDepartmentCommand command) { return(Ok(await Mediator.Send(command))); }
public async Task <IActionResult> Update([FromBody] UpdateDepartmentCommand command) { await Mediator.Send(command); return(NoContent()); }
public static Department ToDepartment(this UpdateDepartmentCommand command) { return(command.MapTo <UpdateDepartmentCommand, Department>()); }
public async Task <IActionResult> Edit(UpdateDepartmentCommand command) { try { await Mediator.Send(command); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException ex) { var vm = new UpdateDepartmentVM { DepartmentID = command.DepartmentID.Value, Name = command.Name, Budget = command.Budget, StartDate = command.StartDate, InstructorID = command.InstructorID, RowVersion = command.RowVersion }; var exceptionEntry = ex.Entries.Single(); var clientValues = (Department)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save changes. The department was deleted by another user."); } else { var databaseValues = (Department)databaseEntry.ToObject(); if (databaseValues.Name != clientValues.Name) { ModelState.AddModelError("Name", $"Current value: {databaseValues.Name}"); } if (databaseValues.Budget != clientValues.Budget) { ModelState.AddModelError("Budget", $"Current value: {databaseValues.Budget:c}"); } if (databaseValues.StartDate != clientValues.StartDate) { ModelState.AddModelError("StartDate", $"Current value: {databaseValues.StartDate:d}"); } if (databaseValues.InstructorID != clientValues.InstructorID) { //Models.Instructor databaseInstructor = await _context.Instructors.FirstOrDefaultAsync(i => i.ID == databaseValues.InstructorID); //ModelState.AddModelError("InstructorID", $"Current value: {databaseInstructor?.FullName}"); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); vm.RowVersion = (byte[])databaseValues.RowVersion; ModelState.Remove("RowVersion"); } var instructorsLookup = await Mediator.Send(new GetInstructorLookupCommand()); ViewData["InstructorID"] = new SelectList(instructorsLookup, "ID", "FullName", command.InstructorID); return(View(vm)); } ////----------------------------- //if (id == null) //{ // return NotFound(); //} //var departmentToUpdate = await _context.Departments.Include(i => i.Administrator).FirstOrDefaultAsync(m => m.DepartmentID == id); //if (departmentToUpdate == null) //{ // Models.Department deletedDepartment = new Models.Department(); // await TryUpdateModelAsync(deletedDepartment); // ModelState.AddModelError(string.Empty, // "Unable to save changes. The department was deleted by another user."); // var instructorsList = await Mediator.Send(new GetInstructorLookupCommand()); // ViewData["InstructorID"] = new SelectList(instructorsList, "ID", "FullName", deletedDepartment.InstructorID); // return View(deletedDepartment); //} //_context.Entry(departmentToUpdate).Property("RowVersion").OriginalValue = rowVersion; //if (await TryUpdateModelAsync<Models.Department>( // departmentToUpdate, // "", // s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID)) //{ // try // { // await _context.SaveChangesAsync(); // return RedirectToAction(nameof(Index)); // } // catch (DbUpdateConcurrencyException ex) // { // var exceptionEntry = ex.Entries.Single(); // var clientValues = (Models.Department)exceptionEntry.Entity; // var databaseEntry = exceptionEntry.GetDatabaseValues(); // if (databaseEntry == null) // { // ModelState.AddModelError(string.Empty, // "Unable to save changes. The department was deleted by another user."); // } // else // { // var databaseValues = (Models.Department)databaseEntry.ToObject(); // if (databaseValues.Name != clientValues.Name) // { // ModelState.AddModelError("Name", $"Current value: {databaseValues.Name}"); // } // if (databaseValues.Budget != clientValues.Budget) // { // ModelState.AddModelError("Budget", $"Current value: {databaseValues.Budget:c}"); // } // if (databaseValues.StartDate != clientValues.StartDate) // { // ModelState.AddModelError("StartDate", $"Current value: {databaseValues.StartDate:d}"); // } // if (databaseValues.InstructorID != clientValues.InstructorID) // { // Models.Instructor databaseInstructor = await _context.Instructors.FirstOrDefaultAsync(i => i.ID == databaseValues.InstructorID); // ModelState.AddModelError("InstructorID", $"Current value: {databaseInstructor?.FullName}"); // } // ModelState.AddModelError(string.Empty, "The record you attempted to edit " // + "was modified by another user after you got the original value. The " // + "edit operation was canceled and the current values in the database " // + "have been displayed. If you still want to edit this record, click " // + "the Save button again. Otherwise click the Back to List hyperlink."); // departmentToUpdate.RowVersion = (byte[])databaseValues.RowVersion; // ModelState.Remove("RowVersion"); // } // } //} //var instructors = await Mediator.Send(new GetInstructorLookupCommand()); //ViewData["InstructorID"] = new SelectList(instructors, "ID", "FullName", departmentToUpdate.InstructorID); //return View(departmentToUpdate); }
public async Task <IActionResult> Edit(UpdateDepartmentCommand command) => Json(await Mediator.Send(command));