Пример #1
0
        public async Task <Item> PostItemAsync(PostAdModel model)
        {
            DateTime time = DateTime.Now;

            Item item = new Item()
            {
                CategoryID     = model.CategoryId,
                Name           = model.Title,
                Description    = model.Description,
                IsDeleted      = false,
                Price          = model.Price,
                SellerID       = model.SellerId,
                LocationID     = model.LocationId,
                Documents      = model.Images,
                CreatedDate    = time,
                CreatedDateUtc = time.ToUniversalTime(),
                CoverPhotoId   = model.Images[0].Id,
                PictureCount   = model.Images.Count
            };

            _UnitOfWork.ItemRepository.Insert(item);
            await _UnitOfWork.SaveChangesAsync();

            return(item);
        }
Пример #2
0
        public async Task <ActionResult> Add(ItemPostViewModel model, List <HttpPostedFileBase> images)
        {
            List <Document> documents = new List <Document>();

            foreach (var img in images)
            {
                Document document = new Document()
                {
                    Id          = Guid.NewGuid(),
                    ContentType = img.ContentType,
                    Extension   = Path.GetExtension(img.FileName),
                    Name        = img.FileName,
                    Content     = await img.InputStream.ToByteArrayAsync()
                };

                documents.Add(document);
            }

            Seller seller = await _unitOfWork.SellerRepository.GetSellerByAccountIdAsync(User.Identity.GetUserId());

            PostAdModel postedAd = new PostAdModel()
            {
                CategoryId  = model.CategoryId.Value,
                Description = model.Description,
                Title       = model.Name,
                LocationId  = model.LocationId.Value,
                Price       = model.Price.Value,
                Images      = documents,
                SellerId    = seller.Id
            };
            await _ItemService.PostItemAsync(postedAd);

            return(RedirectToAction("Add"));
        }
        //public ActionResult Index1()
        //{

        //    return View(ClassifiedApi.GetAllPosts());
        //}

        // GET: Classified/Create
        public ActionResult PostAd(int?categoryID)
        {
            Session["categoryID"] = categoryID;

            if (Session["User"] == null)
            {
                return(RedirectToAction("Login", "Login"));
            }

            PostAdModel postAdModel = new PostAdModel();

            postAdModel.User = new TAC_ClassifiedContact();
            TAC_User model  = (TAC_User)Session["User"];
            var      record = dbContext.TAC_User.Find(model.UserId);

            postAdModel.User.ContactName  = record.First_Name + " " + record.Last_Name;
            postAdModel.User.ContactPhone = record.Phone;
            postAdModel.User.ContactCity  = record.City;
            return(View(postAdModel));
        }
Пример #4
0
        public IActionResult PostAd([FromBody] PostAdModel data)
        {
            if (string.IsNullOrWhiteSpace(data.Name))
            {
                return(BadRequest("Invalid input :" + nameof(data.Name)));
            }
            if (string.IsNullOrWhiteSpace(data.Occupation))
            {
                return(BadRequest("Invalid input :" + nameof(data.Occupation)));
            }
            int inMemoryCachyExpireDays = Convert.ToInt32(_configuration["InMemoryCacheDays"]);

            if (inMemoryCachyExpireDays <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(inMemoryCachyExpireDays));
            }
            string fileName = _configuration["AdHtmlTemplateFileName"];

            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentOutOfRangeException(nameof(fileName));
            }
            string bucketName = _configuration["AdBucketNameInGoogleCloudStorage"];

            if (string.IsNullOrWhiteSpace(bucketName))
            {
                throw new ArgumentOutOfRangeException(nameof(bucketName));
            }
            Guid   assetKey            = Guid.NewGuid();
            string objectNameWithExt   = string.Format("{0}{1}", assetKey.ToString("N"), Path.GetExtension(fileName));
            string contentType         = Utility.GetMimeTypes()[Path.GetExtension(fileName)];
            var    anonymousDataObject = data.ConvertToAnonymousType(data);

            _adService.UploadObjectInGoogleStorage(fileName, inMemoryCachyExpireDays, objectNameWithExt, bucketName, anonymousDataObject, contentType, Constants.AD_HTML_FILE_TEMPLATE);
            return(Ok(new { Name = "Chinna", Email = "*****@*****.**", ObjectName = objectNameWithExt }));
        }
        public ActionResult PostAd(PostAdModel postAdModel, HttpPostedFileBase fileUpload)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    postAdModel.Classified.Summary    = postAdModel.Classified.Description;
                    postAdModel.Classified.PostedDate = DateTime.Now;
                    if (Session["User"] != null)
                    {
                        TAC_User model = (TAC_User)Session["User"];
                        postAdModel.Classified.CreatedBy = model.UserId;
                    }
                    else
                    {
                        return(View());
                    }
                    if (Session["categoryID"] != null)
                    {
                        postAdModel.Classified.CategoryId = (int)Session["categoryID"];
                    }
                    else
                    {
                        ModelState.AddModelError("CategoryId", "Please select category");
                        return(View(model: postAdModel));
                    }
                    //fileupload logic
                    if (Request.Files.Count > 0)
                    {
                        int MaxContentLength = 1024 * 1024 * 3; //3 MB
                        var file             = Request.Files[0];

                        if (file != null && file.ContentLength > 0 && file.ContentLength < MaxContentLength)
                        {
                            var fileName = Path.GetFileName(file.FileName);
                            var path     = Path.Combine(Server.MapPath("../Resources/Uploaded_Images/"), fileName);
                            file.SaveAs(path);
                            postAdModel.Classified.ClassifiedImage = Path.Combine("../Resources/Uploaded_Images/", fileName);
                        }
                        else
                        {
                            if (file.ContentLength > MaxContentLength)
                            {
                                ViewBag.Message = "Please upload an image less 3MB ";
                            }
                        }
                    }

                    dbContext.TAC_Classified.Add(postAdModel.Classified);
                    dbContext.SaveChanges();
                    postAdModel.User.ClassifiedId = postAdModel.Classified.ClassifiedId;
                    dbContext.TAC_ClassifiedContact.Add(postAdModel.User);
                    dbContext.SaveChanges();

                    return(RedirectToAction("MyAccount"));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = "Sorry, your data was not saved.";
            }
            return(View(postAdModel));
        }