public async Task <ActionResult> UpdateByUniqueId([FromBody] UpdateCategoryByUniqueId u) { UpdateCategoryCommand updateCommand = new UpdateCategoryCommand() { DisplayName = u.DisplayName, UniqueId = u.UniqueId, Name = u.Name, WhatWeAreLookingFor = u.WhatWeAreLookingFor }; var result = await _mediator.Send(updateCommand); return(NoContent()); }
public ResultDto UpdateCategory(long id, CategoryDto category) { return(Result(() => { var command = new UpdateCategoryCommand { CategoryId = id, Description = category.Description, ParentCategoryId = category.ParentId, IsPublic = category.IsPublic, }; CommandDispatcher.Send(command); })); }
public async Task Update_Authenticated_Success() { // Arrange var client = _factory.CreateAuthenticatedClient(); var command = new UpdateCategoryCommand { Name = "Changed" }; // Act var response = await client.PutAsJsonAsync($"{Uri}/1", command); // Assert response.StatusCode.Should().Be(HttpStatusCode.OK); }
public async Task <ActionResult <Category> > UpdateCategoryAsync(int id, UpdateCategoryCommand updateCategoryCommand) { var existingCategory = await _queries.FindByIdAsync(id); if (existingCategory == null) { return(NotFound()); } _mapper.Map(updateCategoryCommand, existingCategory); await _behavior.UpdateCategoryAsync(existingCategory); return(existingCategory); }
public void IsValid_ShouldBeTrue_WhenListTitleIsUnique() { var command = new UpdateCategoryCommand { Id = 1, CategoryName = "Shopping" }; var validator = new UpdateCategoryCommandValidator(Context); var result = validator.Validate(command); result.IsValid.ShouldBe(true); }
public void Handle(UpdateCategoryCommand message) { var category = Category.CategoryFactory.FullCategory(message.Id, message.Name, message.Description, message.Image, message.IsFeatured, message.Active, message.Created); if (!category.IsValid()) { NotifyErrors(category.ValidationResult); return; } _categoryRepository.Update(category); if (Commit()) { _bus.RaiseEvent(new UpdatedCategoryEvent(category.Id, category.Name, category.Description, category.Image, category.IsFeatured, category.Active, category.Created)); } }
public async Task <IActionResult> UpdateImage(int id, [FromForm] IFormFile uploadedFile) { var updateCommand = new UpdateCategoryCommand { Id = id }; await using var stream = new MemoryStream(); await uploadedFile.CopyToAsync(stream); updateCommand.Picture = stream.ToArray(); await _mediator.Send(updateCommand); return(NoContent()); }
public async Task <IActionResult> PutCategory(long id, UpdateCategoryCommand category) { if (id != category.Id) { return(BadRequest()); } string UserId = getUserId(); category.UserId = UserId; await _mediator.Send(category); return(NoContent()); }
public async Task Handle_Update_Category() { var category = new UpdateCategoryCommand() { Description = "Notebook", Id = 1, CategoryName = "Laptop1" }; var handler = new UpdateCategoryCommandHandler.Handler(_context); await handler.Handle(category, CancellationToken.None); var categoryName = _context.Categories.Find(1); categoryName.CategoryName.ShouldBe("Laptop1"); }
public async Task <CommandResult <Category> > Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { var category = await _repository.GetCategoryById(request.Id); if (category == null) { return(CommandResult <Category> .Fail(category, "Name not exist")); } category.Update(request.Name, request.Image, request.SubCategories); _repository.Update(category); PublishEvents(category); return(CommandResult <Category> .Success(category)); }
public async Task Update_Category_With_InvalidCommand_ShouldThrowException() { var command = new UpdateCategoryCommand { CategoryId = (CategoryId)Guid.Empty, CategoryName = string.Empty }; IRequestHandler <UpdateCategoryCommand> handler = new CommandHandler(this.MockRepositoryFactory.Object, this._validator); await Should.ThrowAsync <ValidationException>(async() => await handler.Handle(command, this.CancellationToken)); this.MockRepository.Verify(x => x.UpdateAsync(It.IsAny <Category>()), Times.Never); }
public async Task Update_Category() { using (var context = GetContextWithData()) { var handler = new UpdateCategoryCommandHandler(context); var command = new UpdateCategoryCommand { Id = (await context.Categories.FirstOrDefaultAsync()).Id, Name = "Test2" }; await handler.Handle(command, CancellationToken.None); Assert.Equal(command.Name, (await context.Categories.FindAsync(command.Id)).Name); } }
public async Task Update_NotFound_Category_ShouldThrowException() { var command = new UpdateCategoryCommand { CategoryId = IdentityFactory.Create <CategoryId>(), CategoryName = this.Fixture.Create <string>() }; this.MockRepository .Setup(x => x.FindOneAsync(It.IsAny <Expression <Func <Category, bool> > >())) .ReturnsAsync((Category)null); IRequestHandler <UpdateCategoryCommand> handler = new CommandHandler(this.MockRepositoryFactory.Object, this._validator); await Should.ThrowAsync <ValidationException>(async() => await handler.Handle(command, this.CancellationToken)); }
public async Task Handler_update_new_category_with_the_correct_properties() { var category = Persister <Data.Entity.Category> .New().Persist(); category.Active = !category.Active; category.Name = category.Name + "2"; category.Description = category.Description + "2"; var command = new UpdateCategoryCommand(category.Id, category); var response = await mediator.ProcessCommandAsync <Data.Entity.Category>(command); Assert.IsTrue(response.Successful, "The command response is successful"); var updatedCategory = await Context.Categories.SingleAsync(p => p.Id == response.Result.Id); Assert.That.This(updatedCategory).HasSameProperties(category); }
public async Task Handle_GivenValidId_ShouldUpdatePersistedCategory() { var command = new UpdateCategoryCommand { Id = 1, CategoryName = "Shopping", }; var handler = new UpdateCategoryCommand.UpdateCategoryCommandHandler(Context); await handler.Handle(command, CancellationToken.None); var entity = Context.Categories.Find(command.Id); entity.ShouldNotBeNull(); entity.CategoryName.ShouldBe(command.CategoryName); }
public async Task <IActionResult> Edit(UpdateCategoryCommand command) { var validator = new UpdateCategoryValidator(); var result = await validator.ValidateAsync(command); if (result.IsValid) { await Mediatr.Send(command); return(View()); } var errors = result.Errors.Select(x => x.ErrorMessage).ToArray(); ModelState.AddModelError("CategoryName", string.Join(",", errors)); return(View()); }
public async Task <ActionResult> UpdateCategory([FromBody] UpdateCategoryDto viewRequest) { if (!TryValidateModel(viewRequest)) { return(BadRequest(ValidationHelper.GetModelErrors(ModelState))); } var request = this._mapper.Map <UpdateCategoryRequest>(viewRequest); request.UserName = HttpContext.User.Identity.Name; var command = new UpdateCategoryCommand { Data = request }; return(await Go(command)); }
public async Task <IActionResult> UpdateCategory(Guid categid, Category category) { var query = new UpdateCategoryCommand(category, categid); var output = await _mediator.Send(query); if (!String.IsNullOrEmpty(output.Name)) { ModelState.AddModelError("category", output.Name); return(BadRequest(ModelState)); } else if (output.ID == Guid.Empty) { ModelState.AddModelError("category", "Category deos not exist"); return(BadRequest(ModelState)); } return(CreatedAtAction(nameof(GetCategoryByID), new { categid = output.ID }, output)); }
public void IsValid_ShouldBeFalse_WhenListTitleIsNotUnique() { Context.Categories.Add(new Category { CategoryName = "Shopping" }); Context.SaveChanges(); var command = new UpdateCategoryCommand { Id = 2, CategoryName = "Shopping" }; var validator = new UpdateCategoryCommandValidator(Context); var result = validator.Validate(command); result.IsValid.ShouldBe(false); }
public async Task <IActionResult> Put(int id, [FromBody] UpdateCategoryCommand request) { try { var data = await this._mediator.Send(request); var result = new ApiResult <bool>() { Message = ApiMessage.UpdateOk, Data = data }; return(this.Ok(result)); } catch (Exception e) { throw e; } }
public Task <bool> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { return(Task.FromResult(false)); } Category category = new Category(request.Id, request.ContactBookId, request.Name); _contactUnitOfWork.CategoriesRepository.UpdateCategory(category); //Storing the update event if (_contactUnitOfWork.Commit()) { _eventHandler.RaiseEvent(new CategoryUpdatedEvent(category.Id, category.ContactBookId, category.Name)); } return(Task.FromResult(true)); }
public ICommandResult Handle(UpdateCategoryCommand command) { if (command.IsValid) { var category = categoryRepository.GetById(command.CategoryId); if (category == null) { command.Errors.Add(new ValidationFailure("CategoryId", "Categoria não foi encontrado")); return(new CommandResult(false, "Problemas ao atulizar o categoria.", command.Errors)); } category.Name = command.Name; categoryRepository.Update(category); return(new CommandResult(true, "Produto salvo com sucesso.", category)); } else { return(new CommandResult(false, "Problemas ao atulizar o categoria.", command.Errors)); } }
public ICommandResult Handle(UpdateCategoryCommand command) { var category = new Category(command.Id, command.Title, command.Description); AddNotifications(category.Notifications); if (Invalid) { return(new CommandResult(false, "Falha ao atualizar Categoria", Notifications)); } bool result = _repository.Update(category); if (!result) { return(new CommandResult(false, "Falha ao atualizar Categoria")); } return(new CommandResult(true, "Categoria atualizada com sucesso")); }
public async Task <ActionResult> Patch(UpdateCategoryCommand command, CancellationToken cancellationToken) { if (command == null) { return(this.BadRequest("Command should be specified.")); } var category = await this.db.Categories.SingleAsync(c => c.Id == command.Id && c.ExternalId == command.ExternalId, cancellationToken); if (category == null) { return(this.NotFound()); } category.Code = command.Code; category.Name = command.Name; return((await this.db.SaveChangesAsync(cancellationToken)) > 0 ? this.Ok() : this.BadRequest("Не удалось обновить!")); }
public async Task UpdateCategoryHandler_Updates_Category() { var category = await GetRandomCategory(); var message = new UpdateCategoryCommand() { Id = category.Id, Name = "New name", Description = "New description" }; var handler = new UpdateCategoryCommandHandler(RequestDbContext); var result = await handler.Handle(message, CancellationToken.None); Assert.IsType <SuccessResult>(result); using (var context = TestContext.CreateNewContext()) { var updatedCategory = await context.Categories.FindAsync(category.Id); Assert.Equal("New name", updatedCategory.Name); Assert.Equal("New description", updatedCategory.Description); } }
public async Task <ActionResult> Update(int id, [FromBody] UpdateCategoryCommand command) { try { if (command.Id == 0) { command.Id = id; } await Mediator.Send(command); return(Ok()); } catch (NotFoundException ex) { return(NotFound(ex.Message)); } catch (ConflictException ex) { return(Conflict(ex.Message)); } }
public async Task Update_Category_Successfully() { var category = Category.Create(this.Fixture.Create <string>()); this.MockRepository .Setup(x => x.FindOneAsync(It.IsAny <Expression <Func <Category, bool> > >())) .ReturnsAsync(category); var command = new UpdateCategoryCommand { CategoryId = category.CategoryId, CategoryName = this.Fixture.Create <string>() }; IRequestHandler <UpdateCategoryCommand> handler = new CommandHandler(this.MockRepositoryFactory.Object, this._validator); await handler.Handle(command, this.CancellationToken); category.DisplayName.ShouldBe(command.CategoryName); this.MockRepository.Verify(x => x.UpdateAsync(It.IsAny <Category>()), Times.Once); }
public async Task <BaseApiResponse> Update(UpdateCategoryRequest request) { request.CheckNotNull(nameof(request)); var command = new UpdateCategoryCommand( request.Name, request.Url, request.Thumb, request.Type, request.IsShow, request.Sort) { AggregateRootId = request.Id }; var result = await ExecuteCommandAsync(command); if (!result.IsSuccess()) { return(new BaseApiResponse { Code = 400, Message = "命令没有执行成功:{0}".FormatWith(result.GetErrorMessage()) }); } return(new BaseApiResponse()); }
public async Task <CategoryResponse> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { var category = (await _categoryRepository.FindAsync(c => c.Id == request.Id && !c.IsDeleting)).Data; var checkName = (await _categoryRepository.ExistAsync(x => x.Name == request.Name)).Data; if (checkName) { throw new DomainException(ErrorType.CategoryWithThisNameAlreadyExist); } if (category == null) { throw new DomainException(ErrorType.CategoryDoesNotExist); } category.Name = request.Name; _categoryRepository.Update(category); await _categoryRepository.SaveAsync(); return(category.ToResponse()); }
public async Task <OperationResult <string> > Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { try { var getCategory = await unitOfWork.CategoryRepository.GetCategoryByIdAsync(request.Id, cancellationToken); if (getCategory.Result != null) { getCategory.Result.SetProperties(request.Name, request.ParentId); var updateCategory = unitOfWork.CategoryRepository.UpdateCategory(getCategory.Result, cancellationToken); if (updateCategory.Success) { unitOfWork.CommitSaveChange(); return(OperationResult <string> .BuildSuccessResult(updateCategory.Result)); } } return(OperationResult <string> .BuildFailure(getCategory.ErrorMessage)); } catch (Exception ex) { return(OperationResult <string> .BuildFailure(ex.Message)); } }