Пример #1
0
        public static Database.node toRaw(Node data)
        {
            var raw = new Database.node()
            {
                physical_address = data.PhysicalAddress,
                friendly_name = data.FriendlyName,
                water_on = data.IsWaterOn,
                light_on = data.IsLightOn,
                behavior_id = data.BehaviorId,
                user_id = data.UserId,
            };

            if (data.Id != null)
            {
                raw.id = (int)data.Id;
            }

            if (data.Sensors != null)
            {
                foreach (var sensor in data.Sensors)
                {
                    raw.sensors.Add(Sensor.toRaw(sensor));
                }
            }

            return raw;
        }
Пример #2
0
        public static async Task<Node> Insert(Node data)
        {
            var ctx = ModelContext.GetInstance();
                        
            ctx.nodes.Add(toRaw(data));

            await ctx.SaveChangesAsync();

            return (Node)ctx.nodes.OrderByDescending(x => x.id).FirstOrDefault();
        }
Пример #3
0
        public static async Task<Node> Update(Node data)
        {
            if (data.Sensors != null)
            {
                await Sensor.Update(data.Sensors);
            }

            var ctx = ModelContext.GetNewInstance();

            var raw = toRaw(data);
            ctx.nodes.Attach(raw);
            System.Data.Entity.Infrastructure.DbEntityEntry<Database.node> entry = ctx.Entry(raw);
            entry.State = System.Data.Entity.EntityState.Modified;
            
            await ctx.SaveChangesAsync();
                        
            return Get((int)data.Id, data.UserId);
        }
        public async Task<HttpResponseMessage> Handshake(Node node)
        {
            try
            {
                if (node == null) throw new ArgumentException("node");

                if (node.Id == null)
                {
                    node = await Model.Node.Insert(node);
                }
                else
                {
                    node = await Model.Node.Update(node);
                }

                node.Sensors = Node.ListSensors((int)node.Id);

                return Request.CreateResponse(HttpStatusCode.OK, node);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
            }
        }