Esempio n. 1
0
        public ActionResult Save(CategoryFormModel form)
        {
            if (ModelState.IsValid)
            {
                //TODO: Create Automapper form => command
                var command = new CreateOrUpdateCategoryCommand()
                {
                    CategoryId  = form.CategoryId,
                    Name        = form.Name,
                    Description = form.Description
                };

                IEnumerable <ValidationResult> errors = commandBus.Validate(command);
                ModelState.AddModelErrors(errors);
                if (ModelState.IsValid)
                {
                    var result = commandBus.Submit(command);
                    if (result.Success)
                    {
                        return(RedirectToAction("Index"));
                    }
                }
            }

            if (form.CategoryId == 0)
            {
                return(View("Create", form));
            }
            else
            {
                return(View("Edit", form));
            }
        }
 // POST /api/categories
 public HttpResponseMessage Post(CategoryModel category)
 {
     if (ModelState.IsValid)
     {
         var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description);
         var result  = commandBus.Submit(command);
         if (result.Success)
         {
             var categoryWithExpense = new CategoryWithExpense {
                 CategoryName = category.CategoryName, Description = category.Description, TotalExpenses = 0
             };
             var    response = Request.CreateResponse <CategoryModel>(HttpStatusCode.Created, category);
             string uri      = Url.Route(null, new { id = category.CategoryId });
             response.Headers.Location = new Uri(Request.RequestUri, uri);
             return(response);
         }
     }
     else
     {
         var errors = new Dictionary <string, IEnumerable <string> >();
         foreach (var keyValue in ModelState)
         {
             errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage);
         }
         return(Request.CreateResponse(HttpStatusCode.BadRequest, errors));
     }
     throw new HttpResponseException(HttpStatusCode.BadRequest);
 }
Esempio n. 3
0
        public void CategoryCreateTest()
        {
            using (var lifetime = container.BeginLifetimeScope())
            {
                ICategoryRepository categoryRepository = lifetime.Resolve <ICategoryRepository>();
                DefaultCommandBus   commandBus         = lifetime.Resolve <DefaultCommandBus>();
                IMappingEngine      mapper             = lifetime.Resolve <IMappingEngine>();

                Category category = new Category()
                {
                    Name        = "Test Category",
                    Description = "This is a test category"
                };

                CreateOrUpdateCategoryCommand command = mapper.Map <CreateOrUpdateCategoryCommand>(category);
                IValidationHandler <CreateOrUpdateCategoryCommand> validationHandler = lifetime.Resolve <IValidationHandler <CreateOrUpdateCategoryCommand> >();
                IEnumerable <ValidationResult> validations = commandBus.Validate(command, validationHandler);
                foreach (var val in validations)
                {
                    Assert.IsNull(val, "Error: Category creation did not validate " + val.Message);
                }
                ICommandHandler <CreateOrUpdateCategoryCommand> commnadHandler = lifetime.Resolve <ICommandHandler <CreateOrUpdateCategoryCommand> >();
                ICommandResult result = commandBus.Submit(command, commnadHandler);
                Assert.IsNotNull(result, "Error: Category was not created by commandBus");
                Assert.IsTrue(result.Success, "Error: Category was not created by commandBus");
            }
        }
Esempio n. 4
0
        private void ValidateCreateOrUpdateCategoryCommand(string id, CreateOrUpdateCategoryCommand command)
        {
            var categoryBySlug = _categoryRepository.FirstOrDefault(it => it.Code == command.Code && id != it.Id);

            Contract.Assert(categoryBySlug == null);
            var categoryByCode = _categoryRepository.FirstOrDefault(it => it.Slug == command.Slug && id != it.Id);

            Contract.Assert(categoryByCode == null);
        }
 // PUT /api/categories/5
 public HttpResponseMessage Put(int id, CategoryModel category)
 {
     if (ModelState.IsValid)
     {
         var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description);
         var result  = commandBus.Submit(command);
         return(new HttpResponseMessage(HttpStatusCode.NoContent));
     }
     throw new HttpResponseException(HttpStatusCode.BadRequest);
 }
