// check if route is legal, from start to finish. static bool CheckRoute(RDJTPRequest req) { if (req.Method == "echo") { return(true); } if (!req.Path.Contains("/")) { return(false); } var path = req.Path.Split("/"); if (path[1] != "api") { return(false); } if (path[2] != "categories") { return(false); } return(true); }
static RDJTPRequest ParseRequest(string content) { var request = new RDJTPRequest(); request = JsonConvert.DeserializeObject <RDJTPRequest>(content); return(request); }
// no need for checks. just return body. static RDJTPResponse HandleEcho(RDJTPRequest req) { var response = new RDJTPResponse() { Status = "1 OK", Body = req.Body }; return(response); }
// check if date null or empty and if its able to parse static string CheckDate(RDJTPRequest req) { var msg = ""; if (string.IsNullOrEmpty(req.Date)) { msg = "illegal date"; } var tmpdate = new double(); if (!double.TryParse(req.Date, out tmpdate)) { msg = "illegal date"; } return(msg); }
static RDJTPResponse HandleUpdate(RDJTPRequest req, List <Category> categories) { var path = req.Path.Split("/"); if (path.Length < 4) { return(HandleException(RDJTPStatus.Bad_Request)); } var newElement = new Category(); // if body is illegal, we will have exception on deserialize try { newElement = JsonConvert.DeserializeObject <Category>(req.Body); } catch (Exception) { return(new RDJTPResponse() { Status = "4 Bad Request Illegal Body" }); } // path[3] is id var elm = categories.Find(x => x.cid == Convert.ToInt32(path[3])); if (elm == null) { return(HandleException(RDJTPStatus.Not_Found)); } categories[categories.IndexOf(elm)] = newElement; return(new RDJTPResponse() { Status = "3 Updated" }); }
static RDJTPResponse HandleCreate(RDJTPRequest req, List <Category> categories) { var newElement = JsonConvert.DeserializeObject <Category>(req.Body); if (string.IsNullOrEmpty(newElement.name)) { return(HandleException(RDJTPStatus.Bad_Request)); } var listlength = categories.Count; newElement.cid = listlength + 1; categories.Add(newElement); var body = JsonConvert.SerializeObject(newElement); return(new RDJTPResponse() { Status = "2 Created", Body = body }); }
// body check for specified methods. static bool CheckRequest(RDJTPRequest req) { if (req.Method == "create" || req.Method == "update" || req.Method == "echo") { if (req.Body == null) { return(false); } } if (req.Method != "echo") { if (req.Path == null) { return(false); } } return(true); }
// body check and exception message building for request static string CheckResource(RDJTPRequest req) { var msg = ""; if (req.Method == "create" || req.Method == "update" || req.Method == "echo") { if (req.Body == null) { msg += "missing body"; } } if (req.Method != "echo") { if (req.Path == null) { msg += "missing path"; } } return(msg); }
static RDJTPResponse HandleRead(RDJTPRequest req, List <Category> categories) { var response = new RDJTPResponse(); var path = req.Path.Split("/"); // if only 3 elements exists in path, we have /api/categories // if over, we have /api/categories/<id> if (path.Length < 4) { response.Status = "1 Ok"; response.Body = JsonConvert.SerializeObject(categories); return(response); } else { response.Status = "1 Ok"; int cid; // path[3] is id - try to parse if (!int.TryParse(path[3], out cid)) { return(HandleException(RDJTPStatus.Bad_Request)); } var element = categories.Find(x => x.cid == cid); if (element == null) { return(HandleException(RDJTPStatus.Not_Found)); } response.Body = JsonConvert.SerializeObject(element); return(response); } }
static RDJTPResponse HandleDelete(RDJTPRequest req, List <Category> categories) { var path = req.Path.Split("/"); if (path.Length < 4) { return(HandleException(RDJTPStatus.Bad_Request)); } var elm = categories.Find(x => x.cid == Convert.ToInt32(path[3])); if (elm == null) { return(HandleException(RDJTPStatus.Not_Found)); } categories.Remove(elm); return(new RDJTPResponse() { Status = "1 OK" }); }