예제 #1
0
 public void AddFollowing(string followingName)
 {
     if (user == null)
     {
         throw new Exception("User is not set");
     }
     if (followingName == null)
     {
         throw new Exception("Following not found");
     }
     using (var context = new TweetContext())
     {
         if (user.Following == null)
         {
             user.Following = new List <Following>();
         }
         List <Following> followings = user.Following.Where(b => b.Name == followingName).ToList();
         if (followings.Count > 0)
         {
             return;
         }
         Following following = new Following();
         following.Name = followingName;
         user.Following.Add(following);
         context.Users.Update(user);
         context.SaveChanges();
     }
 }
예제 #2
0
 public void DeleteUser(string name)
 {
     using (var context = new TweetContext())
     {
         try{
             List <User> userlist = context.Users.Where(b => b.Name.Equals(name)).ToList();
             if (userlist.Count < 0)
             {
                 throw new Exception("User not exists");
             }
             List <User> followingList = context.Users.Where(b => b.Name.Equals(name)).Include(u => u.Following).ToList();
             foreach (User following in followingList)
             {
                 Twitter t = new Twitter(userlist[0].Name);
                 t.RemoveFollowing(following.Name);
             }
             List <User> followList = context.Users.Where(b => true).Include(u => u.Following).ToList();
             foreach (User follow in followingList)
             {
                 Twitter t = new Twitter(follow.Name);
                 t.RemoveFollowing(userlist[0].Name);
             }
             context.Users.Remove(userlist[0]);
             context.SaveChanges();
         }catch (Exception ex) {
             Console.WriteLine(ex);
         }
     }
 }
예제 #3
0
 public void RemoveUser(string R_user)
 {
     using (var context = new TweetContext())
     {
         List <User> user = context.Users.Where(b => b.Name.Equals(R_user)).ToList();
         context.Users.Remove(user[0]);
         context.SaveChanges();
     }
 }
예제 #4
0
 public static void DeleteUser(string name)
 {
     using (var context = new TweetContext())
     {
         List <User> user = context.Users.Where(b => b.Name.Equals(name)).ToList();
         context.Users.Remove(user[0]);
         context.SaveChanges();
     }
 }
예제 #5
0
 public static void RemoveSession(string username)
 {
     using (var context = new TweetContext())
     {
         List <User> users = context.Users.Where(b => b.Name.Equals(username)).ToList();
         User        aUser = users[0];
         aUser.Session = null;
         context.Users.Update(aUser);
         context.SaveChanges();
     }
 }
예제 #6
0
        public static void DeleteUser(string name)
        {
            User user = new User();

            user.Name = name;
            using (var context = new TweetContext())
            {
                List <User> userlist = context.Users.Where(b => b.Name.Equals(name)).ToList();
                if (userlist.Count <= 0)
                {
                    throw new Exception("User not exists");
                }
                context.Users.Remove(userlist[0]);
                context.SaveChanges();
            }
        }
예제 #7
0
        public static void AddUser(string name, string password)
        {
            User user = new User();

            user.Name     = name;
            user.Password = password;
            using (var context = new TweetContext())
            {
                List <User> userlist = context.Users.Where(b => b.Name.Equals(name)).ToList();
                if (userlist.Count > 0)
                {
                    throw new Exception("User already exists");
                }
                context.Users.Add(user);
                context.SaveChanges();
            }
        }
예제 #8
0
        public void PostTweet(string message)
        {
            if (user == null)
            {
                throw new Exception("User is not set");
            }
            Tweet tweet = new Tweet();

            tweet.User        = user.Name;
            tweet.Message     = message;
            tweet.DateCreated = DateTime.Now;
            using (var context = new TweetContext())
            {
                context.Tweets.Add(tweet);
                context.SaveChanges();
            }
        }
예제 #9
0
 public static void RemoveUser(string user)
 {
     if (user == null)
     {
         throw new Exception("User is not set");
     }
     using (var context = new TweetContext())
     {
         List <User> listuser = context.Users.Where(b => b.Name.Equals(user)).ToList();
         if (listuser.Count < 1)
         {
             throw new Exception("User not found");
         }
         context.Users.Remove(listuser[0]);
         context.SaveChanges();
     }
 }