Esempio n. 6
0
        public void Handle_RequestHasCategoryIdAndCategoryIdNotFound_ShouldThrowArgException()
        {
            // Arrange
            var handler = new CreateOrUpdateCategoryCommandHandler(GetCategoryRepository().Object, GetMapper());
            var request = new CreateOrUpdateCategoryCommand
            {
                Id = 2
            };

            // Act
            var del = new AsyncTestDelegate(() => handler.Handle(request, CancellationToken.None));

            // Assert
            Assert.ThrowsAsync <System.ArgumentException>(del);
        }
Esempio n. 7
0
        public async Task Handle_NewCategory_ShouldCreateCategory()
        {
            // Arrange
            var categoryRepositoryMock = GetCategoryRepository();

            var handler = new CreateOrUpdateCategoryCommandHandler(categoryRepositoryMock.Object, GetMapper());
            var request = new CreateOrUpdateCategoryCommand
            {
                Name   = "Work",
                UserId = 1
            };

            // Act
            var actual = await handler.Handle(request, CancellationToken.None);

            // Assert
            categoryRepositoryMock.Verify(x => x.SaveCategory(It.IsAny <Category>()), Times.Once);
            Assert.AreEqual("Work", actual.Name);
        }
 // POST /api/categories
 public HttpResponseMessage <CategoryWithExpense> Post(CategoryModel category)
 {
     if (ModelState.IsValid)
     {
         var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description);
         var result  = commandBus.Submit(command);
         if (result.Success)
         {
             var categoryWithExpense = new CategoryWithExpense {
                 CategoryName = category.CategoryName, Description = category.Description, TotalExpenses = 0
             };
             var    response = new HttpResponseMessage <CategoryWithExpense>(categoryWithExpense, HttpStatusCode.Created);
             string uri      = Url.Route(null, new { id = category.CategoryId });
             response.Headers.Location = new Uri(Request.RequestUri, uri);
             return(response);
         }
     }
     throw new HttpResponseException(HttpStatusCode.BadRequest);
 }
Esempio n. 9
0
 // POST /api/category
 public HttpResponseMessage Post(CategoryModel category)
 {
     if (ModelState.IsValid)
     {
         var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description);
         var result  = commandBus.Submit(command);
         if (result.Success)
         {
             var    response = Request.CreateResponse(HttpStatusCode.Created, category);
             string uri      = Url.Link("DefaultApi", new { id = category.CategoryId });
             response.Headers.Location = new Uri(uri);
             return(response);
         }
     }
     else
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
     }
     throw new HttpResponseException(HttpStatusCode.BadRequest);
 }
Esempio n. 10
0
        public void CategoryUpdateTest()
        {
            Category category;

            using (var lifetime = container.BeginLifetimeScope())
            {
                ICategoryRepository categoryRepository = lifetime.Resolve <ICategoryRepository>();
                DefaultCommandBus   commandBus         = lifetime.Resolve <DefaultCommandBus>();
                IMappingEngine      mapper             = lifetime.Resolve <IMappingEngine>();

                category = categoryRepository.Get(c => c.Name == "Test Category");
                Assert.IsNotNull(category, "Error: Category was now found.");

                category.Name = "Updated Test Category";

                CreateOrUpdateCategoryCommand command = mapper.Map <CreateOrUpdateCategoryCommand>(category);
                IValidationHandler <CreateOrUpdateCategoryCommand> validationHandler = lifetime.Resolve <IValidationHandler <CreateOrUpdateCategoryCommand> >();
                IEnumerable <ValidationResult> validations = commandBus.Validate(command, validationHandler);

                foreach (var val in validations)
                {
                    Assert.IsNull(val, "Error: Category creation did not validate " + val.Message);
                }
            }

            using (var lifetime = container.BeginLifetimeScope())
            {
                DefaultCommandBus commandBus = lifetime.Resolve <DefaultCommandBus>();
                IMappingEngine    mapper     = lifetime.Resolve <IMappingEngine>();

                CreateOrUpdateCategoryCommand command = mapper.Map <CreateOrUpdateCategoryCommand>(category);

                ICommandHandler <CreateOrUpdateCategoryCommand> commnadHandler = lifetime.Resolve <ICommandHandler <CreateOrUpdateCategoryCommand> >();
                ICommandResult result = commandBus.Submit(command, commnadHandler);
                Assert.IsNotNull(result, "Error: Category was not updated by CommandBus");
                Assert.IsTrue(result.Success, "Error: Provincia was not updated by CommandBus");
            }
        }