public IHttpActionResult Create(CreateCategoryBindingModel model)
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var userId = User.Identity.GetUserId();

                var houseHold = _db.Households
                                .FirstOrDefault(p => p.Id == model.HouseHoldId);

                if (houseHold == null)
                {
                    return(BadRequest("HouseHold doesn't exist"));
                }

                if (houseHold.CreatorId == userId ||
                    houseHold.Members.Any(p => p.Id == userId))
                {
                    var category = new Categories();
                    category.Name        = model.Name;
                    category.HouseholdId = model.HouseHoldId;

                    _db.Categories.Add(category);
                    _db.SaveChanges();

                    return(Ok());
                }
                else
                {
                    return(BadRequest("Not authorized"));
                }
            }
Esempio n. 2
0
        public CategoryDTO CreateCategory(CreateCategoryBindingModel model)
        {
            var checkCategory = this.FindCategoryByName(model);

            if (checkCategory != null)
            {
                if (checkCategory.IsDeleted == true)
                {
                    checkCategory.IsDeleted = false;
                    this.dbContext.Categories.Update(checkCategory);
                    this.dbContext.SaveChanges();

                    return(this.mapper.Map <CategoryDTO>(checkCategory));
                }
                else
                {
                    return(null);
                }
            }

            var category = this.CreateCategoryByName(model);

            this.dbContext.Categories.Add(category);
            this.dbContext.SaveChanges();

            return(this.mapper.Map <CategoryDTO>(category));
        }
Esempio n. 3
0
        private Category FindCategoryByName(CreateCategoryBindingModel model)
        {
            var category = this.dbContext.Categories
                           .FirstOrDefault(c => c.Name == model.Name);

            return(category);
        }
        public void FindAllCategoriesShouldReturnOnlyCategoriesWhereIsDeletedIsFalse()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"FindAllCategoriesShouldReturnOnlyCategoriesWhereIsDeletedIsFalse_Category_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var categoryName  = Guid.NewGuid().ToString();
            var categoryName1 = Guid.NewGuid().ToString();
            var categoryName2 = Guid.NewGuid().ToString();
            var model         = new CreateCategoryBindingModel {
                Name = categoryName
            };
            var model1 = new CreateCategoryBindingModel {
                Name = categoryName1
            };
            var model2 = new CreateCategoryBindingModel {
                Name = categoryName2
            };

            categoriesService.CreateCategory(model);
            categoriesService.CreateCategory(model1);
            categoriesService.CreateCategory(model2);
            categoriesService.RemoveCategory(model);
            categoriesService.RemoveCategory(model1);

            var categories = categoriesService.FindAllCategories();

            Assert.True(categories.Count == 1);
        }
        public IHttpActionResult Create(CreateCategoryBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId    = User.Identity.GetUserId();
            var household = Context
                            .Households
                            .FirstOrDefault(p => p.Id == model.HouseholdId);

            if (household == null || household.OwnerId != userId)
            {
                return(NotFound());
            }

            var category = Mapper.Map <Category>(model);

            category.DateCreated = DateTime.Now;

            Context.Categories.Add(category);
            Context.SaveChanges();

            var result = Mapper.Map <CategoryViewModel>(category);

            return(Ok(result));
        }
