public ActionResult Delete(int?id)
 {
     try
     {
         if (id == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         AreaOFInterest AOI = db.AreaOFInterests.Find(id);
         if (AOI == null)
         {
             return(HttpNotFound());
         }
         else
         {
             db.AreaOFInterests.Remove(AOI);
             db.SaveChanges();
         }
         return(RedirectToAction("AOIAdmin"));
     }
     catch (Exception ex)
     {
         return(View("Error", new HandleErrorInfo(ex, "AreaOfInterest", "Delete")));
     }
 }
        public ActionResult Add()
        {
            AreaOFInterest  AOI    = new AreaOFInterest();
            AreaOfInterest2 model2 = new AreaOfInterest2();

            return(View(model2));
        }
 public ActionResult Edit([Bind(Include = "AOIId,AOIName,AOIIcon")] AreaOFInterest areaOFInterest, HttpPostedFileBase image1)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var db = new Entities();
             if (image1 != null)
             {
                 string ext = Path.GetExtension(image1.FileName).ToUpper();
                 if (ext == ".PNG" || ext == ".JPG" || ext == ".JPEG")
                 {
                     areaOFInterest.AOIIcon = new byte[image1.ContentLength];
                     image1.InputStream.Read(areaOFInterest.AOIIcon, 0, image1.ContentLength);
                 }
                 else
                 {
                     ViewBag.ErrorMessage = "The icon should be .png or .jpg or .jpeg";
                 }
             }
             db.Entry(areaOFInterest).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("AOIAdmin"));
         }
         return(View(areaOFInterest));
     }
     catch (Exception ex)
     {
         return(View("Error", new HandleErrorInfo(ex, "AreaOfInterest", "Edit")));
     }
 }
        public ActionResult DeleteConfirmed(int id)
        {
            AreaOFInterest areaOFInterest = db.AreaOFInterests.Find(id);

            db.AreaOFInterests.Remove(areaOFInterest);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        // GET: AreaOFInterestsmmmmm/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AreaOFInterest areaOFInterest = db.AreaOFInterests.Find(id);

            if (areaOFInterest == null)
            {
                return(HttpNotFound());
            }
            return(View(areaOFInterest));
        }
 public ActionResult Add(AreaOfInterest2 model, HttpPostedFileBase image1)
 {
     try
     {
         AreaOFInterest model2 = new AreaOFInterest();
         var            db     = new Entities();
         if (ModelState.IsValid)
         {
             if (image1 != null)
             {
                 string ext = Path.GetExtension(image1.FileName).ToUpper();
                 if (ext == ".PNG" || ext == ".JPG" || ext == ".JPEG")
                 {
                     model2.AOIIcon = new byte[image1.ContentLength];
                     image1.InputStream.Read(model2.AOIIcon, 0, image1.ContentLength);
                     model2.AOIName = model.AOIName;
                     db.AreaOFInterests.Add(model2);
                     db.SaveChanges();
                     return(Content("<script language='javascript' type='text/javascript'>alert('New area of interest added successfully');window.location = '/AreaOfInterest/AOIAdmin/';</script>"));
                     //return RedirectToAction("AOIAdmin");
                 }
                 else
                 {
                     ViewBag.ErrorMessage = "The icon should be .png or .jpg or .jpeg";
                 }
             }
             else
             {
                 ViewBag.ErrorMessages = "Upload icon of area of interest";
             }
         }
         return(View(model));
     }
     catch (Exception ex)
     {
         return(View("Error", new HandleErrorInfo(ex, "AreaOfInterest", "Add")));
     }
 }
 private static string HtmlPageContent(AreaOFInterest item, string pag_content)
 {
     pag_content += $"{$"<div class='box'><img src ='data:image;base64,"}{@Convert.ToBase64String(@item.AOIIcon)} 'width ='60' height ='60'/><hr/><p id ='SearchData'> {@item.AOIName}</p><a href=\"/AreaOfInterest/EachArea/{@item.AOIId}\"> See Projects </a></div>";
     return(pag_content);
 }
        public ActionResult UpdateSummaryReportTab(ProjectVM model, HttpPostedFileBase File)
        {
            try
            {
                ///////////////////////////AOI///////////////////////////////////////////////////////
                AreaOFInterest areaOFInterest = new AreaOFInterest();

                if (model.AOIList != null)
                {
                    if (model.AOIList.Count > 0)
                    {
                        // retrive all existing aoi of this project delete them then add all new aoi
                        List <ProjectsAOI> pAOI = (from s in db.ProjectsAOIs where s.SPtId == model.SPtId select s).ToList();
                        if (pAOI != null)
                        {
                            for (int item = 0; item < pAOI.Count; item++)
                            {
                                //Delete them from ProjectsAOI table
                                ProjectsAOI paoi = db.ProjectsAOIs.Find(pAOI[item].Id);
                                //   new ProjectsAOI { Id = pAOI[item].Id };
                                db.Entry(paoi).State = EntityState.Deleted;
                                db.SaveChanges();
                            }
                        }
                        // Now I will add every AOI again to mapping table
                        ProjectsAOI projectAOI = new ProjectsAOI();

                        for (int i = 0; i < model.AOIList.Count; i++)
                        {
                            string AOIName = model.AOIList.ElementAt(i).Trim().ToUpper();
                            var    temp    = (from s in db.AreaOFInterests where s.AOIName.ToUpper() == AOIName select s).FirstOrDefault();
                            //means it exists in AreaOfInterest table
                            if (AOIName != "")
                            {
                                if (temp != null)
                                {
                                    //get area id
                                    projectAOI.SPtId = model.SPtId;
                                    projectAOI.AOIId = temp.AOIId;
                                }
                                // else means no aoi has this name I will add it to area table then to mapping table
                                if (temp == null)
                                {
                                    AreaOFInterest newAOI = new AreaOFInterest();
                                    newAOI.AOIName = model.AOIList.ElementAt(i).Trim();
                                    using (Entities entity = new Entities())
                                    {
                                        entity.AreaOFInterests.Add(newAOI);
                                        entity.SaveChanges();
                                    }
                                    // get last added aoi then add it to aoi mapping table
                                    AreaOFInterest LastArea = db.AreaOFInterests.OrderByDescending(o => o.AOIId).FirstOrDefault();
                                    projectAOI.SPtId = model.SPtId;
                                    projectAOI.AOIId = LastArea.AOIId;
                                }
                                using (Entities entity = new Entities())
                                {
                                    entity.ProjectsAOIs.Add(projectAOI);
                                    entity.SaveChanges();
                                }
                            }
                        }
                    }
                }
                ///////////////////////////Awards///////////////////////////////////////////////////////


                if (model.AwardsList != null)
                {
                    if (model.AwardsList.Count > 0)
                    {
                        // retrive all existing awards of this project delete them then add all new awards
                        List <ProjectAward> pAward = (from s in db.ProjectAwards where s.ProjectId == model.SPtId select s).ToList();
                        if (pAward != null)
                        {
                            foreach (var item in pAward)
                            {
                                //delete them from ProjectAward table
                                ProjectAward PAward = db.ProjectAwards.Find(item.Id);
                                db.Entry(PAward).State = EntityState.Deleted;
                                db.SaveChanges();
                                //delete them from Award table

                                Award Award = db.Awards.Find(item.AwardId);
                                var   itemm = item.AwardId;
                                db.Entry(Award).State = EntityState.Deleted;
                                db.SaveChanges();
                            }
                        }
                        // Now I will add every award again to award table and mapping table
                        for (int i = 0; i < model.AwardsList.Count; i++)
                        {
                            Award award = new Award();

                            award.awardName = model.AwardsList.ElementAt(i).Trim();

                            using (Entities entity = new Entities())
                            {
                                entity.Awards.Add(award);
                                entity.SaveChanges();
                            }
                            //get last added award id
                            Award        LastAward    = db.Awards.OrderByDescending(o => o.awardId).FirstOrDefault();
                            ProjectAward projectAward = new ProjectAward();
                            projectAward.AwardId   = LastAward.awardId;
                            projectAward.ProjectId = model.SPtId;
                            using (Entities entity = new Entities())
                            {
                                entity.ProjectAwards.Add(projectAward);
                                entity.SaveChanges();
                            }
                        }
                    }
                }

                ///////////////programming languages//////////////////////////////////////////////////
                string PL = "";
                //set programming languages in one string
                if (model.PLList != null)
                {
                    if (model.PLList.Count > 0)
                    {
                        for (int i = 0; i < model.PLList.Count - 1; i++)
                        {
                            if (model.PLList.ElementAt(i).Trim() != "")
                            {
                                PL += model.PLList.ElementAt(i).Trim().ToLower() + ",";
                            }
                        }
                        if (model.PLList.ElementAt(model.PLList.Count - 1).Trim() != "")
                        {
                            PL += model.PLList.ElementAt(model.PLList.Count - 1).Trim().ToLower();
                        }
                    }
                }

                ProjectEdit(model.SPtId, model.SPName, model.SPVideos, model.SPAbstract, model.SPGrade, PL, model.Section, model.Year, File);
                return(new JsonResult
                {
                    Data = "Saved successfully"
                });
            }
            catch (Exception ex)
            {
                return(new JsonResult
                {
                    Data = "an error occurred " + ex.Message
                });
            }
        }
        public ActionResult SaveReport(ProjectVM model)
        {
            try
            {
                AreaOFInterest aoi  = new AreaOFInterest();
                ProjectsAOI    paoi = new ProjectsAOI();
                List <int>     Ids  = new List <int>();

                if (model.AOIList != null)
                {
                    if (model.AOIList.Count > 0)
                    {
                        for (var i = 0; i < model.AOIList.Count; i++)
                        {
                            var item = model.AOIList.ElementAt(i).Trim().ToUpper();

                            var area = from s in db.AreaOFInterests where s.AOIName.ToUpper() == item select s;
                            //if area not found it will be added to the table
                            if (!area.Any())
                            {
                                aoi.AOIName = model.AOIList.ElementAt(i).Trim();

                                using (Entities entity = new Entities())
                                {
                                    entity.AreaOFInterests.Add(aoi);
                                    entity.SaveChanges();
                                }
                            }
                        }
                        for (var i = 0; i < model.AOIList.Count; i++)
                        {
                            var item = model.AOIList.ElementAt(i).Trim().ToUpper();

                            int areaId = (from FC in db.AreaOFInterests
                                          where FC.AOIName.ToUpper() == item
                                          select FC.AOIId).ToList().FirstOrDefault();

                            //assigen area to tables
                            if (areaId != 0)
                            {
                                paoi.AOIId = areaId;
                                paoi.SPtId = model.SPtId;
                                using (Entities entity = new Entities())
                                {
                                    entity.ProjectsAOIs.Add(paoi);
                                    entity.SaveChanges();
                                }
                            }
                        }
                    }
                }
                Award award = new Award();

                if (model.AwardsList != null)
                {
                    if (model.AwardsList.Count > 0)
                    {
                        for (int i = 0; i < model.AwardsList.Count; i++)
                        {
                            award.awardName = model.AwardsList.ElementAt(i).Trim();

                            using (Entities entity = new Entities())
                            {
                                entity.Awards.Add(award);
                                entity.SaveChanges();
                            }
                        }
                        /*********************************Add awards to projectAward table (to mapping between project and award)*******************************************************************/
                        ProjectAward projectAward = new ProjectAward();
                        for (int i = 0; i < model.AwardsList.Count; i++)
                        {
                            var item = model.AwardsList.ElementAt(i).Trim();
                            //get IDs of each award
                            int?awardId = (from FC in db.Awards
                                           where FC.awardName == item
                                           select FC.awardId).ToList().FirstOrDefault();
                            projectAward.ProjectId = model.SPtId;
                            projectAward.AwardId   = (int)awardId;
                            //assigen awards to mapping tables
                            if (awardId != null)
                            {
                                using (Entities entity = new Entities())
                                {
                                    entity.ProjectAwards.Add(projectAward);
                                    entity.SaveChanges();
                                }
                            }
                        }
                    }
                }
                string PL = "";
                //set programming languages in one string
                if (model.PLList != null)
                {
                    if (model.PLList.Count > 0)
                    {
                        for (int i = 0; i < model.PLList.Count - 1; i++)
                        {
                            PL += model.PLList.ElementAt(i).Trim().ToLower() + ",";
                        }
                        PL += model.PLList.ElementAt(model.PLList.Count - 1).Trim().ToLower();
                    }
                }

                ProjectEdit(model.SPtId, model.SPName, model.SPVideos, model.SPAbstract, model.SPGrade, PL);
                return(new JsonResult
                {
                    Data = "Saved successfully"
                });
            }
            catch (Exception ex)
            {
                return(new JsonResult
                {
                    Data = "an error occurred " + ex.Message
                });
            }
        }