public static CategoryRequestModel Create() { var model = new CategoryRequestModel { Name = "Test Category Name1234124" }; return model; }
public async Task Create(CategoryRequestModel request) { var category = _mapper.Map <CategoryEntity>(request); if (category.Invalid) { _notificationService.AddEntityNotification(category.ValidationResult); return; } await _categoryRepository.Create(category); }
public async Task <IActionResult> UpdateAsync(int groupId, int categoryId, [FromBody] CategoryRequestModel category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await categoriesService.UpdateAsync(User, groupId, categoryId, category); return(HandlePutResult(result)); }
public async Task <IActionResult> Create([FromBody] CategoryRequestModel model) { if (await categoryService.CategoryExistsAsync(model.Name)) { return(BadRequest("Category already exists.")); } int categoryId = await categoryService.CreateCategoryAsync(model.Name); return(Ok(categoryId)); }
public async Task <IActionResult> CreateAsync(int groupId, [FromBody] CategoryRequestModel category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await categoriesService.CreateAsync(User, groupId, category); return(HandlePostResult($"groups/{groupId}/categories", result)); }
public async Task <IActionResult> Put(int id, [FromBody] CategoryRequestModel model) { var exists = await this.categories.ExistsAsync(id); if (!exists) { return(BadRequest()); } return(this.OkOrNotFound(await this.categories.EditAsync(id, model.Name))); }
public async Task <IActionResult> Edit(int id, [FromBody] CategoryRequestModel model) { if (await categoryService.CategoryExistsAsync(model.Name)) { return(BadRequest("Category already exists.")); } CategoryDto category = await categoryService.EditCategoryAsync(id, model.Name); return(Ok(category)); }
public IActionResult Post([FromBody] CategoryRequestModel model) { if (this.category.Duplicate(model.Name)) { return(this.BadRequest("Duplicate category!")); } var id = this.category.Create(model.Name); return(this.Ok(id)); }
public async Task <IActionResult> Post([FromBody] CategoryRequestModel model) { var exists = await this.categories.ExistsAsync(model.Name); if (exists) { return(BadRequest($@"Category ""{model.Name}"" already exists.")); } return(this.OkOrNotFound(await this.categories.CreateAsync(model.Name))); }
public void Post(CategoryRequestModel model) { if (model.Id > 0) { categoryService.Update(model.ToDto()); } else { categoryService.Add(model.ToDto()); } }
public async Task <ActionResult <CategoryResponseModel> > Edit(CategoryRequestModel request) { await this.categoriesService.Edit(request.CategoryId, new Category { Name = request.Name, Description = request.Description, ImageUrl = string.Empty, }); return(new CategoryResponseModel { }); }
public IHttpActionResult Create(CategoryRequestModel category) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var categoryToAdd = Mapper.Map <Category>(category); var addedCategoryId = this.categoriesServices.AddCategory(categoryToAdd); return(this.Ok(addedCategoryId)); }
public async Task ShouldFailCreateWhenDescriptionInvalid() { var category = new CategoryRequestModel() { Name = "Asian", Color = "red", Description = "" }; var validationErrorsMessages = await GetErrorsOnCreate(category); Assert.True(validationErrorsMessages[0]._Message == ErrorMessages.ErrorDescription); }
public async Task ShouldFailUpdateWhen() { var model = new CategoryRequestModel() { Name = "Salad", Color = "red", Description = "Spice food" }; var validationErrorsMessages = await GetErrorsOnUpdate(model); Assert.True(validationErrorsMessages[0]._Key == "CategoryEntityNotFound"); }
public async Task <IActionResult> Post([FromBody] CategoryRequestModel model) { var categoryExist = await this.categoryService.Exist(model.Name); if (categoryExist) { return(BadRequest("That Category already exists")); } var categoryId = this.categoryService.Create(model.Name); return(Ok(categoryId)); }
public async Task <IActionResult> Edit(int id, [FromBody] CategoryRequestModel model) { var categoryExist = await this.categoryService.Exist(model.Name); if (categoryExist) { return(BadRequest("That Category already exists")); } var success = this.categoryService.Edit(id, model.Name); return(Ok(id)); }
public async Task <IActionResult> Put([FromBody] CategoryRequestModel category) { var result = await categoryService.Update(category); if (result.HasError) { return(BadRequest(result.Errors)); } else { return(Ok("Category successfully updated.")); } }
public IActionResult Post([FromBody] CategoryRequestModel categoryRequest) { Category category = _mapper.Map <CategoryRequestModel, Category>(categoryRequest); ValidationResponse <Category> validationResponse = _categoryService.Add(category); if (validationResponse.Status == ValidationStatus.Failed) { return(BadRequest(validationResponse.Message)); } CategoryResponseModel categoryResponse = _mapper.Map <Category, CategoryResponseModel>(validationResponse.ResponseData); return(CreatedAtAction("Get", new { id = categoryResponse.Id }, categoryResponse)); }
public Task <BaseResponseModel <bool> > Update(CategoryRequestModel model) { var config = new MapperConfiguration(cfg => { cfg.CreateMap <CategoryRequestModel, Category>(); }); IMapper iMapper = config.CreateMapper(); var category = iMapper.Map <CategoryRequestModel, Category>(model); var result = repository.Update(category); return(result); }
public async Task <JsonResult> CreateCategory(CategoryRequestModel requestModel) { if (CurrentUserEntity.RoleId != RolesContext.Administrator && CurrentUserEntity.Id != requestModel.PartnerId) { return(new JsonResult(new { statusCode = 401, data = "Unauthorized" })); } if (string.IsNullOrEmpty(requestModel.CategoryName)) { return(new JsonResult(new { statusCode = 400, message = "Bad Request" })); } var category = await _categoryService.CreateCategory(requestModel); return(new JsonResult(new { statusCode = 200, message = "OK", data = category })); }
public async Task Update(Guid id, CategoryRequestModel request) { var category = await _categoryRepository.GetById(id); if (VerifyEntityExistence <CategoryEntity>(category, _notificationService)) { return; } category.Update(request.Name, request.Color, request.Description); if (category.Invalid) { _notificationService.AddEntityNotification(category.ValidationResult); return; } await _categoryRepository.Update(id, category); }
public async Task <IActionResult> Post([FromBody] CategoryRequestModel model) { model.Name = model.Name.Trim(); var categoryNameExists = await this.categoryService.Exists(model.Name); if (categoryNameExists) { this.ModelState.AddModelError(nameof(CategoryRequestModel.Name), "Category name already exists."); return(BadRequest(this.ModelState)); } var id = await this.categoryService.Create(model.Name); return(this.Ok(id)); }
public async Task <CategoryResponseModel> CreateCategory(CategoryRequestModel requestModel) { if (requestModel == null) { throw new ArgumentNullException(); } var categoryResponse = new CategoryResponseModel() { CategoryName = requestModel.CategoryName, CreateDate = DateTime.Now }; categoryResponse.CategoryId = await _db.CategoriesRequest.CategoriesRequest.CreateAsync(requestModel); return(categoryResponse); }
public async Task <IActionResult> Get() { var result = _categoryService.GetPage(1, 20, true); var rm = new CategoryRequestModel { [nameof(CategoryRequestModel.ShowOnMenu)] = true, [nameof(CategoryRequestModel.Slug)] = "turkiye" }; // rm[nameof(rm.Status)] = 2; var ct = await _categoryService.FirstOrDefaultAsync(rm); return(Ok(result)); }
public IHttpActionResult Add(CategoryRequestModel categoryModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (this.categories.GetAll().Any(c => c.Name == categoryModel.Name)) { return(this.BadRequest("There is a category with this name!")); } var category = this.Mapper.Map <Category>(categoryModel); this.categories.Add(category); this.categories.Save(); return(this.Ok()); }
public async Task <IActionResult> Post(CategoryRequestModel model) { var nameExists = await this.categoryService.ExistsAsync(model.Name); if (nameExists) { this.ModelState.AddModelError(nameof(CategoryRequestModel.Name), WebConstants.CategoryNameExistsMsg); return(this.BadRequest(this.ModelState)); } var id = await this.categoryService.CreateAsync(model.Name); if (id < 0) { return(this.NotFound(WebConstants.CategoryNotCreatedMsg)); } return(this.CreatedAtAction(nameof(Get), new { id }, model)); }
public async Task ShouldCreateCategory() { var model = new CategoryRequestModel() { Name = "Asian", Color = "red", Description = "Spice food" }; await _categoryService .Create(model); await _categoriRepository .Received(1) .Create(Arg.Is <CategoryEntity>(category => category.Name == "Asian" && category.Color == "red" && category.Description == "Spice food" && category.IsEnabled == true)); }
public void PutCategoryAlreadyExistTest() { Category category = GetCategory(); CategoryRequestModel categoryRequest = GetCategoryRequestModel(); ValidationResponse <Category> validationResponse = GetFailedValidationResponse(); _mockMapper .Setup(mapper => mapper.Map <CategoryRequestModel, Category>(categoryRequest)) .Returns(category); _mockService .Setup(serv => serv.Update(category)) .Returns(validationResponse); IActionResult actionResult = _controller.Put(_categoryId, categoryRequest); BadRequestObjectResult actual = (BadRequestObjectResult)actionResult; Assert.Equal(StatusCodes.Status400BadRequest, actual.StatusCode); }
public IHttpActionResult Post([FromBody] CategoryRequestModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (this.data.Categories .Find(x => x.Name.ToLowerInvariant() == model.Name.ToLowerInvariant()).FirstOrDefault() != null) { return(this.BadRequest("A category with that name exists already!")); } var categoryToAdd = Mapper.Map <Category>(model); this.data.Categories.Add(categoryToAdd); this.data.SaveChanges(); return(this.Ok(categoryToAdd.Id)); }
public async Task <CategoryDto> FirstOrDefaultAsync(CategoryRequestModel requestModel) { Expression <Func <Category, bool> > expression = null; if (requestModel.IsChanged(nameof(requestModel.ShowOnMenu)) && requestModel.ShowOnMenu) { expression = (p) => p.ShowOnMenu; } if (requestModel.IsChanged(nameof(requestModel.Slug)) && !string.IsNullOrEmpty(requestModel.Slug)) { expression = expression.And((p) => p.Slug == requestModel.Slug); } if (requestModel.IsChanged(nameof(requestModel.Status)) && requestModel.Status > 0) { expression = expression.And((p) => p.Status == requestModel.Status); } var category = await _categoryRepository.FirstOrDefaultAsync(expression); return(await Task.FromResult(AutoMapper.Mapper.Map <CategoryDto>(category))); }
public async Task <IActionResult> Put(int id, [FromBody] CategoryRequestModel model) { if (!await this.categories.CategoryIdExistsAsync(id)) { return(this.BadRequest("Fault! Such 'categoryId' does not exist in database.")); } if (!await this.categories.CategoryNameExistsAsync(model.Name)) { return(this.BadRequest("Fault! Category with this name already exist in database.")); } var result = await this.categories.EditAsync(id, model.Name); if (result == 0) { return(this.BadRequest("Fault!")); } return(this.Ok($"Category with Id={result} was set to '{model.Name}'.")); }