public async Task <IActionResult> PutCategory(int id, [FromBody] CategoryForCreationDTO categoryForCreation) { var category = _mapper.Map <Category>(categoryForCreation); category.Id = id; _unitOfWork.Repository <Category>().Update(category); try { await _unitOfWork.Complete(); } catch (DbUpdateConcurrencyException) { if (!await _unitOfWork.Repository <Category>().Contains(d => category.Id == id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult> Create(CategoryForCreationDTO model) { var result = await this.categories.CreateAsync(model); if (result.Failure) { return(BadRequest(result.Error)); } return(Created(nameof(this.Create), result.Data)); }
public async Task <ActionResult <Category> > PostCategory([FromBody] CategoryForCreationDTO categoryForCreation) { var category = _mapper.Map <Category>(categoryForCreation); await _unitOfWork.Repository <Category>().Add(category); await _unitOfWork.Complete(); // mapp to a returnable obj var categoryToReturn = _mapper.Map <CategoryToReturnDTO>(category); return(new CreatedAtRouteResult(nameof(GetCategory), new { category.Id }, categoryToReturn)); }
public IHttpActionResult CreateCategory(CategoryForCreationDTO model) { var category = new Category { CategoryName = model.CategoryName, }; if (ModelState.IsValid) { _categoryRepository.Add(category); return(Ok()); } return(BadRequest()); }
public async Task <Result> UpdateAsync(int id, CategoryForCreationDTO model) { var categoryAlreadyExists = await CategoryNameIsTakenAsync(model.Name); if (categoryAlreadyExists) { return("Категорията вече съществува."); } var category = await this.data.Categories .Where(c => c.Id == id) .FirstOrDefaultAsync(); this.mapper.Map(model, category); return(true); }
public async Task <Result <int> > CreateAsync(CategoryForCreationDTO model) { var categoryAlreadyExists = await CategoryNameIsTakenAsync(model.Name); if (categoryAlreadyExists) { return("Категорията вече съществува."); } var category = this.mapper.Map <Category>(model); await this.data.AddAsync(category); await this.data.SaveChangesAsync(); return(category.Id); }
public IActionResult CreateCategory(CategoryForCreationDTO model) { if (!ModelState.IsValid) { return(BadRequest(Messages.ModelNullOrEmpty)); } var category = new Category { CategoryName = model.CategoryName, DateCreated = DateTime.Now, IsDeleted = false }; var result = _categoryService.Create(category); if (result.Success) { return(Ok(result.Message)); } return(BadRequest(result.Message)); }
public async Task <IActionResult> CreateCategory([FromBody] CategoryForCreationDTO category) { if (category.ParentCategoryId != null) { var categoryExisted = await _categoryService.GetCategoryAsync((Guid)category.ParentCategoryId, false); if (categoryExisted == null) { _loggerManager.LogError($"Category with id: {category.ParentCategoryId} does not exist in the database"); return(NotFound("Parent ID does not existed")); } } var categoryEntity = _mapper.Map <Category>(category); _categoryService.CreateCategory(categoryEntity); await _categoryService.SaveAsync(); var categoryToReturn = _mapper.Map <CategoryDTO>(categoryEntity); return(CreatedAtRoute("CategoryById", new { categoryId = categoryToReturn.Id }, categoryToReturn)); }
public async Task <ActionResult> Update(int id, CategoryForCreationDTO model) { return(await this.categories .UpdateAsync(id, model) .ToActionResult()); }