示例#1
0
        // GET: /v2
        public ActionResult IndexV2(int?page)
        {
            ViewBag.SelectedSubverse = "frontpage";
            var submissions = new List <SetSubmission>();

            try
            {
                var frontPageResultModel = new SetFrontpageViewModel();

                // show user sets
                // get names of each set that user is subscribed to
                // for each set name, get list of subverses that define the set
                // for each subverse, get top ranked submissions
                if (User.Identity.IsAuthenticated && Utils.User.SetsSubscriptionCount(User.Identity.Name) > 0)
                {
                    var userSetSubscriptions = _db.Usersetsubscriptions.Where(usd => usd.Username == User.Identity.Name);
                    var blockedSubverses     = _db.UserBlockedSubverses.Where(x => x.Username.Equals(User.Identity.Name)).Select(x => x.SubverseName);

                    foreach (var set in userSetSubscriptions)
                    {
                        Usersetsubscription setId = set;
                        var userSetDefinition     = _db.Usersetdefinitions.Where(st => st.Set_id == setId.Set_id);

                        foreach (var subverse in userSetDefinition)
                        {
                            // get top ranked submissions
                            var submissionsExcludingBlockedSubverses = SetsUtility.TopRankedSubmissionsFromASub(subverse.Subversename, _db.Messages, subverse.Userset.Name, 2, 0)
                                                                       .Where(x => !blockedSubverses.Contains(x.Subverse));
                            submissions.AddRange(submissionsExcludingBlockedSubverses);
                        }
                    }

                    frontPageResultModel.HasSetSubscriptions = true;
                    frontPageResultModel.UserSets            = userSetSubscriptions;
                    frontPageResultModel.SubmissionsList     = new List <SetSubmission>(submissions.OrderByDescending(s => s.Rank));

                    return(View("~/Views/Home/IndexV2.cshtml", frontPageResultModel));
                }

                // show default sets since user is not logged in or has no set subscriptions
                // get names of default sets
                // for each set name, get list of subverses
                // for each subverse, get top ranked submissions
                var defaultSets = _db.Usersets.Where(ds => ds.Default && ds.Usersetdefinitions.Any());
                var defaultFrontPageResultModel = new SetFrontpageViewModel();

                foreach (var set in defaultSets)
                {
                    Userset setId = set;
                    var     defaultSetDefinition = _db.Usersetdefinitions.Where(st => st.Set_id == setId.Set_id);

                    foreach (var subverse in defaultSetDefinition)
                    {
                        // get top ranked submissions
                        submissions.AddRange(SetsUtility.TopRankedSubmissionsFromASub(subverse.Subversename, _db.Messages, set.Name, 2, 0));
                    }
                }

                defaultFrontPageResultModel.DefaultSets         = defaultSets;
                defaultFrontPageResultModel.HasSetSubscriptions = false;
                defaultFrontPageResultModel.SubmissionsList     = new List <SetSubmission>(submissions.OrderByDescending(s => s.Rank));

                return(View("~/Views/Home/IndexV2.cshtml", defaultFrontPageResultModel));
            }
            catch (Exception)
            {
                return(RedirectToAction("HeavyLoad", "Error"));
            }
        }
示例#2
0
        public async Task<ActionResult> CreateSet([Bind(Include = "Name, Description")] AddSet setTmpModel)
        {
            if (!User.Identity.IsAuthenticated) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            int maximumOwnedSets = Settings.MaximumOwnedSets;

            // TODO
            // ###############################################################################################
            try
            {
                // abort if model is in invalid state
                if (!ModelState.IsValid) return View();

                // setup default values
                var set = new Userset
                {
                    Name = setTmpModel.Name,
                    Description = setTmpModel.Description,
                    Created_on = DateTime.Now,
                    Created_by = User.Identity.Name,
                    Default = false,
                    Public = true,
                    Subscribers = 0
                };

                // only allow users with less than maximum allowed sets to create a set
                var amountOfOwnedSets = _db.Usersets
                    .Where(s => s.Created_by == User.Identity.Name)
                    .ToList();

                if (amountOfOwnedSets.Count <= maximumOwnedSets)
                {
                    _db.Usersets.Add(set);
                    await _db.SaveChangesAsync();

                    // subscribe user to the newly created set
                    UserHelper.SubscribeToSet(User.Identity.Name, set.Set_id);

                    // go to newly created Set
                    return RedirectToAction("EditSet", "Sets", new { setId = set.Set_id });
                }

                ModelState.AddModelError(string.Empty, "Sorry, you can not own more than " + maximumOwnedSets + " sets.");
                return View();
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, "Something bad happened.");
                return View();
            }
            // ###############################################################################################
        }