Exemple #1
0
        /// <summary>
        /// db context functions to get all tweets
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList <GetAllTweetsDTO> GetAllTweets(Guid id)
        {
            IList <GetAllTweetsDTO> tweetList  = new List <GetAllTweetsDTO>();
            IList <GetAllTweetsDTO> tweetList1 = new List <GetAllTweetsDTO>();

            tweetList = (from u in DBContext.Follow.Where(ds => ds.Follower_UserID == id)
                         join uf in DBContext.Follow on u.Followed_UserID equals uf.Followed_UserID
                         join t in DBContext.Tweet on uf.Followed_UserID equals t.UserID
                         join user in DBContext.User on t.UserID equals user.ID
                         orderby t.CreatedAt descending
                         select new GetAllTweetsDTO()
            {
                MessageId = t.ID,
                Message = t.Message,
                CreatedAt = t.CreatedAt,
                UserName = user.FirstName + user.LastName,
                IsAuthor = false,
                TweetID = t.ID
            }).Distinct().ToList();
            foreach (var iter in tweetList)
            {
                LikeTweet t = DBContext.LikeTweet.Where(x => (x.UserID == id) && (x.TweetID == iter.TweetID)).FirstOrDefault();
                if (t != null)
                {
                    iter.isLiked = true;
                }
                else
                {
                    iter.isLiked = false;
                }
            }

            tweetList1 = (from u in DBContext.User.Where(tr => tr.ID == id)
                          join t in DBContext.Tweet on u.ID equals t.UserID
                          orderby t.CreatedAt descending
                          select new GetAllTweetsDTO()
            {
                MessageId = t.ID,
                Message = t.Message,
                CreatedAt = t.CreatedAt,
                UserName = u.FirstName + u.LastName,
                IsAuthor = true,
                isLiked = false,
                TweetID = t.ID
            }).ToList();
            tweetList = tweetList.Concat(tweetList1).ToList();

            return(tweetList);
        }
        public bool DisLikeTweet(Guid userid, Guid tweetid)
        {
            LikeTweet tweet = DBContext.LikeTweet.Where(ds => ds.UserID == userid && ds.TweetID == tweetid).FirstOrDefault();

            if (tweet != null)
            {
                DBContext.LikeTweet.Remove(tweet);
                DBContext.SaveChanges();
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public bool LikeTweet(LikeTweetDTO liketweetdto)
        {
            LikeTweet liketweet1 = DBContext.LikeTweet.Where(ds => ds.UserID == liketweetdto.LoggedInUserID && ds.TweetID == liketweetdto.TweetID).FirstOrDefault();

            if (liketweet1 != null)
            {
                return(false);
            }

            else
            {
                LikeTweet liketweet = new LikeTweet();
                liketweet.ID      = System.Guid.NewGuid();
                liketweet.TweetID = liketweetdto.TweetID;
                liketweet.UserID  = liketweetdto.LoggedInUserID;
                DBContext.LikeTweet.Add(liketweet);
                DBContext.SaveChanges();
                return(true);
            }
        }