Example #1
0
        public ActionResult Activate(int?id = 1)
        {
            if (id != null)
            {
                var offer = _offerService.GetOffers(o => o.Id == id.Value, o => o.UserProfile).SingleOrDefault();
                if (offer != null && offer.UserProfileId == User.Identity.GetUserId() && offer.State == OfferState.inactive)
                {
                    offer.State       = OfferState.active;
                    offer.DateCreated = DateTime.Now;
                    offer.DateDeleted = offer.DateCreated.AddDays(offerDays);
                    _offerService.SaveOffer();

                    if (Request.Url != null)
                    {
                        offer.JobId = MarketHangfire.SetDeactivateOfferJob(offer.Id,
                                                                           Url.Action("Activate", "Offer", new { id = offer.Id }, Request.Url.Scheme),
                                                                           TimeSpan.FromDays(30));
                    }
                    _offerService.SaveOffer();
                    return(View());
                }
            }
            return(HttpNotFound());
        }
Example #2
0
        public async System.Threading.Tasks.Task <ActionResult> Create(CreateOfferViewModel model, HttpPostedFileBase[] images)
        {
            Tinify.Key = ConfigurationManager.AppSettings["TINYPNG_APIKEY"];
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var currentUserId = User.Identity.GetUserId();
            var userProfile   = _userProfileService.GetUserProfiles(u => u.Id == currentUserId, i => i.ApplicationUser).SingleOrDefault();

            if (userProfile != null)
            {
                var appUser = userProfile.ApplicationUser;
                if (appUser != null)
                {
                    if (!(appUser.EmailConfirmed))
                    {
                        return(HttpNotFound("you are not confirmed email or phone number"));
                    }
                }
            }
            else
            {
                return(View("_CreateOfferConfirmationError"));
            }
            if (userProfile.Offers.Count(o => o.State == OfferState.active) >= 10)
            {
                return(View("CrateOfferLimitError"));
            }
            Offer offer = Mapper.Map <CreateOfferViewModel, Offer>(model);

            Game game         = _gameService.GetGameByValue(model.Game);
            var  gameFilters  = _filterService.GetFilters(f => f.Game.Value == game.Value, i => i.Game, i => i.FilterItems).ToList();
            var  modelFilters = model.FilterValues;
            //var gameFilterItems = _filterItemService.GetFilterItems().Where(f => f.Filter.Game == game).ToList();
            var modelFilterItems = model.FilterItemValues;

            if (modelFilters != null && gameFilters.Count != 0)
            {
                if (game != null && modelFilters.Length == gameFilters.Count)
                {
                    for (int i = 0; i < gameFilters.Count; i++)
                    {
                        if (gameFilters[i].Value != modelFilters[i])
                        {
                            return(View("_CreateOfferFilterError"));
                        }

                        bool isContainsFilterItems = false;
                        foreach (var fItem in gameFilters[i].FilterItems)
                        {
                            if (fItem.Value == modelFilterItems[i])
                            {
                                offer.FilterItems.Add(fItem);
                                offer.Filters.Add(gameFilters[i]);
                                isContainsFilterItems = true;
                            }
                        }
                        if (!isContainsFilterItems)
                        {
                            return(View("_CreateOfferFilterError"));
                        }
                    }
                }
                else
                {
                    return(View("_CreateOfferFilterError"));
                }
            }


            foreach (var image in images)
            {
                if (image != null && image.ContentLength <= 1000000 && (image.ContentType == "image/jpeg" || image.ContentType == "image/png"))
                {
                    var extName  = System.IO.Path.GetExtension(image.FileName);
                    var fileName = $@"{Guid.NewGuid()}{extName}";
                    // сохраняем файл в папку Files в проекте
                    string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "\\Content\\Images\\Avatars", fileName);
                    var    urlPath  = Url.Content("~/Content/Images/Screenshots/" + fileName);
                    image.SaveAs(fullPath);


                    offer.ScreenshotPathes.Add(new ScreenshotPath {
                        Value = urlPath
                    });
                }
                else
                {
                    offer.ScreenshotPathes.Add(new ScreenshotPath {
                        Value = null
                    });
                }
            }

            offer.GameId        = game.Id;
            offer.UserProfileId = _userProfileService.GetUserProfileById(User.Identity.GetUserId()).Id;
            offer.DateDeleted   = offer.DateCreated.AddDays(offerDays);

            _offerService.CreateOffer(offer);
            _offerService.SaveOffer();

            if (Request.Url != null)
            {
                offer.JobId = MarketHangfire.SetDeactivateOfferJob(offer.Id,
                                                                   Url.Action("Activate", "Offer", new { id = offer.Id }, Request.Url.Scheme), TimeSpan.FromDays(30));
            }

            _offerService.SaveOffer();
            var offerModel = Mapper.Map <Offer, OfferViewModel>(offer);

            return(View("OfferCreated", offerModel));
        }