Esempio n. 1
0
        public ActionResult ViewDonations()
        {
            ActionResult oResponse = null;

            DonationVM newVM = new DonationVM();

            if (Session["Username"] == null || (Int16)Session["Role"] != 1)
            {
                //Power User & User are redirected
                oResponse = RedirectToAction("Index", "Home");
            }
            else
            {
                try
                {
                    //Call method from DAL to be put into variable
                    List <IDonationDO> donationInfo = DonationAccess.ViewAllDonations();
                    //put mapping into a variable
                    newVM.DonationList = DonationMap.MapDOtoPO(donationInfo);
                    //Return view
                    oResponse = View(newVM);
                }
                catch (Exception e)
                {
                    newVM.ErrorMessage = "Sorry, We couldn't obtain the list of donations list";
                    ErrorLog.LogError(e); //log error
                }
                finally
                {
                    //Onshore standards
                }
            }

            return(oResponse);
        }
Esempio n. 2
0
        public ActionResult UpdateProduct(int Id)
        {
            //Declare DonateVM

            DonationVM model;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                //Get the product

                Donate_Product product = db.Donate_Product.Find(Id);

                //make sure product exists
                if (product == null)
                {
                    return(Content("That product does not exist"));
                }

                //init model

                model = new DonationVM(product);

                //make  a select list

                model.Categories = new SelectList(db.Donate_Category.ToList(), "Id", "Name");

                //get all gallery images

                model.GalleryImages = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + Id + "/Gallery/Thumbs"))
                                      .Select(fn => Path.GetFileName(fn));
            }
            return(View(model));
        }
Esempio n. 3
0
        public ActionResult ViewDonationsbyUserID(long UserID)
        {
            ActionResult oResponse = null;

            if (Session["Username"] == null) //Guest
            {
                oResponse = RedirectToAction("Index", "Home");
            }
            else //Everyone else
            {
                DonationVM newVM = new DonationVM();//Creating new instance
                try
                {
                    //Uses method form DAL then assigns to variable
                    List <IDonationDO> userDonationInfo = UserAccess.ViewDonationsbyUserID(UserID);
                    //Mapping assigned to a variable
                    newVM.DonationList = DonationMap.MapDOtoPO(userDonationInfo);
                    //Return view
                    oResponse = View(newVM);
                }
                catch (Exception e)
                {
                    newVM.ErrorMessage = "Sorry we cannot process your request at this time";
                    ErrorLog.LogError(e);
                    oResponse = View(newVM);
                }
                finally
                {
                    //Onshore standards
                }
            }
            return(oResponse);
        }
Esempio n. 4
0
        public ActionResult DeleteDonation(long DonationID)
        {
            ActionResult oResponse = null;

            if (Session["Username"] == null || (Int16)Session["Role"] != 1)
            {
                //Power User & User redirected
                oResponse = RedirectToAction("Index", "Home");
            }
            else //Admin
            {
                try
                {
                    //Pull method from DAL
                    DonationAccess.DeleteDonation(DonationID);
                }
                catch (Exception e) //bad connection to sql
                {
                    //new instance of VM
                    DonationVM newVM = new DonationVM();
                    newVM.ErrorMessage = "Sorry we could not process your request at this time";
                    ErrorLog.LogError(e);
                }
                finally
                {
                    //Onshore standards
                }
            }
            return(RedirectToAction("ViewDonations", "Donation"));
        }
Esempio n. 5
0
        public ActionResult Add_Donate_Product()
        {
            //Intial Model

            DonationVM model = new DonationVM();

            //add selet list to categorey models
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model.Categories = new SelectList(db.Donate_Category.ToList(), "Id", "Name");
            }

            return(View(model));
        }
Esempio n. 6
0
        public ActionResult CreateDonations()
        {
            ActionResult oResponse = null;

            if (Session["Username"] == null) //Guest can not see anything past the homepage
            {
                oResponse = RedirectToAction("Index", "Home");
            }
            else
            {
                DonationVM newVM = new DonationVM();             //Create a new instance of DonationVM
                newVM.Donation.UserID = (long)Session["UserID"]; //Pull UserID from Session to add to list of donations

                oResponse = View(newVM);
            }


            return(oResponse);
        }
