Example #1
0
        public ActionResult Edit(SmartphonesViewModel viewModel)
        {
            if (viewModel == null)
            {
                TempData["ErrorMessage"] = "Ooooops, a serious error occured: No ViewModel.";
                return(RedirectToAction("Index", "Home"));
            }
            HttpPostedFileBase file = Request.Files[0];

            if (string.IsNullOrEmpty(file.FileName) && string.IsNullOrEmpty(viewModel.ImagePath))
            {
                ModelState.AddModelError("", "Please add an image");
            }
            if (ModelState.IsValid)
            {
                UnitOfWork unitOfWork   = new UnitOfWork();
                Product    dbProduct    = unitOfWork.ProductRepository.GetFirst(item => item.ID == viewModel.ProductId);
                Smartphone dbSmartphone = unitOfWork.SmartphonesRepository.GetFirst(item => item.ProductID == viewModel.ProductId);
                if (dbProduct == null)
                {
                    dbProduct = new Product();
                }

                if (dbSmartphone == null)
                {
                    dbSmartphone = new Smartphone();
                }
                dbProduct.CategoryID     = viewModel.CategoryID;
                dbProduct.Name           = viewModel.Name;
                dbProduct.OS             = viewModel.OS;
                dbProduct.Price          = (decimal)viewModel.Price;
                dbProduct.Processor      = viewModel.Processor;
                dbProduct.RAM            = viewModel.RAM;
                dbProduct.Storage        = viewModel.Storage;
                dbSmartphone.Camera      = viewModel.Camera;
                dbSmartphone.SIMCardType = viewModel.SIMCardType;

                if (file.ContentLength > 0 && string.IsNullOrEmpty(file.FileName) == false)
                {
                    string imagesPath     = Server.MapPath(Constants.ImagesSmartphonesDirectory);
                    string uniqueFileName = string.Format("{0}_{1}", DateTime.Now.Ticks, file.FileName);
                    string savedFileName  = Path.Combine(imagesPath, Path.GetFileName(uniqueFileName));
                    file.SaveAs(savedFileName);
                    dbProduct.ImageName = uniqueFileName;
                }
                dbProduct.Smartphones.Add(dbSmartphone);
                unitOfWork.ProductRepository.Save(dbProduct);
                bool isSaved = unitOfWork.Save() > 0;

                if (isSaved)
                {
                    TempData["Message"] = "The smartphone was saved successfully";
                }

                return(RedirectToAction("Index", "Home"));
            }

            return(View(viewModel));
        }
Example #2
0
        public ActionResult Details(int id)
        {
            UnitOfWork           unitOfWork          = new UnitOfWork();
            Product              product             = unitOfWork.ProductRepository.GetByID(id);
            Smartphone           smartphone          = unitOfWork.SmartphonesRepository.GetFirst(item => item.ProductID == id);
            SmartphonesViewModel smartphoneViewModel = new SmartphonesViewModel(product, smartphone);

            return(View(smartphoneViewModel));
        }
Example #3
0
        [CustomAuthorize]//allows access only to admins or if a given user has access
        public ActionResult Edit(int ProductId = 0)
        {
            SmartphonesViewModel smartphoneViewModel = new SmartphonesViewModel();
            UnitOfWork           unitOfWork          = new UnitOfWork();
            Product    dbProduct    = unitOfWork.ProductRepository.GetFirst(item => item.ID == ProductId);
            Smartphone dbSmartphone = unitOfWork.SmartphonesRepository.GetFirst(item => item.ProductID == ProductId);

            if (dbProduct != null && dbSmartphone != null)
            {
                smartphoneViewModel = new SmartphonesViewModel(dbProduct, dbSmartphone);
            }
            return(View(smartphoneViewModel));
        }
Example #4
0
        [AllowAnonymous]//allows access to all kind of users
        public ActionResult Index(int categoryID, string sortColumn, string direction, string keywords, int pageSize = Constants.DefaultPageSize, int pageIndex = 1)
        {
            UnitOfWork                  unitOfWork           = new UnitOfWork();
            List <Product>              productsList         = unitOfWork.ProductRepository.GetAll().Where(item => item.CategoryID == categoryID).ToList();
            List <Smartphone>           smartphonesList      = unitOfWork.SmartphonesRepository.GetAll();
            List <SmartphonesViewModel> smartphonesViewModel = new List <SmartphonesViewModel>();

            foreach (Product product in productsList)
            {
                foreach (Smartphone smartphone in smartphonesList)
                {
                    SmartphonesViewModel newSmartphone = new SmartphonesViewModel(product, smartphone);
                    smartphonesViewModel.Add(newSmartphone);
                    break;
                }
            }
            IQueryable <SmartphonesViewModel> records = smartphonesViewModel.AsQueryable();

            if (!string.IsNullOrEmpty(keywords))
            {
                records = records.Where(record => record.SmartphonesInfo.ToLower().Contains(keywords.ToLower()));
            }
            string sortColDirection = sortColumn + direction;

            switch (sortColDirection)
            {
            case "Price":
                records = records.OrderBy(record => record.Price);
                break;

            case "PriceDesc":
                records = records.OrderByDescending(record => record.Price);
                break;

            case "NameDesc":
                records = records.OrderByDescending(record => record.Name);
                break;

            default:
                records = records.OrderBy(record => record.Name);
                break;
            }
            List <SmartphonesViewModel> recordsToList = records
                                                        .Skip((pageIndex - 1) * pageSize)
                                                        .Take(pageSize)
                                                        .ToList();
            int allRecordsCount = records.Count();
            SearchViewModel <SmartphonesViewModel> searchViewModel = new SearchViewModel <SmartphonesViewModel>(recordsToList, pageSize, pageIndex, allRecordsCount, sortColumn, direction);

            return(View(searchViewModel));
        }
        public ActionResult Search(string keywords)
        {
            UnitOfWork                        unitOfWork           = new UnitOfWork();
            List <Product>                    pcProduct            = unitOfWork.ProductRepository.GetAll(pc => pc.CategoryID == 1 || pc.CategoryID == 2);
            List <Product>                    smartphoneProduct    = unitOfWork.ProductRepository.GetAll(smartphone => smartphone.CategoryID == 3);
            List <PC>                         computers            = unitOfWork.PCsRepository.GetAll();
            List <Smartphone>                 smartphones          = unitOfWork.SmartphonesRepository.GetAll();
            PCsViewModel                      computersViewModel   = new PCsViewModel(pcProduct, computers);
            SmartphonesViewModel              smartphonesViewModel = new SmartphonesViewModel(smartphoneProduct, smartphones);
            IQueryable <PCsViewModel>         pcRecords            = computersViewModel.pcsViewModel.AsQueryable();
            IQueryable <SmartphonesViewModel> smartphoneRecords    = smartphonesViewModel.smartphonesViewModel.AsQueryable();

            if (!string.IsNullOrEmpty(keywords))
            {
                pcRecords         = pcRecords.Where(record => record.PCsInfo.ToLower().Contains(keywords.ToLower()));
                smartphoneRecords = smartphoneRecords.Where(record => record.SmartphonesInfo.ToLower().Contains(keywords.ToLower()));
            }
            List <PCsViewModel>           pcRecordsToList         = pcRecords.ToList();
            List <SmartphonesViewModel>   smartphoneRecordsToList = smartphoneRecords.ToList();
            AllItemsSearchEngineViewModel allItemsViewModel       = new AllItemsSearchEngineViewModel(pcRecordsToList, smartphoneRecordsToList);

            return(View(allItemsViewModel));
        }