Exemplo n.º 1
0
 public void Add(CategoryCreateModel category)
 {
     if (!_typesRepository.Add(new Category(category)))
     {
         throw new ApplicationException();
     }
 }
 public void CreateCategory(CategoryCreateModel model)
 {
     using (UnitOfWork unitOfWork = _unitOfWorkFactory.Create())
     {
         unitOfWork.Categories.Create(Mapper.Map <Category>(model));
     }
 }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([FromBody] CategoryCreateModel category)
        {
            if (ModelState.IsValid)
            {
                CategoryResultModel response = await this.categoryService.CreateCategory(category.Name, category.Description);

                if (!response.Success)
                {
                    FailedResponseModel badResponse = new FailedResponseModel()
                    {
                        Errors = response.Errors
                    };

                    return(BadRequest(badResponse));
                }

                CategorySuccessResponseModel successResponse = new CategorySuccessResponseModel()
                {
                    Name = response.Name
                };

                return(Ok(successResponse));
            }

            return(BadRequest(new FailedResponseModel {
                Errors = ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage))
            }));
        }
Exemplo n.º 4
0
        public async Task Create_ValidModel_RedirectsToIndex()
        {
            // Arrange
            var testCategoryToCreate = new CategoryCreateModel
            {
                CategoryName = "Test category for create",
                Description  = "Test category for create description"
            };
            var testCategory = new Category
            {
                CategoryId   = 27,
                CategoryName = "Test category for create",
                Description  = "Test category for create description"
            };
            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(x => x.Map <CategoryCreateModel, Category>(testCategoryToCreate))
            .Returns(testCategory);
            var categoriesServiceMock = new Mock <ICategoriesService>();

            categoriesServiceMock.Setup(x => x.CreateCategoryAsync(testCategory))
            .ReturnsAsync(testCategory);
            var controller = new CategoriesController(categoriesServiceMock.Object, mapperMock.Object);

            // Act
            var result = await controller.Create(testCategoryToCreate);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
        }
Exemplo n.º 5
0
 public static Category toEntity(this CategoryCreateModel categoryCreate)
 {
     return(new Category()
     {
         Categoryname = categoryCreate.categoryName,
         Id = categoryCreate.categoryID.Value
     });
 }
Exemplo n.º 6
0
        public void CreateCategory(CategoryCreateModel model)
        {
            using (var unitOfWork = _unitOfWorkFactory.Create())
            {
                var category = Mapper.Map <Category>(model);

                unitOfWork.Categories.Create(category);
            }
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create(CategoryCreateModel category)
        {
            var model = new Category {
                CategoryId = Guid.NewGuid().ToString(),
                name       = category.name
            };
            await _categoryRepo.add(model);

            return(Ok());
        }
        public async Task <IActionResult> Post([FromBody] CategoryCreateModel categoryCreateModel)
        {
            if (await _categoryService.Exists(categoryCreateModel.Title))
            {
                return(Conflict("Such category already exists"));
            }
            CategoryDto categoryCreateDto  = _mapper.Map <CategoryDto>(categoryCreateModel);
            CategoryDto createdCategoryDto = await _categoryService.CreateCategory(categoryCreateDto);

            return(CreatedAtAction(nameof(Get), new { id = createdCategoryDto.CategoryId }, createdCategoryDto));
        }
        public async Task <IActionResult> Create(CategoryCreateModel category)
        {
            if (ModelState.IsValid)
            {
                await _categoriesService.CreateCategoryAsync(_mapper.Map <CategoryCreateModel, Category>(category));

                return(RedirectToAction(nameof(Index)));
            }

            return(View(_mapper.Map <CategoryViewModel>(category)));
        }
Exemplo n.º 10
0
        public ActionResult <CategoryReadModel> Create(CategoryCreateModel category)
        {
            var categoryModel = _mapper.Map <Category>(category);

            _categoryService.Create(categoryModel);
            _categoryService.SaveChanges();

            var categoryReadModel = _mapper.Map <CategoryReadModel>(categoryModel);

            return(CreatedAtRoute(nameof(GetById), new { Id = categoryReadModel.Id }, categoryReadModel));
            //return Ok(categoryReadModel);
        }
        public async Task <IActionResult> UpdateCategory([FromBody] CategoryCreateModel createCategoryDtoIn)
        {
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}

            var model    = mapper.Map <CategoryModel>(createCategoryDtoIn);
            var category = await categoryService.UpdateCategory(model);

            return(Ok(category));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Create(CategoryCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var id = Guid.NewGuid();
            await _commands.Send(new CreateCategoryCommand { Id = id, Name = model.Name });

            return(RedirectToAction("ForCategoryCreation", "Wait", new { Id = id }));
        }
        public async Task <ActionResult> Edit(Guid id)
        {
            var category = await AuctionFacade.GetCategoryAsync(id);

            var model = new CategoryCreateModel
            {
                Category       = category,
                ParentCategory = category.Parent == null ? "" : category.Parent.Name
            };

            return(View(model));
        }
        public async Task <ActionResult> Create(CategoryCreateModel model)
        {
            try
            {
                await AuctionFacade.CreateCategoryWithinParentCategoryNameAsync(model.Category, model.ParentCategory);

                return(RedirectToAction("Index"));
            } catch
            {
                return(View());
            }
        }
        public IActionResult Create(CategoryCreateModel model)
        {
            try
            {
                _categoryService.CreateCategory(model);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
        public async Task <IActionResult> Create([FromBody] CategoryCreateModel subcategory)
        {
            if (string.IsNullOrWhiteSpace(subcategory.Name))
            {
                return(BadRequest(ModelConstants.InvalidCategoryName));
            }

            return(await this.Execute(true, false, async() =>
            {
                string subcategoryId = await this.subcategories.Create(subcategory.Name);

                return this.Ok(new { subcategoryId = subcategoryId });
            }));
        }
        public async Task <IActionResult> Update(string id, [FromBody] CategoryCreateModel subcategory)
        {
            if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(subcategory.Name))
            {
                return(BadRequest(ModelConstants.InvalidCategoryName));
            }

            return(await this.Execute(true, false, async() =>
            {
                await this.subcategories.Update(id, subcategory.Name);

                return this.Ok();
            }));
        }
        public async Task <ActionResult> Edit(Guid id, CategoryCreateModel model)
        {
            try
            {
                if (model.ParentCategory != null)
                {
                    model.Category.ParentId =
                        (await AuctionFacade.GetCategoryIdsByNameAsync(model.ParentCategory)).SingleOrDefault();
                }
                await AuctionFacade.EditCategory(model.Category);

                return(RedirectToAction("Index"));
            } catch
            {
                return(View());
            }
        }
Exemplo n.º 19
0
        public async Task <IActionResult> Update(string id, [FromBody] CategoryCreateModel category)
        {
            var s = "";

            System.Console.WriteLine(s);

            if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(category.Name))
            {
                return(BadRequest(ModelConstants.InvalidCategoryName));
            }

            return(await this.Execute(true, false, async() =>
            {
                await this.categories.UpdateName(id, category.Name);

                return this.Ok();
            }));
        }
Exemplo n.º 20
0
        public IActionResult createCategory([FromBody] CategoryCreateModel categoryCreate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
                // bookRequest.Authors = _bookRepository.GetAll();
            }

            if (_categoryRepository.GetAll().Where(x => x.Categoryname == categoryCreate.categoryName).Any())
            {
                return(Content("The Book Title is in Database Already!"));
            }
            else
            {
                _categoryRepository.Create(categoryCreate.toEntity());
                return(Ok(categoryCreate));
            }
        }
