Exemplo n.º 1
0
        public ReturnViewModel Create(CategoryBaseModel model)
        {
            var result = new ReturnViewModel();
            var entity = new Category
            {
                Name        = model.CategoryName,
                Description = model.Description,
                Address     = model.Address,
                PhoneNumber = model.PhoneNumber,
                Url         = model.Url,
                Image       = model.Image,
                AddedOn     = DateTime.Now,
            };

            try
            {
                _repository.Create(entity);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                return(result);
            }
            result.IsSuccess = true;
            result.Message   = entity.Id.ToString();
            return(result);
        }
        public IActionResult Edit(CategoryBaseModel model)
        {
            var exists = this.categories.Exists(model.Id);

            if (!exists || !ModelState.IsValid)
            {
                return(BadRequest());
            }

            this.categories.Edit(model.Id, model.Name);

            return(RedirectToAction(nameof(App.Controllers.CategoriesController.All), "Categories"));
        }
        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));
        }