public HttpResponseMessage Post([FromBody] string value) { if (value == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The key value pair is malformed. (no key and/or value supplied)")); } if (!value.Contains(@"||")) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The key value pair is not in the expected format; 'key || value'")); } var keyValuePair = value.Split(new string[] { @"||" }, StringSplitOptions.RemoveEmptyEntries); if (keyValuePair.Length <= 1) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The key value pair is malformed. (no key and/or value supplied)")); } using (var db = new ContosoDBEntities()) { db.DatabaseKeyValues.Add(new DatabaseKeyValue { Key = keyValuePair[0].Trim(), Value = keyValuePair[1].Trim() }); db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.Created)); }
public HttpResponseMessage Put(string key, [FromBody] string newValue) { bool found = false; using (var db = new ContosoDBEntities()) { var keyValuePairs = db.DatabaseKeyValues; foreach (var keyValue in keyValuePairs) { if (keyValue.Key.Equals(key)) { keyValue.Value = newValue; found = true; break; } } db.SaveChanges(); } if (!found) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "The requested key does not exist.")); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public HttpResponseMessage Delete(string key) { bool found = false; using (var db = new ContosoDBEntities()) { var keyValuePairs = db.DatabaseKeyValues; foreach (var keyValue in keyValuePairs) { if (keyValue.Key.Equals(key)) { db.DatabaseKeyValues.Remove(keyValue); found = true; break; } } if (found) { db.SaveChanges(); } } if (!found) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "The key you are trying to delete does not exist.")); } return(Request.CreateResponse(HttpStatusCode.OK)); }