public IHttpActionResult AddMessage(int ConservationId, string newMessage) { int CurrenUser = 0; using (var db = new UserDAL()) { CurrenUser = db.GetUserByUserNameOrEmail(User.Identity.Name).UserID; } Message mess = new Message(); mess.ConservationId = ConservationId; mess.Content = newMessage; mess.CreatedDate = DateTime.Now; mess.UserId = CurrenUser; mess.Status = true; using (var db = new ConservationDAL()) { db.AddNewMessage(mess); Conversation current = db.GetConservationById(ConservationId); current.UpdatedTime = DateTime.Now; db.UpdateTime(current); } //Update Updated Time for Conservation return(Ok(new HTTPMessageDTO { Status = WsConstant.HttpMessageType.SUCCESS })); }
public IHttpActionResult GetAllMessageByConservationId(int conservationId) { List <MessageBasicInfoDTO> MessageList; using (var db = new ConservationDAL()) { MessageList = db.GetAllMessageByConservationId(conservationId, User.Identity.Name); } return(Ok(new HTTPMessageDTO { Status = WsConstant.HttpMessageType.SUCCESS, Data = MessageList })); }
public IHttpActionResult AddConservation(ConservationBasicInfoDTO newConservation) { int receiverId = 0; int CurrentUser = 0; Conversation returnedConservation; try { using (var db = new UserDAL()) { CurrentUser = db.GetUserByUserNameOrEmail(User.Identity.Name).UserID; receiverId = db.GetUserByUserNameOrEmail(newConservation.ReceiverName).UserID; } //Add conservation var conservation = new Conversation { CreatorId = CurrentUser, ReceiverId = receiverId, Title = newConservation.Title, UpdatedTime = DateTime.Now, CreatedDate = DateTime.Now, Status = true, IsCreatorRead = true, IsReceiverRead = false, }; using (var db = new ConservationDAL()) { returnedConservation = db.AddNewConservation(conservation); } //Add First Message of Conservation var message = new Message { UserId = CurrentUser, ConservationId = returnedConservation.ConservationId, Content = newConservation.Content, CreatedDate = DateTime.Now, Status = true }; using (var db = new ConservationDAL()) { db.AddNewMessage(message); } } catch (Exception) { return(Redirect("/#/Error")); } return(Ok(new HTTPMessageDTO { Status = WsConstant.HttpMessageType.SUCCESS })); }
public IHttpActionResult GetConservationByStatus(bool status) { List <ConservationBasicInfoDTO> ConservationList; using (var db = new ConservationDAL()) { ConservationList = db.GetAllConservationByUserIdAndStatus(User.Identity.Name, status); } if (ConservationList != null) { return(Ok(new HTTPMessageDTO { Status = WsConstant.HttpMessageType.SUCCESS, Data = ConservationList })); } else { return(Ok(new HTTPMessageDTO { Status = WsConstant.HttpMessageType.NOT_FOUND })); } }
public void SendMessage(int ConservationId, string Message) { int UserId = 0; using (var db = new UserDAL()) { UserId = db.GetUserByUserNameOrEmail(HttpContext.Current.User.Identity.Name).UserID; } Message newMess = new Message(); MessageBasicInfoDTO info = new MessageBasicInfoDTO(); using (var db = new Ws_DataContext()) { newMess.ConservationId = ConservationId; newMess.Content = Message; newMess.CreatedDate = DateTime.Now; newMess.Status = true; newMess.UserId = UserId; newMess = db.Message.Add(newMess); db.SaveChanges(); using (var dbCurrent = new ConservationDAL()) { Conversation current = dbCurrent.GetConservationById(ConservationId); current.UpdatedTime = DateTime.Now; if (UserId == current.CreatorId) { current.IsReceiverRead = false; } if (UserId == current.ReceiverId) { current.IsCreatorRead = false; } dbCurrent.UpdateTime(current); } var GetInfo = (from p in db.Message where p.MessageId == newMess.MessageId select new { p.User.UserName, p.User.User_Information.ProfileImage }).SingleOrDefault(); info.CreatorImage = GetInfo.ProfileImage; info.CreatorName = GetInfo.UserName; info.Content = newMess.Content; } if (DateTime.Now.Subtract(newMess.CreatedDate).Hours <= 24 && DateTime.Now.Subtract(newMess.CreatedDate).Hours >= 1) { info.CreatedDate = DateTime.Now.Subtract(newMess.CreatedDate).Hours + " Tiếng cách đây"; } else if (DateTime.Now.Subtract(newMess.CreatedDate).Hours > 24) { info.CreatedDate = newMess.CreatedDate.ToString("H:mm:ss dd/MM/yy"); } else { info.CreatedDate = DateTime.Now.Subtract(newMess.CreatedDate).Minutes + " Phút cách đây"; } //Get list connection Id specify by UserId List <string> ListConnetion = new List <string>(); using (var db = new Ws_DataContext()) { //Get Id of Receiver var Id = (from p in db.Conversation where p.ConservationId == ConservationId select new { p.CreatorId, p.ReceiverId }).SingleOrDefault(); if (newMess.UserId == Id.CreatorId) { ListConnetion = db.Connection.Where(x => x.UserId == Id.ReceiverId).Select(x => x.ConnectionString).ToList(); } else { ListConnetion = db.Connection.Where(x => x.UserId == Id.CreatorId).Select(x => x.ConnectionString).ToList(); } if (ListConnetion.Count() != 0) { var currentConversation = db.Conversation.Where(x => x.ConservationId == ConservationId).SingleOrDefault(); if (UserId == currentConversation.CreatorId) { currentConversation.IsReceiverRead = true; } if (UserId == currentConversation.ReceiverId) { currentConversation.IsCreatorRead = true; } db.Conversation.AddOrUpdate(currentConversation); db.SaveChanges(); } } //Send it to caller Clients.Caller.ReceiverMessage(info); //Send new Message to Receiver if Connecting foreach (var item in ListConnetion) { Clients.Client(item).NewMessageNotification("Bạn đã nhận 1 tin nhắn mới, xem tại Tin nhắn!"); Clients.Client(item).ReceiverMessage(info); } }