Ejemplo n.º 1
0
        public async Task <DbResponse <CatalogDisplayModel> > AddAsync(CatalogAddModel model, string userName, ICloudStorage cloudStorage, IFormFile image)
        {
            try
            {
                var registrationId = _db.Registration.GetRegID_ByUserName(userName);
                if (registrationId == 0)
                {
                    return(new DbResponse <CatalogDisplayModel>(false, "Invalid User"));
                }

                model.CreatedByRegistrationId = registrationId;

                if (string.IsNullOrEmpty(model.CatalogName) && string.IsNullOrEmpty(model.SlugUrl))
                {
                    return(new DbResponse <CatalogDisplayModel>(false, "Invalid Data"));
                }

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

                if (_db.Catalog.IsExistSlugUrl(model.SlugUrl))
                {
                    return(new DbResponse <CatalogDisplayModel>(false, "SlugUrl already Exist", null, "SlugUrl"));
                }


                if (image != null)
                {
                    var fileName = FileBuilder.FileNameImage("catalog", image.FileName);
                    model.ImageFileName = await cloudStorage.UploadFileAsync(image, fileName);
                }

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

                var data = _mapper.Map <CatalogDisplayModel>(_db.Catalog.catalog);

                return(new DbResponse <CatalogDisplayModel>(true, "Success", data));
            }
            catch (Exception e)
            {
                return(new DbResponse <CatalogDisplayModel>(false, e.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Add(CatalogAddModel model, IFormFile fileImage)
        {
            ViewBag.ParentCatalog = new SelectList(_catalog.ListDdl().Data, "value", "label");

            var response = await _catalog.AddAsync(model, User.Identity.Name, _cloudStorage, fileImage);

            if (!response.IsSuccess)
            {
                ModelState.AddModelError(response.FieldName, response.Message);
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            return(RedirectToAction("Index"));
        }