/// <summary> /// User can unfollow another user that he was following /// </summary> /// <param name="id"></param> /// <param name="idToUnfollow"></param> public void unFollowById(int id, int idToUnfollow) { MicoBloggingEntities db = new MicoBloggingEntities(); //get the of following user by kind of self join //if records exit he can unfollow else not var followingdata = from ftable in db.UserFollowers where ftable.FollowerId == id & ftable.UserId == idToUnfollow select ftable; if (followingdata != null) { foreach (var item in followingdata) { db.UserFollowers.Remove(item); } try { db.SaveChanges(); } catch (Exception e) { // Provide for exceptions. Console.WriteLine(e); } } }
//User can follow another user public bool FollowById(UserFollowerModel followerdata) { MicoBloggingEntities db = new MicoBloggingEntities(); try { var data = from userfollow in db.UserFollowers where userfollow.FollowerId == followerdata.UserId & userfollow.UserId == followerdata.FollowerId select userfollow; Console.WriteLine(data); Console.WriteLine(data.Count()); if (data.Count() == 0) { UserFollower dbusrfoll = new UserFollower(); dbusrfoll.UserId = followerdata.FollowerId; dbusrfoll.FollowerId = followerdata.UserId; db.UserFollowers.Add(dbusrfoll); db.SaveChanges(); return(true); } } catch (Exception) { Console.WriteLine("Sorry Some Error Occured"); } return(false); }
/// <summary> /// checks if the user exists in the tbale or not /// </summary> /// <param name="usermodel"></param> /// <returns></returns> public UserModel CheckLogin(UserModel usermodel) { MicoBloggingEntities db = new MicoBloggingEntities(); try { var obj = db.Users.Where(a => a.Email.Equals(usermodel.Email) && a.Password.Equals(usermodel.Password)).FirstOrDefault(); if (obj != null) { usermodel.Uid = obj.Uid; usermodel.FName = obj.FName; usermodel.LName = obj.LName; usermodel.Contact = obj.Contact; usermodel.Country = obj.Country; usermodel.ImageUrl = obj.ImageUrl; } else { usermodel.Email = null; usermodel.Password = null; usermodel.FName = null; usermodel.LName = null; usermodel.Contact = null; usermodel.Country = null; usermodel.ImageUrl = null; } } catch (Exception) { Console.WriteLine("Sorry error in database while fetching User"); } return(usermodel); }
/// <summary> /// Adds the User to the Database /// </summary> /// <param name="usermodel"></param> /// <returns></returns> public bool Signup(UserModel usermodel) { UserModel userDetails = new UserModel(); User dbusr = new User(); MicoBloggingEntities db = new MicoBloggingEntities(); try { if (usermodel.Email == null) { return(false); } if (db == null) { throw new Exception("The dbContext has not been set"); } var userobj = (from prevuser in db.Users where prevuser.Email == usermodel.Email select prevuser).SingleOrDefault(); if (userobj != null) { return(false); } else { dbusr.Email = usermodel.Email; dbusr.Password = usermodel.Password; dbusr.FName = usermodel.FName; dbusr.LName = usermodel.LName; dbusr.Contact = usermodel.Contact; dbusr.Country = usermodel.Country; dbusr.ImageUrl = usermodel.ImageUrl; // Console.WriteLine(dbusr); db.Users.Add(dbusr); try { db.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex); return(false); } } } catch (Exception) { Console.WriteLine("Sorry some error occured"); } return(true); }
/// <summary> /// returns the list of all users /// </summary> /// <param name="id"></param> /// <param name="searchvalue"></param> /// <returns></returns> public List <UserModel> getAllUsers(int id, string searchvalue) { if (searchvalue == null) { searchvalue = ""; } MicoBloggingEntities db = new MicoBloggingEntities(); List <UserModel> userList = new List <UserModel>(); try { User dbuser = new User(); var followingids = from f in db.UserFollowers where f.FollowerId == id select f.UserId; var userslist = from u in db.Users where (followingids.Contains(u.Uid) == false & u.Uid != id) & (u.FName.Contains(searchvalue) || u.LName.Contains(searchvalue) || u.Email.Contains(searchvalue)) select u; foreach (var usr in userslist) { UserModel usermodel = new UserModel(); usermodel.FName = usr.FName; usermodel.LName = usr.LName; usermodel.Uid = usr.Uid; usermodel.ImageUrl = usr.ImageUrl; usermodel.Contact = usr.Contact; usermodel.Country = usr.Country; usermodel.Email = usr.Email; // usermodel.Password = usr.Password; userList.Add(usermodel); } }catch (Exception) { Console.WriteLine("Sorry Error in dataBase While Fetching Users"); } return(userList); }
/// <summary> /// Deletes the tweet /// </summary> /// <param name="id"></param> /// <param name="tweetId"></param> public void deleteTweet(int id, int tweetId) { MicoBloggingEntities db = new MicoBloggingEntities(); try { var tweetdata = from tweet in db.Tweets where tweet.UserId == id & tweet.Tid == tweetId select tweet; //First delete from the foriegn key tABLE var tweetInAnother = from tweetAnother in db.TweetLikeDislikes where tweetAnother.TweetId == tweetId select tweetAnother; if (tweetdata != null) { if (tweetInAnother.Count() == 0) { foreach (var itemdata in tweetdata) { db.Tweets.Remove(itemdata); } } else { foreach (var tweetdataanother in tweetInAnother) { db.TweetLikeDislikes.Remove(tweetdataanother); } db.SaveChanges(); foreach (var itemdata in tweetdata) { db.Tweets.Remove(itemdata); } } } db.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); // Provide for exceptions. } }
/// <summary> /// get the Users that are being followed by the Current user /// </summary> /// <param name="id"></param> /// <param name="searchvalue"></param> /// <returns></returns> public List <UserModel> getFollowingById(int id, string searchvalue) { //searchstring is used //to consume the same service //and methods for the Search Results MicoBloggingEntities db = new MicoBloggingEntities(); List <UserModel> followerList = new List <UserModel>(); if (searchvalue == null) { searchvalue = ""; } User dbuser = new User(); //kind of self join on user table try { var followingids = from f in db.UserFollowers where f.FollowerId == id select f.UserId; var following = db.Users.Where(t => followingids.Contains(t.Uid) & (t.FName.Contains(searchvalue) || t.LName.Contains(searchvalue) || t.Email.Contains(searchvalue))); //assigns data to usermodel or DTO foreach (var followingusr in following) { UserModel usermodel = new UserModel(); usermodel.FName = followingusr.FName; usermodel.LName = followingusr.LName; usermodel.Uid = followingusr.Uid; usermodel.ImageUrl = followingusr.ImageUrl; usermodel.Uid = followingusr.Uid; //usermodel.Contact = follower.Contact; //usermodel.Country = follower.Country; followerList.Add(usermodel); } } catch (Exception) { Console.WriteLine("Sorry Some Error Occured"); } return(followerList); }
//Update the Tweet Like dislike table when user Clicks the Like Button public bool tweetLikeClicked(TweetLikeDislikeModel data) { MicoBloggingEntities db = new MicoBloggingEntities(); TweetLikeDislike dbtweetlikedislike = new TweetLikeDislike(); try { //finds the Previous record of that tweet var findPrevData = (from tweetLD in db.TweetLikeDislikes where tweetLD.TweetId == data.TweetId & tweetLD.LikedDislikedBy == data.LikedDislikedBy select tweetLD).FirstOrDefault(); //if there is no Record it will create the tweetrecord if (findPrevData != null) { Console.WriteLine(findPrevData); findPrevData.LikeDislike = "true"; db.SaveChanges(); return(true); } //if record exists the data is updated else { dbtweetlikedislike.TweetId = data.TweetId; dbtweetlikedislike.LikedDislikedBy = data.LikedDislikedBy; dbtweetlikedislike.LikeDislike = data.LikeDislike; db.TweetLikeDislikes.Add(dbtweetlikedislike); db.SaveChanges(); return(true); } } catch (Exception ex) { Console.WriteLine("Sorry some Error ocuured" + ex); return(false); } }
/// <summary> /// edits the tweets /// </summary> /// <param name="id"></param> /// <param name="tweetdata"></param> /// <returns></returns> public bool editTweet(int id, TweetModel tweetdata) { MicoBloggingEntities db = new MicoBloggingEntities(); try { Tweet tweetToEdit = (from tweet in db.Tweets where tweet.Tid == id & tweet.UserId == tweetdata.UserId select tweet).FirstOrDefault(); tweetToEdit.Body = tweetdata.Body; db.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex); } return(true); }
/// <summary> /// adds data if no record exist of that tweet /// else updates the like or dislike value /// </summary> /// <param name="data"></param> /// <returns></returns> public bool tweetDisLikeClicked(TweetLikeDislikeModel data) { MicoBloggingEntities db = new MicoBloggingEntities(); TweetLikeDislike dbtweetlikedislike = new TweetLikeDislike(); try { var findPrevData = (from tweetLD in db.TweetLikeDislikes where tweetLD.TweetId == data.TweetId & tweetLD.LikedDislikedBy == data.LikedDislikedBy select tweetLD).FirstOrDefault(); if (findPrevData != null) { Console.WriteLine(findPrevData); findPrevData.LikeDislike = "false"; db.SaveChanges(); return(true); } else { dbtweetlikedislike.TweetId = data.TweetId; dbtweetlikedislike.LikedDislikedBy = data.LikedDislikedBy; dbtweetlikedislike.LikeDislike = data.LikeDislike; db.TweetLikeDislikes.Add(dbtweetlikedislike); db.SaveChanges(); return(true); } } catch (Exception ex) { Console.WriteLine("Sorry some Error ocuured" + ex); return(false); } }
/// <summary> /// gets the Followe of the User /// </summary> /// <param name="id"></param> /// <returns></returns> public List <UserModel> getFollowerById(int id) { MicoBloggingEntities db = new MicoBloggingEntities(); List <UserModel> followerList = new List <UserModel>(); User dbuser = new User(); //finds the follower of the user try { var followrids = from f in db.UserFollowers where f.UserId.Equals(id) select f.FollowerId; var followers = db.Users.Where(t => followrids.Contains(t.Uid)); //Assign the data to the usermodel foreach (var follower in followers) { //Model class or DTO is used UserModel usermodel = new UserModel(); usermodel.FName = follower.FName; usermodel.LName = follower.LName; // usermodel.Email = follower.Email; usermodel.ImageUrl = follower.ImageUrl; usermodel.Uid = follower.Uid; //usermodel.Contact = follower.Contact; //usermodel.Country = follower.Country; followerList.Add(usermodel); } } catch (Exception) { Console.WriteLine("Sorry Some Error Occured"); } return(followerList); }
/// <summary> /// Get the analytics for the website /// </summary> /// <returns></returns> public AnalyticsViewModel GetAnalytics() { AnalyticsViewModel analyticsVM = new AnalyticsViewModel(); MicoBloggingEntities db = new MicoBloggingEntities(); //Query selects the total tweets var tweetData = db.Tweets.Select(t => t).ToList(); //tweets are filtered by Today's date int totalTweetsToday = tweetData.Where(t => DateTime.Compare(t.CreationTime.Date, DateTime.Now.Date) == 0).Count(); //Adds Data to Analytics View Model analyticsVM.TotalTweetsToday = totalTweetsToday; //selects the tweet //group by userid //finds count in descending order Queue <int> list = new Queue <int>(); try { foreach (var line in tweetData.GroupBy(tweet => tweet.UserId) .Select(group => new { UserId = group.Key, Count = group.Count() }).OrderByDescending(x => x.Count)) { int UserId = line.UserId; list.Enqueue(UserId); } //Selects the user firstname //that have most tweets till date if (list.Count() > 0) { int userId = list.Dequeue(); string mostTweetsBy = (from userD in db.Users where userD.Uid == userId select userD.FName).Single(); //puts the data in the Viewmodel analyticsVM.MostTweetsBy = mostTweetsBy; } } catch (Exception) { Console.WriteLine("Sorry Some Error Occured"); } /* Query to select the Most Liked tweets * grouping by tweet id * where twiteliek is true * order by descending */ var tweetLikeDislike = db.TweetLikeDislikes.Select(t => t).ToList(); Queue <int> listLD = new Queue <int>(); var Like = db.TweetLikeDislikes.Select(t => t.LikeDislike).ToList(); try { foreach (var tweetLData in tweetLikeDislike.Where(x => x.LikeDislike == "true ").GroupBy(tweet => tweet.TweetId) .Select(group => new { tweetId = group.Key, count = group.Count(), }).OrderByDescending(x => x.tweetId) ) { int tweetid = Convert.ToInt32(tweetLData.tweetId); listLD.Enqueue(tweetid); break; } /* * finds the Tweet Body in database * that has maximum Likes*/ int tweetId = listLD.Dequeue(); string mostLikedTweet = (from tweet in db.Tweets where tweet.Tid == tweetId select tweet.Body).Single(); //Inserts into Analytics analyticsVM.MostLikedTweets = mostLikedTweet; } catch (Exception) { Console.WriteLine("Sorry Some Error Occured"); } return(analyticsVM); }