PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Server/DeviceHive.API/Controllers/DeviceNotificationController.cs

https://github.com/oryol/devicehive-.net
C# | 104 lines | 69 code | 12 blank | 23 comment | 13 complexity | cc3ecbae3b4b9dae3166f71cf78e8007 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. using DeviceHive.API.Business;
  8. using DeviceHive.API.Filters;
  9. using DeviceHive.API.Mapping;
  10. using DeviceHive.Core.Messaging;
  11. using DeviceHive.Data.Model;
  12. using Newtonsoft.Json.Linq;
  13. using Ninject;
  14. namespace DeviceHive.API.Controllers
  15. {
  16. /// <resource cref="DeviceNotification" />
  17. public class DeviceNotificationController : BaseController
  18. {
  19. private ObjectWaiter<DeviceNotification> _notificationWaiter;
  20. private INotificationManager _notificationManager;
  21. private readonly MessageBus _messageBus;
  22. public DeviceNotificationController(ObjectWaiter<DeviceNotification> notificationWaiter,
  23. INotificationManager notificationManager, MessageBus messageBus)
  24. {
  25. _notificationWaiter = notificationWaiter;
  26. _notificationManager = notificationManager;
  27. _messageBus = messageBus;
  28. }
  29. /// <name>query</name>
  30. /// <summary>
  31. /// Queries device notifications.
  32. /// </summary>
  33. /// <param name="deviceGuid">Device unique identifier.</param>
  34. /// <param name="start">Notification start date (inclusive, UTC).</param>
  35. /// <param name="end">Notification end date (inclusive, UTC)</param>
  36. /// <returns cref="DeviceNotification">If successful, this method returns array of <see cref="DeviceNotification"/> resources in the response body.</returns>
  37. [AuthorizeUser]
  38. public JToken Get(Guid deviceGuid, DateTime? start = null, DateTime? end = null)
  39. {
  40. var device = DataContext.Device.Get(deviceGuid);
  41. if (device == null || !IsNetworkAccessible(device.NetworkID))
  42. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  43. return new JArray(DataContext.DeviceNotification.GetByDevice(device.ID, start, end).Select(n => Mapper.Map(n)));
  44. }
  45. /// <name>get</name>
  46. /// <summary>
  47. /// Gets information about device notification.
  48. /// </summary>
  49. /// <param name="deviceGuid">Device unique identifier.</param>
  50. /// <param name="id">Notification identifier.</param>
  51. /// <returns cref="DeviceNotification">If successful, this method returns a <see cref="DeviceNotification"/> resource in the response body.</returns>
  52. [AuthorizeUser]
  53. public JObject Get(Guid deviceGuid, int id)
  54. {
  55. var device = DataContext.Device.Get(deviceGuid);
  56. if (device == null || !IsNetworkAccessible(device.NetworkID))
  57. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  58. var notification = DataContext.DeviceNotification.Get(id);
  59. if (notification == null || notification.DeviceID != device.ID)
  60. ThrowHttpResponse(HttpStatusCode.NotFound, "Device notification not found!");
  61. return Mapper.Map(notification);
  62. }
  63. /// <name>insert</name>
  64. /// <summary>
  65. /// Creates new device notification.
  66. /// </summary>
  67. /// <param name="deviceGuid">Device unique identifier.</param>
  68. /// <param name="json" cref="DeviceNotification">In the request body, supply a <see cref="DeviceNotification"/> resource.</param>
  69. /// <returns cref="DeviceNotification">If successful, this method returns a <see cref="DeviceNotification"/> resource in the response body.</returns>
  70. [HttpCreatedResponse]
  71. [AuthorizeDeviceOrUser(Roles = "Administrator")]
  72. public JObject Post(Guid deviceGuid, JObject json)
  73. {
  74. EnsureDeviceAccess(deviceGuid);
  75. var device = DataContext.Device.Get(deviceGuid);
  76. if (device == null || !IsNetworkAccessible(device.NetworkID))
  77. ThrowHttpResponse(HttpStatusCode.NotFound, "Device not found!");
  78. var notification = Mapper.Map(json);
  79. notification.Device = device;
  80. Validate(notification);
  81. DataContext.DeviceNotification.Save(notification);
  82. _notificationManager.ProcessNotification(notification);
  83. _notificationWaiter.NotifyChanges(device.ID);
  84. _messageBus.Notify(new DeviceNotificationAddedMessage(deviceGuid, notification.ID));
  85. return Mapper.Map(notification);
  86. }
  87. private IJsonMapper<DeviceNotification> Mapper
  88. {
  89. get { return GetMapper<DeviceNotification>(); }
  90. }
  91. }
  92. }