public void UpdateCurrency(BtcRequest btcRequest) { Validate(btcRequest); string filePath = "currencies.json"; string json = File.ReadAllText(filePath); dynamic jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(json); switch (btcRequest.Currency) { case "BRL": jsonObject["BRL"] = btcRequest.Value; break; case "EUR": jsonObject["EUR"] = btcRequest.Value; break; case "CAD": jsonObject["CAD"] = btcRequest.Value; break; } WriteToFile(filePath, jsonObject); }
private void Validate(BtcRequest btcRequest) { List <string> availableCurrencies = new List <string>() { "BRL", "EUR", "CAD" }; if (!availableCurrencies.Contains(btcRequest.Currency)) { throw new Exception("Moeda inválida"); } if (float.Parse(btcRequest.Value) <= 0) { throw new Exception("Valor inválido"); } }
public IActionResult Post([FromBody] BtcRequest btcRequest) { try { _cryptoService.UpdateCurrency(btcRequest); return(Ok(new SuccessResponse() { Message = "Valor alterado com sucesso!" })); } catch (Exception ex) { return(BadRequest(new ErrorResponse() { Message = ex.Message })); } }