Пример #1
0
        public IActionResult CreateCategory([FromBody] CategoryCreateDto categoryDto)
        {
            if (categoryDto == null)
            {
                return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) }));
            }

            if (_repo.CategoryExists(categoryDto.Title))
            {
                return(StatusCode(400, new { message = "Category with this title already exists" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) }));
            }

            var categoryObj = _mapper.Map <Category>(categoryDto);

            categoryObj.CreationDateTime = DateTime.Now;
            if (!_repo.CreateCategory(categoryObj))
            {
                return(StatusCode(500, new { message = $"Something went wrong when saving the record {categoryDto.Title}" }));
            }

            return(CreatedAtRoute("GetCategory", new { Version = HttpContext.GetRequestedApiVersion().ToString(), id = categoryObj.Id }, categoryObj));
        }
Пример #2
0
 public bool CategoryExists(long id)
 {
     return(_repository.CategoryExists(id));
 }