private async Task <AdvertisementItemDetails> MapToDetailsViewModel(AdvertisementItem advertisementFromDb)
        {
            var viewModel = new AdvertisementItemDetails();

            viewModel.Id                = advertisementFromDb.Id;
            viewModel.CityName          = advertisementFromDb.CityName != "brak" ? advertisementFromDb.CityName : String.Empty;
            viewModel.Title             = advertisementFromDb.Title;
            viewModel.Description       = advertisementFromDb.Description;
            viewModel.Size              = advertisementFromDb.Size;
            viewModel.CategoryInfoModel = new Models.Shared.Categories.CategoryInfoModel {
                Id = advertisementFromDb.CategoryId, Name = advertisementFromDb.Category.Name
            };
            viewModel.Price          = advertisementFromDb.Price;
            viewModel.IsOnlyForSell  = advertisementFromDb.IsOnlyForSell;
            viewModel.SellerId       = advertisementFromDb.UserId;
            viewModel.SellerName     = advertisementFromDb.User.UserName;
            viewModel.IsActive       = advertisementFromDb.ExpirationDate > DateTime.Now;
            viewModel.ExpirationDate = advertisementFromDb.ExpirationDate.Value;
            viewModel.Photos         = await GetPhotosList(advertisementFromDb.AdvertisementPhotos.Where(p => !p.IsMainPhoto).ToList());

            viewModel.IsSellerOnline = this.chatHubCacheService.IsUserConnected(advertisementFromDb.UserId);
            if (!String.IsNullOrEmpty(advertisementFromDb.User.UserProfilePhotoName))
            {
                viewModel.SellerProfileImage = await photosService.GetUserProfilePhotoInBytes(advertisementFromDb.User.UserProfilePhotoName);
            }
            return(viewModel);
        }
        public ActionResult EditAdvertisementItem(AdvertisementViewModel model)
        {
            try
            {
                string            statusmessage = "";
                string            dateNow       = CustomDataHelper.CurrentDateTimeSL.GetCurrentDate().ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz");
                AdvertisementItem advertisement = new AdvertisementItem()
                {
                    ItemId       = model.Item.ItemId,
                    ItemName     = model.Item.ItemName,
                    FileURL      = model.Item.FileURL,
                    ModifiedDate = dateNow
                };
                if (model.Item.UploadFile != null && model.Item.UploadFile.ContentLength > 0)
                {
                    if (CheckPostedFileType(model.Item.UploadFile, "Video"))
                    {
                        if (CheckPostedFileLength(model.Item.UploadFile, "Video"))
                        {
                            //string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
                            var    uploadDir      = "/Content/Advertisement/AdvertisementVideo/";
                            string extension      = Path.GetExtension(model.Item.UploadFile.FileName);
                            string uniqueFileName = Guid.NewGuid().ToString();
                            var    videoPath      = Path.Combine(Server.MapPath(uploadDir), uniqueFileName + extension);
                            var    videoUrl       = Path.Combine(uploadDir, uniqueFileName + extension);
                            model.Item.UploadFile.SaveAs(videoPath);

                            AdvertisementItem advertisementget = _advertisementItemService.GetAdvertisementItemById(model.Item.ItemId);
                            if (!string.IsNullOrEmpty(advertisementget.FileURL))
                            {
                                if (System.IO.File.Exists(Server.MapPath(advertisementget.FileURL)))
                                {
                                    System.IO.File.Delete(Server.MapPath(advertisementget.FileURL));
                                }
                            }
                            advertisement.FileURL = videoUrl;
                        }
                        else
                        {
                            statusmessage = "Video File should be less than 10MB";
                        }
                    }
                    else
                    {
                        statusmessage = "Video File should be .mp4 format";
                    }
                }
                _advertisementItemService.UpdateAdvertisementItem(advertisement);
                return(RedirectToAction("Index", "Advertisement", new { message = statusmessage }));
            }
            catch (Exception ex)
            {
                return(Json(ex));
            }
        }
        private async Task <AdvertisementBasicDto> FillAdvertisement(AdvertisementItem source)
        {
            AdvertisementBasicDto result;

            switch (source)
            {
            case Game g:
                result = mapper.Map <Advertisement, AdvertisementGameDto>(g.Advertisement);
                mapper.Map(g, result);
                result.Discriminator = nameof(Game);
                var tempGenre = await genreRepository.GetAsync(g.GenreId);

                var tempRegionGame = await regionRepository.GetAsync(g.GameRegionId);

                ((AdvertisementGameDto)result).Genre  = mapper.Map <Genre, GenreDto>(tempGenre);
                ((AdvertisementGameDto)result).Region = mapper.Map <Region, RegionDto>(tempRegionGame);
                break;

            case Console c:
                result = mapper.Map <Advertisement, AdvertisementConsoleDto>(c.Advertisement);
                mapper.Map(c, result);
                result.Discriminator = nameof(Console);
                var tempRegionConsole = await regionRepository.GetAsync(c.ConsoleRegionId);

                ((AdvertisementConsoleDto)result).Region = mapper.Map <Region, RegionDto>(tempRegionConsole);
                break;

            case Accessory a:
                result = mapper.Map <Advertisement, AdvertisementAccessoryDto>(a.Advertisement);
                mapper.Map(a, result);
                result.Discriminator = nameof(Accessory);
                break;

            default:
                throw new NotSupportedException();
            }

            var state = await stateRepository.GetAsync(source.StateId);

            var system = await systemRepository.GetAsync(source.SystemId);

            result.State  = mapper.Map <State, StateDto>(state);
            result.System = mapper.Map <Models.System, SystemDto>(system);

            if (source.Advertisement.ShowEmail)
            {
                result.Email = source.Advertisement.User.Email;
            }

            if (source.Advertisement.ShowPhone && !string.IsNullOrEmpty(source.Advertisement.User.PhoneNumber))
            {
                result.PhoneNumber = source.Advertisement.User.PhoneNumber;
            }
            return(result);
        }
        private async Task UpdateCityNameIfDoesNotSetYet(AdvertisementItem advertisementFromDb)
        {
            if (String.IsNullOrEmpty(advertisementFromDb.CityName))
            {
                var city = await googleMapsAPIService.GetCity(advertisementFromDb.Latitude, advertisementFromDb.Longitude);

                if (!String.IsNullOrEmpty(city))
                {
                    advertisementFromDb.CityName = city;
                }
                else
                {
                    advertisementFromDb.CityName = "brak";
                }

                this.advertisementItemDbService.SaveAdvertisementItem(advertisementFromDb);
            }
        }
        public bool DeleteAdvertisement(int advertisementId, string userId)
        {
            AdvertisementItem advertisement = this.advertisementItemDbService.GetById(advertisementId);

            if (advertisement == null)
            {
                throw new Exception("Nie znaleziono ogłoszenia o podanym ID");
            }

            if (advertisement.UserId != userId)
            {
                throw new Exception("Próba usunięcia ogłoszenia przez nieuprawnionego usera");
            }

            advertisement.ExpirationDate = DateTime.Now;

            this.advertisementItemDbService.SaveAdvertisementItem(advertisement);

            return(true);
        }
 public ActionResult DeleteAdvertisementItem(int id)
 {
     try
     {
         string            statusmessage    = "";
         AdvertisementItem advertisementget = _advertisementItemService.GetAdvertisementItemById(id);
         if (!string.IsNullOrEmpty(advertisementget.FileURL))
         {
             if (System.IO.File.Exists(Server.MapPath(advertisementget.FileURL)))
             {
                 System.IO.File.Delete(Server.MapPath(advertisementget.FileURL));
             }
         }
         _advertisementItemService.DeleteAdvertisementItem(id);
         return(RedirectToAction("Index", "Advertisement", new { message = statusmessage }));
     }
     catch (Exception ex)
     {
         return(View(ex));
     }
 }
        public void CreateNewAdvertisementItem(NewAdvertisementItem newAdvertisementModel, string userId)
        {
            AdvertisementItem model = null;

            if (newAdvertisementModel.Id > 0)
            {
                model                = this.advertisementItemDbService.GetByIdWithPhotos(newAdvertisementModel.Id);
                model.Title          = newAdvertisementModel.AdvertisementTitle;
                model.Description    = newAdvertisementModel.AdvertisementDescription;
                model.Size           = newAdvertisementModel.Size;
                model.Price          = newAdvertisementModel.AdvertisementPrice;
                model.ExpirationDate = GetExpirationDate();
                model.Latitude       = newAdvertisementModel.Latitude;
                model.Longitude      = newAdvertisementModel.Longitude;
                model.IsOnlyForSell  = newAdvertisementModel.IsOnlyForSell;
                model.CategoryId     = newAdvertisementModel.Category.Id;
                model.AdvertisementPhotos.AddRange(CreateAdvertisementPhotosModels(newAdvertisementModel.PhotosNames));
            }
            else
            {
                model = new AdvertisementItem
                {
                    UserId              = userId,
                    Title               = newAdvertisementModel.AdvertisementTitle,
                    Description         = newAdvertisementModel.AdvertisementDescription,
                    Size                = newAdvertisementModel.Size,
                    Price               = newAdvertisementModel.AdvertisementPrice,
                    CreationDate        = DateTime.Now,
                    ExpirationDate      = GetExpirationDate(),
                    Latitude            = newAdvertisementModel.Latitude,
                    Longitude           = newAdvertisementModel.Longitude,
                    IsOnlyForSell       = newAdvertisementModel.IsOnlyForSell,
                    CategoryId          = newAdvertisementModel.Category.Id,
                    AdvertisementPhotos = CreateAdvertisementPhotosModels(newAdvertisementModel.PhotosNames)
                };
            }

            this.advertisementItemDbService.SaveNewAdvertisementItem(model);
        }
        public void SaveNewAdvertisementItem(AdvertisementItem advertisementItem)
        {
            if (advertisementItem.Id > 0)
            {
                var oldPhotos = advertisementItem.AdvertisementPhotos.Where(a => a.AdvertisementPhotoId > 0);
                foreach (var photo in oldPhotos)
                {
                    dbContext.Entry(photo).State = EntityState.Deleted;
                }
                var newPhotos = advertisementItem.AdvertisementPhotos.Where(a => a.AdvertisementPhotoId == 0);
                foreach (var photo in newPhotos)
                {
                    dbContext.Entry(photo).State = EntityState.Added;
                }
                dbContext.Entry(advertisementItem).State = EntityState.Modified;
            }
            else
            {
                dbContext.AdvertisementItem.Add(advertisementItem);
                dbContext.Entry(advertisementItem).State = EntityState.Added;
            }

            dbContext.SaveChanges();
        }
        private void SaveAdvertisement(HttpContext context)
        {
            try
            {
                string Id                = context.Request.Form["ctl00$cphMain$hId"].Trim();
                string title             = context.Request.Form["ctl00$cphMain$txtTitle"].Trim();
                string sSiteFunId        = context.Request.Form["siteFunId"].Trim();
                string sLayoutPositionId = context.Request.Form["layoutPositionId"].Trim();
                string sTimeout          = context.Request.Form["ctl00$cphMain$txtTimeout"].Trim();
                string sAdLinkJson       = context.Request.Form["adLinkJson"].Trim();
                sAdLinkJson = sAdLinkJson.Trim(',');

                int timeout = 0;
                if (!string.IsNullOrWhiteSpace(sTimeout))
                {
                    int.TryParse(sTimeout, out timeout);
                }
                Guid siteFunId = Guid.Empty;
                Guid.TryParse(sSiteFunId, out siteFunId);
                Guid layoutPositionId = Guid.Empty;
                Guid.TryParse(sLayoutPositionId, out layoutPositionId);

                string sDescr  = context.Request.Form["ctl00$cphMain$txtaDescr"].Trim();
                string content = context.Request.Form["content"].Trim();
                content = HttpUtility.HtmlDecode(content);

                Guid gId = Guid.Empty;
                if (!string.IsNullOrWhiteSpace(Id))
                {
                    Guid.TryParse(Id, out gId);
                }

                AdvertisementInfo model = new AdvertisementInfo();
                model.Id = gId;
                model.LastUpdatedDate  = DateTime.Now;
                model.Title            = title;
                model.Timeout          = timeout;
                model.SiteFunId        = siteFunId;
                model.LayoutPositionId = layoutPositionId;

                AdvertisementItemInfo adiModel = null;
                if (gId.Equals(Guid.Empty))
                {
                    if (!(string.IsNullOrWhiteSpace(sDescr) && string.IsNullOrWhiteSpace(content)))
                    {
                        adiModel             = new AdvertisementItemInfo();
                        adiModel.Descr       = sDescr;
                        adiModel.ContentText = content;
                    }
                }
                else
                {
                    adiModel                 = new AdvertisementItemInfo();
                    adiModel.Descr           = sDescr;
                    adiModel.ContentText     = content;
                    adiModel.AdvertisementId = gId;
                }

                IList <AdvertisementLinkInfo> listAdLink = null;

                if (!string.IsNullOrWhiteSpace(sAdLinkJson))
                {
                    DataTable dtPostData = JsonConvert.DeserializeObject <DataTable>(sAdLinkJson);
                    if (dtPostData != null && dtPostData.Rows.Count > 0)
                    {
                        listAdLink = new List <AdvertisementLinkInfo>();

                        foreach (DataRow dr in dtPostData.Rows)
                        {
                            AdvertisementLinkInfo postAdlModel = new AdvertisementLinkInfo();
                            postAdlModel.Id = dr["AdLinkId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["AdLinkId"].ToString());
                            postAdlModel.AdvertisementId  = gId;
                            postAdlModel.ContentPictureId = dr["ContentPictureId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ContentPictureId"].ToString());
                            postAdlModel.ActionTypeId     = dr["ActionTypeId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ActionTypeId"].ToString());
                            postAdlModel.Url       = dr["Url"] == DBNull.Value ? "" : dr["Url"].ToString();
                            postAdlModel.Sort      = dr["Sort"] == DBNull.Value ? 0 : int.Parse(dr["Sort"].ToString());
                            postAdlModel.IsDisable = dr["IsDisable"] == DBNull.Value ? false : bool.Parse(dr["IsDisable"].ToString());

                            listAdLink.Add(postAdlModel);
                        }
                    }
                }

                Advertisement     bll    = new Advertisement();
                AdvertisementItem aiBll  = new AdvertisementItem();
                AdvertisementLink adlBll = new AdvertisementLink();

                int effect = -1;
                if (!gId.Equals(Guid.Empty))
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        bll.Update(model);
                        aiBll.Update(adiModel);
                        if (listAdLink != null)
                        {
                            foreach (var adlModel in listAdLink)
                            {
                                adlBll.Update(adlModel);
                            }
                        }

                        scope.Complete();

                        effect = 1;
                    }
                }
                else
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        Guid adId = bll.InsertByOutput(model);

                        if (listAdLink != null && listAdLink.Count > 0)
                        {
                            foreach (var adlModel in listAdLink)
                            {
                                adlModel.AdvertisementId = adId;
                                adlBll.Insert(adlModel);
                            }
                        }

                        if (adiModel != null)
                        {
                            adiModel.AdvertisementId = adId;
                            aiBll.Insert(adiModel);
                        }

                        scope.Complete();

                        effect = 1;
                    }
                }

                if (effect < 1)
                {
                    context.Response.Write("{\"success\": false,\"message\": \"操作失败,请正确输入\"}");
                    return;
                }

                context.Response.Write("{\"success\": true,\"message\": \"操作成功\"}");
            }
            catch (Exception ex)
            {
                context.Response.Write("{\"success\": false,\"message\": \"异常:" + ex.Message + "\"}");
            }
        }
        public void SaveAdvertisementItem(AdvertisementItem advertisement)
        {
            this.dbContext.Entry(advertisement).State = EntityState.Modified;

            this.dbContext.SaveChanges();
        }
