public async Task <ActionResult <Category> > Post(Category category)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Subcategory subcategory = new Subcategory();

                    await _categoryRepository.AddAsync(category);

                    subcategory.IdCategory = category.IdCategory;
                    subcategory.Name       = "Sem subcategoria";
                    subcategory.DateCreate = DateTime.Now;

                    await _subcategoryRepository.AddAsync(subcategory);

                    return(category);
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #2
0
        public async Task <Subcategory> AddAsync(Subcategory subcategory)
        {
            Guard.Against.NullInvalid(subcategory, Resources.Subcategory_Invalid_ErrorMessage);
            Guard.Against.NullString(subcategory.Name, Resources.Subcategory_InvalidName_ErrorMessage);
            Guard.Against.LessOne(subcategory.CategoryId, Resources.Category_InvalidId_ErrorMessage);

            if (!await _categoryRepository.ExistAsync(subcategory.CategoryId))
            {
                var message = string.Format(Resources.Category_NotFoundById_ErrorMessage, subcategory.CategoryId);

                throw new RutrackerException(message, ExceptionEventTypes.InvalidParameters);
            }

            if (await _subcategoryRepository.ExistAsync(x => x.Name == subcategory.Name))
            {
                var message = string.Format(Resources.Subcategory_AlreadyExistsName_ErrorMessage, subcategory.Name);

                throw new RutrackerException(message, ExceptionEventTypes.InvalidParameters);
            }

            subcategory.AddedDate = _dateService.Now();

            var result = await _subcategoryRepository.AddAsync(subcategory);

            await _unitOfWork.SaveChangesAsync();

            return(result);
        }
Example #3
0
        public async Task <ActionResult <Subcategory> > Post(Subcategory subcategory)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    await _subcategoryRepository.AddAsync(subcategory);

                    return(subcategory);
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }