Ejemplo n.º 1
0
        public ActionResult Edit(RequestEditView model, int id)
        {
            Boolean isValid = true;

            ViewBag.ErrorMessageSouvenirName = null;
            ViewBag.ErrorMessageCountry      = null;
            ViewBag.ErrorMessageAmount       = null;
            ViewBag.ErrorMessagePrice        = null;
            ViewBag.ErrorMessageCurrency     = null;
            ViewBag.ErrorMessageDescription  = null;
            ViewBag.ErrorMessageStatus       = null;


            if (model.souvenirName == null || model.souvenirName.Length < 3 || model.souvenirName.Length > 30)
            {
                ViewBag.ErrorMessageSouvenirName = "The name of your souvenir should have between 3 and 30 characters.";
                isValid = false;
            }
            if (model.countrySouv == null)
            {
                ViewBag.ErrorMessageCountry = "Please select a country.";
                isValid = false;
            }
            if (model.amount < 1 || model.amount > 1000)
            {
                ViewBag.ErrorMessageAmount = "Please enter a quantity between 1 and 1000.";
                isValid = false;
            }
            string tmp = model.price.Replace(",", "");

            tmp = tmp.Replace(".", "");
            if (model.price == null || model.price.Length > 10)
            {
                ViewBag.ErrorMessagePrice = "Please enter a price under 10 digits.";
                isValid = false;
            }
            if ((model.price.Length - tmp.Length) > 1)
            {
                ViewBag.ErrorMessagePrice = "Maximum one ',' or '.' !";
                isValid = false;
            }
            if (model.currency == null)
            {
                ViewBag.ErrorMessageCurrency = "Please select a currency.";
                isValid = false;
            }
            if (model.descriptionSouv == null || model.descriptionSouv.Length > 300 || model.descriptionSouv.Length < 5)
            {
                ViewBag.ErrorMessageDescription = "Please enter a description of your souvenir, it should be more than 5 and less than 300 characters.";
                isValid = false;
            }
            if (model.status == null)
            {
                ViewBag.ErrorMessageStatus = "Please select a status.";
                isValid = false;
            }

            if (isValid == true)
            {
                SouvenirInfo souvenirInfo = db.souvenirInfos.Find(souvenirID_tmp);
                if (model.price.Contains(","))
                {
                    model.price = model.price.Replace(",", ".");
                }
                NumberStyles style   = NumberStyles.AllowDecimalPoint;
                CultureInfo  culture = CultureInfo.CreateSpecificCulture("en-US");
                double       d;
                double.TryParse(model.price, style, culture, out d);
                souvenirInfo.price           = Math.Round(d, 2);
                souvenirInfo.currency        = model.currency;
                souvenirInfo.descriptionSouv = model.descriptionSouv;
                db.Entry(souvenirInfo).CurrentValues.SetValues(souvenirInfo);
                db.SaveChanges();

                Souvenir souvenir = db.souvenirs.Find(souvenirID_tmp);
                souvenir.souvenirName = model.souvenirName;
                souvenir.countrySouv  = model.countrySouv;
                db.Entry(souvenir).CurrentValues.SetValues(souvenir);
                db.SaveChanges();

                Request request = db.requests.Find(souvenirID_tmp);
                request.amount = model.amount;
                request.status = model.status;
                request.reward = (double)(request.amount * request.souvenir.souvenirInfo.price * rewardpercent) / 100;
                request.reward = Math.Round(request.reward, 2);
                db.Entry(request).CurrentValues.SetValues(request);
                db.SaveChanges();

                souvenirID_tmp = null;
                return(RedirectToAction("MyOwnRequests"));
            }
            else
            {
                return(View("Edit", model));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(RequestCreate model, HttpPostedFileBase file)
        {
            Boolean isValid = true;

            ViewBag.ErrorMessageSouvenirName = null;
            ViewBag.ErrorMessageCountry      = null;
            ViewBag.ErrorMessageAmount       = null;
            ViewBag.ErrorMessagePrice        = null;
            ViewBag.ErrorMessageCurrency     = null;
            ViewBag.ErrorMessageDescription  = null;

            if (model.souvenirName == null || model.souvenirName.Length < 3 || model.souvenirName.Length > 30)
            {
                ViewBag.ErrorMessageSouvenirName = "The name of your souvenir should have between 3 and 30 characters.";
                isValid = false;
            }
            if (model.countrySouv == null)
            {
                ViewBag.ErrorMessageCountry = "Please select a country.";
                isValid = false;
            }
            //Fehlermeldung von amount wird noch nicht angezeigt
            if (model.amount < 1 || model.amount > 1000)
            {
                ViewBag.ErrorMessageAmount = "Please enter a quantity between 1 and 1000.";
                isValid = false;
            }
            if (model.price == null)
            {
                ViewBag.ErrorMessagePrice = "Please enter a price";
                isValid = false;
            }
            else
            {
                string tmp = model.price.Replace(",", "");
                tmp = tmp.Replace(".", "");
                if (model.price == null || model.price.Length > 10)
                {
                    ViewBag.ErrorMessagePrice = "Please enter a price under 10 digits.";
                    isValid = false;
                }
                if ((model.price.Length - tmp.Length) > 1)
                {
                    ViewBag.ErrorMessagePrice = "Maximum one ',' or '.' !";
                    isValid = false;
                }
            }
            if (model.currency == null)
            {
                ViewBag.ErrorMessageCurrency = "Please select a currency.";
                isValid = false;
            }
            if (model.descriptionSouv == null || model.descriptionSouv.Length > 300 || model.descriptionSouv.Length < 5)
            {
                ViewBag.ErrorMessageDescription = "Please enter a description of your souvenir, it should be more than 5 and less than 300 characters.";
                isValid = false;
            }

            if (isValid == true)
            {
                SouvenirInfo souvenirInfo = new SouvenirInfo();
                if (model.price.Contains(","))
                {
                    model.price = model.price.Replace(",", ".");
                }
                NumberStyles style   = NumberStyles.AllowDecimalPoint;
                CultureInfo  culture = CultureInfo.CreateSpecificCulture("en-US");
                double       d;
                double.TryParse(model.price, style, culture, out d);
                souvenirInfo.price           = Math.Round(d, 2);
                souvenirInfo.currency        = model.currency;
                souvenirInfo.descriptionSouv = model.descriptionSouv;


                db.souvenirInfos.Add(souvenirInfo);
                db.SaveChanges();

                Souvenir souvenir = new Souvenir();

                try
                {
                    string fileName  = souvenirInfo.souvenirInfoID.ToString();
                    string extension = Path.GetExtension(file.FileName);
                    fileName += extension;
                    souvenir.selectedPictureSouvenir = "~/Content/" + fileName;
                    fileName = Path.Combine(Server.MapPath("~/Content/"), fileName);
                    file.SaveAs(fileName);
                }
                catch (Exception)
                {
                    souvenir.selectedPictureSouvenir = "~/Content/defaultSouvenir.png";
                }
                souvenir.souvenirID   = souvenirInfo.souvenirInfoID;
                souvenir.souvenirName = model.souvenirName;
                souvenir.countrySouv  = model.countrySouv;
                souvenir.souvenirInfo = souvenirInfo;

                db.souvenirs.Add(souvenir);
                db.SaveChanges();

                Customer customer = db.customers.Find(Session["userName"]);

                Request request = new Request();
                request.souvenir  = souvenir;
                request.customer  = customer;
                request.amount    = model.amount;
                request.reward    = (double)(request.amount * request.souvenir.souvenirInfo.price * rewardpercent) / 100;
                request.reward    = Math.Round(request.reward, 2);
                request.status    = "new";
                request.requestID = souvenirInfo.souvenirInfoID;

                db.requests.Add(request);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            else
            {
                return(View("Create", model));
            }
        }