예제 #1
0
        public JsonResult MostFunnyUsers()
        {
            using (GagsDbContext model = new GagsDbContext())
            {
                var allGags = model.Gags.Include("Owner");
                ConcurrentDictionary<Guid, int> counters = new ConcurrentDictionary<Guid, int>();
                foreach (var gag in allGags)
                {
                    if (!counters.ContainsKey(gag.Owner.Id))
                        counters[gag.Owner.Id] = 0;
                    counters[gag.Owner.Id]++;
                }
                var top10 = counters.OrderByDescending(x => x.Value).Take(10);
                var context = new List<StatsModel>(10);
                foreach (var user in top10)
                {
                    context.Add(new StatsModel()
                    {
                        Name = model.Users.Where(x => x.Id == user.Key).First().Nickname,
                        Value = user.Value
                    });
                }
                JsonResult result = new JsonResult() { Data = context, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                return  result;

            }
        }
예제 #2
0
 public ActionResult Index(bool isHot = false, int page = 1)
 {
     ViewBag.isHot = isHot;
     ViewBag.page = page;
     using (GagsDbContext model = new GagsDbContext())
     {
         var gags = loadGags(model, isHot, page);
         return CreateViewModelContext(gags, model);
     }
 }
예제 #3
0
        public JsonResult TheMostActiveUser()
        {
            using (GagsDbContext model = new GagsDbContext())
            {
                var userLikes = model.UserLikes.
                    Join(model.Users, ul=>ul.UserId, u=> u.Id,(ul, u)=> new  {user =u, likes=ul }).
                    GroupBy(x => x.user.Id).OrderByDescending(x => x.Count());
                var userComments = model.Comments.Include("Owner").GroupBy(x => x.Owner.Id).OrderByDescending(x => x.Count());
                var userGags = model.Gags.Include("Owner").GroupBy(x => x.Owner.Id).OrderByDescending(x => x.Count());

                Dictionary<string, int> users = new Dictionary<string, int>();
                var arr = userLikes.ToArray();
                foreach (var user in userLikes)
                {
                    var name = user.First().user.Nickname;
                    if (!users.ContainsKey(name))
                    {
                        users[name] = 0;
                    }
                    users[name] += user.Count();
                }

                foreach (var user in userComments)
                {
                    var name = user.First().Owner.Nickname;
                    if (!users.ContainsKey(name))
                    {
                        users[name] = 0;
                    }
                    users[name] += user.Count();
                }
                foreach (var user in userGags)
                {
                    var name = user.First().Owner.Nickname;
                    if (!users.ContainsKey(name))
                    {
                        users[name] = 0;
                    }
                    users[name] += user.Count();
                }
                var topUsers = users.OrderByDescending(x => x.Value).Take(10).Select(x => new { Name = x.Key, Value = x.Value });
                var context = new List<StatsModel>(10);
                foreach (var user in topUsers)
                {
                    context.Add(new StatsModel()
                    {
                        Name = user.Name,
                        Value = user.Value
                    });
                }
                JsonResult result = new JsonResult() { Data = context, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                return result;
            }
        }
예제 #4
0
 public bool DeleteGag(string id)
 {
     using (GagsDbContext model = new GagsDbContext())
     {
         var gId = new Guid(id);
         var gag = model.Gags.Include("Comments").Where(x => x.Id == gId).FirstOrDefault();
         if (gag == null)
         {
             return false;
         }
         model.Comments.RemoveRange(gag.Comments);
         model.Gags.Remove(gag);
         model.SaveChanges();
     }
     return true;
 }
예제 #5
0
        public string Like(string id)
        {
            Guid gagID;
            if( Guid.TryParse(id, out gagID))
            {
                using (GagsDbContext model = new GagsDbContext())
                {
                    var userId = new Guid(User.Identity.GetUserId());
                    var user = model.Users.Where(u => u.Id == userId).FirstOrDefault();
                    if (user == null)
                    {
                        user = new User()
                        {
                            Nickname = User.Identity.GetUserName(),
                            Id = userId,
                            DateOfBirth = DateTime.Now
                        };
                        model.Users.Add(user);
                    }
                    var isExist = model.UserLikes.Where(x => x.UserId == userId && x.GagId == gagID).FirstOrDefault();
                    if (isExist != null)
                    {
                        return "already liked";
                    }
                    else
                    {
                        var gag = model.Gags.Where(x => x.Id == gagID).FirstOrDefault();
                        if (gag == null)
                        {
                            return "gag not found";
                        }
                        model.UserLikes.Add(new UsersLikes()
                        {
                            GagId = gagID,
                            UserId = userId
                        });
                        gag.Likes++;

                        model.SaveChanges();
                        return gag.Likes.ToString();
                    }
                }
            }
            return string.Empty;
        }
예제 #6
0
 private IEnumerable<Gag> loadGags(GagsDbContext model, bool isHot, int page)
 {
     var gags = model.Gags.Include("Owner").Include("Comments");
     var orderdGags = gags.OrderByDescending(x => x.UploadDate);
     if (isHot)
     {
         orderdGags = orderdGags.OrderByDescending(x => x.Likes).OrderByDescending(x=> x.Comments.Count);
     }
     return orderdGags.Skip(PAGE_SIZE * (page - 1)).Take(PAGE_SIZE).ToArray();
 }
예제 #7
0
        private ActionResult CreateViewModelContext(IEnumerable<Gag> gags, GagsDbContext model)
        {
            bool isUserLogged = true;
            Guid userId = Guid.Empty;

            var userStringId = User.Identity.GetUserId();
            if (string.IsNullOrWhiteSpace(userStringId))
            {
                isUserLogged = false;
            }
            else
            {
                userId = new Guid(userStringId);
            }
            var gagViewModel = new List<GagViewModel>(10);
            foreach (var gag in gags)
            {
                var isLiked = !isUserLogged || model.UserLikes.Any(x => x.GagId == gag.Id && x.UserId == userId);
                gagViewModel.Add(
                    new GagViewModel(gag)
                    {
                        Liked = isLiked
                    });
            }
            return View(gagViewModel);
        }
예제 #8
0
        public ActionResult Search(string text, int page = 1, bool Gags = false, bool Comments = false, bool Commentiers = false, bool Owner = false)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return RedirectToAction("Index", "Gag");
            }

            ViewBag.text = text;
            ViewBag.page = page;
            using (GagsDbContext model = new GagsDbContext())
            {
                var searchGags = model.Gags.Include("Owner").Include("Comments").Include("Comments.Owner").
                            Where(x =>(Gags && x.Title.Contains(text))||
                                      (Gags && x.Description.Contains(text)) ||
                                      (Owner && x.Owner.Nickname.Contains(text) )||
                                      ((Comments || Commentiers)&& x.Comments.Any(y => (Comments && y.Description.Contains(text)) ||
                                        (Commentiers && y.Owner.Nickname.Contains(text))))).
                            OrderByDescending(x => x.UploadDate).
                            Skip(PAGE_SIZE * (page - 1)).
                            Take(PAGE_SIZE).ToArray();
                return CreateViewModelContext(searchGags, model);
            }
        }
예제 #9
0
        public JsonResult TheMostLikeGags()
        {
            using (GagsDbContext model = new GagsDbContext())
            {
                var top10 = model.Gags.OrderByDescending(x => x.Likes).Take(10);
                var context = new List<StatsModel>(10);
                foreach (var gags in top10)
                {
                    context.Add(new StatsModel()
                    {
                        Name = gags.Title,
                        Value = gags.Likes
                    });
                }
                JsonResult result = new JsonResult() { Data = context, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                return result;

            }
        }