Пример #1
0
        public async Task <IActionResult> Edit(EditModel model)
        {
            if (ModelState.IsValid)
            {
                var command = new AddOrEditCommand();
                _mapper.Map(model, command);
                var result = await _mediator.Send(command);

                if (result.Success)
                {
                    TempData[NotificationMessageKey] = $"Task {(model.IsAdding ? "created" : "updated")}";
                    return(RedirectToAction("Index"));
                }

                ModelState.AddModelError(string.Empty, result.ErrorMessage);
            }

            return(View(model));
        }
Пример #2
0
        public async Task <CommandResult <int> > Handle(AddOrEditCommand command, CancellationToken cancellationToken)
        {
            var category = await GetCategory(command.CategoryId);

            Infrastructure.Data.Task task;
            if (command.IsAdding)
            {
                task = new Infrastructure.Data.Task(command.Description, category);
                Context.Tasks.Add(task);
            }
            else
            {
                task = await GetTask(command.Id);

                task.SetDetails(command.Description, category);
            }

            await Context.SaveChangesAsync();

            return(CommandResult <int> .SuccessResult(task.Id));
        }