public IActionResult UpdateByID(long accID, string apiKey, [FromBody] JsonElement reqBody) { if (!api.validAPIKey(apiKey)) { return(new UnauthorizedObjectResult("Invalid API key")); } if (accID < 1) { return(BadRequest("Invalid Account ID")); } // Validate the attributes of the PATCH request, each attribute specified, given account id // in the request must be an attribute of a Account Dictionary <string, object> req = JsonConvert.DeserializeObject <Dictionary <string, object> >(Convert.ToString(reqBody)); HashSet <string> reqAttributes = new HashSet <string>(req.Keys); if (!api.validAttributes(attributes, reqAttributes)) { return(BadRequest("Invalid attribute(s) in request body")); } UserAccount acc = uas.getUsingID(accID); // Http POST cannot update a Account that does not exist if (acc == null) { return(NotFound($"No account exists with AccountID {accID}")); } // Otherwise fufill the POST request and update the corresponding Account // Http POST may only specify a few attributes to update or provide all of them // Update the Account with the specified POST attributes try { if (reqAttributes.Contains("AccountUsername")) { acc.AccountUsername = Convert.ToString(req["AccountUsername"]); } if (reqAttributes.Contains("EmailAddress")) { acc.EmailAddress = Convert.ToString(req["EmailAddress"]); } if (reqAttributes.Contains("Password")) { acc.PasswordHash = Convert.ToString(req["Password"]); } } // Formatting or improper data typing raised exception, bad request catch (Exception e) { Console.WriteLine(e.ToString()); return(BadRequest()); } // Write changes, if any uas.update(acc); return(api.serveJson(uas.getJSON(acc))); }
public IActionResult UpdateByID(long RID, string apiKey, [FromBody] JsonElement reqBody) { if (!api.validAPIKey(apiKey)) { return(new UnauthorizedObjectResult("Invalid API key")); } if (RID < 1) { return(BadRequest("Invalid Receipt ID")); } // Validate the attributes of the PATCH request, each attribute specified // in the request must be an attribute of a Receipt Dictionary <string, object> req = JsonConvert.DeserializeObject <Dictionary <string, object> >(Convert.ToString(reqBody)); HashSet <string> reqAttributes = new HashSet <string>(req.Keys); if (!api.validAttributes(attributes, reqAttributes)) { return(BadRequest()); } Receipt r = rs.getUsingID(RID); // Http POST cannot update a Receipt that does not exist if (r == null) { return(NotFound("Cannot update a Receipt that does not exist!")); } // Otherwise fufill the PATCH request and update the corresponding Receipt // Http PATCH may only specify a few attributes to update or provide all of them // TID is not an updatable attribute // Update the Receipt with the specified POST attributes try { if (reqAttributes.Contains("ImgURL")) { r.setImageLink(Convert.ToString(req["ImgURL"])); } if (reqAttributes.Contains("PurchaseDate")) { r.setPurchaseDate(Convert.ToDateTime(req["PurchaseDate"])); } if (reqAttributes.Contains("Notes")) { r.setNote(Convert.ToString(req["Notes"])); } } // Formatting or improper data typing raised exception, bad request catch (Exception e) { Console.WriteLine(e.ToString()); return(BadRequest()); } // Write changes, if any rs.update(r); return(api.serveJson(rs.getJSON(r))); }
public IActionResult UpdateByID(long TID, string apiKey, [FromBody] JsonElement reqBody) { if (!api.validAPIKey(apiKey)) { return(new UnauthorizedObjectResult("Invalid API key")); } if (TID < 1) { return(BadRequest("Invalid TID")); } // Validate the attributes of the PATCH request, each attribute specified // in the request must be an attribute of a Transaction Dictionary <string, object> req = JsonConvert.DeserializeObject <Dictionary <string, object> >(Convert.ToString(reqBody)); HashSet <string> reqAttributes = new HashSet <string>(req.Keys); if (!api.validAttributes(updateAttr, reqAttributes)) { return(BadRequest("Invalid attribute(s) in request body")); } Transaction t = ts.getUsingID(TID); // Http POST cannot update a transaction that does not exist if (t == null) { return(NotFound("Cannot update a Transaction that does not exist!")); } // Otherwise fufill the POST request and update the corresponding transaction // Http POST may only specify a few attributes to update or provide all of them // AccountID is not an updatable attribute // Update the transaction with the specified POST attributes try { if (reqAttributes.Contains("SGID")) { t.setSGID(Convert.ToInt64(req["SGID"])); } if (reqAttributes.Contains("TransactionName")) { t.setTransactionName(Convert.ToString(req["TransactionName"])); } if (reqAttributes.Contains("Amount")) { t.setAmount(Convert.ToDecimal(req["Amount"])); } if (reqAttributes.Contains("DateTransactionMade")) { t.setDateTransactionMade(Convert.ToDateTime(req["DateTransactionMade"])); } if (reqAttributes.Contains("DateTransactionEntered")) { t.setDateTransactionEntered(Convert.ToDateTime(req["DateTransactionEntered"])); } if (reqAttributes.Contains("IsExpense")) { t.setIsExpense(Convert.ToBoolean(req["IsExpense"])); } if (reqAttributes.Contains("TransactionCategory")) { t.setTransactionCategory(ts.castCategory(Convert.ToString(req["TransactionCategory"]))); } } // Formatting or improper data typing raised exception, bad request catch (Exception e) { Console.WriteLine(e.ToString()); return(BadRequest()); } // Write changes, if any ts.update(t); return(api.serveJson(ts.getJSON(t))); }
public IActionResult UpdateByID(long SGID, string apiKey, [FromBody] JsonElement reqBody) { if (!api.validAPIKey(apiKey)) { return(new UnauthorizedObjectResult("Invalid API key")); } if (SGID < 1) { return(BadRequest("Invalid SGID specified")); } // Validate the attributes of the PATCH request, each attribute specified // in the request must be an attribute of a SavingsGoal Dictionary <string, object> req = JsonConvert.DeserializeObject <Dictionary <string, object> >(Convert.ToString(reqBody)); HashSet <string> reqAttributes = new HashSet <string>(req.Keys); if (!api.validAttributes(updateableAttrs, reqAttributes)) { return(BadRequest("Invalid attribute(s) in request body")); } SavingsGoal s = sgs.getUsingID(SGID); // Http PATCH cannot update a SavingsGoal that does not exist if (s == null) { return(NotFound("Cannot update a SavingsGoal that does not exist!")); } // PATCH request body should pass no more than a single attribute unless: // Updating GoalAmt, in which a boolean ExtendDate should be passed // Updating ContrAmt AND Period simultaneously try { if (reqAttributes.Contains("GoalAmt")) { // Updating Goal Amount must also specify ExtendDate if (!reqAttributes.SetEquals(updateGoalAmt)) { return(BadRequest("Invalid attribute(s) in request body, expected exactly { GoalAmt, ExtendDate }")); } s.updateGoalAmt(Convert.ToDecimal(req["GoalAmt"]), Convert.ToBoolean(req["ExtendDate"])); } else if (reqAttributes.Contains("ContrAmt")) { // Update Contribution Amount and Period simultaneously if (reqAttributes.Count > 1) { if (!reqAttributes.SetEquals(updateContrAmt)) { return(BadRequest("Invalid attribute(s) in request body, expected exactly { ContrAmt, Period }")); } s.updateContrAmtAndPeriod(Convert.ToDecimal(req["ContrAmt"]), sgs.castPeriod(Convert.ToString(req["Period"]))); } // Update just Contribution Amount else { s.updateContrAmt(Convert.ToDecimal(req["ContrAmt"])); } } // All other attributes should specify only a single value else { if (reqAttributes.Count != 1) { return(BadRequest("Invalid attribute(s) in request body, expected only one of the following: { Name, Period, EndDate }")); } if (reqAttributes.Contains("Name")) { s.updateName(Convert.ToString(req["Name"])); } else if (reqAttributes.Contains("Period")) { s.updatePeriod(sgs.castPeriod(Convert.ToString(req["Period"]))); } else if (reqAttributes.Contains("EndDate")) { s.updateEndDate(Convert.ToDateTime(req["EndDate"])); } } } catch (Exception e) { Console.WriteLine(e.ToString()); return(BadRequest()); } // Write changes, if any sgs.update(s); return(api.serveJson(sgs.getJSON(s))); }
public IActionResult UpdateByID(long SID, string apiKey, [FromBody] JsonElement reqBody) { if (!api.validAPIKey(apiKey)) { return(new UnauthorizedObjectResult("Invalid API key")); } if (SID < 1) { return(BadRequest()); } // Validate the attributes of the PATCH request, each attribute specified // in the request must be an attribute of a SavingsGoal Dictionary <string, object> req = JsonConvert.DeserializeObject <Dictionary <string, object> >(Convert.ToString(reqBody)); HashSet <string> reqAttributes = new HashSet <string>(req.Keys); if (!api.validAttributes(updateAttr, reqAttributes)) { return(BadRequest()); } Subscription s = subservice.getUsingID(SID); // Http PATCH cannot update a Subscription that does not exist if (s == null) { return(NotFound()); } bool attrUpdated = false; try { if (reqAttributes.Contains("PurchaseDate")) { s.setPurchaseDate(Convert.ToDateTime(req["PurchaseDate"])); attrUpdated = true; } else if (reqAttributes.Contains("Amount")) { s.setAmount(Convert.ToDecimal(req["Amount"])); attrUpdated = true; } else if (reqAttributes.Contains("RenewFrequency")) { s.setRenewFrequency(subservice.castSubscriptionFrequency(Convert.ToString(req["RenewFrequency"]))); attrUpdated = true; } else if (reqAttributes.Contains("Notes")) { s.setNotes((Convert.ToString(req["Notes"]))); attrUpdated = true; } } catch (Exception e) { Console.WriteLine(e.ToString()); return(BadRequest()); } if (!attrUpdated) { return(BadRequest()); } // Write changes, if any subservice.update(s); return(api.serveJson(subservice.getJSON(s))); }