Ejemplo n.º 1
0
        public HttpResponseMessage Post([FromBody] DatedValueV2 value) //Insert Value
        {
            try
            {
                //Simple types are harder to post than complex types!
                //http://weblog.west-wind.com/posts/2012/Mar/21/ASPNET-Web-API-and-Simple-Value-Parameters-from-POSTed-data
                if (value == null)
                {
                    throw new InvalidOperationException("Expected value in body, got " + Request.Content.ReadAsStringAsync().Result);
                }

                int id = (from int key in values.Keys
                          select key).Max() + 1;
                DatedValueV2 justCreated = new DatedValueV2
                {
                    Id           = values.Count,
                    Value        = value.Value,
                    FamilyValues = value.FamilyValues,
                    LastModified = DateTime.Now
                };
                values.Add(values.Count, justCreated);

                //Return address to resource just created.

                return(DefaultPostResponse(id, justCreated));
            }
            catch (Exception ex)
            {
                Tracing.Err.TraceEvent(TraceEventType.Error, 0, ex.ToString());
                throw;
            }
        }
Ejemplo n.º 2
0
        public HttpResponseMessage Put([FromBody] DatedValueV2 value)
        {
            try
            {
                //Update existing
                value.LastModified = DateTime.Now;
                value.FamilyValues = value.FamilyValues;
                values[value.Id]   = value;

                //Tricky... this will *only* be called by jquery xhr!
                return(DefaultPutResponse(value.Id, value));
            }
            catch (Exception ex)
            {
                Tracing.Err.TraceEvent(TraceEventType.Error, 0, ex.ToString());
                throw;
            }
        }
Ejemplo n.º 3
0
        public HttpResponseMessage Get(int id)
        {
            try
            {
                if (!values.ContainsKey(id))
                {
                    //Add extra message to distinguish from mere routing problems.
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "That id is not in the dictionary."));
                }
                DatedValueV2 value = values[id];

                //Cache stale or no IfModifiedSince date
                if (Request.Headers.IfModifiedSince == null || value.LastModified > Request.Headers.IfModifiedSince)
                {
                    ValueViewModel result = (from KeyValuePair <int, DatedValueV2> s in values
                                             where s.Key == id
                                             select ViewModelFactory(s)).First();
                    HttpResponseMessage m = Request.CreateResponse(HttpStatusCode.OK, result);
                    //DateTime d = DateTime.UtcNow;
                    //string dateInRFormat = value.LastModified.ToUniversalTime().ToString("r");
                    //
                    //m.Headers.ETag = new EntityTagHeaderValue(dateInRFormat);
                    m.Headers.CacheControl = new CacheControlHeaderValue()
                    {
                        NoCache = false,
                        Public  = false,
                        Private = true,
                        MaxAge  = new TimeSpan(24, 0, 0)
                    };
                    return(m);
                }


                return(new HttpResponseMessage(HttpStatusCode.NotModified));
            }
            catch (Exception ex)
            {
                Tracing.Err.TraceEvent(TraceEventType.Error, 0, ex.ToString());
                throw;
            }
        }