devicehive-.net /src/Server/DeviceHive.API/Controllers/NetworkController.cs

Language C# Lines 174
MD5 Hash 3bcbeb793f9c6eedfa53458541bfb2b8
Repository https://github.com/oryol/devicehive-.net.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using DeviceHive.API.Filters;
using DeviceHive.API.Mapping;
using DeviceHive.Data.Model;
using DeviceHive.Data.Repositories;
using Newtonsoft.Json.Linq;

namespace DeviceHive.API.Controllers
{
    /// <resource cref="Network" />
    public class NetworkController : BaseController
    {
        /// <name>list</name>
        /// <summary>
        /// Gets list of device networks.
        /// <para>If caller belongs to the Client user role, the result list is limited to networks the user has access to.</para>
        /// </summary>
        /// <returns cref="Network">If successful, this method returns array of <see cref="Network"/> resources in the response body.</returns>
        [AuthorizeUser]
        public JArray Get()
        {
            var networks = RequestContext.CurrentUser.Role == (int)UserRole.Client ?
                DataContext.Network.GetByUser(RequestContext.CurrentUser.ID) :
                DataContext.Network.GetAll();

            return new JArray(networks.Select(n => Mapper.Map(n)));
        }

        /// <name>get</name>
        /// <summary>
        /// Gets information about device network and its devices.
        /// </summary>
        /// <param name="id">Network identifier.</param>
        /// <returns cref="Network">If successful, this method returns a <see cref="Network"/> resource in the response body.</returns>
        /// <response>
        ///     <parameter name="devices" type="array" cref="Device">Array of devices registered in the current network.</parameter>
        ///     <parameter name="devices[].network" mode="remove" />
        /// </response>
        [AuthorizeUser]
        public JObject Get(int id)
        {
            var network = DataContext.Network.Get(id);
            if (network == null || !IsNetworkAccessible(network.ID))
                ThrowHttpResponse(HttpStatusCode.NotFound, "Network not found!");

            var jNetwork = Mapper.Map(network);

            var deviceMapper = GetMapper<Device>();
            var devices = DataContext.Device.GetByNetwork(id);
            jNetwork["devices"] = new JArray(devices.Select(d => deviceMapper.Map(d)));
            return jNetwork;
        }

        /// <name>insert</name>
        /// <summary>
        /// Creates new device network.
        /// </summary>
        /// <param name="json" cref="Network">In the request body, supply a <see cref="Network"/> resource.</param>
        /// <returns cref="Network">If successful, this method returns a <see cref="Network"/> resource in the response body.</returns>
        [HttpCreatedResponse]
        [AuthorizeUser(Roles = "Administrator")]
        public JObject Post(JObject json)
        {
            var network = Mapper.Map(json);
            Validate(network);
            
            if (DataContext.Network.Get(network.Name) != null)
                ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such name already exists!");
            if (network.Key != null && DataContext.Network.GetByKey(network.Key) != null)
                ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such key already exists!");
            
            DataContext.Network.Save(network);
            return Mapper.Map(network);
        }

        /// <name>update</name>
        /// <summary>
        /// Updates an existing device network.
        /// </summary>
        /// <param name="id">Network identifier.</param>
        /// <param name="json" cref="Network">In the request body, supply a <see cref="Network"/> resource.</param>
        /// <returns cref="Network">If successful, this method returns a <see cref="Network"/> resource in the response body.</returns>
        /// <request>
        ///     <parameter name="name" required="false" />
        /// </request>
        [AuthorizeUser(Roles = "Administrator")]
        public JObject Put(int id, JObject json)
        {
            var network = DataContext.Network.Get(id);
            if (network == null)
                ThrowHttpResponse(HttpStatusCode.NotFound, "Network not found!");

            Mapper.Apply(network, json);
            Validate(network);

            var existing = DataContext.Network.Get(network.Name);
            if (existing != null && existing.ID != network.ID)
                ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such name already exists!");

            if (network.Key != null)
            {
                existing = DataContext.Network.GetByKey(network.Key);
                if (existing != null && existing.ID != network.ID)
                    ThrowHttpResponse(HttpStatusCode.Forbidden, "Network with such key already exists!");
            }

            DataContext.Network.Save(network);
            return Mapper.Map(network);
        }

        /// <name>delete</name>
        /// <summary>
        /// Deletes an existing device network.
        /// </summary>
        /// <param name="id">Network identifier.</param>
        [HttpNoContentResponse]
        [AuthorizeUser(Roles = "Administrator")]
        public void Delete(int id)
        {
            DataContext.Network.Delete(id);
        }

        private IJsonMapper<Network> Mapper
        {
            get
            {
                var mapper = GetMapper<Network>();
                if (RequestContext.CurrentUser.Role == (int)UserRole.Client)
                    return new ClientNetworkMapper(mapper);
                return mapper;
            }
        }

        private class ClientNetworkMapper : IJsonMapper<Network>
        {
            private IJsonMapper<Network> _mapper;

            #region Constructor

            public ClientNetworkMapper(IJsonMapper<Network> mapper)
            {
                if (mapper == null)
                    throw new ArgumentNullException("mapper");

                _mapper = mapper;
            }
            #endregion

            #region IJsonMapper<Network> Members

            public JObject Map(Network entity)
            {
                var jObject = _mapper.Map(entity);
                jObject.Remove("key"); // do not expose network key to clients
                return jObject;
            }

            public Network Map(JObject json)
            {
                return _mapper.Map(json);
            }

            public void Apply(Network entity, JObject json)
            {
                _mapper.Apply(entity, json);
            }
            #endregion
        }
    }
}
Back to Top