Example #1
0
        /// <summary>
        /// Follow User
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="followingId"></param>
        /// <returns></returns>
        public bool FollowUser(int userId, int followingId)
        {
            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    var result = dbContext.Follows.Where(p => p.UserId == followingId && p.FollowerId == userId).FirstOrDefault();
                    if (result == null)
                    {
                        Follow follow = new Follow()
                        {
                            UserId     = followingId,
                            FollowerId = userId,
                            Active     = true
                        };
                        dbContext.Follows.Add(follow);
                        dbContext.SaveChanges();
                    }
                    else
                    {
                        if (result.Active == false)
                        {
                            result.Active = true;
                            dbContext.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
Example #2
0
        public void addhash(List <string> hashes, int cid)
        {
            for (int i = 0; i < hashes.Count; i++)
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    String str = hashes[i];

                    var hsh   = dbContext.Hashes.Where(s => s.Content == str).FirstOrDefault();
                    var maxid = 0;
                    if (hsh == null)
                    {
                        Hash hash = new Hash()
                        {
                            Content = hashes[i],
                            //PostDate = DateTime.Now,
                            HashCount   = 1,                          //Alert
                            SearchCount = 0
                        };
                        dbContext.Hashes.Add(hash);
                        dbContext.SaveChanges();
                        //var maxid=dbContext.Hashes.SqlQuery<Int32>("Select Max(HashId) From Hash");
                        maxid = dbContext.Hashes.Max(s => s.HashId);
                        var             x  = dbContext.Hashes.Where(s => s.Content == hashes[i]);
                        TweetHashMapper mp = new TweetHashMapper()
                        {
                            TweetId = cid,
                            HashId  = (int)maxid
                        };
                        dbContext.TweetHashMappers.Add(mp);
                    }
                    else
                    {
                        String st   = hashes[i];
                        Hash   hash = dbContext.Hashes.Where(s => s.Content == st).FirstOrDefault();
                        hash.HashCount += 1;
                        TweetHashMapper mp = new TweetHashMapper()
                        {
                            TweetId = cid,
                            HashId  = hash.HashId
                        };
                        dbContext.TweetHashMappers.Add(mp);
                        //dbContext.Database.SqlQuery<Hash>("Update hash set hashcount=hashcount+1 where Content=" + hashes[i]);
                    }
                    //dbContext.Tweets.Add(tweet);
                    dbContext.SaveChanges();
                }
            }
            return;
        }
Example #3
0
        public List <TweetHashMapper> updateTweetHashMapper(int tweetId)
        {
            //TweetDTO retVal = null;
            List <TweetHashMapper> mapperList = new List <TweetHashMapper>();

            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    var tweetHashMappersList = dbContext.TweetHashMappers.Where(s => s.TweetId == tweetId).ToList();
                    mapperList.AddRange(tweetHashMappersList);
                    //tweetHashMappersList.RemoveAll(x => x.TweetId == tweetId);
                    for (int i = 0; i < tweetHashMappersList.Count; i++)
                    {
                        dbContext.TweetHashMappers.Remove(tweetHashMappersList[i]);
                    }
                    dbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(mapperList);
        }
Example #4
0
        public bool updateHashforedit(List <TweetHashMapper> tweetHashMappers, int tweetId)
        {
            //TweetDTO retVal = null;

            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    foreach (var x in tweetHashMappers)
                    {
                        var hash = dbContext.Hashes.Where(s => s.HashId == x.HashId).FirstOrDefault();

                        hash.HashCount -= 1;
                        if (hash.HashCount <= 0)
                        {
                            dbContext.Hashes.Remove(hash);
                        }
                    }

                    dbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
        public Boolean RegisterUserToDatabase(UserDTO user)
        {
            Boolean result;

            try
            {
                using (GlitterDBEntities context = new GlitterDBEntities())
                {
                    User userDTO = new User
                    {
                        Name           = user.Name,
                        ContactNumber  = user.ContactNumber,
                        ProfileImage   = user.ProfileImage,
                        Email          = user.Email,
                        Country        = user.Country,
                        FollowerCount  = 0,
                        FollowingCount = 0
                    };
                    context.Users.Add(userDTO);
                    if (context.SaveChanges() > 0)
                    {
                        result = false;
                    }
                    result = true;
                }
            }
            catch (Exception)
            {
                result = false;
            }
            return(result);
        }
Example #6
0
        public int AddNewTweet(TweetDTO twt)
        {
            int currid;

            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    Tweet tweet = new Tweet()
                    {
                        Content      = twt.Content,
                        PostDate     = DateTime.Now,
                        UserId       = twt.UserId, //Alert
                        LikeCount    = 0,
                        DislikeCount = 0,
                        Active       = true,
                        UpdateDate   = DateTime.Now
                    };

                    dbContext.Tweets.Add(tweet);
                    dbContext.SaveChanges();
                    currid = dbContext.Tweets.Max(s => s.TweetId);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(currid);
        }
Example #7
0
        public void AddNewTweetHash(List <string> hashes, int tweetId)
        {
            for (int i = 0; i < hashes.Count; i++)
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    String str = hashes[i];

                    var hsh   = dbContext.Hashes.Where(s => s.Content == str).FirstOrDefault();
                    var maxid = 0;
                    if (hsh == null)
                    {
                        Hash hash = new Hash()
                        {
                            Content     = hashes[i],
                            HashCount   = 1,//Alert
                            SearchCount = 0
                        };
                        dbContext.Hashes.Add(hash);
                        dbContext.SaveChanges();//
                        maxid = dbContext.Hashes.Max(s => s.HashId);
                        var             x       = dbContext.Hashes.Where(s => s.Content == hashes[i]);
                        TweetHashMapper mpEntry = new TweetHashMapper()
                        {
                            TweetId = tweetId,
                            HashId  = (int)maxid
                        };
                        dbContext.TweetHashMappers.Add(mpEntry);
                        dbContext.SaveChanges();//
                    }
                    else
                    {
                        String st   = hashes[i];
                        Hash   hash = dbContext.Hashes.Where(s => s.Content == st).FirstOrDefault();
                        hash.HashCount += 1;
                        TweetHashMapper mp = new TweetHashMapper()
                        {
                            TweetId = tweetId,
                            HashId  = hash.HashId
                        };
                        dbContext.TweetHashMappers.Add(mp);
                    }
                    dbContext.SaveChanges();//
                }
            }
            return;
        }
