PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/oryol/devicehive-.net
C# | 174 lines | 113 code | 23 blank | 38 comment | 26 complexity | 3bcbeb793f9c6eedfa53458541bfb2b8 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.Web.Http;
  6. using DeviceHive.API.Filters;
  7. using DeviceHive.API.Mapping;
  8. using DeviceHive.Data.Model;
  9. using DeviceHive.Data.Repositories;
  10. using Newtonsoft.Json.Linq;
  11. namespace DeviceHive.API.Controllers
  12. {
  13. /// <resource cref="Network" />
  14. public class NetworkController : BaseController
  15. {
  16. /// <name>list</name>
  17. /// <summary>
  18. /// Gets list of device networks.
  19. /// <para>If caller belongs to the Client user role, the result list is limited to networks the user has access to.</para>
  20. /// </summary>
  21. /// <returns cref="Network">If successful, this method returns array of <see cref="Network"/> resources in the response body.</returns>
  22. [AuthorizeUser]
  23. public JArray Get()
  24. {
  25. var networks = RequestContext.CurrentUser.Role == (int)UserRole.Client ?
  26. DataContext.Network.GetByUser(RequestContext.CurrentUser.ID) :
  27. DataContext.Network.GetAll();
  28. return new JArray(networks.Select(n => Mapper.Map(n)));
  29. }
  30. /// <name>get</name>
  31. /// <summary>
  32. /// Gets information about device network and its devices.
  33. /// </summary>
  34. /// <param name="id">Network identifier.</param>
  35. /// <returns cref="Network">If successful, this method returns a <see cref="Network"/> resource in the response body.</returns>
  36. /// <response>
  37. /// <parameter name="devices" type="array" cref="Device">Array of devices registered in the current network.</parameter>
  38. /// <parameter name="devices[].network" mode="remove" />
  39. /// </response>
  40. [AuthorizeUser]
  41. public JObject Get(int id)
  42. {
  43. var network = DataContext.Network.Get(id);
  44. if (network == null || !IsNetworkAccessible(network.ID))
  45. ThrowHttpResponse(HttpStatusCode.NotFound, "Network not found!");
  46. var jNetwork = Mapper.Map(network);
  47. var deviceMapper = GetMapper<Device>();
  48. var devices = DataContext.Device.GetByNetwork(id);
  49. jNetwork["devices"] = new JArray(devices.Select(d => deviceMapper.Map(d)));
  50. return jNetwork;
  51. }
  52. /// <name>insert</name>
  53. /// <summary>
  54. /// Creates new device network.
  55. /// </summary>
  56. /// <param name="json" cref="Network">In the request body, supply a <see cref="Network"/> resource.</param>
  57. /// <returns cref="Network">If successful, this method returns a <see cref="Network"/> resource in the response body.</returns>
  58. [HttpCreatedResponse]
  59. [AuthorizeUser(Roles = "Administrator")]
  60. public JObject Post(JObject json)
  61. {
  62. var network = Mapper.Map(json);
  63. Validate(network);
  64. if (DataContext.Network.Get(network.Name) != null)
  65. ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such name already exists!");
  66. if (network.Key != null && DataContext.Network.GetByKey(network.Key) != null)
  67. ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such key already exists!");
  68. DataContext.Network.Save(network);
  69. return Mapper.Map(network);
  70. }
  71. /// <name>update</name>
  72. /// <summary>
  73. /// Updates an existing device network.
  74. /// </summary>
  75. /// <param name="id">Network identifier.</param>
  76. /// <param name="json" cref="Network">In the request body, supply a <see cref="Network"/> resource.</param>
  77. /// <returns cref="Network">If successful, this method returns a <see cref="Network"/> resource in the response body.</returns>
  78. /// <request>
  79. /// <parameter name="name" required="false" />
  80. /// </request>
  81. [AuthorizeUser(Roles = "Administrator")]
  82. public JObject Put(int id, JObject json)
  83. {
  84. var network = DataContext.Network.Get(id);
  85. if (network == null)
  86. ThrowHttpResponse(HttpStatusCode.NotFound, "Network not found!");
  87. Mapper.Apply(network, json);
  88. Validate(network);
  89. var existing = DataContext.Network.Get(network.Name);
  90. if (existing != null && existing.ID != network.ID)
  91. ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such name already exists!");
  92. if (network.Key != null)
  93. {
  94. existing = DataContext.Network.GetByKey(network.Key);
  95. if (existing != null && existing.ID != network.ID)
  96. ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such key already exists!");
  97. }
  98. DataContext.Network.Save(network);
  99. return Mapper.Map(network);
  100. }
  101. /// <name>delete</name>
  102. /// <summary>
  103. /// Deletes an existing device network.
  104. /// </summary>
  105. /// <param name="id">Network identifier.</param>
  106. [HttpNoContentResponse]
  107. [AuthorizeUser(Roles = "Administrator")]
  108. public void Delete(int id)
  109. {
  110. DataContext.Network.Delete(id);
  111. }
  112. private IJsonMapper<Network> Mapper
  113. {
  114. get
  115. {
  116. var mapper = GetMapper<Network>();
  117. if (RequestContext.CurrentUser.Role == (int)UserRole.Client)
  118. return new ClientNetworkMapper(mapper);
  119. return mapper;
  120. }
  121. }
  122. private class ClientNetworkMapper : IJsonMapper<Network>
  123. {
  124. private IJsonMapper<Network> _mapper;
  125. #region Constructor
  126. public ClientNetworkMapper(IJsonMapper<Network> mapper)
  127. {
  128. if (mapper == null)
  129. throw new ArgumentNullException("mapper");
  130. _mapper = mapper;
  131. }
  132. #endregion
  133. #region IJsonMapper<Network> Members
  134. public JObject Map(Network entity)
  135. {
  136. var jObject = _mapper.Map(entity);
  137. jObject.Remove("key"); // do not expose network key to clients
  138. return jObject;
  139. }
  140. public Network Map(JObject json)
  141. {
  142. return _mapper.Map(json);
  143. }
  144. public void Apply(Network entity, JObject json)
  145. {
  146. _mapper.Apply(entity, json);
  147. }
  148. #endregion
  149. }
  150. }
  151. }