public NotificationResponseModel GetNotification(NotificationReceiveModel model) { using (var context = new DatabaseContext()) { var notification = context.Notifications .Include(n => n.FromUser) .FirstOrDefault(n => model.Id.HasValue && n.Id == model.Id.Value || n.FromUserId == model.FromUserId && n.ToUserId == model.ToUserId); if (notification == null) { throw new Exception("Уведомление не найдено"); } return(new NotificationResponseModel() { Id = notification.Id, FromUserId = notification.FromUserId, FromUserName = notification.FromUser.UserName, Message = notification.Message, UserPicture = new FileModel() { FileName = notification.FromUser.PictureName, Extension = notification.FromUser.PictureExtension, BinaryForm = notification.FromUser.Picture } }); } }
public void DeleteNotification(NotificationReceiveModel notification) { try { _notificationLogic.Delete(notification); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public NotificationResponseModel GetNotification(NotificationReceiveModel notification) { try { return(_notificationLogic.GetNotification(notification)); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
/// <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 notification from Notifications database table /// </summary> /// <param name="model"><see cref="NotificationReceiveModel"/></param> public void Delete(NotificationReceiveModel model) { using (var context = new DatabaseContext()) { if (model.Id.HasValue) { var notification = context.Notifications.FirstOrDefault(n => n.Id == model.Id.Value); if (notification == null) { throw new Exception("Удаляемое уведомление не найдено!"); } context.Notifications.Remove(notification); context.SaveChanges(); } else { throw new Exception("Ошибка передачи данных, не было задано свойство Id модели"); } } }
/// <summary> /// Adding a new notification to Notifications database table /// </summary> /// <param name="model"><see cref="NotificationReceiveModel"/></param> public void Create(NotificationReceiveModel model) { using (var context = new DatabaseContext()) { //maybe tmp if (context.Notifications.FirstOrDefault(n => n.FromUserId == model.FromUserId && n.ToUserId == model.ToUserId && n.Message == model.Message && n.IsAccepted == model.IsAccepted) != null) { throw new Exception("Такое уведомление уже есть! тестовая проверка"); } context.Notifications.Add(new DbModels.Notification() { FromUserId = model.FromUserId, ToUserId = model.ToUserId, IsAccepted = false, Message = model.Message }); context.SaveChanges(); } }
public OperationResultInfo UpdateNotification(NotificationReceiveModel notification) { try { _notificationLogic.Update(notification); return(new OperationResultInfo() { ErrorInfo = string.Empty, OperationResult = OperationsResults.Successfully, ToListener = ListenerType.NotificationListListener }); } catch (Exception ex) { return(new OperationResultInfo() { ErrorInfo = ex.Message, OperationResult = OperationsResults.Unsuccessfully, ToListener = ListenerType.NotificationListListener }); } }
//TODO : можно сделать лучше public void SynchronizeAddingFriend(NotificationReceiveModel notificationReceiveModel) { if (notificationReceiveModel.IsAccepted == false) { _mainLogic.DeleteNotification(notificationReceiveModel); } else { //binding users _mainLogic.AddFriend(new FriendReceiveModel() { UserId = notificationReceiveModel.FromUserId, FriendId = notificationReceiveModel.ToUserId }); _logger.Info($"Sync adding to friends users: {notificationReceiveModel.FromUserId},{notificationReceiveModel.ToUserId}"); var user1 = _mainLogic.GetUser(new UserReceiveModel() { Id = notificationReceiveModel.FromUserId })?.JsonData as UserResponseModel; var user2 = _mainLogic.GetUser(new UserReceiveModel() { Id = notificationReceiveModel.ToUserId })?.JsonData as UserResponseModel; if (user1 != null && user2 != null) { var connectedUser1 = _connectedClients.FirstOrDefault(c => c.Id == user1.UserId); if (connectedUser1 != null) { Task.Run(() => { _logger.Info($"Sync send info about adding to friends for user {connectedUser1.Id}"); connectedUser1.SendMessage(_encoder.Encryption(_jsonStringSerializer.Serialize(new OperationResultInfo() { ErrorInfo = string.Empty, OperationResult = OperationsResults.Successfully, ToListener = ListenerType.FriendListListener, JsonData = _jsonStringSerializer.Serialize(new UserListResponseModel() { UserId = user2.UserId, UserName = user2.UserName, Picture = user2.File, IsOnline = user2.IsOnline }) }))); //connectedUser1.SendMessage(_jsonStringSerializer.Serialize(new OperationResultInfo() //{ // ErrorInfo = string.Empty, // OperationResult = OperationsResults.Successfully, // ToListener = ListenerType.FriendListListener, // JsonData = _jsonStringSerializer.Serialize(new UserListResponseModel() // { // UserId = user2.UserId, // UserName = user2.UserName, // Picture = user2.File, // IsOnline = user2.IsOnline // }) //})); }); } var connectedUser2 = _connectedClients.FirstOrDefault(c => c.Id == user2.UserId); if (connectedUser2 != null) { _logger.Info($"Sync send info about adding to friends for user {connectedUser2.Id}"); Task.Run(() => { connectedUser2.SendMessage(_encoder.Encryption(_jsonStringSerializer.Serialize(new OperationResultInfo() { ErrorInfo = string.Empty, OperationResult = OperationsResults.Successfully, ToListener = ListenerType.FriendListListener, JsonData = _jsonStringSerializer.Serialize(new UserListResponseModel() { UserId = user1.UserId, UserName = user1.UserName, Picture = user1.File, IsOnline = user1.IsOnline }) }))); //connectedUser2.SendMessage(_jsonStringSerializer.Serialize(new OperationResultInfo() //{ // ErrorInfo = string.Empty, // OperationResult = OperationsResults.Successfully, // ToListener = ListenerType.FriendListListener, // JsonData = _jsonStringSerializer.Serialize(new UserListResponseModel() // { // UserId = user1.UserId, // UserName = user1.UserName, // Picture = user1.File, // IsOnline = user1.IsOnline // }) //})); }); } } _mainLogic.DeleteNotification(notificationReceiveModel); } }