private List <OwlConversation> GetConversations(string where, Dictionary <string, object> param = null, string overrideSql = null) { List <OwlConversation> result = new List <OwlConversation>(); DataTable dt = null; if (string.IsNullOrWhiteSpace(overrideSql)) { dt = GetQuery(string.Format("SELECT DISTINCT c.* FROM OwlConversation c {0}", where), param); } else { dt = GetQuery(overrideSql, param); } foreach (DataRow dr in dt.Rows) { OwlConversation temp = new OwlConversation(); temp.Id = int.Parse(dr["Id"].ToString()); temp.Subject = dr["Subject"].ToString(); temp.Created = DateTime.Parse(dr["Created"].ToString()); temp.UserIds = dr["UserIds"].ToString().Split(',').Select(int.Parse).ToList(); result.Add(temp); } return(result); }
private int CreateConversation(MySqlCommand cmd, OwlConversation conversation) { cmd.Parameters.Clear(); cmd.CommandText = @"INSERT INTO OwlConversation(Subject, UserIds) Values(@Subject, @UserIds);"; cmd.CommandText += " SELECT last_insert_id();"; cmd.Parameters.AddWithValue("Subject", conversation.Subject); cmd.Parameters.AddWithValue("UserIds", string.Join(",", conversation.UserIds)); return(int.Parse(cmd.ExecuteScalar().ToString())); }
public ActionResult OpenConversation(int conversationId) { int userId = int.Parse(User.Identity.Name); OwlConversation conversation = Lib.DatabaseManager.OwlManager.Current.GetConversation(conversationId); Conversation_VM viewModel = new Conversation_VM(); viewModel.Conversation = conversation; // Mark conversation as read! Lib.DatabaseManager.OwlManager.Current.MarkAsRead(conversationId, userId); ViewData["OwlsCount"] = Lib.DatabaseManager.OwlManager.Current.MustReadCount(userId); return(PartialView("_OwlConversation", viewModel)); }
public int CreateOwl(Owl owl, OwlConversation conversation) { try { MySqlCommand cmd = BeginTransaction(); int result = CreateOwl(cmd, owl); foreach (int userId in conversation.UserIds.Where(x => x != owl.UserId)) { CreateConversationMustRead(cmd, conversation.Id, userId); } Commit(cmd); return(result); } catch { return(-1); } }
public int CreateConversation(OwlConversation conversation, int createdByUserId) { try { MySqlCommand cmd = BeginTransaction(); int result = CreateConversation(cmd, conversation); foreach (int userId in conversation.UserIds.Where(x => x != createdByUserId)) { CreateConversationMustRead(cmd, result, userId); } Commit(cmd); return(result); } catch { return(-1); } }
public ActionResult CreateConversation(Models.Modals.Owls.CreateConversation_VM viewModel) { int userId = int.Parse(User.Identity.Name); viewModel.Error = ""; // Check if all required fields has a value if (string.IsNullOrWhiteSpace(viewModel.Recipient)) { viewModel.Error = "You must enter a recipient."; } else if (string.IsNullOrWhiteSpace(viewModel.Content)) { viewModel.Error = "You must enter a message."; } // Check if all recipients was found List <User> users = new List <User>(); if (!string.IsNullOrWhiteSpace(viewModel.Recipient)) { string[] recipients = viewModel.Recipient.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); foreach (string recipient in recipients) { if (!string.IsNullOrWhiteSpace(recipient.Trim())) { User u = Lib.DatabaseManager.UserManager.Current.GetFromCharName(recipient.Trim()); if (u == null) { // Error! viewModel.Error = recipient + " does not exist as a user!"; break; } else { users.Add(u); } } } } if (users.Count == 0) { viewModel.Error = "You must enter a valid recipient."; } users.Add(new Lib.Entity.User { Id = userId }); // If there are at least one error - return it to the view if (!string.IsNullOrWhiteSpace(viewModel.Error)) { return(PartialView("_OwlsCreateConversation", viewModel)); } // All data is given - create the conversation! if (string.IsNullOrWhiteSpace(viewModel.Subject)) { viewModel.Subject = "No subject"; } OwlConversation conversation = new OwlConversation(); conversation.Subject = viewModel.Subject; conversation.UserIds = users.Select(x => x.Id).ToList(); conversation.Id = Lib.DatabaseManager.OwlManager.Current.CreateConversation(conversation, userId); if (conversation.Id <= 0) { viewModel.Error = "Unknown error occurred!"; return(PartialView("_OwlsCreateConversation", viewModel)); } Owl owl = new Owl(); owl.OwlConversationId = conversation.Id; owl.UserId = userId; owl.Content = viewModel.Content.Replace(System.Environment.NewLine, "<br>"); owl.Id = Lib.DatabaseManager.OwlManager.Current.CreateOwl(owl, conversation); return(Owls()); }