public async Task <ObjectResult> AddUser([FromBody] UserId item1) { UserList dBUserList = JsonConvert.DeserializeObject <UserList>(System.IO.File.ReadAllText(path)); if (dBUserList == null) { UserMoney p2 = new UserMoney { Cur = "EUR" }; List <UserMoney> users2 = new List <UserMoney>(); users2.Add(p2); UserInfo p1 = new UserInfo { Id = item1.Id, Base = "UAH", Currency = users2 }; List <UserInfo> users = new List <UserInfo>(); users.Add(p1); UserList info = new UserList { users = new List <UserInfo>(users) }; await System.IO.File.WriteAllTextAsync(path, JsonConvert.SerializeObject(info, Formatting.Indented)); dBUserList = JsonConvert.DeserializeObject <UserList>(System.IO.File.ReadAllText(path)); return(new ObjectResult(null)); } UserMoney p = new UserMoney { Cur = "USD" }; List <UserMoney> users13 = new List <UserMoney>(); users13.Add(p); UserInfo p12 = new UserInfo { Id = item1.Id, Base = "UAH", Currency = users13 }; for (int i = 0; i < dBUserList.users.Count; i++) { if (dBUserList.users[i].Id == item1.Id) { return(new ObjectResult(null)); } } dBUserList.users.Add(p12); await System.IO.File.WriteAllTextAsync(path, JsonConvert.SerializeObject(dBUserList, Formatting.Indented)); return(new ObjectResult(null)); }
public async Task <ObjectResult> DeleteCur([FromBody] UserMoney item3, int id) { UserList dBUserList = JsonConvert.DeserializeObject <UserList>(System.IO.File.ReadAllText(path)); Regex regex = new Regex(@"[A-Z]{3}"); if ((regex.IsMatch(item3.Cur) && item3.Cur.Length == 3) == false) { Response.StatusCode = 415; return(new ObjectResult(null)); } bool Success = false; for (int i = 0; i < dBUserList.users.Count; i++) { if (dBUserList.users[i].Id == id) { if (dBUserList.users[i].Currency.Count == 1) { Response.StatusCode = 402; return(new ObjectResult(null)); } UserMoney p = new UserMoney { Cur = item3.Cur }; for (int j = 0; j < dBUserList.users[i].Currency.Count; j++) { if (dBUserList.users[i].Currency[j].Cur == item3.Cur) { dBUserList.users[i].Currency.RemoveAt(j); Success = true; } } } } if (Success == false) { Response.StatusCode = 404; return(new ObjectResult(null)); } await System.IO.File.WriteAllTextAsync(path, JsonConvert.SerializeObject(dBUserList, Formatting.Indented)); return(new ObjectResult(null)); }
public async Task <ObjectResult> AddCur([FromBody] UserMoney item3, int id) { Regex regex = new Regex(@"[A-Z]{3}"); if ((regex.IsMatch(item3.Cur) && item3.Cur.Length == 3) == false) { Response.StatusCode = 415; return(new ObjectResult(null)); } var client = new HttpClient(); var content = await client.GetStringAsync("https://currencyapi.net/api/v1/rates?key=8d4b1e2ef6c0235840439e0337e6972ace1c&base=USD"); Money currency = JsonConvert.DeserializeObject <Money>(content); List <string> curr = new List <string>(); foreach (KeyValuePair <string, decimal> keyValue in currency.Rates) { curr.Add(keyValue.Key); } if ((curr.Contains(item3.Cur)) == false) { Response.StatusCode = 415; return(new ObjectResult(null)); } UserList dBUserList = JsonConvert.DeserializeObject <UserList>(System.IO.File.ReadAllText(path)); for (int i = 0; i < dBUserList.users.Count; i++) { if (dBUserList.users[i].Id == id) { if (dBUserList.users[i].Currency.Count == 5) { Response.StatusCode = 402; return(new ObjectResult(null)); } UserMoney p = new UserMoney { Cur = item3.Cur }; for (int j = 0; j < dBUserList.users[i].Currency.Count; j++) { if (dBUserList.users[i].Currency[j].Cur == item3.Cur) { Response.StatusCode = 409; return(new ObjectResult(null)); } } dBUserList.users[i].Currency.Add(p); break; } } await System.IO.File.WriteAllTextAsync(path, JsonConvert.SerializeObject(dBUserList, Formatting.Indented)); return(new ObjectResult(null)); }