private async Task <int> HandleAsync(AddLabelCommand command) { var label = await this.context.Labels.FirstOrDefaultAsync(l => l.Name == command.Name); if (label == null) { label = new Label { Name = command.Name }; await this.context.Labels.AddAsync(label); await this.context.SaveChangesAsync(); } if (await this.context.ItemLabels.FirstOrDefaultAsync(il => il.ItemId == command.ItemId && il.LabelId == label.Id) != null) { return(label.Id); } await this.context.ItemLabels.AddAsync(new ItemLabel { ItemId = command.ItemId, Label = label }); await this.context.SaveChangesAsync(); return(label.Id); }
public void AddLabelTest() { var mediator = new Mock <IMediator>(); AddLabelCommand command = new AddLabelCommand { Label = new Label() { Name = "Label2" } }; mediator.Setup(e => e.Send(command, new System.Threading.CancellationToken())).Returns(Task.FromResult(1)); LabelController controller = new LabelController(mediator.Object); var result = controller.AddLabel(command); var response = result.Result as OkObjectResult; Assert.AreEqual(1, (int)response.Value); }
public async Task <int> AddLabel([FromBody] AddLabelCommand command) { return(await commandProcessor.ProcessAsync(command)); }
public async Task <IActionResult> AddLabel([FromBody] AddLabelCommand command) { return(Ok(await _mediator.Send(command))); }