Exemplo n.º 21
0
        public ActionResult Create(CategoryCreateModel createModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            try
            {
                _categoryService.Add(createModel);
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(500, e.Message));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 22
0
        public ActionResult CreateAction(CategoryCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData["danger"] = "Error";
                return(RedirectToAction("Create", "Category"));
            }

            Category cat = new Category
            {
                Name = model.Name,
                Id   = Guid.NewGuid()
            };

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

            TempData["success"] = "Category created";
            return(RedirectToAction("Index", "Category"));
        }
Exemplo n.º 23
0
        public async Task <IActionResult> AddCategory([FromBody] CategoryCreateModel createCategoryModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                var category = Mapper.Map <CategoryCreateModel, Category>(createCategoryModel);
                category.Status = Status.Active;
                await _categoryService.AddCategory(category);

                return(Ok(ModelFactory.CreateModel(category)));
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }
Exemplo n.º 24
0
        public bool Add(CategoryCreateModel entity)
        {
            Guid?parent = entity.ParentId;

            if (parent == Guid.Empty)
            {
                parent = null;
            }

            Category category = new Category
            {
                Type       = entity.Type,
                Name       = entity.Name,
                ParentId   = parent,
                CreatedBy  = entity.CreatedBy,
                CreatedAt  = entity.CreatedAt,
                ModifiedAt = entity.ModifiedAt,
                ModifiedBy = entity.ModifiedBy,
                CountryId  = entity.CountryId
            };


            return(_repository.Add(category));
        }
        public IActionResult Create([FromBody] CategoryCreateModel entity)
        {
            if (ModelState.IsValid)
            {
                string currentUser = HttpContext?.User?.Identity?.Name;
                if (!String.IsNullOrEmpty(currentUser))
                {
                    try
                    {
                        AuditedEntityMapper <CategoryCreateModel> .FillCreateAuditedEntityFields(entity, currentUser, CountryId);

                        bool statusResult = _categoryService.Add(entity);
                        if (statusResult)
                        {
                            return(Ok().WithSuccess(LOCALIZATION_SUCCESS_DEFAULT));
                        }
                        else
                        {
                            return(NotFound().WithError(LOCALIZATION_ERROR_NOT_FOUND));
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, ex.Message);
                        return(RedirectToAction(nameof(Index)).WithError(ex.Message));
                    }
                }
                else
                {
                    _logger.LogError(LOCALIZATION_ERROR_USER_MUST_LOGIN);
                    return(NotFound().WithError(LOCALIZATION_ERROR_USER_MUST_LOGIN));
                }
            }

            return(View(entity));
        }
Exemplo n.º 26
0
 public async Task <ActionResult> CreateAsync(CategoryCreateModel model) => View(await _dbContext.AddCategoryAsync(model));
Exemplo n.º 27
0
 public IActionResult CreateCategory(CategoryCreateModel model)
 {
     _categoryService.CreateCategory(model);
     return(RedirectToAction("Index"));
 }
Exemplo n.º 28
0
        public async Task <IActionResult> AddCategory(CategoryCreateModel model)
        {
            await _categoryService.AddCategoryAsync(model);

            return(RedirectToAction("IndexСategoriesAdmin", "Category"));
        }
Exemplo n.º 29
0
        public async Task <IActionResult> Create(CategoryCreateModel inputModel)
        {
            var createdId = await service.CreateAsync(inputModel);

            return(Ok(new { id = createdId }));
        }
Exemplo n.º 30
0
        public async Task <IActionResult> Update([FromRoute] long id, CategoryCreateModel updateModel)
        {
            await service.UpdateAsync(id, updateModel);

            return(Ok());
        }