public async Task <ActionResult> ShowIndentsPerPage(int?page, string[] categories)
        {
            int pageSize         = 4;
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;

            List <Category> categoriesInEnumEquivalent = null;

            if (categories != null)
            {
                List <string> categoriesStringCopy = new List <string>(categories);

                if (Session["Language"] != null)
                {
                    string lan = Session["Language"].ToString();

                    if (lan == "en")
                    {
                        categoriesInEnumEquivalent = CategoryExtension.
                                                     TranslateFromEnglishToEnumEquivalents(categoriesStringCopy.ToArray());
                    }
                    else
                    {
                        categoriesInEnumEquivalent = CategoryExtension.
                                                     TranslateFromRussianToEnumEquivalents(categoriesStringCopy.ToArray());
                    }
                }
            }

            IndentDTOPage indentsDTOSinglePageModel = await _indentService.GetIndentsPerPage(currentPageIndex,
                                                                                             pageSize, categoriesInEnumEquivalent);

            IndentPageViewModel indentPageViewModel = new IndentPageViewModel();

            indentPageViewModel = _mapper.Map <IndentPageViewModel>(indentsDTOSinglePageModel);
            indentPageViewModel.AvailableCategories = categories;

            return(View(indentPageViewModel));
        }
        public async Task <ActionResult> SaveIndent(IndentViewModel indentViewModel, string indentCategory, string indentPrice)
        {
            var clientId = Session["Id"];

            if (clientId == null)
            {
                return(RedirectToAction("Registration", "Customer"));
            }

            int customerId = Convert.ToInt32(clientId);

            string imgSrc = null;
            string fileMimeType;

            if (indentViewModel.IndentDate.Year < DateTime.Now.Year ||
                (indentViewModel.IndentDate.Year == DateTime.Now.Year && indentViewModel.IndentDate.Month < DateTime.Now.Month) ||
                (indentViewModel.IndentDate.Year == DateTime.Now.Year && indentViewModel.IndentDate.Month == DateTime.Now.Month &&
                 indentViewModel.IndentDate.Day < DateTime.Now.Day))
            {
                TempData["ErrorMessage"] = "Были введены некорректные данные, попробуйте снова!";

                return(RedirectToAction("CreateIndent", "Customer"));
            }

            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];

                fileMimeType = file.ContentType;

                if (!fileMimeType.Equals("application/octet-stream"))
                {
                    if (fileMimeType.Equals("image/jpeg"))
                    {
                        imgSrc = Path.Combine(Server.MapPath("~/Content/Indent"), "indentId.jpeg");
                        if (System.IO.File.Exists(imgSrc))
                        {
                            System.IO.File.Delete(imgSrc);
                        }
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "Загружать фотографии разрешается только в jpeg формате!";

                        return(RedirectToAction("CreateIndent", "Customer"));
                    }
                }
            }

            double?priceOfIndent;

            Regex patternForPrice = new Regex(@"^[0-9]*\,?[0-9]+\s?$", RegexOptions.IgnoreCase);

            if (indentPrice == null)
            {
                priceOfIndent = null;
            }
            else if (patternForPrice.IsMatch(indentPrice.Trim()))
            {
                try
                {
                    priceOfIndent = Convert.ToDouble(indentPrice.Trim());
                }
                catch (Exception e)
                {
                    TempData["ErrorMessage"] = "Были введены некорректные данные, попробуйте снова!";

                    return(RedirectToAction("CreateIndent", "Customer"));
                }
            }
            else
            {
                TempData["ErrorMessage"] = "Были введены некорректные данные, попробуйте снова!";

                return(RedirectToAction("CreateIndent", "Customer"));
            }

            if (indentViewModel.Title == null || indentViewModel.City == null || indentViewModel.IndentDescription == null)
            {
                TempData["ErrorMessage"] = "Были введены некорректные данные, попробуйте снова!";

                return(RedirectToAction("CreateIndent", "Customer"));
            }

            if (Session["Language"] != null)
            {
                string lan = Session["Language"].ToString();

                if (lan == "en")
                {
                    indentViewModel.Category = CategoryExtension.
                                               TranslateFromEnglishToEnumEquivalents(new string[] { indentCategory }).FirstOrDefault();
                }
                else
                {
                    indentViewModel.Category = CategoryExtension.
                                               TranslateFromRussianToEnumEquivalents(new string[] { indentCategory }).FirstOrDefault();
                }
            }

            indentViewModel.Price    = priceOfIndent;
            indentViewModel.ImgSrc   = imgSrc;
            indentViewModel.Customer = new CustomerDTO()
            {
                CustomerId = customerId
            };

            IndentDTO indentDTO = _mapper.Map <IndentDTO>(indentViewModel);
            int?      indentId;

            try
            {
                indentId = await _indentService.Create(indentDTO);
            }
            catch (ValidationException e)
            {
                TempData["ErrorMessage"] = e.Message;

                return(RedirectToAction("CreateIndent", "Customer"));
            }

            if (imgSrc != null)
            {
                imgSrc = imgSrc.Replace("indentId", indentId.ToString());
                Request.Files[0].SaveAs(imgSrc);
            }

            return(RedirectToAction("GetCustomerIndents", "Customer"));
        }
