Example #1
0
        public void InitMetricRecord()
        {
            var metricRecord = new MetricRecord();

            metricRecord.Kind.Is("");
            metricRecord.Name.Is("");
            metricRecord.File.Is("");
            metricRecord.Value.Is(0);
        }
Example #2
0
        private bool NewMetricRecord(MetricRecord newRec)
        {
            // API call
            HttpWebRequest request = WebRequest.Create(CreateRecordEndpoint) as HttpWebRequest;

            request.ContentType = "text/json";
            request.Method      = "POST";
            //request.Headers.Add("Subscription-Key", VumacamSubscriptionKey);
            //request.Headers.Add("Authorization", "Bearer " + BearerToken);


            string JsonQuery = "{" +
                               "\"userId\":\"" + newRec.UserId + "\"," +
                               "\"timestamp\":\"" + newRec.Timestamp + "\"," +
                               "\"metricTypeId\":\"" + newRec.MetricType + "\"," +
                               "\"measurement\":\"" + newRec.Quantity + "\"," +
                               "}";

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(JsonQuery);
                writer.Flush();
                writer.Close();
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            if (response.StatusCode == HttpStatusCode.OK)
            {
                string result = "";
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }

                //result = result.Substring(1);
                //result = result.Substring(0, result.Length - 1);
                //result = result.Replace("\\\"", "\"");

                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #3
0
        public IHttpActionResult CreateRecord([FromBody] JObject jsonResult)
        {
            // Get all data from jsonResult
            int    userId       = Convert.ToInt32(jsonResult["userId"].ToString());
            double measurement  = Convert.ToDouble(jsonResult["measurement"].ToString());
            int    metricTypeId = Convert.ToInt32(jsonResult["metricTypeId"].ToString());

            // If jsonResult does not contain a timestamp (not backdated), give it one.
            DateTime?timestamp = null;

            if (jsonResult.ContainsKey("timestamp"))
            {
                timestamp = Convert.ToDateTime(jsonResult["timestamp"].ToString());
            }
            else
            {
                timestamp = DateTime.Now;
            }

            CoachItEntities _db = new CoachItEntities();

            // Get the userId to which the entry has to be linked, else return an appropriate error.
            webpages_Users webUser = _db.webpages_Users.FirstOrDefault(x => x.UserId == userId);

            if (webUser == null)
            {
                _db.Dispose();

                return(BadRequest($"User '{userId}' not found"));
            }

            MetricRecord newRecord = new MetricRecord()
            {
                UserId       = webUser.UserId,
                Measurement  = measurement,
                MetricTypeId = metricTypeId,
                Timestamp    = Convert.ToDateTime(timestamp)
            };

            _db.MetricRecords.Add(newRecord);

            _db.SaveChanges();

            return(Ok("Record added succesfully"));
        }
Example #4
0
        public IHttpActionResult UpdateRecord([FromBody] JObject jsonResult)
        {
            // Get all data from jsonResult
            int recordId = Convert.ToInt32(jsonResult["recordId"].ToString());

            CoachItEntities _db = new CoachItEntities();

            // Get the entry that has to be updated.
            MetricRecord metricRecord = _db.MetricRecords.FirstOrDefault(x => x.Id == recordId);

            if (metricRecord == null)
            {
                _db.Dispose();

                return(BadRequest($"Record '{recordId}' not found"));
            }

            if (jsonResult.ContainsKey("measurement"))
            {
                double measurement = Convert.ToDouble(jsonResult["measurement"].ToString());
                metricRecord.Measurement = measurement;
            }

            if (jsonResult.ContainsKey("metricTypeId"))
            {
                int metricTypeId = Convert.ToInt32(jsonResult["metricTypeId"].ToString());
                metricRecord.MetricTypeId = metricTypeId;
            }

            if (jsonResult.ContainsKey("timestamp"))
            {
                DateTime timestamp = Convert.ToDateTime(jsonResult["timestamp"].ToString());
                metricRecord.Timestamp = timestamp;
            }

            _db.SaveChanges();

            return(Ok("Record added succesfully"));
        }
Example #5
0
        public IHttpActionResult AddNewMetricRecord([FromBody] JObject jsonResult)
        {
            MetricRecord newRec = jsonResult.ToObject <MetricRecord>();

            // Check if same class and type not already logged for the provided timestamp
            if (CheckSameTypeForDate(newRec.UserId, newRec.Timestamp, newRec.MetricType, newRec.Quantity))
            {
                return(Ok("Metric and date already inserted."));
            }

            // check values to be in reasonable range.
            if (newRec.Quantity > 500)
            {
                return(Ok("Quantity not within acceptable range."));
            }

            if (NewMetricRecord(newRec))
            {
                return(Ok("Record added succesfully"));
            }

            return(BadRequest("Problem adding new metric."));
        }
Example #6
0
        public IHttpActionResult DeleteRecord([FromBody] JObject jsonResult)
        {
            // Get all data from jsonResult
            int recordId = Convert.ToInt32(jsonResult["recordId"].ToString());

            CoachItEntities _db = new CoachItEntities();

            // Get the entry that has to be deleted.
            MetricRecord metricRecord = _db.MetricRecords.FirstOrDefault(x => x.Id == recordId);

            if (metricRecord == null)
            {
                _db.Dispose();

                return(BadRequest($"Record '{recordId}' not found"));
            }

            _db.MetricRecords.Remove(metricRecord);

            _db.SaveChanges();
            _db.Dispose();

            return(Ok("Record deleted succesfully"));
        }