public ConnectedStatuses Connect(UserDTO user) { if (!_users.ContainsValue(CurrentCallback) && !SearchClientsByName(user.Name)) //exists user in chatroom { lock (_syncObj) { if (ContainsUser(user)) //exists user in db { if(!ValidateUserPassword(user)) { return ConnectedStatuses.PasswordNotValid; } } else { AddUserToDataBase(user); } RegisterUser(user); //register user in chatroom foreach (var key in _users.Keys) { var callback = _users[key]; try { UpdateClients(callback); } catch { _users.Remove(key); return ConnectedStatuses.Error; } } } return ConnectedStatuses.Connected; } return ConnectedStatuses.AlreadyConnected; }
private bool ValidateUserPassword(UserDTO user) { var dbUser = _userRepository.GetUserByName(user.Name); dbUser.Password = user.Password; return dbUser.IsPasswordValid(); }
private void RegisterUser(UserDTO user) { var newUser = GetUserByName(user.Name); _users.Add(newUser, CurrentCallback); CurrentCallback.ReturnUser(newUser); }
private bool ContainsUser(UserDTO user) { return _userRepository.Contains(user.Name); }
private void AddUserToDataBase(UserDTO user) { _userRepository.Add(Mapper.Map<UserDTO, User>(user)); }
public List<MessageDTO> GetLogByUser(UserDTO user) { var byUser = Mapper.Map<UserDTO, User>(user); var messages = _messageRepository.GetMessagesByUser(byUser).ToList(); return Mapper.Map<List<Message>, List<MessageDTO>>(messages); }
public void Disconnect(UserDTO user) { foreach (var c in _users.Keys) { if (user.Name != c.Name) continue; lock (_syncObj) { _users.Remove(c); NotifyClientsForUserDisconnect(); } return; } }