protected override void processGETRequest() { if (CompetitionResultsTemplate.Match(Prefix, Request.Url) != null) { //Parse the template to get the ID of the resource UriTemplateMatch uriTemplate = CompetitionResultsTemplate.Match(Prefix, Request.Url); using (var db = new AppelContext()) { try { int ID = Convert.ToInt32(uriTemplate.BoundVariables["id"]); var x = db.Results.Where(f => f.CompetitionID == ID).ToList(); setResponseVariables(HttpStatusCode.OK, serializeResultsArray(x, uriTemplate)); } catch (System.InvalidOperationException) { setResponseVariables(HttpStatusCode.NotFound, null); } catch (Exception e) { setResponseVariables(HttpStatusCode.NotFound, null); } } } else { setResponseVariables(HttpStatusCode.NotFound, null); } }
protected override void processDELETERequest() { //Check if the request is on the Root Template if (CompetitionRootTemplate.Match(Prefix, Request.Url) != null) { setResponseVariables(HttpStatusCode.MethodNotAllowed, null); } //Check if the request is on the Resource Template else if (CompetitionResourceTemplate.Match(Prefix, Request.Url) != null) { //Parse the template to get the ID of the resource UriTemplateMatch uriTemplate = CompetitionResourceTemplate.Match(Prefix, Request.Url); using (var db = new AppelContext()) { try { int ID = Convert.ToInt32(uriTemplate.BoundVariables["id"]); Competition x = db.Competitions.Where(f => f.CompetitionID == ID).First(); db.Competitions.Remove(x); db.SaveChanges(); setResponseVariables(HttpStatusCode.NoContent, null); } catch (System.InvalidOperationException) { setResponseVariables(HttpStatusCode.NotFound, null); } catch (Exception e) { setResponseVariables(HttpStatusCode.NotFound, null); } } } }
protected override void processPUTRequest() { //Check if the request is on the Root Template if (CompetitionRootTemplate.Match(Prefix, Request.Url) != null) { setResponseVariables(HttpStatusCode.MethodNotAllowed, null); } //Check if the request is on the Resource Template else if (CompetitionResourceTemplate.Match(Prefix, Request.Url) != null) { StreamReader stream = new StreamReader(Request.InputStream); string JSONInput = stream.ReadToEnd(); //Check if the input matches the Input Schema (has reqired fields) if (Competition.validSchema(JSONInput)) { Competition deserializedFencer = JsonConvert.DeserializeObject<Competition>(JSONInput); if (deserializedFencer.hasRequiredFields()) { //Parse the template to get the ID of the resource UriTemplateMatch uriTemplate = CompetitionResourceTemplate.Match(Prefix, Request.Url); using (var db = new AppelContext()) { try { int ID = Convert.ToInt32(uriTemplate.BoundVariables["id"]); Competition x = db.Competitions.Where(f => f.CompetitionID == ID).First(); x.updateCompetition(deserializedFencer); db.SaveChanges(); setResponseVariables(HttpStatusCode.OK, serializeCompetition(x)); } catch (System.InvalidOperationException e) { setResponseVariables(HttpStatusCode.NotFound, null); } catch (Exception e) { setResponseVariables(HttpStatusCode.NotFound, null); } } } else { setResponseVariables("Unprocessable", null); } } else { setResponseVariables("Unprocessable", null); } } }
protected override void processPOSTRequest() { //Check if the request is on the Resource Template if (CompetitionResourceTemplate.Match(Prefix, Request.Url) != null) { setResponseVariables(HttpStatusCode.MethodNotAllowed, null); } //Check if the request is on the Root Template else if (CompetitionRootTemplate.Match(Prefix, Request.Url) != null) { StreamReader stream = new StreamReader(Request.InputStream); string JSONInput = stream.ReadToEnd(); //Check if the input matches the Input Schema (has reqired fields) if (Competition.validSchema(JSONInput)) { Competition deserializedCompetition = JsonConvert.DeserializeObject<Competition>(JSONInput); if (deserializedCompetition.hasRequiredFields()) { using (var db = new AppelContext()) { try { db.Competitions.Add(deserializedCompetition); db.SaveChanges(); setResponseVariables(HttpStatusCode.Created, serializeCompetition(deserializedCompetition)); } catch (Exception e) { setResponseVariables(HttpStatusCode.NotFound, null); } } } else { setResponseVariables("Unprocessable", null); } } else { setResponseVariables("Unprocessable", null); } } }
public void updateResult(Result x, AppelContext db) { Placing = x.Placing; Competition = db.Competitions.Where(f => f.CompetitionID == x.CompetitionID).First(); Fencer = db.Fencers.Where(f => f.FencerID == x.FencerID).First(); }
protected override void processGETRequest() { //Check if the request is on the Root Template if (FencerRootTemplate.Match(Prefix, Request.Url) != null) { setResponseVariables(HttpStatusCode.MethodNotAllowed, null); } //Check if the request is on the Resource Template else if (FencerResourceTemplate.Match(Prefix, Request.Url) != null) { //Parse the template to get the ID of the resource UriTemplateMatch uriTemplate = FencerResourceTemplate.Match(Prefix, Request.Url); using (var db = new AppelContext()) { try { int ID = Convert.ToInt32(uriTemplate.BoundVariables["id"]); Fencer x = db.Fencers.Where(f => f.FencerID == ID).First(); setResponseVariables(HttpStatusCode.OK, serializeFencer(x)); } catch (System.InvalidOperationException) { setResponseVariables(HttpStatusCode.NotFound, null); } catch(Exception e) { setResponseVariables(HttpStatusCode.NotFound, null); } } } }
private bool HasValidAPIKey(HttpRequest Request, HttpResponse Response) { if (Request.Headers != null) { string key = Request.Headers.Get("X-API-Key"); if (key == null) { return false; } else { try { using (var db = new AppelContext()) { APIKey k = db.APIKeys.Where(a => a.Key == key).First(); if (DateTime.Now > k.resetTime) { k.resetTime = DateTime.Now.AddMinutes(60); k.numberOfQueriesThisHour = 0; } if (!k.isBlocked && k.numberOfQueriesThisHour < k.maxNumberOfQueries) { k.numberOfQueriesEver += 1; k.numberOfQueriesThisHour += 1; Response.Headers.Add("X-RateLimit-Limit", k.maxNumberOfQueries.ToString()); Response.Headers.Add("X-RateLimit-Remaining", (k.numberOfQueriesThisHour - k.maxNumberOfQueries).ToString()); return true; } else if (k.isBlocked) { k.numberOfQueriesEver += 1; k.numberOfQueriesThisHour += 1; Response.Headers.Add("X-RateLimit-Limit", k.maxNumberOfQueries.ToString()); Response.Headers.Add("X-RateLimit-Remaining", (k.numberOfQueriesThisHour - k.maxNumberOfQueries).ToString()); return false; } else { k.numberOfQueriesEver += 1; k.numberOfQueriesThisHour += 1; rateLimitExceeded = true; Response.Headers.Add("X-RateLimit-Limit", k.maxNumberOfQueries.ToString()); Response.Headers.Add("X-RateLimit-Remaining", (k.numberOfQueriesThisHour - k.maxNumberOfQueries).ToString()); return false; } } } catch (Exception e) { return false; } } } return false; }