public ActionResult Modifier(int id)
        {
            tb_ads ad = annoncesRepository.GetById(id);

            if (ad == null)
            {
                throw new HttpException(404, "Annonce not found");
            }

            ModifierAdsViewModel model = new ModifierAdsViewModel
            {
                id_ad          = ad.id_ad,
                ad_title       = ad.ad_title,
                id_section     = ad.id_section,
                id_category    = ad.id_category,
                ad_description = ad.ad_description,
                ad_price       = (double)ad.ad_price,
                id_devise      = (int)ad.id_devise
            };



            ViewBag.id_devise         = new SelectList(deviseRepository.GetAll(), "id_devise", "description", model.id_devise);
            ViewBag.selected_category = this.populateCategoryBySection();

            return(View(model));
        }
Example #2
0
 public AnnonceModel Create(tb_ads ad)
 {
     return(new AnnonceModel
     {
         id_ad = ad.id_ad,
         ad_description = ad.ad_description,
         ad_title = ad.ad_title,
         image_filename = String.IsNullOrEmpty(ad.tb_ad_image.FirstOrDefault().image_filename) ? "default-img.jpg" : ad.tb_ad_image.FirstOrDefault().image_filename,
         devise = ad.tb_devise.description,
         price = ad.ad_price,
         category_title = ad.tb_category.category_title,
         commune_name = ad.tb_commune.commune,
         ad_status = ad.ad_status
     });
 }
