PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/oryol/devicehive-.net
C# | 94 lines | 58 code | 11 blank | 25 comment | 14 complexity | ecf8b9be935853f685f734444b9137ae 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.Data.Model;
  11. using Newtonsoft.Json.Linq;
  12. namespace DeviceHive.API.Controllers
  13. {
  14. /// <resource cref="User" />
  15. [AuthorizeUser(Roles = "Administrator")]
  16. public class UserNetworkController : BaseController
  17. {
  18. /// <name>getNetwork</name>
  19. /// <summary>
  20. /// Gets information about user/network association.
  21. /// </summary>
  22. /// <param name="id">User identifier.</param>
  23. /// <param name="networkId">Network identifier.</param>
  24. /// <returns cref="UserNetwork">If successful, this method returns the following structure in the response body.</returns>
  25. public JObject Get(int id, int networkId)
  26. {
  27. var user = DataContext.User.Get(id);
  28. if (user == null)
  29. ThrowHttpResponse(HttpStatusCode.NotFound, "User not found!");
  30. var network = DataContext.Network.Get(networkId);
  31. if (network == null)
  32. ThrowHttpResponse(HttpStatusCode.NotFound, "Network not found!");
  33. var userNetwork = DataContext.UserNetwork.Get(id, networkId);
  34. if (userNetwork == null)
  35. ThrowHttpResponse(HttpStatusCode.NotFound, "User/network association not found!");
  36. return Mapper.Map(userNetwork);
  37. }
  38. /// <name>assignNetwork</name>
  39. /// <summary>
  40. /// Associates network with the user.
  41. /// </summary>
  42. /// <param name="id">User identifier.</param>
  43. /// <param name="networkId">Network identifier.</param>
  44. /// <param name="json" cref="UserNetwork">In the request body, supply the empty object.</param>
  45. /// <returns cref="UserNetwork">If successful, this method returns the following structure in the response body.</returns>
  46. /// <request>
  47. /// <parameter name="network" mode="remove" />
  48. /// </request>
  49. public JObject Put(int id, int networkId, JObject json)
  50. {
  51. var user = DataContext.User.Get(id);
  52. if (user == null)
  53. ThrowHttpResponse(HttpStatusCode.NotFound, "User not found!");
  54. var network = DataContext.Network.Get(networkId);
  55. if (network == null)
  56. ThrowHttpResponse(HttpStatusCode.NotFound, "Network not found!");
  57. var userNetwork = DataContext.UserNetwork.Get(id, networkId);
  58. if (userNetwork == null)
  59. userNetwork = new UserNetwork(user, network);
  60. Mapper.Apply(userNetwork, json);
  61. Validate(userNetwork);
  62. DataContext.UserNetwork.Save(userNetwork);
  63. return Mapper.Map(userNetwork);
  64. }
  65. /// <name>unassignNetwork</name>
  66. /// <summary>
  67. /// Removes association between network and user.
  68. /// </summary>
  69. /// <param name="id">User identifier.</param>
  70. /// <param name="networkId">Network identifier.</param>
  71. [HttpNoContentResponse]
  72. public void Delete(int id, int networkId)
  73. {
  74. var userNetwork = DataContext.UserNetwork.Get(id, networkId);
  75. if (userNetwork != null)
  76. DataContext.UserNetwork.Delete(userNetwork.ID);
  77. }
  78. private IJsonMapper<UserNetwork> Mapper
  79. {
  80. get { return GetMapper<UserNetwork>(); }
  81. }
  82. }
  83. }