Esempio n. 7
0
        public ActionResult CreateDonations(DonationVM iDonation)
        {
            ActionResult oResponse = null;   //defining our varivale

            if (Session["Username"] == null) //Guest
            {
                oResponse = RedirectToAction("Index", "Home");
            }

            else  //Users,Power User, Admin
            {
                if (ModelState.IsValid)//If info was entered correctly
                {
                    try
                    {
                        //set mapping to a variable to be used to convert POtoDO
                        IDonationDO DonationForm = DonationMap.MapPOtoDO(iDonation.Donation);
                        //Use the method from DAL to preform action
                        DonationAccess.CreateDonation(DonationForm);
                        //Redirect to view using UserID from database
                        oResponse = RedirectToAction("ViewDonationsbyUserID", "User", new { UserID = (long)Session["UserID"] });
                    }
                    catch (Exception e) //If problem with sql connection
                    {
                        iDonation.ErrorMessage = "We are sorry, We cannot process your request at this time";
                        ErrorLog.LogError(e);        //Log to file
                        oResponse = View(iDonation); //return view
                    }
                    finally
                    {
                        //Onshore standards
                    }
                }
                else //If info was incorrect
                {
                    oResponse = View(iDonation);
                }
            }
            return(oResponse);
        }
Esempio n. 8
0
        public ActionResult UpdateDonation(DonationVM iDonation)
        {
            ActionResult oResponse = null;

            if (Session["Username"] == null || (Int16)Session["Role"] != 1)
            {
                //Power User & User redirected
                oResponse = RedirectToAction("Index", "Home");
            }
            else //Admin
            {
                if (ModelState.IsValid) //Info correct
                {
                    try
                    {
                        //put mapping into a variable
                        IDonationDO update = DonationMap.MapPOtoDO(iDonation.Donation);
                        //Use of method from DAL
                        DonationAccess.UpdateDonation(update);
                        //Return the list of donations
                        oResponse = RedirectToAction("ViewDonations", "Donation");
                    }
                    catch (Exception e)
                    {
                        iDonation.ErrorMessage = "Sorry we are unable to handle your request";
                        ErrorLog.LogError(e); //Log into file
                        oResponse = RedirectToAction("UpdateDonation", "Donation");
                    }
                    finally
                    {
                        //Onshore standards
                    }
                }
                else //Info incorrect
                {
                    oResponse = View(iDonation);
                }
            }
            return(oResponse);
        }
Esempio n. 9
0
        public ActionResult UpdateDonation(long DonationID)
        {
            ActionResult oResponse = null;

            if (Session["Username"] == null || (Int16)Session["Role"] != 1)
            {
                //Power User & User redirected
                oResponse = RedirectToAction("Index", "Home");
            }
            else //Admin
            {
                //Create a new instance of the object
                DonationVM newVM = new DonationVM();
                //set the method to a variable to be used
                IDonationDO donation = DonationAccess.ViewDonationsByID(DonationID);
                //set mapping to a variable
                newVM.Donation = DonationMap.MapDOtoPO(donation);
                //return view
                oResponse = View(newVM);
            }

            return(oResponse);
        }
 public DonationCommand(DonationVM vm)
 {
     CurrentVM = vm;
 }
