/// <summary> /// Synchronization deleting friend /// </summary> /// <param name="friendReceiveModel"></param> public void SynchronizeDeletingFriend(FriendReceiveModel friendReceiveModel) { var onlineFriend = _connectedClients.FirstOrDefault(cc => cc.Id == friendReceiveModel.FriendId); if (onlineFriend != null) { _logger.Info($"Sync deleting friend {onlineFriend.Id} by user {friendReceiveModel.UserId}"); Task.Run(() => { var responseJson = _jsonStringSerializer.Serialize(new OperationResultInfo() { ErrorInfo = string.Empty, OperationResult = OperationsResults.Successfully, ToListener = ListenerType.FriendListDeleteListener, JsonData = _jsonStringSerializer.Serialize(new UserListResponseModel() { UserId = friendReceiveModel.UserId }) }); onlineFriend.SendMessage(_encoder.Encryption(responseJson)); //onlineFriend.SendMessage(responseJson); }); } }
/// <summary> /// Synchronization users add to friends notifications /// </summary> public void SynchronizeAddFriendNotifications(FriendReceiveModel friendReceiveModel) { var notification = new NotificationReceiveModel() { FromUserId = friendReceiveModel.UserId, ToUserId = friendReceiveModel.FriendId }; if (_mainLogic.GetNotification(notification) == null) { _logger.Info($"Sync notification for user {friendReceiveModel.FriendId} from user {friendReceiveModel.UserId}"); notification.IsAccepted = false; notification.Message = "Пользователь " + $"{(_mainLogic.GetUser(new UserReceiveModel() { Id = notification.FromUserId }).JsonData as UserResponseModel).UserName}" + " хочет добавить вас в друзья"; _mainLogic.AddNotification(notification); var endClient = _connectedClients.FirstOrDefault(c => c.Id == notification.ToUserId); if (endClient != null) { Task.Run(() => { var notificationDb = _mainLogic.GetNotification(notification); endClient.SendMessage(_encoder.Encryption(_jsonStringSerializer.Serialize(new OperationResultInfo() { ErrorInfo = string.Empty, OperationResult = OperationsResults.Successfully, ToListener = ListenerType.NotificationListListener, JsonData = _jsonStringSerializer.Serialize(notificationDb) }))); //endClient.SendMessage(_jsonStringSerializer.Serialize(new OperationResultInfo() //{ // ErrorInfo = string.Empty, // OperationResult = OperationsResults.Successfully, // ToListener = ListenerType.NotificationListListener, // JsonData = _jsonStringSerializer.Serialize(notificationDb) //})); }); } } }
/// <summary> /// Deleting a friend from Friends database table /// </summary> /// <param name="model"><see cref="FriendReceiveModel"/></param> public void Delete(FriendReceiveModel model) { using (var context = new DatabaseContext()) { var friend = context.Friends.FirstOrDefault(f => f.UserId == model.UserId && f.FriendId == model.FriendId); if (friend == null) { throw new Exception("Пользователь не найден у вас в друзьях"); } //unbinding current user with his friend context.Friends.RemoveRange( friend, context.Friends.FirstOrDefault(f => f.UserId == model.FriendId && f.FriendId == model.UserId) ); context.SaveChanges(); } }
public OperationResultInfo DeleteFriend(FriendReceiveModel model) { try { _friendLogic.Delete(model); return(new OperationResultInfo() { ErrorInfo = string.Empty, OperationResult = OperationsResults.Successfully, ToListener = ListenerType.FriendListDeleteListener }); } catch (Exception ex) { return(new OperationResultInfo() { ErrorInfo = ex.Message, OperationResult = OperationsResults.Unsuccessfully, ToListener = ListenerType.FriendListDeleteListener }); } }
/// <summary> /// Adding a new friend to Friends database table /// </summary> /// <param name="model"><see cref="FriendReceiveModel"/></param> public void Create(FriendReceiveModel model) { using (var context = new DatabaseContext()) { if (context.Friends.FirstOrDefault(f => f.UserId == model.UserId && f.FriendId == model.FriendId) != null) { throw new Exception("Данный пользователь уже находится у вас в друзьях"); } context.Friends.AddRange( new DbModels.Friend() { UserId = model.UserId, FriendId = model.FriendId }, new DbModels.Friend() { UserId = model.FriendId, FriendId = model.UserId }); context.SaveChanges(); } }