Beispiel #1
0
        public async Task <IActionResult> PutUser([FromRoute] long id, [FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.Id)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task GetRoutes()
        {
            var hasData = await _context.Routes.AnyAsync(r => r.Transportation == (int)Transportation.Ship);

            if (hasData)
            {
                return;
            }

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("https://wa-eitvn.azurewebsites.net/index.php?r=api/routes");

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // List data response.
            HttpResponseMessage response = await client.GetAsync("");

            if (response.IsSuccessStatusCode)
            {
                // Parse the response body.
                var dataObjectString = await response.Content.ReadAsStringAsync();

                var dataObjects = JsonConvert.DeserializeObject <List <PublicRouteModel> >(dataObjectString);

                var newData = new List <Route>();
                foreach (var item in dataObjects)
                {
                    newData.Add(new Route
                    {
                        Destination      = item.To_city,
                        NumberOfSegments = item.Segment,
                        Start            = item.From_city,
                        TotalTime        = item.Hours,
                        Transportation   = (int)Transportation.Ship
                    });
                }

                _context.Routes.AddRange(newData);
                await _context.SaveChangesAsync();
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            //Make any other calls using HttpClient here.

            //Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();
        }
Beispiel #3
0
 public async Task AddUser(User user)
 {
     _context.Users.Add(user);
     await _context.SaveChangesAsync();
 }