Example #3
0
        public ActionResult Valider(int id)
        {
            tb_ads ad = annoncesRepository.GetById(id);

            if (ad == null)
            {
                throw new HttpException(404, "Annonce not found");
            }

            ad.ad_status       = 1; // set to active
            ad.ad_is_published = 1;
            annoncesRepository.Update(ad);
            annoncesRepository.Save();

            return(RedirectToAction("Index", "Annonces", new { area = "Admin" }));
        }
        public ActionResult Publier(int id)
        {
            tb_ads ad = annoncesRepository.GetById(id);

            if (ad == null)
            {
                throw new HttpException(404, "Annonce not found");
            }

            //if pending cannot published
            if (ad.ad_status < 3)
            {
                ad.ad_status = 1; // set to active
                annoncesRepository.Update(ad);
                annoncesRepository.Save();
            }

            return(RedirectToAction("Index", "MesAnnonces"));
        }
        public ActionResult vendu(int id)
        {
            tb_ads ad = annoncesRepository.GetById(id);

            if (ad == null)
            {
                throw new HttpException(404, "Annonce not found");
            }

            try
            {
                ad.ad_status = 4; // set to vendu
                annoncesRepository.Update(ad);
                annoncesRepository.Save();
                return(RedirectToAction("Index", "MesAnnonces"));
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0},{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);

                        raise = new InvalidOperationException(message, raise);
                    }
                }
                TempData["Error"] = raise.Message;
                return(RedirectToAction("Index", "MesAnnonces"));
            }
            catch (Exception exp)
            {
                TempData["Error"] = exp.Message;
                return(RedirectToAction("Index", "MesAnnonces"));
            }
        }
        public ActionResult Supprimer(int id)
        {
            tb_ads ad = annoncesRepository.GetById(id);

            if (ad == null)
            {
                throw new HttpException(404, "Annonce not found");
            }

            try
            {
                annoncesRepository.DeleteAdsImages(ad);
                annoncesRepository.Delete(ad);
                annoncesRepository.Save();
                return(RedirectToAction("Index", "MesAnnonces"));
            }
            catch (DataException exp)
            {
                TempData["Error"] = exp.Message;
                return(RedirectToAction("Index", "MesAnnonces"));
            }
        }
        public ActionResult Modifier(ModifierAdsViewModel model)
        {
            if (ModelState.IsValid)
            {
                tb_ads ad = annoncesRepository.GetById(model.id_ad);
                if (ad == null)
                {
                    throw new HttpException(404, "Annonce not found");
                }

                int?section = catRepository.GetAll().Where(c => c.id_category == model.id_category).FirstOrDefault().id_section;

                try
                {
                    ad.ad_title       = model.ad_title;
                    ad.ad_description = model.ad_description;
                    ad.ad_price       = (double)model.ad_price;
                    ad.id_devise      = model.id_devise;
                    ad.id_section     = (int)section;
                    ad.id_category    = model.id_category;
                    ad.ad_phone       = ad.ad_phone;

                    annoncesRepository.Update(ad);
                    annoncesRepository.Save();

                    return(RedirectToAction("Index", "MesAnnonces"));
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;

                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0},{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);

                            raise = new InvalidOperationException(message, raise);
                        }
                    }

                    ModelState.AddModelError("", raise.Message);
                    ViewBag.selected_devise   = new SelectList(deviseRepository.GetAll(), "id_devise", "description", (object)model.id_devise);
                    ViewBag.selected_category = this.populateCategoryBySection();
                    return(View(model));
                }
                catch (Exception exp) {
                    ModelState.AddModelError("", exp.Message);
                    ViewBag.selected_devise   = new SelectList(deviseRepository.GetAll(), "id_devise", "description", (object)model.id_devise);
                    ViewBag.selected_category = this.populateCategoryBySection();
                    return(View(model));
                }
            }

            ModelState.AddModelError("", "model not valid");
            ViewBag.selected_devise   = new SelectList(deviseRepository.GetAll(), "id_devise", "description", (object)model.id_devise);
            ViewBag.selected_category = this.populateCategoryBySection();
            return(View(model));
        }
        public ActionResult Create(tb_ads annonces, HttpPostedFileBase image1, HttpPostedFileBase image2, HttpPostedFileBase image3)
        {
            try
            {
                IEnumerable <tb_category> category = catRepository.GetAll();

                List <HttpPostedFileBase> imageList = new List <HttpPostedFileBase>();

                if (image1 != null)
                {
                    imageList.Add(image1);
                }
                if (image2 != null)
                {
                    imageList.Add(image2);
                }
                if (image3 != null)
                {
                    imageList.Add(image3);
                }

                if (annonces.id_category != null)
                {
                    // get section of selected category
                    int?           section = category.Where(c => c.id_category == annonces.id_category).Select(x => x.id_section).FirstOrDefault();
                    MembershipUser user    = Membership.GetUser();

                    annonces.id_user         = (Guid)user.ProviderUserKey;
                    annonces.id_account      = 2;
                    annonces.ad_code         = Guid.NewGuid().ToString();
                    annonces.ad_date_created = DateTime.Now;
                    annonces.ad_date_expired = DateTime.Now;
                    annonces.id_departement  = 1;
                    annonces.ad_name         = "test user";
                    annonces.ad_is_published = 0;
                    annonces.ad_status       = 1;
                    annonces.id_section      = section;
                    ModelState.Clear();


                    // TODO: Add insert logic here
                    if (ModelState.IsValid)
                    {
                        if (imageList.Count > 0)
                        {
                            annoncesRepository.Add(annonces);
                            annoncesRepository.Save();

                            foreach (var file in imageList)
                            {
                                if (file.ContentLength > 0)
                                {
                                    var fileName          = Path.GetFileName(file.FileName);
                                    var extension         = Path.GetExtension(fileName);
                                    var guid              = Guid.NewGuid().ToString();
                                    var directory         = "C:";
                                    var filepathlarge     = Path.Combine(directory + "/Photos/large/", guid + extension);
                                    var filepathmedium    = Path.Combine(directory + "/Photos/medium/", guid + extension);
                                    var filepaththumbnail = Path.Combine(directory + "/Photos/thumbnail/", guid + extension);

                                    //var filepathlarge = Path.Combine(Server.MapPath("~/Photos/large/"), guid + extension);
                                    //var filepathmedium = Path.Combine(Server.MapPath("~/Photos/medium/"), guid + extension);
                                    //var filepaththumbnail = Path.Combine(Server.MapPath("~/Photos/thumbnail/"), guid + extension);


                                    tb_ad_image ads_photo = new tb_ad_image
                                    {
                                        ad_code        = annonces.ad_code,
                                        id_ad          = annonces.id_ad,
                                        image_filename = guid + extension,
                                        image_path     = filepaththumbnail
                                    };


                                    file.SaveAs(filepathlarge);

                                    Instructions medium    = new Instructions("width=800&height=600&format=jpg&mode=max");
                                    Instructions thumbnail = new Instructions("width=72&height=72&format=jpg&mode=max");

                                    //Let the image builder add the correct extension based on the output file type (which may differ).
                                    ImageJob imedium    = new ImageJob(filepathlarge, filepathmedium, medium, false, true);
                                    ImageJob ithumbnail = new ImageJob(filepathlarge, filepaththumbnail, thumbnail, false, true);

                                    imedium.Build();
                                    ithumbnail.Build();

                                    imageRepository.Add(ads_photo);
                                    imageRepository.Save();
                                }
                            }

                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            annoncesRepository.Add(annonces);
                            annoncesRepository.Save();

                            tb_ad_image ads_photo = new tb_ad_image
                            {
                                ad_code        = annonces.ad_code,
                                id_ad          = annonces.id_ad,
                                image_filename = "default-img.jpg",
                                image_path     = "~/Photos/default-img.jpg"
                            };

                            imageRepository.Add(ads_photo);
                            imageRepository.Save();



                            return(RedirectToAction("Index"));
                        }
                    }
                }
                ModelState.AddModelError("Error:001", "An Error has been detected,please review submitted data.");

                ViewBag.ad_type           = new SelectList(adtypeRepository.GetAll(), "id_ad_type", "description");
                ViewBag.id_commune        = new SelectList(communeRepository.GetAll(), "id_commune", "commune");
                ViewBag.id_devise         = new SelectList(deviseRepository.GetAll(), "id_devise", "symbole");
                ViewBag.id_category       = this.populateCategoryBySection();
                ViewBag.id_item_condition = new SelectList(condRepository.GetAll(), "id_item_condition", "item_condition");
                return(View(annonces));
            }
            catch (Exception exp)
            {
                ModelState.AddModelError("An Error has been detected,please review submitted data.", exp.Message);

                ViewBag.ad_type           = new SelectList(adtypeRepository.GetAll(), "id_ad_type", "description");
                ViewBag.id_commune        = new SelectList(communeRepository.GetAll(), "id_commune", "commune");
                ViewBag.id_devise         = new SelectList(deviseRepository.GetAll(), "id_devise", "symbole");
                ViewBag.id_category       = this.populateCategoryBySection();
                ViewBag.id_item_condition = new SelectList(condRepository.GetAll(), "id_item_condition", "item_condition");
                return(View(annonces));
            }
        }