예제 #10
0
        public static void DeleteUser(string name, string password)
        {
            User user = new User();

            user.Name     = name;
            user.Password = password;
            using (var context = new TweetContext())
            {
                List <User> userlist = context.Users.Where(b => b.Name.Equals(name)).ToList();

                if (userlist.Count == 0)
                {
                    throw new Exception("Not found User");
                }
                context.Users.Remove(user);
                context.SaveChanges();
            }
        }
예제 #11
0
 public void RemoveFollowing(string followingName)
 {
     if (user == null)
     {
         throw new Exception("User is not set");
     }
     if (followingName == null)
     {
         throw new Exception("Following not found");
     }
     using (var context = new TweetContext())
     {
         Following following = new Following();
         following.Name = followingName;
         user.Following.Remove(following);
         context.Users.Update(user);
         context.SaveChanges();
     }
 }
예제 #12
0
 public void Del_User(string name)
 {
     using (var context = new TweetContext())
     {
         List <User> userlist = context.Users.Where(b => b.Name.Equals(name)).ToList();
         if (userlist.Count == 0)
         {
             throw new Exception("User not exists");
         }
         List <User> followlist = context.Users.Where(b => true).Include(b => b.Following).ToList();
         foreach (User temp in followlist)
         {
             Twitter ntwitter = new Twitter(temp.Name);
             ntwitter.RemoveFollowing(userlist[0].Name);
         }
         context.Users.Remove(userlist[0]);
         context.SaveChanges();
     }
 }
예제 #13
0
        public static void DeleteUser(string name, string password)
        {
            User user = new User();

            user.Name     = name;
            user.Password = password;
            using (var context = new TweetContext())
            {
                List <User> userlist = context.Users.Where(b => b.Name.Equals(name)).ToList();
                if (userlist.Count > 0)
                {
                    throw new Exception("User already exists");
                }
                List <User> follow = context.Users.Where(b => b.Name.Equals(name)).ToList();
                foreach (User y in follow)
                {
                    Twitter twit = new Twitter(y.Name);
                    twit.RemoveFollowing(userlist[0].Name);
                }
                context.Users.Remove(userlist[0]);
                context.SaveChanges();
            }
        }
