public async Task <IActionResult> AddBrand(BrandAddModel model, IFormFile fileLogo)
        {
            if (fileLogo == null)
            {
                return(UnprocessableEntity("Insert logo!"));
            }

            var response = await _brand.AddAsync(model, User.Identity.Name, _cloudStorage, fileLogo);

            return(Json(response));
        }
Exemple #2
0
        public async Task <DbResponse <BrandModel> > AddAsync(BrandAddModel model, string userName, ICloudStorage cloudStorage,
                                                              IFormFile fileLogo)
        {
            try
            {
                var registrationId = _db.Registration.GetRegID_ByUserName(userName);
                if (registrationId == 0)
                {
                    return(new DbResponse <BrandModel>(false, "Invalid User"));
                }

                model.CreatedByRegistrationId = registrationId;

                if (string.IsNullOrEmpty(model.Name))
                {
                    return(new DbResponse <BrandModel>(false, "Invalid Data"));
                }

                if (_db.Brand.IsExistName(model.Name))
                {
                    return(new DbResponse <BrandModel>(false, "Brand Name already Exist", null, "Name"));
                }

                var fileName = FileBuilder.FileNameImage("brand-logo", fileLogo.FileName);
                model.LogoFileName = await cloudStorage.UploadFileAsync(fileLogo, fileName);

                _db.Brand.Add(model);
                _db.SaveChanges();

                var data = _mapper.Map <BrandModel>(_db.Brand.Brand);

                return(new DbResponse <BrandModel>(true, "Success", data));
            }
            catch (Exception e)
            {
                return(new DbResponse <BrandModel>(false, e.Message));
            }
        }