Beispiel #1
0
        //[Authorize(Roles = "Admin")]
        public async Task <IActionResult> Create([FromForm] CategoryCreationModel model)
        {
            var create = await _categoryService.CreateCategoryAsync(model);

            var locationUri = _uriService.GetCategoryUri(create.Payload.Id.ToString());

            return(Created(locationUri, _mapper.Map <CategoryDetailsModel>(create.Payload)));
        }
        public async Task<ActionResult<CategoryReturnModel>> PostCategory(CategoryCreationModel category)
        {
            var categoryEntity = _mapper.Map<Category>(category);
            var categoryReturn = _mapper.Map<CategoryReturnModel>(categoryEntity);
            _categoryDb.Categories.Add(categoryEntity);
            await _categoryDb.SaveChangesAsync();

            return CreatedAtAction("GetCategory", new { id = categoryEntity.Id }, categoryReturn);
        }
        public IActionResult Create(CategoryCreationModel model)
        {
            if (model == null)
            {
                return(BadRequest("category is null"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var vm = new CategoryBaseModel
            {
                CategoryName = model.CategoryName,
                Description  = model.Description,
                Address      = model.Address,
                PhoneNumber  = model.PhoneNumber,
                Url          = model.Url
            };
            var result = _categoryService.Create(vm);

            if (result.IsSuccess)
            {
                //
                var data = new
                {
                    categoryId = long.Parse(result.Message),
                    model.CategoryName,
                    model.Description,
                    model.PhoneNumber,
                    model.Address
                };
                // upload file
                if (model.File != null && model.File.Length > 0)
                {
                    var imageName            = $"category_{data.categoryId}";
                    var imagePath            = _appService.Upload(model.File, imageName).Result;
                    var isUpdateImageSuccess = _categoryService.UpdateImageById(data.categoryId, imagePath);
                }
                return(CreatedAtRoute("GetCategory", new { id = data.categoryId }, data));
            }
            return(BadRequest(result));
        }
Beispiel #4
0
        public async Task <PayloadResult <Category> > CreateCategoryAsync(CategoryCreationModel categoryModel)
        {
            var category = _mapper.Map <CategoryCreationModel, Category>(categoryModel);

            await _dataContext.AddAsync(category);

            var create = await _dataContext.SaveChangesAsync() > 0;

            if (!create)
            {
                return new PayloadResult <Category>
                       {
                           Errors  = new[] { "Could not save changes." },
                           Success = false
                       }
            }
            ;

            return(new PayloadResult <Category>
            {
                Payload = category
            });
        }