public IHttpActionResult PutOpenAPSTempBasal(int id, OpenAPSTempBasal openAPSTempBasal)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != openAPSTempBasal.Id)
            {
                return(BadRequest());
            }

            db.Entry(openAPSTempBasal).State = System.Data.Entity.EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OpenAPSTempBasalExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> PostOpenAPSTempBasal(OpenAPSTempBasal openAPSTempBasal)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.OpenAPSTempBasals.Add(openAPSTempBasal);
            db.SaveChanges();
            return(await updateMongo(openAPSTempBasal));
        }
        public IHttpActionResult GetOpenAPSTempBasal(int id)
        {
            OpenAPSTempBasal openAPSTempBasal = db.OpenAPSTempBasals.Find(id);

            if (openAPSTempBasal == null)
            {
                return(NotFound());
            }

            return(Ok(openAPSTempBasal));
        }
        public IHttpActionResult DeleteOpenAPSTempBasal(int id)
        {
            OpenAPSTempBasal openAPSTempBasal = db.OpenAPSTempBasals.Find(id);

            if (openAPSTempBasal == null)
            {
                return(NotFound());
            }

            db.OpenAPSTempBasals.Remove(openAPSTempBasal);
            db.SaveChanges();

            return(Ok(openAPSTempBasal));
        }
        private async Task <IHttpActionResult> updateMongo(OpenAPSTempBasal openAPSTempBasal)
        {
            _client   = new MongoClient(new MongoUrl("[Your Mongo URL]"));
            _database = _client.GetDatabase("[Your Collection]");
            var document = new BsonDocument();

            document.Add("enteredBy", "OpenAPS");
            document.Add("eventType", "Temp Basal");
            document.Add("glucose", openAPSTempBasal.bg.ToString());
            document.Add("glucoseType", "Sensor");
            document.Add("carbs", "0");
            document.Add("insulin", openAPSTempBasal.rate.ToString());
            document.Add("duration", openAPSTempBasal.duration.ToString());
            document.Add("units", "mg/dl");
            document.Add("created_at", openAPSTempBasal.timestamp.Value.GetDateTimeFormats()[101].ToString());
            var collection = _database.GetCollection <BsonDocument>("treatments");
            await collection.InsertOneAsync(document);

            return(CreatedAtRoute("DefaultApi", new { id = openAPSTempBasal.Id }, openAPSTempBasal));
        }