public static void CreateUser(string username, string authCode, string avatar)
        {
            ValidateUsername(username);
            ValidateAuthCode(authCode);
            using (NewsFeedContext context = new NewsFeedContext())
            {
                var usernameToLower = username.ToLower();

                var dbUser = context.Users.FirstOrDefault(u => u.Username == usernameToLower);

                if (dbUser != null)
                {
                    throw new ServerErrorException("Username already exists", "ERR_DUP_USR");
                }

                dbUser = new User()
                {
                    Username = usernameToLower,
                    AuthenticationCode = authCode,
                    Avatar = avatar
                };
                context.Users.Add(dbUser);
                context.SaveChanges();
            }
        }
 public static void DeleteRequest(int id)
 {
     using (NewsFeedContext context = new NewsFeedContext())
     {
         var request = context.Requests.Find(id);
         context.Requests.Remove(request);
         context.SaveChanges();
     }
 }
 public static void CreatePost(string title, string content, string sessionKey)
 {
     using (NewsFeedContext context = new NewsFeedContext())
     {
         Post dbPost = new Post()
         {
             Title = title,
             Content = content,
             PublicDate = DateTime.Now,
             UserId = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey).Id
         };
         context.Posts.Add(dbPost);
         context.SaveChanges();
     }
 }
 public static void SendRequest(string username, string sessionKey)
 {
     using (NewsFeedContext context = new NewsFeedContext())
     {
         Request dbRequest = new Request()
         {
             RecipientId = (from user in context.Users
                            where user.Username == username
                            select user.Id).FirstOrDefault(),
             SenderId = (from user in context.Users
                         where user.SessionKey == sessionKey
                         select user.Id).FirstOrDefault(),
             Title = "Friend request",
         };
         context.Requests.Add(dbRequest);
         context.SaveChanges();
     }
 }
        //Accept request
        public static void AddFriend(int senderId, string sessionKey)
        {
            using (NewsFeedContext context = new NewsFeedContext())
            {
                //Add request sender as friend in request receiver's list
                Friend dbFriendSender = new Friend()
                {
                    Username = (from request in context.Requests
                                where request.SenderId == senderId
                                select request.Sender.Username).FirstOrDefault(),
                    Avatar = (from request in context.Requests
                              where request.SenderId == senderId
                              select request.Sender.Avatar).FirstOrDefault(),
                    UserId = (from user in context.Users
                              where user.SessionKey == sessionKey
                              select user.Id).FirstOrDefault()
                };

                //Add request receiver as friend in request sender's list
                Friend dbFriendReceiver = new Friend()
                {
                    Username = (from user in context.Users
                                where user.SessionKey == sessionKey
                                select user.Username).FirstOrDefault(),
                    Avatar = (from user in context.Users
                              where user.SessionKey == sessionKey
                              select user.Avatar).FirstOrDefault(),
                    UserId = (from request in context.Requests
                              where request.SenderId == senderId
                              select request.Sender.Id).FirstOrDefault()
                };
                context.Friends.Add(dbFriendSender);
                context.Friends.Add(dbFriendReceiver);
                context.SaveChanges();
            }
        }
        public static string LoginUser(string username, string authCode, out string avatar)
        {
            ValidateUsername(username);
            ValidateAuthCode(authCode);
            var context = new NewsFeedContext();
            using (context)
            {
                var usernameToLower = username.ToLower();
                var user = context.Users.FirstOrDefault(u => u.Username == usernameToLower && u.AuthenticationCode == authCode);
                if (user == null)
                {
                    throw new ServerErrorException("Invalid username or password", "ERR_INV_USR");
                }

                var sessionKey = GenerateSessionKey((int)user.Id);
                user.SessionKey = sessionKey;
                avatar = user.Avatar;
                context.SaveChanges();
                return sessionKey;
            }
        }