Beispiel #1
0
        public async Task <JsonResult> PostAccount([FromBody] SendAccount account, [FromHeader] string Authorization)
        {
            string jwt          = Authorization.Split(' ')[1];
            var    user         = HttpContext.User.Identity.Name;
            var    account_main = await _context.Accounts.Where(x => x.id_user == Convert.ToInt32(user)).FirstOrDefaultAsync();

            _context.Entry(account_main).State = EntityState.Modified;
            account_main = _account.Changeacount(account, account_main);
            var category = await _context.categoryAccounts.Where(x => x.id_account == account_main.id).FirstOrDefaultAsync();

            if (category == null)
            {
                category = new CategoryAccount
                {
                    id_account = account_main.id,
                    level0     = account.level0,
                    level1     = account.level1
                };
                await _context.categoryAccounts.AddAsync(category);
            }
            else
            {
                if (category.level1 != 0)
                {
                    _context.Entry(category).State = EntityState.Modified;
                    category.level0 = account.level0;
                    category.level1 = account.level1;
                }
            }
            var    http = new HttpClient();
            string url  = String.Format
                              ("https://geocode-maps.yandex.ru/1.x/?apikey=a2c8035f-05f9-4489-aea1-ad9b2a841572&geocode={0}&format=json", account.address);

            try
            {
                var result = await http.GetStringAsync(url);

                JObject o     = JObject.Parse(result);
                var     res   = o["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]["Point"];
                var     coor  = res.Value <String>("pos").ToString().Split(' ');
                var     coord = await _context.Coordinates.Where(x => x.account_id == account_main.id).FirstOrDefaultAsync();

                if (coord == null)
                {
                    try
                    {
                        coord = new ServicesModel.Models.Geo.Coordinate
                        {
                            account_id = account_main.id,
                            lat        = double.Parse(coor[1], CultureInfo.InvariantCulture),
                            lon        = double.Parse(coor[2], CultureInfo.InvariantCulture)
                        };
                        await _context.Coordinates.AddAsync(coord);
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                    }
                }
                else
                {
                    _context.Entry(coord).State = EntityState.Modified;
                    coord.lat = Convert.ToInt32(coor[0]);
                    coord.lon = Convert.ToInt32(coor[1]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            await _context.SaveChangesAsync();

            return(new JsonResult(_responce.Return_Responce(System.Net.HttpStatusCode.OK, null, "Данные внесены")));
        }