Ejemplo n.º 1
0
        public IHttpActionResult Post(RentAd rentAd)
        {
            repo.Insert(rentAd);
            string uri = Url.Link("GetRentAdById", new { id = rentAd.Id });

            return(Created(uri, rentAd));
        }
Ejemplo n.º 2
0
        public IHttpActionResult Get(int id)
        {
            RentAd rentAd = repo.Get(id);

            if (rentAd == null)
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
            return(Ok(rentAd));
        }
Ejemplo n.º 3
0
 public ActionResult UploadAd(RentAd rent)
 {
     if (ModelState.IsValid)
     {
         repo.Insert(rent);
         return(RedirectToAction("Profile", "User"));
     }
     else
     {
         return(View(rent));
     }
 }
Ejemplo n.º 4
0
 public IHttpActionResult Put([FromBody] RentAd rentAd, [FromUri] int id)
 {
     rentAd.Id = id;
     repo.Update(rentAd);
     return(Ok(rentAd));
 }
Ejemplo n.º 5
0
 public ActionResult Edit(RentAd rent)
 {
     repo.Edit(rent);
     return(RedirectToAction("Profile", "User"));
 }
Ejemplo n.º 6
0
        public ActionResult Create(AdsCreateViewModel viewModel)
        {
            var extension = "";

            if (viewModel.ThumbnailFile != null)
            {
                extension = Path.GetExtension(viewModel.ThumbnailFile.FileName).ToLower();


                if (viewModel.ThumbnailFile.ContentLength / 1024 >= 150)
                {
                    ModelState.AddModelError("ThumbnailFile", "سایز فایل باید کمتر از ۱۵۰ کیلو بایت باشد");
                    ModelState.AddModelError("", "این خطای فرم نمونه است");
                }
                if (
                    !(extension == ".jpg" || extension == ".jpeg" ||
                      extension == ".png" || extension == ".gif")
                    )
                {
                    ModelState.AddModelError("ThumbnailFile", "فرمت تصویر مورد قبول نیست");
                }
            }
            AdDbContext ctx = new AdDbContext();

            if (ModelState.IsValid)
            {
                var fileName = $"{Guid.NewGuid().ToString()}{extension}";
                var fullPath = Path.Combine(Server.MapPath("~/images/thumbnails"), fileName);
                viewModel.ThumbnailFile.SaveAs(fullPath);
                var thumbnailClientPath = $"/images/thumbnails/{fileName}";

                Ad ad;
                if (viewModel.AdType == "sale")
                {
                    ad = new SaleAd()
                    {
                        Age                = viewModel.Age,
                        Area               = viewModel.Area,
                        CategoryId         = viewModel.CategoryId,
                        Title              = viewModel.Title,
                        UnitCount          = viewModel.UnitCount,
                        PricePerUnit       = viewModel.PricePerUnit,
                        ThumbnailPhotoPath = thumbnailClientPath
                    };
                }
                else
                {
                    ad = new RentAd()
                    {
                        Age                = viewModel.Age,
                        Area               = viewModel.Area,
                        CategoryId         = viewModel.CategoryId,
                        Title              = viewModel.Title,
                        UnitCount          = viewModel.UnitCount,
                        ThumbnailPhotoPath = thumbnailClientPath,
                        Diposite           = viewModel.Diposite ?? 0,
                        Rent               = viewModel.Rent ?? 0
                    };
                }

                if (viewModel.MediaFile1 != null)
                {
                    AddMedia(viewModel.MediaFile1, ad);
                }
                if (viewModel.MediaFile2 != null)
                {
                    AddMedia(viewModel.MediaFile2, ad);
                }
                if (viewModel.MediaFile3 != null)
                {
                    AddMedia(viewModel.MediaFile3, ad);
                }

                ctx.Ads.Add(ad);
                ctx.SaveChanges();

                TempData["Message"] = "ثبت با موفقیت انجام شد.";
                return(RedirectToAction("Index", "Ads"));
            }
            TempData["Message"]      = "خطایی رخ داده";
            TempData["MessageClass"] = "danger";
            ViewBag.CategoryId       = new SelectList(ctx.Categories.ToList(), "Id", "Title");
            ViewBag.AdType           = new SelectList(LookupHelper.GetAdTypesLookup(), "Value", "Text");
            return(View(viewModel));
        }