Example #8
0
        public bool DislikeTweet(int userId, int tweetId)
        {
            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    var likeDislikeMapper = dbContext.LikeDislikeMappers.Where(y => y.UserId == userId && y.TweetId == tweetId).FirstOrDefault();
                    var tweet             = dbContext.Tweets.Where(y => y.TweetId == tweetId).FirstOrDefault();

                    if (likeDislikeMapper == null)
                    {
                        LikeDislikeMapper likeDislikeMapperObj = new LikeDislikeMapper()
                        {
                            UserId  = userId,
                            TweetId = tweetId,
                            Like    = false
                        };

                        dbContext.LikeDislikeMappers.Add(likeDislikeMapperObj);
                        tweet.DislikeCount += 1;
                        dbContext.SaveChanges();
                    }
                    else
                    {
                        if (likeDislikeMapper.Like == true)
                        {
                            likeDislikeMapper.Like = false;
                            tweet.LikeCount       -= 1;
                            tweet.DislikeCount    += 1;
                            dbContext.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(true);
        }
Example #9
0
        public List <TweetDTO> GetAllSearchedTweets(string searchHash)
        {
            List <TweetDTO> retVal = null;

            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    var hashBySearchId = (from h in dbContext.Hashes
                                          where h.Content.ToLower().Contains(searchHash.ToLower())
                                          select h.HashId).ToList();

                    //var tweets = dbContext.Database.SqlQuery<TweetDTO>("select t.Content, t.PostDate, t.UserId, t.LikeCount, t.DislikeCount from TweetHashMapper m join Tweets t on m.TweetId=t.TweetId where m.HashId=" + hashBySearchId).ToList();

                    var tweets = (from t in dbContext.Tweets
                                  join mp in dbContext.TweetHashMappers on t.TweetId equals mp.TweetId
                                  join h in dbContext.Hashes on mp.HashId equals h.HashId
                                  where h.Content.ToLower().Contains(searchHash.ToLower()) && t.Active == true
                                  select t).ToList();

                    foreach (var hash in hashBySearchId)
                    {
                        var hashObj = dbContext.Hashes.Where(h => h.HashId == hash).FirstOrDefault();
                        hashObj.SearchCount += 1;
                    }
                    dbContext.SaveChanges();

                    retVal = this.Mapper.Map <List <TweetDTO> >(tweets);
                    //foreach (var item in tweets)
                    //{
                    //    TweetDTO twt = new TweetDTO
                    //    {
                    //        Content = item.Content,
                    //        PostDate = item.PostDate,
                    //        UserId = item.UserId,
                    //        LikeCount = (int)item.LikeCount,
                    //        DislikeCount = (int)item.DislikeCount,
                    //    };
                    //    retVal.Add(twt);
                    //}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(retVal);
        }
Example #10
0
        public void RegisterUser(UserDTO newUser)
        {
            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    //dbContext.Users.Add(this.Mapper.Map<User>(newUser));


                    dbContext.Users.Add(this.Mapper.Map <User>(newUser));
                    dbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #11
0
 public bool updateTweet(int tweetId, string Content)
 {
     //TweetDTO retVal = null;
     try
     {
         using (GlitterDBEntities dbContext = new GlitterDBEntities())
         {
             var tweet = dbContext.Tweets.Where(s => s.TweetId == tweetId).FirstOrDefault();
             tweet.Content    = Content;
             tweet.UpdateDate = DateTime.Now;
             dbContext.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(true);
 }
Example #12
0
        public bool deleteTweet(int tweetId)
        {
            //TweetDTO retVal = null;

            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    var tweet = dbContext.Tweets.Where(s => s.TweetId == tweetId).FirstOrDefault();
                    tweet.Active = false;
                    dbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
Example #13
0
        /// <summary>
        /// Unfollow User
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="followingId"></param>
        /// <returns></returns>
        public bool UnfollowUser(int userId, int followingId)
        {
            try
            {
                using (GlitterDBEntities dbContext = new GlitterDBEntities())
                {
                    var follow = dbContext.Follows.Where(p => p.UserId == followingId && p.FollowerId == userId).FirstOrDefault();
                    if (follow != null)
                    {
                        follow.Active = false;
                        dbContext.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
Example #14
0
 public void Save()
 {
     db.SaveChanges();
 }