Пример #1
0
        public async Task <ObjectResult> AddNewBase([FromBody] UserInfo item3, int id)
        {
            UserList dBUserList = JsonConvert.DeserializeObject <UserList>(System.IO.File.ReadAllText(path));
            Regex    regex      = new Regex(@"[A-Z]{3}");

            if ((regex.IsMatch(item3.Base) && item3.Base.Length == 3) == false)
            {
                Response.StatusCode = 415;

                return(new ObjectResult(null));
            }
            for (int i = 0; i < dBUserList.users.Count; i++)
            {
                if (dBUserList.users[i].Id == id)
                {
                    dBUserList.users[i].Base = item3.Base;
                    break;
                }
            }
            await System.IO.File.WriteAllTextAsync(path, JsonConvert.SerializeObject(dBUserList, Formatting.Indented));

            return(new ObjectResult(null));
        }
Пример #2
0
        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));
        }