예제 #14
0
        public new HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse  response = new HTTPResponse(200);
            StringBuilder sb       = new StringBuilder();

            session = request.GetPropertyByKey("X-session");
            string[] urlToken = request.Url.Split("/");
            if (urlToken.Length > 2)
            {
                action = urlToken[2];
            }
            else
            {
                action = null;
            }
            requestMethod = request.Method;
            string username = request.GetRequestByKey("username");
            string password = request.GetRequestByKey("password");

            if (IsMethod(HTTP_OPTIONS))
            {
                return(response);
            }

            if (IsAction(USER_ACTION))
            {
                if (IsMethod(HTTP_POST))
                {
                    if (!IsNOE(username) && !IsNOE(password))
                    {
                        try
                        {
                            Twitter.AddUser(username, password);
                            response.Status = 201;
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                        }
                    }
                    else
                    {
                        response.SetBody("Username and password required");
                        response.Status = 400;
                        return(response);
                    }
                }
            }
            else if (IsAction(LOGIN_ACTION))
            {
                if (IsMethod(HTTP_POST))
                {
                    if (!IsNOE(username) && !IsNOE(password))
                    {
                        if (Twitter.IsValidUser(username, password))
                        {
                            string newSession = Twitter.GenSession(username);
                            if (!IsNOE(newSession))
                            {
                                response.Status = 201;
                                dynamic jobj = new JObject();
                                jobj.Session  = newSession;
                                response.Type = "application/json";
                                string resp = JsonConvert.SerializeObject(jobj);
                                response.Body = Encoding.UTF8.GetBytes(resp);
                                return(response);
                            }
                            response.SetBody("Can't create session");
                            response.Status = 500;
                            return(response);
                        }
                        else
                        {
                            response.SetBody("Invalid username/password");
                            response.Status = 404;
                            return(response);
                        }
                    }
                    else
                    {
                        response.SetBody("Username and password required");
                        response.Status = 400;
                        return(response);
                    }
                }
            }
            else if (!IsNOE(session))
            {
                User user = Twitter.GetUserFromSession(session);
                if (user == null)
                {
                    response.SetBody("User not found, please login again");
                    response.Status = 404;
                    return(response);
                }
                Twitter twitter = new Twitter(user.Name);
                if (IsNOE(action))
                {
                    if (IsMethod(HTTP_GET))
                    {
                        try
                        {
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            if (IsNOE(resp))
                            {
                                response.SetBody("No post in timline");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                }
                else if (IsAction(LOGOUT_ACTION))
                {
                    if (IsMethod(HTTP_POST))
                    {
                        try {
                            Twitter.RemoveSession(user.Name);
                            response.Status = 200;
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                }
                else if (IsAction(TWEET_ACTION))
                {
                    if (IsMethod(HTTP_GET))
                    {
                        try
                        {
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = null;
                            if (IsNOE(urlToken[3]))
                            {
                                resp = JsonConvert.SerializeObject(twitter.GetTimeline(user));
                            }
                            else
                            {
                                User aUser = Twitter.GetUserFromName(urlToken[3]);
                                if (aUser == null)
                                {
                                    response.SetBody("User not found");
                                    response.Status = 404;
                                    return(response);
                                }
                                resp = JsonConvert.SerializeObject(twitter.GetTimeline(aUser));
                            }
                            if (IsNOE(resp))
                            {
                                response.SetBody("No post in timline");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                    else if (IsMethod(HTTP_POST))
                    {
                        try
                        {
                            string message = request.GetRequestByKey("message");
                            if (IsNOE(message))
                            {
                                response.SetBody("Message required");
                                response.Status = 400;
                                return(response);
                            }
                            Tweet tweet = new Tweet();
                            tweet.User        = user.Name;
                            tweet.Message     = message;
                            tweet.DateCreated = DateTime.Now;
                            using (var context = new TweetContext())
                            {
                                context.Tweets.Add(tweet);
                                context.SaveChanges();
                            }
                            response.Status = 201;
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                    else if (IsMethod(HTTP_DELETE))
                    {
                        try {
                            throw (new NotImplementedException());
                        } catch (Exception ex) {
                            response.SetBody(ex.Message);
                            response.Status = 501;
                            return(response);
                        }
                    }
                }
                //action following
                else if (IsAction(FOLLOWING_ACTION))
                {
                    if (IsMethod(HTTP_GET))
                    {
                        response      = new HTTPResponse(200);
                        response.Type = "application/json";
                        string resp = JsonConvert.SerializeObject(user.Following);
                        if (resp == null)
                        {
                            response.SetBody("No following");
                            response.Status = 404;
                            return(response);
                        }
                        response.Body = Encoding.UTF8.GetBytes(resp);
                        return(response);
                    }
                    //add new following
                    else if (IsMethod(HTTP_POST))
                    {
                        string following = request.GetRequestByKey("followingname");
                        try
                        {
                            twitter.AddFollowing(following);
                            response      = new HTTPResponse(201);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No folowing");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                    else if (IsMethod(HTTP_DELETE))
                    {
                        string following = request.GetRequestByKey("followingname");
                        try
                        {
                            twitter.RemoveFollowing(following);
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No following");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                }
                //add follow
                else if (IsAction(FOLLOW_ACTION))
                {
                    if (IsMethod(HTTP_POST))
                    {
                        string following = request.GetRequestByKey("followingname");
                        try
                        {
                            twitter.AddFollowing(following);
                            response      = new HTTPResponse(201);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No folowing");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                }
                //unfollow
                else if (IsAction(UNFOLLOW_ACTION))
                {
                    if (IsMethod(HTTP_DELETE))
                    {
                        try
                        {
                            twitter.RemoveFollowing(following);
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No following");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }

                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                }
            }
            response.Status = 400;
            return(response);
        }