Esempio n. 11
0
        public ActionResult Add_Donate_Product(DonationVM model, HttpPostedFileBase file)
        {
            //check model state
            if (!ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    model.Categories = new SelectList(db.Donate_Category.ToList(), "Id", "Name");
                    return(View(model));
                }
            }
            //declare product id
            int id;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Donate_Product product = new Donate_Product();
                //product.ProductCode = model.ProductCode;
                product.Name        = model.Name;
                product.Description = model.Description;

                product.CategoryId = model.CategoryId;
                product.Date       = DateTime.Now;
                product.AdminId    = User.Identity.GetUserId();


                db.Donate_Product.Add(product);
                db.SaveChanges();

                //get id
                id = product.Id;
            }

            TempData["SM"] = "You have addedd a product sussecfully";


            #region
            //Create necessary directiories

            var OriginalDirectorey = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));

            var PathString1 = Path.Combine(OriginalDirectorey.ToString(), "Products");
            var PathString2 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString());
            var PathString3 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Thumbs");
            var PathString4 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Gallery");
            var PathString5 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs");



            if (!Directory.Exists(PathString1))
            {
                Directory.CreateDirectory(PathString1);
            }


            if (!Directory.Exists(PathString2))
            {
                Directory.CreateDirectory(PathString2);
            }


            if (!Directory.Exists(PathString3))
            {
                Directory.CreateDirectory(PathString3);
            }


            if (!Directory.Exists(PathString4))
            {
                Directory.CreateDirectory(PathString4);
            }


            if (!Directory.Exists(PathString5))
            {
                Directory.CreateDirectory(PathString5);
            }


            //Create if a file was upload
            if (file != null && file.ContentLength > 0)
            {
                //Get file Extention
                string ext = file.ContentType.ToLower();


                if (ext != "image/jpg" &&
                    ext != "image/jpeg" &&
                    ext != "image/pjpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/png")
                {
                    using (ApplicationDbContext db = new ApplicationDbContext())
                    {
                        model.Categories = new SelectList(db.Donate_Category.ToList(), "Id", "Name");
                        ModelState.AddModelError("", "The Image was not uplaoded - wrong image extintion");
                        return(View(model));
                    }
                }

                string ImageName = file.FileName;

                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Donate_Product prod = db.Donate_Product.Find(id);
                    prod.ImageName = ImageName;
                    db.SaveChanges();
                }

                //set original and thumb image pathss
                var path  = string.Format("{0}\\{1}", PathString2, ImageName);
                var path2 = string.Format("{0}\\{1}", PathString3, ImageName);

                //save original
                file.SaveAs(path);


                //create and save thumbs
                WebImage image = new WebImage(file.InputStream);

                image.Resize(400, 400);
                image.Save(path2);
            }

            #endregion
            //redirect

            return(RedirectToAction("Add_Donate_Product"));
        }
Esempio n. 12
0
        public ActionResult UpdateProduct(DonationVM model, HttpPostedFileBase file)
        {
            //get product id
            int id = model.Id;

            //populate categories selectlist and gallery images
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model.Categories = new SelectList(db.Donate_Category.ToList(), "Id", "Name");
            }
            model.GalleryImages = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + id + "/Gallery/Thumbs"))
                                  .Select(fn => Path.GetFileName(fn));


            //check model state

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

            //make sure product name isunique

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                if (db.Donate_Product.Where(x => x.Id != id).Any(x => x.Name == model.Name))
                {
                    ModelState.AddModelError("", "That product name is tacken!");
                    return(View(model));
                }
            }
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Donate_Product product = db.Donate_Product.Find(id);

                product.Name        = model.Name;
                product.Description = model.Description;
                product.CategoryId  = model.CategoryId;
                if (model.ImageName != null)
                {
                    product.ImageName = model.ImageName;
                }

                db.SaveChanges();
            }

            //set tempdata message
            TempData["SM"] = "You have Updated a product sussecfully";


            #region Image Upload
            //Create if a file was upload
            if (file != null && file.ContentLength > 0)
            {
                //Get file Extention
                string ext = file.ContentType.ToLower();


                if (ext != "image/jpg" &&
                    ext != "image/jpeg" &&
                    ext != "image/pjpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/png")
                {
                    using (ApplicationDbContext db = new ApplicationDbContext())
                    {
                        model.Categories = new SelectList(db.Donate_Category.ToList(), "Id", "Name");
                        ModelState.AddModelError("", "The Image was not uplaoded - wrong image extintion");
                        return(View(model));
                    }
                }


                var OriginalDirectorey = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));

                var PathString1 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString());
                var PathString2 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Thumbs");


                //Delete files from directories

                DirectoryInfo di1 = new DirectoryInfo(PathString1);
                DirectoryInfo di2 = new DirectoryInfo(PathString2);

                foreach (FileInfo file1 in di1.GetFiles())
                {
                    file1.Delete();
                }

                foreach (FileInfo file2 in di2.GetFiles())
                {
                    file2.Delete();
                }


                //save images name

                string imageName = file.FileName;

                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Donate_Product product = db.Donate_Product.Find(id);
                    product.ImageName = imageName;
                    db.SaveChanges();
                }
                //save original and thumb images

                var path  = string.Format("{0}\\{1}", PathString1, imageName);
                var path2 = string.Format("{0}\\{1}", PathString2, imageName);

                //save original
                file.SaveAs(path);


                //create and save thumbs
                WebImage image = new WebImage(file.InputStream);

                image.Resize(400, 400);
                image.Save(path2);
            }


            #endregion

            //Redirect

            return(RedirectToAction(nameof(UpdateProduct)));
        }
Esempio n. 13
0
 public DonationUC()
 {
     InitializeComponent();
     DataContext = new DonationVM();
 }