public Profile GetProfile(Guid id)
 {
     try
     {
         return(_profilesRepository.GetProfile(id));
     }
     catch (SqlException exception)
     {
         var response = new HttpResponseMessage(HttpStatusCode.NotFound)
         {
             Content = new StringContent(exception.Message)
         };
         throw new HttpResponseException(response);
     }
 }
Beispiel #2
0
 public IEnumerable <Profile> GetChatMembers(Guid id)
 {
     try
     {
         var list     = _chatsRepository.GetChatMembers(id).ToList();
         var profiles = new List <Profile>();
         foreach (var profileId in list)
         {
             var profile = _profilesRepository.GetProfile(profileId);
             profiles.Add(profile);
             if ((DateTime.Now.TimeOfDay - profile.LastQueryDate.TimeOfDay).Minutes < 2 ||
                 !profile.IsOnline.Equals(true))
             {
                 continue;
             }
             _profilesRepository.LogoutProfile(profile.Id);
             profile.IsOnline = false;
         }
         return(profiles);
     }
     catch (SqlException exception)
     {
         var response = new HttpResponseMessage(HttpStatusCode.NotFound)
         {
             Content = new StringContent(exception.Message)
         };
         throw new HttpResponseException(response);
     }
 }
Beispiel #3
0
        public ProfileResponseDto GetProfile(int pid)
        {
            var profile = _profilesRepository.GetProfile(pid);

            if (profile == null)
            {
                throw new ProfileNotFoundException("Didn't find profile");
            }
            return(_profileMapper.map(profile));
        }
        public async Task <Profile> GetProfile(string profileId)
        {
            var profileEntity = await _profilesRepository.GetProfile(profileId);

            if (profileEntity == null)
            {
                return(null);
            }

            var contract = ProfileMapper.ToProfileContract(profileEntity);

            return(contract);
        }
Beispiel #5
0
        public async Task <Profile> GetCurrentProfile(ClaimsPrincipal claimsPrincipal)
        {
            var profileId = 0;

            var user = await _userManager.GetUserAsync(claimsPrincipal);

            if (user != null)
            {
                profileId = user.ProfileId;
            }

            var profile = _profilesRepository.GetProfile(profileId);

            return(profile);
        }
Beispiel #6
0
 public int CountMessages(Guid chatId, Guid profileId)
 {
     try
     {
         var profile = _profilesRepository.GetProfile(profileId);
         if (!profile.IsOnline || (DateTime.Now.TimeOfDay - profile.LastQueryDate.TimeOfDay).Minutes >= 1)
         {
             _profilesRepository.LoginProfile(profileId);
         }
         return(_messagesRepository.CountMessages(chatId));
     }
     catch (SqlException exception)
     {
         var response = new HttpResponseMessage(HttpStatusCode.NotFound)
         {
             Content = new StringContent(exception.Message)
         };
         throw new HttpResponseException(response);
     }
 }
Beispiel #7
0
 /// <inheritdoc />
 public Chat CreateChat(Chat chat)
 {
     Logger.Debug("Создание чата...");
     using (var connection = new SqlConnection(_connectionString))
     {
         try
         {
             connection.Open();
         }
         catch (SqlException exception)
         {
             Logger.Error($"Не могу подключиться к БД, {exception.Message}");
             throw;
         }
         using (var transaction = connection.BeginTransaction())
         {
             using (var command = connection.CreateCommand())
             {
                 chat.ChatId = Guid.NewGuid();
                 Logger.Info("Создание чата...");
                 command.Transaction = transaction;
                 command.CommandText = "INSERT INTO Chats (ChatId, ChatName) VALUES (@ChatId, @ChatName)";
                 command.Parameters.AddWithValue("@ChatId", chat.ChatId);
                 command.Parameters.AddWithValue("@ChatName", chat.ChatName);
                 try
                 {
                     command.ExecuteNonQuery();
                 }
                 catch (SqlException exception)
                 {
                     Logger.Error(exception.Message);
                     throw;
                 }
             }
             Logger.Info($"Чат {chat.ChatName} успешно создан!");
             foreach (var profile in chat.ChatMembers)
             {
                 using (var command = connection.CreateCommand())
                 {
                     command.Transaction = transaction;
                     command.CommandText = "INSERT INTO ChatMembers (ProfileId, ChatId) VALUES (@Id, @ChatId)";
                     command.Parameters.AddWithValue("@Id", profile);
                     command.Parameters.AddWithValue("@ChatId", chat.ChatId);
                     Logger.Info("Добавление членов чата в таблицу...");
                     try
                     {
                         command.ExecuteNonQuery();
                     }
                     catch (SqlException exception)
                     {
                         Logger.Error(exception.Message);
                         throw;
                     }
                 }
                 Logger.Info($"Пользователь {profile} добавлен в чат {chat.ChatName}!");
             }
             transaction.Commit();
             chat.ChatMembers = chat.ChatMembers.Select(x => _profilesRepository.GetProfile(x).Id);
             Logger.Info("Создание чата завершено.");
             return(chat);
         }
     }
 }