Exemple #3
0
        public async Task <ActionResult> ModificateProfile(string information, string categoryFirst, string categorySecond)
        {
            var clientId = Session["Id"];

            if (clientId == null)
            {
                return(RedirectToAction("Registration", "Executor"));
            }

            int executorId = Convert.ToInt32(clientId);

            HashSet <Category> categories = null;

            if (categoryFirst != null && categorySecond != null)
            {
                if (categoryFirst.Equals(categorySecond))
                {
                    TempData["ErrorMessage"] = "Категории должны различаться!";

                    return(RedirectToAction("ModificateProfile", "Executor"));
                }
                else
                {
                    if (Session["Language"] != null)
                    {
                        string lan = Session["Language"].ToString();
                        IEnumerable <Category> categoriesInEnumEquivalent = new List <Category>();

                        if (lan == "en")
                        {
                            categoriesInEnumEquivalent = CategoryExtension.
                                                         TranslateFromEnglishToEnumEquivalents(new string[] { categoryFirst, categorySecond });
                        }
                        else
                        {
                            categoriesInEnumEquivalent = CategoryExtension.
                                                         TranslateFromRussianToEnumEquivalents(new string[] { categoryFirst, categorySecond });
                        }

                        categories = new HashSet <Category>(categoriesInEnumEquivalent);
                    }
                }
            }

            string imgSrc       = null;
            string fileMimeType = null;

            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];

                fileMimeType = file.ContentType;

                if (!fileMimeType.Equals("application/octet-stream"))
                {
                    if (fileMimeType.Equals("image/jpeg"))
                    {
                        imgSrc = Path.Combine(Server.MapPath("~/Content/Executor"), executorId.ToString() + ".jpeg");
                        if (System.IO.File.Exists(imgSrc))
                        {
                            System.IO.File.Delete(imgSrc);
                        }
                        file.SaveAs(imgSrc);
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "Загружать фотографии разрешается только в jpeg формате!";

                        return(RedirectToAction("ModificateProfile", "Executor"));
                    }
                }
            }

            ExecutorDTO propertiesAfterEdition = new ExecutorDTO()
            {
                ExecutorId  = executorId,
                ImgSrc      = imgSrc,
                Information = information,
                Categories  = categories
            };

            await _executorService.SaveExecutorPropertiesAfterEdition(propertiesAfterEdition);

            string      cacheKeyForExecutor = "show-executor-" + executorId.ToString();
            ExecutorDTO cachedExecutor      = HttpContext.Cache[cacheKeyForExecutor] as ExecutorDTO;

            if (cachedExecutor != null)
            {
                HttpContext.Cache.Remove(cacheKeyForExecutor);
            }

            return(RedirectToAction("PrivateOffice", "Executor"));
        }