Esempio n. 6
0
        public IHttpActionResult PostCategories(CreateCategoryBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var creatorId = User.Identity.GetUserId();
            var household = db.HouseHolds.Where(p => p.Id == model.HouseHoldId).FirstOrDefault();

            if (household == null)
            {
                return(NotFound());
            }
            //if (household.CreatorId == creatorId ||
            //    household.HouseHoldUser.Any(p => p.Id == creatorId))
            //{
            var category = new Categories();

            category.Name        = model.Name;
            category.HouseHoldId = model.HouseHoldId;

            db.Categories.Add(category);
            db.SaveChanges();

            return(Ok());
            //}
            //else
            //{
            //    return BadRequest("Not authorized");
            //}
        }
        public void RemoveCategoryShouldSetIsDeletedToTrue()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"RemoveCategoryShouldSetIsDeletedToTrue_Category_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var categoryName  = Guid.NewGuid().ToString();
            var categoryName1 = Guid.NewGuid().ToString();
            var model         = new CreateCategoryBindingModel {
                Name = categoryName
            };
            var model1 = new CreateCategoryBindingModel {
                Name = categoryName1
            };

            categoriesService.CreateCategory(model);
            categoriesService.CreateCategory(model1);
            categoriesService.RemoveCategory(model);
            var category1 = categoriesService.CreateCategory(model);

            Assert.NotNull(category1);
            Assert.True(category1.IsDeleted == false);
        }
        public void CreateCategoryShouldSetDeletedToFalseIfProductAlreadyExistsAndIsDeletedIsTrue()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"CreateCategoryShouldCreateCategory_Category_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var categoryName  = Guid.NewGuid().ToString();
            var categoryName1 = Guid.NewGuid().ToString();
            var model         = new CreateCategoryBindingModel {
                Name = categoryName
            };
            var model1 = new CreateCategoryBindingModel {
                Name = categoryName1
            };

            categoriesService.CreateCategory(model);
            categoriesService.CreateCategory(model1);

            var categoryToRemove = dbContext.Categories.FirstOrDefault(x => x.Name == categoryName);

            categoryToRemove.IsDeleted = true;
            dbContext.Categories.Update(categoryToRemove);
            dbContext.SaveChanges();

            var category1 = categoriesService.CreateCategory(model);

            Assert.NotNull(category1);
            Assert.True(category1.IsDeleted == false);
        }
        public async Task CreateAsync(CreateCategoryBindingModel model)
        {
            Category category = this.Mapper.Map <Category>(model);

            await this.Repository.AddAsync(category);

            await this.Repository.SaveChangesAsync();
        }
Esempio n. 10
0
        private Category CreateCategoryByName(CreateCategoryBindingModel model)
        {
            var category = new Category {
                Name = model.Name
            };

            return(category);
        }
