Beispiel #1
0
        private void CorrectFlow()
        {
            id = Builder <int> .CreateNew().Build();

            okResult    = Result.Ok(id);
            errorResult = Result.Error <int>("Error");

            command = Builder <AddOrUpdateCategoryCommand> .CreateNew().Build();

            mediator.Setup(m => m.Send(It.IsAny <AddOrUpdateCategoryCommand>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(okResult);
        }
Beispiel #2
0
        private void CorrectFlow()
        {
            Id = Builder <int> .CreateNew().Build();

            OkResult    = Result.Ok(Id);
            ErrorResult = Result.Error <int>("Error");

            Command = Builder <AddOrUpdateCategoryCommand> .CreateNew()
                      .With(u => u.Id = 1).Build();

            mediator.Setup(m => m.Send(It.IsAny <AddOrUpdateCategoryCommand>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(OkResult);
        }
Beispiel #3
0
            public async Task <Result <int> > Handle(AddOrUpdateCategoryCommand request, CancellationToken cancellationToken)
            {
                if (request.Id == 0)
                {
                    var category = _mapper.Map <Category>(request);

                    var result = await _validator.ValidateAsync(category, cancellationToken);

                    if (!result.IsValid)
                    {
                        return(Result.Error <int>(result.Errors));
                    }

                    await _dataContext.Categories.AddAsync(category, cancellationToken);

                    await _dataContext.SaveChangesAsync(cancellationToken);

                    return(Result.Ok(category.Id));
                }
                else
                {
                    var category = new Category
                    {
                        Id = request.Id
                    };

                    _dataContext.Categories.Attach(category);

                    category.Name = request.Name;

                    var result = await _validator.ValidateAsync(category, cancellationToken);

                    if (!result.IsValid)
                    {
                        return(Result.Error <int>(result.Errors));
                    }

                    await _dataContext.SaveChangesAsync(cancellationToken);

                    return(Result.Ok(request.Id));
                }
            }
Beispiel #4
0
        public async Task <IActionResult> Put(AddOrUpdateCategoryCommand command, CancellationToken cancellationToken = default)
        {
            var result = await _mediator.Send(command, cancellationToken);

            return(result.Process(ModelState));
        }