public static void CreateChannel(string channelName, string userNickname, string password)
        {
            ValidateChannelName(channelName);
            ValidateNickname(userNickname);
            using (ChatContext context = new ChatContext())
            {
                var nameToLower = channelName.ToLower();

                var dbChannel = context.Channels.FirstOrDefault(c => c.Name.ToLower() == nameToLower);

                if (dbChannel != null)
                {
                    if (dbChannel.Name.ToLower() == nameToLower)
                    {
                        throw new ServerErrorException("Channel name already exists", "ERR_DUP_CHNAME");
                    }
                }

                dbChannel = new Channel()
                {
                    Name = nameToLower,
                    Password = password
                };

                var dbUser = context.Users.FirstOrDefault(u => u.Nickname.ToLower() == userNickname.ToLower());
                dbChannel.Users.Add(dbUser);

                context.Channels.Add(dbChannel);
                context.SaveChanges();
            }
        }
 public static void CreateMessage(Message message)
 {
     using (ChatContext context = new ChatContext())
     {
         context.Messages.Add(message);
         context.SaveChanges();
     }
 }
Exemplo n.º 3
0
 static void Main(string[] args)
 {
     ChatContext context = new ChatContext();
     context.Users.Add(new User()
         {
             Username = "******",
             AuthCode = "asdasdads",
             Nickname = "Pesho p",
         });
     context.SaveChanges();
 }
        public static void AddMessage(string channelName, string senderNickname, string messageText)
        {
            ValidateChannelName(channelName);
            ValidateNickname(senderNickname);
            using (ChatContext context = new ChatContext())
            {
                Message currentMsg = new Message()
                {
                    Author = senderNickname,
                    Content = messageText,
                    DateTime = DateTime.Now,
                };

                var dbChannel = context.Channels.FirstOrDefault(c => c.Name.ToLower() == channelName.ToLower());
                dbChannel.History.Add(currentMsg);

                context.SaveChanges();
            }
        }
        public static void ExitChannel(string channelName, string userNickname)
        {
            ValidateChannelName(channelName);
            ValidateNickname(userNickname);
            using (var context = new ChatContext())
            {
                var nameToLower = channelName.ToLower();

                var dbChannel = context.Channels.FirstOrDefault(c => c.Name.ToLower() == nameToLower);
                var dbUser = context.Users.FirstOrDefault(u => u.Nickname.ToLower() == userNickname.ToLower());

                dbChannel.Users.Remove(dbUser);

                if (dbChannel.Users.Count == 0)
                {
                    context.Messages.RemoveRange(dbChannel.History);
                    context.Channels.Remove(dbChannel);
                }

                context.SaveChanges();
            }
        }
        public static void JoinChannel(string channelName, string userNickname, string password)
        {
            ValidateChannelName(channelName);
            ValidateNickname(userNickname);
            using (ChatContext context = new ChatContext())
            {
                var nameToLower = channelName.ToLower();

                var dbChannel = context.Channels.FirstOrDefault(c => c.Name.ToLower() == nameToLower);

                if (dbChannel == null)
                {
                    if (dbChannel.Name.ToLower() == nameToLower)
                    {
                        throw new ServerErrorException("Channel name does not exists.", "ERR_DUP_CHNAME"); //TODO: Fix Error code
                    }
                }

                if (dbChannel.Password != password)
                {
                    throw new ServerErrorException("Password incorrect.", "INV_USR_AUTH"); //TODO: Fix Error code
                }

                var dbUser = context.Users.FirstOrDefault(u => u.Nickname.ToLower() == userNickname.ToLower());

                var alreadyJoinedChannel = dbUser.Channels.FirstOrDefault(ch => ch.Name.ToLower() == channelName.ToLower());
                if (alreadyJoinedChannel != null)
                {
                    throw new ServerErrorException("Already joined channel.", "ERR_JOINED_CHANNEL"); //TODO: Add error code
                }

                dbChannel.Users.Add(dbUser);

                context.SaveChanges();
            }
        }
        /* public members */

        public static void CreateUser(string username, string nickname, string authCode)
        {
            ValidateUsername(username);
            ValidateNickname(nickname);
            ValidateAuthCode(authCode);
            using (ChatContext context = new ChatContext())
            {
                var usernameToLower = username.ToLower();
                var nicknameToLower = nickname.ToLower();

                var dbUser = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower || u.Nickname.ToLower() == nicknameToLower);

                if (dbUser != null)
                {
                    if (dbUser.Username.ToLower() == usernameToLower)
                    {
                        throw new ServerErrorException("Username already exists", "ERR_DUP_USR");
                    }
                    else
                    {
                        throw new ServerErrorException("Nickname already exists", "ERR_DUP_NICK");
                    }
                }

                dbUser = new User()
                {
                    Username = usernameToLower,
                    Nickname = nickname,
                    AuthCode = authCode
                };
                context.Users.Add(dbUser);
                context.SaveChanges();
            }

        }
 public static void LogoutUser(string sessionKey)
 {
     ValidateSessionKey(sessionKey);
     var context = new ChatContext();
     using (context)
     {
         var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
         if (user == null)
         {
             throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
         }
         user.SessionKey = null;
         context.SaveChanges();
     }
 }
        public static string LoginUser(string username, string authCode, out string nickname)
        {
            ValidateUsername(username);
            ValidateAuthCode(authCode);
            var context = new ChatContext();
            using (context)
            {
                var usernameToLower = username.ToLower();
                var user = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower && u.AuthCode == authCode);
                if (user == null)
                {
                    throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
                }

                var sessionKey = GenerateSessionKey(user.UserId);
                user.SessionKey = sessionKey;
                nickname = user.Nickname;
                context.SaveChanges();
                return sessionKey;
            }
        }