public void DeleteMsg(int id) { Models.ComboResponse _response = new Models.ComboResponse(); _response.bool_result = true; _response.ErrorCode = 0; _response.ErrorMsg = ""; ComboMsg msg = new ComboMsg(); msg.LoadByPrimaryKey(id); msg.IsDeleted = true; msg.Save(); _response.Entity = null; SetContentResult(_response); }
public void AddMessage(Models.ComboMessage msg) { Models.ComboResponse _response = new Models.ComboResponse(); _response.bool_result = true; _response.ErrorCode = 0; _response.ErrorMsg = ""; if (string.IsNullOrEmpty(msg.ToIds)) { _response.ErrorCode = 30; _response.ErrorMsg = "Can't add Msg. No to id ."; _response.bool_result = false; SetContentResult(_response); return; } if (msg.ToIds.Split(',').Length == 0) { _response.ErrorCode = 30; _response.ErrorMsg = "Can't add Msg. No to idsor error in format ."; _response.bool_result = false; SetContentResult(_response); return; } BLL.ComboMsg newMsg = new ComboMsg(); newMsg.AddNew(); if (msg.ComboUserID != 0) newMsg.ComboUserID = msg.ComboUserID; else { _response.ErrorCode = 30; _response.ErrorMsg = "Can't add Msg. No user id ."; _response.bool_result = false; SetContentResult(_response); return; } newMsg.MsgText = msg.MsgText.Replace("\n", " "); ; newMsg.MsgDate = DateTime.UtcNow; newMsg.IsRead = false; newMsg.Save(); string[] ToIds = msg.ToIds.Split(','); ComboUserMsg tomsg = new ComboUserMsg(); foreach (string item in ToIds) { tomsg.AddNew(); tomsg.ComboUserID = Convert.ToInt32(item); tomsg.ComboMsgID = newMsg.ComboMsgID; } tomsg.Save(); /**************************/ // save notification and push it to device ComboUser creator = new ComboUser(); ComboUser commentor = new ComboUser(); creator.GetUserByUserId(newMsg.ComboUserID); List<Models.ComboMessage> amsg = newMsg.DefaultView.Table.AsEnumerable().Select(row => { return new Models.ComboMessage { ComboMsgID = Convert.ToInt32(row["ComboMsgID"]), ComboUserID = Convert.ToInt32(row["ComboUserID"]), ComboUserName = creator.UserName, ComboUserDisplayName = creator.DisplayName, MsgText = newMsg.MsgText, MsgDate = newMsg.MsgDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds, ProfilePic = creator.GetColumn("ProfilePic").ToString() }; }).ToList(); foreach (string item in ToIds) { ComboNotification notification = new ComboNotification(); notification.AddNew(); notification.ComboUserID = Convert.ToInt32(item); notification.NotificationType = (int)Combo.Models.NotificationType.RECEIVE_MSG; // new Msg recieved notification.NotificationDate = DateTime.UtcNow; notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(amsg); notification.IsRead = false; notification.Save(); commentor.GetUserByUserId(Convert.ToInt32(item)); NotificationUserSettings settings = new NotificationUserSettings(); settings.LoadByPrimaryKey(Convert.ToInt32(item), (int)Combo.Models.NotificationType.RECEIVE_MSG); bool notify = false; if (settings.RowCount == 0) notify = true; else notify = settings.CanGetNotification(commentor.ComboUserID, creator.ComboUserID, (int)Combo.Models.NotificationType.FOLLOW_FIREND); if (notify) { List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row => { return new Models.ComboNotification { ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]), ComboUserID = Convert.ToInt32(row["ComboUserID"]), IsRead = Convert.ToBoolean(row["IsRead"]), NotificationBody = row["NotificationBody"].ToString(), NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds, NotificationType = Convert.ToInt32(row["NotificationType"]) }; }).ToList(); SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), commentor.DeviceID); } } /**************************/ JavaScriptSerializer js = new JavaScriptSerializer(); Models.Attachment[] att = js.Deserialize<Models.Attachment[]>(js.Serialize(msg.Attachments)); ComboMsgAttachment attachment = new ComboMsgAttachment(); foreach (Models.Attachment item in att) { attachment.AddNew(); attachment.AttachmentID = item.AttachmentID; attachment.ComboMsgID = newMsg.ComboMsgID; } attachment.Save(); msg.ComboMsgID = newMsg.ComboMsgID; msg.MsgDate = newMsg.MsgDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; _response.Entity = new Models.ComboMessage[] { msg }; SetContentResult(_response); return; }