private static void ColAddParams(NotificationAddRequest model, SqlParameterCollection col) { col.AddWithValue("@UserId", model.UserId); col.AddWithValue("@NotificationTypeId", model.NotificationTypeId); col.AddWithValue("@NotificationText", model.NotificationText); col.AddWithValue("@IsRead", model.IsRead); }
public async Task SendMessage(string messageText, int recipientId, string recipientName) { int userId = _authService.GetCurrentUserId(); //Create MessageAddRequest MessageAddRequest message = new MessageAddRequest(); message.RecipientId = recipientId; message.MessageText = messageText; message.DateSent = DateTime.Now; //DB Call int createdMessageId = _service.Add(message, userId); //Create message to send Message createdMessage = new Message(); UserProfile recipient = new UserProfile(); recipient.UserId = recipientId; UserProfile sender = new UserProfile(); sender.UserId = userId; createdMessage.Recipient = recipient; createdMessage.Sender = sender; createdMessage.MessageText = messageText; createdMessage.DateSent = DateTime.Now; Random rnd = new Random(); createdMessage.Id = rnd.Next(500000); // TODO Try/catch sql exception //Get current chatroom and send NotificationAddRequest notification = new NotificationAddRequest(); notification.UserId = recipientId; notification.NotificationTypeId = 2; notification.NotificationText = $"New message from {recipientName}"; Notification notification1 = new Notification(); notification1.NotificationText = $"New message from {recipientName}"; notification1.UserId = recipientId; notification1.NotificationTypeId = 2; notification1.DateCreated = new DateTime(); _notificationService.Add(notification); string chatRoomId = await _chatHubService.GetCurrentUserRoom(userId); await Clients.Group(chatRoomId).SendAsync("AddMessage", createdMessage); await _notificationHubContext.Clients.User(recipientId.ToString()).SendAsync("ReceiveNotification", notification1); }
public int Add(NotificationAddRequest model) { int id = 0; string procName = "[dbo].[Notifications_Insert]"; _data.ExecuteNonQuery(procName, inputParamMapper : delegate(SqlParameterCollection col) { ColAddParams(model, col); SqlParameter idOut = new SqlParameter("@Id", SqlDbType.Int); idOut.Direction = ParameterDirection.Output; col.Add(idOut); }, returnParameters : delegate(SqlParameterCollection returnCollection) { object OId = returnCollection["@Id"].Value; int.TryParse(OId.ToString(), out id); }); return(id); }