Example #11
0
 public void UpdateAdvertisementItem(AdvertisementItem advertisement)
 {
     _resitory.UpdateAdvertisementItem(advertisement);
 }
Example #12
0
 public void SaveAdvertisementItem(AdvertisementItem advertisementItem)
 {
     _resitory.SaveAdvertisementItem(advertisementItem);
 }
Example #13
0
        private void SaveAdvertisement(HttpContext context)
        {
            try
            {
                string Id                = context.Request.Form["ctl00$cphMain$hId"].Trim();
                string title             = context.Request.Form["ctl00$cphMain$txtTitle"].Trim();
                string sSiteFunId        = context.Request.Form["siteFunId"].Trim();
                string sLayoutPositionId = context.Request.Form["layoutPositionId"].Trim();
                string sTimeout          = context.Request.Form["ctl00$cphMain$txtTimeout"].Trim();
                string sSort             = context.Request.Form["ctl00$cphMain$txtSort"].Trim();
                string sStartTime        = context.Request.Form["ctl00$cphMain$txtStartTime"].Trim();
                string sEndTime          = context.Request.Form["ctl00$cphMain$txtEndTime"].Trim();
                string sVirtualViewCount = context.Request.Form["ctl00$cphMain$txtVirtualViewCount"].Trim();
                string sIsDisable        = context.Request.Form["isDisable"].Trim();
                string sAdLinkJson       = context.Request.Form["adLinkJson"].Trim();
                sAdLinkJson = sAdLinkJson.Trim(',');

                int timeout = 0;
                if (!string.IsNullOrWhiteSpace(sTimeout))
                {
                    int.TryParse(sTimeout, out timeout);
                }
                Guid siteFunId = Guid.Empty;
                Guid.TryParse(sSiteFunId, out siteFunId);
                Guid layoutPositionId = Guid.Empty;
                Guid.TryParse(sLayoutPositionId, out layoutPositionId);
                int sort = 0;
                if (!string.IsNullOrWhiteSpace(sSort))
                {
                    int.TryParse(sSort, out sort);
                }
                int virtualViewCount = 0;
                if (!string.IsNullOrWhiteSpace(sVirtualViewCount))
                {
                    int.TryParse(sVirtualViewCount, out virtualViewCount);
                }
                DateTime startTime = DateTime.MinValue;
                if (!string.IsNullOrWhiteSpace(sStartTime))
                {
                    DateTime.TryParse(sStartTime, out startTime);
                }
                DateTime endTime = DateTime.MinValue;
                if (!string.IsNullOrWhiteSpace(sEndTime))
                {
                    DateTime.TryParse(sEndTime, out endTime);
                }

                string sDescr  = context.Request.Form["ctl00$cphMain$txtaDescr"].Trim();
                string content = context.Request.Form["content"].Trim();
                content = HttpUtility.HtmlDecode(content);

                Guid gId = Guid.Empty;
                if (!string.IsNullOrWhiteSpace(Id))
                {
                    Guid.TryParse(Id, out gId);
                }

                AdvertisementInfo model = new AdvertisementInfo();
                model.Id = gId;
                model.LastUpdatedDate  = DateTime.Now;
                model.Title            = title;
                model.Timeout          = timeout;
                model.Sort             = sort;
                model.StartTime        = startTime;
                model.EndTime          = endTime;
                model.VirtualViewCount = virtualViewCount;
                model.SiteFunId        = siteFunId;
                model.LayoutPositionId = layoutPositionId;
                model.IsDisable        = sIsDisable == "1" ? true : false;

                AdvertisementItemInfo adiModel = null;
                if (gId.Equals(Guid.Empty))
                {
                    if (!(string.IsNullOrWhiteSpace(sDescr) && string.IsNullOrWhiteSpace(content)))
                    {
                        adiModel             = new AdvertisementItemInfo();
                        adiModel.Descr       = sDescr;
                        adiModel.ContentText = content;
                    }
                }
                else
                {
                    adiModel                 = new AdvertisementItemInfo();
                    adiModel.Descr           = sDescr;
                    adiModel.ContentText     = content;
                    adiModel.AdvertisementId = gId;
                }

                IList <AdvertisementLinkInfo> listAdLink = null;

                if (!string.IsNullOrWhiteSpace(sAdLinkJson))
                {
                    DataTable dtPostData = JsonConvert.DeserializeObject <DataTable>(sAdLinkJson);
                    if (dtPostData != null && dtPostData.Rows.Count > 0)
                    {
                        listAdLink = new List <AdvertisementLinkInfo>();

                        foreach (DataRow dr in dtPostData.Rows)
                        {
                            AdvertisementLinkInfo postAdlModel = new AdvertisementLinkInfo();
                            postAdlModel.Id = dr["AdLinkId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["AdLinkId"].ToString());
                            postAdlModel.AdvertisementId  = gId;
                            postAdlModel.ContentPictureId = dr["ContentPictureId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ContentPictureId"].ToString());
                            postAdlModel.ActionTypeId     = dr["ActionTypeId"] == DBNull.Value ? Guid.Empty : Guid.Parse(dr["ActionTypeId"].ToString());
                            postAdlModel.Url       = dr["Url"] == DBNull.Value ? "" : dr["Url"].ToString();
                            postAdlModel.Sort      = dr["Sort"] == DBNull.Value ? 0 : int.Parse(dr["Sort"].ToString());
                            postAdlModel.IsDisable = dr["IsDisable"] == DBNull.Value ? false : bool.Parse(dr["IsDisable"].ToString());

                            listAdLink.Add(postAdlModel);
                        }
                    }
                }

                Advertisement     bll    = new Advertisement();
                AdvertisementItem aiBll  = new AdvertisementItem();
                AdvertisementLink adlBll = new AdvertisementLink();
                int effect = -1;
                List <AdvertisementLinkInfo> oldListAdLink = null;
                if (!gId.Equals(Guid.Empty))
                {
                    oldListAdLink = adlBll.GetListByAdId(gId).ToList <AdvertisementLinkInfo>();
                }

                using (TransactionScope scope = new TransactionScope())
                {
                    if (!gId.Equals(Guid.Empty))
                    {
                        bll.Update(model);
                        aiBll.Update(adiModel);
                        if (listAdLink != null)
                        {
                            foreach (var adlModel in listAdLink)
                            {
                                if (oldListAdLink != null && oldListAdLink.Count > 0)
                                {
                                    var delModel = oldListAdLink.Find(m => m.Id.ToString() == adlModel.Id.ToString());
                                    if (delModel != null)
                                    {
                                        oldListAdLink.Remove(delModel);

                                        adlBll.Update(adlModel);
                                    }
                                    else
                                    {
                                        adlBll.Insert(adlModel);
                                    }
                                }
                                else
                                {
                                    adlBll.Insert(adlModel);
                                }
                            }
                        }

                        if (oldListAdLink != null && oldListAdLink.Count > 0)
                        {
                            IList <object> adlIdList = new List <object>();
                            foreach (var oldModel in oldListAdLink)
                            {
                                adlIdList.Add(oldModel.Id);
                            }
                            adlBll.DeleteBatch(adlIdList);
                        }

                        effect = 1;
                    }
                    else
                    {
                        Guid adId = Guid.NewGuid();
                        model.Id = adId;
                        effect   = bll.Insert(model);

                        if (listAdLink != null && listAdLink.Count > 0)
                        {
                            foreach (var adlModel in listAdLink)
                            {
                                adlModel.AdvertisementId = adId;
                                adlBll.Insert(adlModel);
                            }
                        }

                        if (adiModel != null)
                        {
                            adiModel.AdvertisementId = adId;
                            aiBll.Insert(adiModel);
                        }
                    }

                    scope.Complete();
                }

                if (effect == 110)
                {
                    context.Response.Write("{\"success\": false,\"message\": \"" + MessageContent.Submit_Exist + "\"}");
                    return;
                }
                if (effect < 1)
                {
                    context.Response.Write("{\"success\": false,\"message\": \"操作失败,请正确输入\"}");
                    return;
                }

                context.Response.Write("{\"success\": true,\"message\": \"操作成功\"}");
            }
            catch (Exception ex)
            {
                context.Response.Write("{\"success\": false,\"message\": \"异常:" + ex.Message + "\"}");
            }
        }