Exemplo n.º 1
0
        public IActionResult CreateCategory([FromBody] NewCategoryDto category)
        {
            try
            {
                if (category == null)
                {
                    //_logger.LogError("Category object sent from client is null.");
                    return(BadRequest("Category object is null"));
                }

                if (!ModelState.IsValid)
                {
                    //_logger.LogError("Invalid category object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var categoryEntity = _mapper.Map <Category>(category);

                _repository.Category.CreateCategory(categoryEntity);
                _repository.Save();

                var createdCategory = _mapper.Map <CategoryDto>(categoryEntity);

                return(CreatedAtRoute("CategoryById", new { id = createdCategory.Id }, createdCategory));
            }
            catch (Exception ex)
            {
                //_logger.LogError($"Something went wrong inside CreateCategory action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Exemplo n.º 2
0
        public Guid AddNewCategory(NewCategoryDto data)
        {
            var c = new Category
            {
                Id          = Guid.NewGuid(),
                Name        = data.Name,
                Description = data.Description,
                Created     = DateTime.Now,
                Updated     = DateTime.Now,
                IsActive    = true,
                IsDeleted   = false,
                CreatedBy   = Guid.Parse("00000000-0000-0000-0000-000000000001"),
                UpdatedBy   = Guid.Parse("00000000-0000-0000-0000-000000000001")
            };

            context.Categories.Add(c);

            var resultValue = context.SaveChanges();

            if (resultValue > 0)
            {
                var dto = new CategoryDto
                {
                    Id          = c.Id,
                    Description = c.Description,
                    Name        = c.Name
                };
                cacheManager.Update <List <CategoryDto> >(nameof(Category), (cachedData) => { cachedData.Add(dto); });
                return(c.Id);
            }
            return(Guid.Empty);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddCategory(NewCategoryModel model)
        {
            var categories = await _solutionApiClient.GetAllCategoriesAsync();

            ViewBag.Categories = categories.Data;
            var dto = new NewCategoryDto
            {
                Name             = model.Name,
                Description      = model.Description,
                ParentCategoryId = model.ParentCategoryId
            };



            var ph = model.Logo;

            byte[] p1 = null;
            using (var fs1 = ph.OpenReadStream())
                using (var ms1 = new MemoryStream())
                {
                    fs1.CopyTo(ms1);
                    p1 = ms1.ToArray();
                }
            dto.Logo = new NewPhotoDto {
                Data = p1, Type = "Logo"
            };

            await _solutionApiClient.AddCategoryAsync(dto);

            return(Redirect("addCategory"));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddCategory([FromBody] NewCategoryDto newCategory)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (await _categoryRepository.IsDuplicateCategoryAsync(newCategory))
                {
                    ModelState.AddModelError("category", "Category already exists");
                    return(BadRequest(ModelState));
                }

                var categoryId = await _categoryRepository.AddCategoryAsync(newCategory);

                if (categoryId > 0)
                {
                    return(Ok(categoryId));
                }

                return(StatusCode(500, "An error ocurred in server"));
            }
            catch (Exception e)
            {
                _logger.LogCritical($"POST {Route} - {e.GetType().Name} - {e.Message} - {e.StackTrace}");
                return(StatusCode(500, "An error ocurred in server"));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Добавление категории
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <CategoryDto> AddCategoryAsync(NewCategoryDto dto)
        {
            var result = await _categoryRepository.AddAsync(_mapper.Map <Category>(dto));

            await _categoryRepository.SaveChangesAsync();

            return(_mapper.Map <CategoryDto>(result));
        }
Exemplo n.º 6
0
 public Task <ApiResponse <CategoryDto> > AddCategoryAsync(NewCategoryDto category)
 {
     if (category == null)
     {
         throw new ArgumentNullException(nameof(category));
     }
     return(PostAsync <NewCategoryDto, ApiResponse <CategoryDto> >(_categoryOptions.AddCategoryUrl, category));
 }
Exemplo n.º 7
0
        public async Task <int> AddCategoryAsync(NewCategoryDto newCategory)
        {
            var category = new Category
            {
                Name     = newCategory.Name,
                IsActive = true
            };

            await _dbContext.Categories.AddAsync(category);

            if (await _dbContext.SaveChangesAsync() > 0)
            {
                return(category.Id);
            }

            return(0);
        }
Exemplo n.º 8
0
 public async Task <bool> IsDuplicateCategoryAsync(NewCategoryDto category)
 {
     return(await _dbContext.Categories.AnyAsync(c =>
                                                 c.Name.Equals(category.Name, StringComparison.InvariantCultureIgnoreCase) && c.IsActive));
 }
Exemplo n.º 9
0
 public Task <ApiResponse <CategoryDto> > AddCategoryAsync(NewCategoryDto dto)
 {
     return(PostAsync <NewCategoryDto, ApiResponse <CategoryDto> >(_clientOptions.AddCategoryUrl, dto));
 }
Exemplo n.º 10
0
        public async Task <IActionResult> CreateNewCategory([FromRoute] string walletId, [FromBody] NewCategoryDto newCategoryDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var resultado = await _commandDispatcher.ExecuteAsync(new CreateCategory(walletId, newCategoryDto.Description));

            return(Created(string.Empty, resultado.ReturnDto));
        }
Exemplo n.º 11
0
 public async Task <IActionResult> AddCategoryAsync([FromBody] NewCategoryDto category)
 {
     return(ApiResult(await _categoryManager.AddCategoryAsync(category)));
 }
Exemplo n.º 12
0
 public Guid AddCategory(NewCategoryDto data)
 {
     return(service.AddNewCategory(data));
 }
Exemplo n.º 13
0
        public async Task <IActionResult> AddCategoryAsync([FromBody] NewCategoryDto category)
        {
            var cat = await _solutionService.AddCategoryAsync(category);

            return(ApiResult(cat));
        }