public async Task<IHttpActionResult> PplByDay()
        {
            collection = mongoDb.GetCollection<object>("movement");

            var userId = User.Identity.GetUserId();

            var user = await userManager.FindByIdAsync(userId);

            var channel = user.Channels.FirstOrDefault(x => x.ChannelId.Equals(3) && x.UserId.Equals(userId));

            if (channel != null)
            {
                List<MovementModelExtended> data = null;

                if (channel.Price > 0)
                {
                    var result = (await collection.FindAsync<MovementModelExtended>(new BsonDocument(), new FindOptions<object, MovementModelExtended>
                    {
                        //Limit = (int)channel.MaxPayedRows
                    }));

                    data = await result.ToListAsync();
                }

                else
                    data = await (await collection.FindAsync<MovementModelExtended>(new BsonDocument("UserId", userId))).ToListAsync();

                List<object> response = new List<object>();

                for (int i = 1; i <= 24; i++)
                {
                    int count = 0;
                    var obj = new ExpandoObject();

                    foreach (var date in data)
                    {
                        if (i.Equals(date.Created.Hour))
                        {
                            count += 1;
                            if (((IDictionary<string, object>)obj).ContainsKey("date"))
                            {
                                ((IDictionary<string, object>)obj)["date"] = date.Created;
                            }
                            else
                            {
                                obj.AddProp("date", date.Created);
                            }
                        }
                    }
                    var dict = ((IDictionary<string, object>)obj);

                    if (dict.ContainsKey("date"))
                    {
                        obj.AddProp("hour", ((DateTime)dict["date"]).Hour);
                        obj.AddProp("count", count);

                        response.Add(obj);
                    }
                }

                return Ok(new ApiResponse(200, response.ToArray()));
            }
            else
            {
                return this.NotFound(new ApiResponse(404));
            }
        }
        public async Task<IHttpActionResult> Movement(MovementApiModel model)
        {
            if (ModelState.IsValid && model != null)
            {
                collection = mongoDb.GetCollection<object>("movement");

                var userId = (await db.Devices.FirstAsync(x => x.UniqueId.Equals(model.DeviceId))).UserId;

                var mapped = Mapper.Map<MovementModel>(model);
                mapped.Created = DateTime.Now;
                mapped.UserId = userId;

                var bson = BsonSerializer.Deserialize<ExpandoObject>(BsonDocument.Parse(JsonConvert.SerializeObject(mapped)));

                await collection.InsertOneAsync(bson);

                List<object> response = new List<object>();

                var channel = await db.Channels.FirstAsync(x => x.ChannelId.Equals(3) && x.UserId.Equals(userId));

                List<MovementModelExtended> data = null;

                if (channel.Price > 0)
                {
                    var result = (await collection.FindAsync<MovementModelExtended>(new BsonDocument(), new FindOptions<object, MovementModelExtended>
                    {
                        //Limit = (int)channel.MaxPayedRows
                    }));

                    data = await result.ToListAsync();
                }

                else
                    data = await (await collection.FindAsync<MovementModelExtended>(new BsonDocument("UserId", userId))).ToListAsync();

                for (int i = 1; i <= 24; i++)
                {
                    int count = 0;
                    var obj = new ExpandoObject();

                    foreach (var date in data)
                    {
                        if (i.Equals(date.Created.Hour))
                        {
                            count += 1;
                            if (((IDictionary<string, object>)obj).ContainsKey("date"))
                            {
                                ((IDictionary<string, object>)obj)["date"] = date.Created;
                            }
                            else
                            {
                                obj.AddProp("date", date.Created);
                            }
                        }
                    }
                    var dict = ((IDictionary<string, object>)obj);

                    if (dict.ContainsKey("date"))
                    {
                        obj.AddProp("hour", ((DateTime)dict["date"]).Hour);
                        obj.AddProp("count", count);

                        response.Add(obj);
                    }
                }

                NotificationHub.GetHubContext().Clients.All.movement(new ApiResponse(200, response.ToArray()));

                return Ok(new ApiResponse(200, mapped));
            }

            return this.BadRequest(new ApiResponse(400));
        }
        public async Task<IHttpActionResult> FridgeByDate()
        {
            collection = mongoDb.GetCollection<object>("fridge");

            var userId = User.Identity.GetUserId();

            var user = await userManager.FindByIdAsync(userId);

            var channel = await db.Channels.FirstOrDefaultAsync(x => x.ChannelId.Equals(2) && x.UserId.Equals(userId));

            if (channel != null)
            {
                List<FridgeModelExtended> data = null;

                if (channel.Price > 0)
                {
                    var result = (await collection.FindAsync<FridgeModelExtended>(new BsonDocument(), new FindOptions<object, FridgeModelExtended>
                    {
                        //Limit = (int)channel.MaxPayedRows
                    }));

                    data = await result.ToListAsync();
                }

                else
                    data = await (await collection.FindAsync<FridgeModelExtended>(new BsonDocument("UserId", UserId))).ToListAsync();

                List<object> response = new List<object>();

                var distinct = data.Select(x => x.Created.ToShortDateString()).Distinct();

                foreach (var date in distinct)
                {
                    int count = 0;
                    var obj = new ExpandoObject();

                    foreach (var item in data)
                    {
                        if (item.Created.ToShortDateString().Equals(date))
                            count += 1;
                    }

                    obj.AddProp("count", count);
                    obj.AddProp("date", DateTime.Parse(date));

                    response.Add(obj);
                }

                return Ok(new ApiResponse(200, response.ToArray()));
            }
            else
            {
                return this.NotFound(new ApiResponse(404));
            }
        }
        public async Task<IHttpActionResult> Fridge(FridgeApiModel model)
        {
            if (ModelState.IsValid && model != null)
            {
                collection = mongoDb.GetCollection<object>("fridge");

                var userId = (await db.Devices.FirstAsync(x => x.UniqueId.Equals(model.DeviceId))).UserId;

                var mapped = Mapper.Map<FridgeModel>(model);
                mapped.Created = DateTime.Now;
                mapped.UserId = userId;

                var bson = BsonSerializer.Deserialize<ExpandoObject>(BsonDocument.Parse(JsonConvert.SerializeObject(mapped)));

                await collection.InsertOneAsync(bson);

                List<object> response = new List<object>();

                var channel = await db.Channels.FirstAsync(x => x.ChannelId.Equals(2) && x.UserId.Equals(userId));

                List<FridgeModelExtended> data = null;

                if (channel.Price > 0)
                {
                    var result = (await collection.FindAsync<FridgeModelExtended>(new BsonDocument(), new FindOptions<object, FridgeModelExtended>
                    {
                        //Limit = (int)channel.MaxPayedRows
                    }));

                    data = await result.ToListAsync();
                }

                else
                    try
                    {
                        data = await (await collection.FindAsync<FridgeModelExtended>(new BsonDocument("UserId", userId))).ToListAsync();
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                    }

                if (data != null)
                {
                    var distinct = data.Select(x => x.Created.ToShortDateString()).Distinct();

                    foreach (var date in distinct)
                    {
                        int count = 0;
                        var obj = new ExpandoObject();

                        foreach (var item in data)
                        {
                            if (item.Created.ToShortDateString().Equals(date))
                                count += 1;
                        }

                        obj.AddProp("count", count);
                        obj.AddProp("date", DateTime.Parse(date));

                        response.Add(obj);
                    }

                    NotificationHub.GetHubContext().Clients.All.fridge(new ApiResponse(200, response.ToArray()));

                    return Ok(new ApiResponse(200, mapped));
                }
                else
                {
                    return this.NotFound(new ApiResponse(404));
                }
            }

            return this.BadRequest(new ApiResponse(400));
        }