コード例 #1
0
ファイル: BusinessController.cs プロジェクト: chyates/Yelp
        public IActionResult NewBizCategoryType(NewBizViewModel MainVM)
        {
            if (checkLogStatus() == false)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (TryValidateModel(MainVM.CategoryTypeVM))
            {
                string _categorytype = Constants.UppercaseFirst(MainVM.CategoryTypeVM.CategoryType);
                // confirm the category does not already exist
                try
                {
                    BusCategoryType CheckCategoryType = _context.CategoryTypes.SingleOrDefault(cat => cat.CategoryType == _categorytype);
                    if (CheckCategoryType != null)
                    {
                        // the category already has an existing record; throw an error
                        AddAlreadyExistsError("BizVM.CategoryTypeId");
                        return(RedirectToAction("NewBiz"));
                    }
                }
                catch (Exception ex)
                {
                    // there were not any matching entries
                }
                BusCategoryType NewCategoryType = new BusCategoryType();
                NewCategoryType.CategoryType = _categorytype;
                NewCategoryType.CategoryId   = MainVM.CategoryTypeVM.CategoryId;
                _context.CategoryTypes.Add(NewCategoryType);
                _context.SaveChanges();
            }
            return(RedirectToAction("NewBiz"));
        }
コード例 #2
0
ファイル: BusinessController.cs プロジェクト: chyates/Yelp
        public async Task <IActionResult> CreateBiz(NewBizViewModel MainVM)
        {
            if (checkLogStatus() == false)
            {
                return(RedirectToAction("Index", "Home"));
            }

            int BizId = 0;
            BusinessViewModel bizView = MainVM.BizVM;

            try
            {
                if (ModelState.IsValid)
                {
                    // Confirm the business does not already have a record
                    try
                    {
                        Business CheckBusiness = _context.Businesses.SingleOrDefault(biz => biz.Name == bizView.Name);
                        if (CheckBusiness != null)
                        {
                            // the business name already has an existing record; throw an error
                            AddBusinessNameError(CheckBusiness.BusinessId);
                            return(RedirectToAction("NewBiz"));
                        }
                    }
                    catch (Exception ex)
                    {
                        // there were not any existing businesses with the same name
                        // proceed with code
                    }
                    Business newBiz = new Business(bizView);
                    _context.Businesses.Add(newBiz);
                    _context.SaveChanges();
                    _context.Entry(newBiz).GetDatabaseValues();
                    BizId = newBiz.BusinessId;
                }
                else
                {
                    return(RedirectToAction("NewBiz"));
                }
            }
            catch (Exception ex)
            {
                // there were errors in model validation
                return(RedirectToAction("NewBiz"));
            }

            // the new business saved correctly -- save the image to a folder with the biz ID
            // and update the business record with the link

            if (bizView.ImageFiles != null)
            {
                // full path to the file in the temp location
                var filePath = Path.GetTempFileName();

                if (bizView.ImageFiles.Length > 0)
                {
                    var    parsedContentDisposition = ContentDispositionHeaderValue.Parse(bizView.ImageFiles.ContentDisposition);
                    string _fileName      = parsedContentDisposition.FileName.Value.Trim();
                    string _fileExtension = Path.GetExtension(_fileName);
                    BizImageLinkImportModel newFileNameVM = new BizImageLinkImportModel();
                    newFileNameVM.FileName      = _fileName;
                    newFileNameVM.FileExtension = _fileExtension;
                    var uploadDir = _env.WebRootPath + $@"\img\Biz\{BizId}";
                    if (!Directory.Exists(uploadDir))
                    {
                        Directory.CreateDirectory(uploadDir);
                    }
                    using (var fileStream = new FileStream(Path.Combine(uploadDir, bizView.ImageFiles.FileName), FileMode.Create))
                    {
                        await bizView.ImageFiles.CopyToAsync(fileStream);
                    }
                    // save image link to the database
                    Business UpdateBiz = _context.Businesses.SingleOrDefault(biz => biz.BusinessId == BizId);
                    string   ImagePath = $@"/img/Biz/{BizId}/" + bizView.ImageFiles.FileName;
                    ImagePath           = ImagePath.Replace("\"", "");
                    ImagePath           = ImagePath.Replace("'", "");
                    UpdateBiz.ImageLink = ImagePath;
                    _context.SaveChanges();
                }
            }
            // the business and image were added correctly
            return(RedirectToAction("NewBizProperties", new { biz_id = BizId }));
        }