public ServerResult Register(RegisterPacketData data) { try { Device newdvc = new Device { DeviceID = data.DeviceID, OSID = (int)data.OSID, URI = data.URI }; Notification_DBDataContext db = new Notification_DBDataContext(); // tìm recceiver Receiver rcv = db.Receivers.Single(r => r.OldID == data.ReceiverOldID); // kiểm tra device đã tồn tại try { Device dvc = db.Devices.Single(dv => dv.DeviceID == data.DeviceID); //thiết bị đã tồn tại thì cập nhật URI cho thiết bị rồi xóa token key dvc.URI = data.URI; } catch (InvalidOperationException) // không tồn tại thiết bị thì add thiết bị mới vào { rcv.Devices.Add(newdvc); } db.SubmitChanges(); return(ServerResult.Success); } catch (InvalidOperationException) {// không tìm thấy receiver return(ServerResult.NotFound); } catch (Exception ex) { OnUnhandledErrorOccurred(ex); return(ServerResult.Error); } }
private void RemoveAllReadedNotification(Notification_DBDataContext db) { try { db.ReceiverNotifications.DeleteAllOnSubmit(db.ReceiverNotifications.Where(r => r.Readed)); db.SubmitChanges(); } catch (System.Data.SqlClient.SqlException ex) { OnDBInteractionErrorOccurred(ex); } }
/// <summary> /// Lấy dữ liệu thông báo /// </summary> /// <param name="NotificationID">ID thông báo</param> /// <returns>Mảng byte dữ liệu của thông báo</returns> public byte[] NotificationContent(long NotificationID, out ServerResult result) { Notification_DBDataContext db = new Notification_DBDataContext(); byte[] notificationContent = null; try { notificationContent = db.Notifications.Single(n => n.NotificationID == NotificationID).NotificationContent.ToArray(); } catch (InvalidOperationException) // client yêu cầu notification không tồn tại { result = ServerResult.NotFound; return(null); } catch (System.Data.SqlClient.SqlException Sqlex) { OnDbInteractionErrorOccurred(Sqlex); result = ServerResult.Error; return(null); } catch (Exception ex) { OnUnhandledErrorOccurred(ex); result = ServerResult.Error; return(null); } result = ServerResult.Success; return(notificationContent); #region Old Code //try //{ //byte[] notificationContent = (from drcv in db.Receivers // join drcvnoti in db.ReceiverNotifications on drcv.NewID equals drcvnoti.ReceiverNewID // join notis in db.Notifications on drcvnoti.NotificationID equals notis.NotificationID // where drcv.OldID == rcv.ReceiverOldID && drcvnoti.NotificationID == NotificationID // select notis.NotificationContent).Take(1).ElementAt(1).ToArray(); // if (notificationContent == null) // { // return default(T); // } // return _DBNotificationContentConverter.BytesToObject(notificationContent); //} //catch (Exception ex) //{ // OnUnHandledErrorOccurred(ex); // return default(T); //} #endregion }
internal AddNotificationFSPacketData AddNotification(AddNotificationPacketData addNotification) { Notification_DBDataContext db = new Notification_DBDataContext(); Notification notifi = new Notification { NotificationContent = new System.Data.Linq.Binary(addNotification.NotificationContent) }; db.Notifications.InsertOnSubmit(notifi); #region thêm thông báo vào cho các receiver // lấy ra tất cả các người nhận trong Notification_DB theo các ID của người nhận có được ở bước trước var recievers = db.Receivers.Where(rc => addNotification.ReceiversOldID.Contains(rc.OldID)); try { foreach (var receiver in recievers) { // tạo thông báo cho mỗi người nhận ReceiverNotification newreceivernoti = new ReceiverNotification { Notification = notifi }; // thêm thông báo cho người nhận receiver.ReceiverNotifications.Add(newreceivernoti); foreach (var device in receiver.Devices) { // với mỗi thiết bị của người đó, thêm thông báo tới thiết bị Console.WriteLine("add device notification"); device.DeviceNotifications.Add(new DeviceNotification { ReceiverNotification = newreceivernoti }); } } db.SubmitChanges(); return(new AddNotificationFSPacketData(true, ComminucateServerErrorType.None, null)); } catch (System.Data.SqlClient.SqlException Sqlex) { return(new AddNotificationFSPacketData(false, ComminucateServerErrorType.SqlError, Sqlex)); } catch (Exception ex) { return(new AddNotificationFSPacketData(false, ComminucateServerErrorType.Unknow, ex)); } #endregion }