Esempio n. 11
0
        public Category FindCategoryByNameAndCheckIsDeleted(CreateCategoryBindingModel model)
        {
            var category = this.dbContext.Categories
                           .Where(x => x.IsDeleted == false)
                           .FirstOrDefault(c => c.Name == model.Name);

            return(category);
        }
        public async Task <bool> AddCategory(CreateCategoryBindingModel model)
        {
            var category = AutoMapper.Mapper.Map <Category>(model);

            await this.context.Categories.AddAsync(category);

            var result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
Esempio n. 13
0
        public ActionResult Create(CreateCategoryBindingModel model)
        {
            if (this.ModelState.IsValid)
            {
                this.categories.Create(model.Name);

                return(this.RedirectToAllCategories());
            }

            return(this.View(model));
        }
Esempio n. 14
0
        public IActionResult Delete(CreateCategoryBindingModel model)
        {
            var brand = this.categoryService.RemoveCategory(model);

            if (brand == null)
            {
                var creationErrorViewModel = this.errorService.CreateCreateionErrorViewModel(DeletionDoesntExistErrorMessage, HyperLinkForDoesntExistError);
                return(this.RedirectToAction("CreationError", "Error", creationErrorViewModel));
            }

            return(this.Redirect("/Home/Index"));
        }
Esempio n. 15
0
        public async Task <IActionResult> CreateCategory(CreateCategoryBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            await _adminManager.CreateCategoryAsync(model);

            TempData["Success"] = "You successfully created new category.";
            return(RedirectToAction("CreateCategory"));
        }
Esempio n. 16
0
        public async Task CreateCategoryAsync(CreateCategoryBindingModel model)
        {
            await _db.Categories
            .AddAsync(new Category()
            {
                Name   = model.Name,
                Gender = model.Gender,
                Type   = model.Group
            });

            await _db.SaveChangesAsync();
        }
        public async Task <IActionResult> Create(CreateCategoryBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            await this.categoryService.AddCategory(model);

            this.TempData["Success"] = $"Successully created {model.Name} category.";

            return(this.RedirectToAction("Create"));
        }
Esempio n. 18
0
        private Category FindCategoryByModelAndCheckIsDeleted(CreateCategoryBindingModel model)
        {
            var category = this.dbContext.Categories
                           .Where(x => x.IsDeleted == false)
                           .FirstOrDefault(c => c.Name == model.Name);

            if (this.dbContext.Categories.Where(x => x.IsDeleted == false).Count() == 1)
            {
                return(null);
            }

            return(category);
        }
Esempio n. 19
0
        public CategoryDTO RemoveCategory(CreateCategoryBindingModel model)
        {
            var category = this.FindCategoryByModelAndCheckIsDeleted(model);

            if (category == null)
            {
                return(null);
            }

            category.IsDeleted = true;

            this.dbContext.Update(category);
            this.dbContext.SaveChanges();

            return(this.mapper.Map <CategoryDTO>(category));
        }
Esempio n. 20
0
        public IActionResult Create(CreateCategoryBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var category = this.categoryService.CreateCategory(model);

            if (category == null)
            {
                var creationErrorViewModel = this.errorService.CreateCreateionErrorViewModel(CreationAlreadyExistsErrorMessage, HyperLinkForCreationError);
                return(this.RedirectToAction("CreationError", "Error", creationErrorViewModel));
            }

            return(this.Redirect("/Home/Index"));
        }
Esempio n. 21
0
        public async Task <bool> CreateCategory(CreateCategoryBindingModel model)
        {
            var category = DbContext.Categories.FirstOrDefault(c => c.Name == model.Name);

            if (category != null)
            {
                return(false);
            }

            category = new Category()
            {
                Name = model.Name
            };

            DbContext.Categories.Add(category);
            await DbContext.SaveChangesAsync();

            return(true);
        }
        public void RemoveCategoryShouldReturnNullIfCategoryIsNull()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"RRemoveCategoryShouldReturnNullIfCategoryIsNull_Category_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var categoryName = Guid.NewGuid().ToString();
            var model        = new CreateCategoryBindingModel {
                Name = categoryName
            };

            var category = categoriesService.RemoveCategory(model);

            Assert.Null(category);
        }
        public async Task <IActionResult> Create(CreateCategoryBindingModel input)
        {
            if (!this.ModelState.IsValid)
            {
                foreach (var modelState in this.ModelState.Values)
                {
                    foreach (var error in modelState.Errors)
                    {
                        this.TempData[ErrorNotification] = error.ErrorMessage;
                    }
                }

                return(this.View(input));
            }

            int id = await this.categoryService.CreateCategoryAsync(input.Description, input.ImageUrl, input.Name, input.Title);

            this.TempData[SuccessNotification] = string.Format(SuccessfullyCreatedCategory, input.Name);

            return(this.RedirectToRoute("category", new { id, name = input.Name.ToSlug() }));
        }
        public void CreateCategoryShouldCreateCategory()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"CreateCategoryShouldCreateCategory_Category_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var categoryName = Guid.NewGuid().ToString().Substring(0, 15);
            var model        = new CreateCategoryBindingModel {
                Name = categoryName
            };

            var category = categoriesService.CreateCategory(model);

            Assert.True(dbContext.Categories.Count() == 1);
            Assert.True(category.Name == categoryName);
        }
        public void CreateCategoryShouldntCreateCategoryAndShouldReturnNullIfAlreadyExists()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"CreateCategoryShouldntCreateCategoryAndShouldReturnNullIfAlreadyExists_Category_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var categoriesService = new CategoriesService(dbContext, mapper);

            var categoryName = Guid.NewGuid().ToString();
            var model        = new CreateCategoryBindingModel {
                Name = categoryName
            };

            categoriesService.CreateCategory(model);
            var category1 = categoriesService.CreateCategory(model);

            Assert.True(dbContext.Categories.Count() == 1);
            Assert.Null(category1);
        }
Esempio n. 26
0
        public async Task AddCategory_ShouldCreateCategorySuccessfully()
        {
            string onFalseErrorMessage = "Method returned false bool.";
            string onNullErrorMessage  = "Category was not stored in the database.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var categoryService = new CategoryService(context);

            CreateCategoryBindingModel model =
                new CreateCategoryBindingModel
            {
                Name = "TestCategory",
            };

            var methodResult = await categoryService.AddCategory(model);

            Assert.True(methodResult, onFalseErrorMessage);

            var categoryFromDb = context.Categories.FirstOrDefaultAsync();

            AssertExtensions.NotNullWithMessage(categoryFromDb, onNullErrorMessage);
        }