Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1. using MSApi.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Net.Http.Headers;
  7. using System.Threading.Tasks;
  8.  
  9. namespace MSApi.DAL
  10. {
  11.     public class Synchronizer
  12.     {
  13.         private static HttpClient Client;
  14.  
  15.         public Synchronizer()
  16.         {
  17.             Client = new HttpClient();
  18.             Client.BaseAddress = new Uri("http://localhost:65208/");
  19.             Client.DefaultRequestHeaders.Accept.Clear();
  20.             Client.DefaultRequestHeaders.Accept.Add(
  21.                 new MediaTypeWithQualityHeaderValue("application/json"));
  22.         }
  23.  
  24.         private static async Task<T> GetItemAsync<T>(string path) where T : class
  25.         {
  26.             T item = null;
  27.             HttpResponseMessage response = await Client.GetAsync(path);
  28.             if (response.IsSuccessStatusCode)
  29.             {
  30.                 item = await response.Content.ReadAsAsync<T>();
  31.             }
  32.             return item;
  33.         }
  34.  
  35.         public async Task RunAsync()
  36.         {
  37.             List<Book> books = await GetItemAsync<List<Book>>("api/books");
  38.             if (books != null)
  39.             {
  40.                 DateTime lastUpdate = books.Max(x => x.ModifiedDateTime);
  41.                 List<Book> booksToUpdate = BookRepository.GetBooks().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
  42.                 foreach (Book bookToUpdate in booksToUpdate)
  43.                 {
  44.                    await Client.PostAsJsonAsync("api/books", bookToUpdate);
  45.                 }
  46.             }
  47.  
  48.             List<Category> categories = await GetItemAsync<List<Category>>("api/categories");
  49.             if (categories != null)
  50.             {
  51.                 DateTime lastUpdate = categories.Max(x => x.ModifiedDateTime);
  52.                 List<Category> categoriesToUpdate = CategoryRepository.GetCategories().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
  53.                 foreach (Category categoryToUpdate in categoriesToUpdate)
  54.                 {
  55.                     await Client.PostAsJsonAsync("api/categories", categoryToUpdate);
  56.                 }
  57.             }
  58.  
  59.             List<Location> locations = await GetItemAsync<List<Location>>("api/locations");
  60.             if (locations != null)
  61.             {
  62.                 DateTime lastUpdate = locations.Max(x => x.ModifiedDateTime);
  63.                 List<Location> locationsToUpdate = LocationRepository.GetLocations().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
  64.                 foreach (Location locationToUpdate in locationsToUpdate)
  65.                 {
  66.                     await Client.PostAsJsonAsync("api/locations", locationToUpdate);
  67.                 }
  68.             }
  69.  
  70.             List<Order> orders = await GetItemAsync<List<Order>>("api/orders");
  71.             if (orders != null)
  72.             {
  73.                 DateTime lastUpdate = orders.Max(x => x.ModifiedDateTime);
  74.                 List<Order> ordersToUpdate = OrderRepository.GetOrders().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
  75.                 foreach (Order orderToUpdate in ordersToUpdate)
  76.                 {
  77.                     await Client.PostAsJsonAsync("api/orders", orderToUpdate);
  78.                 }
  79.             }
  80.  
  81.             List<Stock> stocks = await GetItemAsync<List<Stock>>("api/stocks");
  82.             if (stocks != null)
  83.             {
  84.                 DateTime lastUpdate = stocks.Max(x => x.ModifiedDateTime);
  85.                 List<Stock> stocksToUpdate = StockRepository.GetStocks().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
  86.                 foreach (Stock stockToUpdate in stocksToUpdate)
  87.                 {
  88.                     await Client.PostAsJsonAsync("api/stocks", stockToUpdate);
  89.                 }
  90.             }
  91.  
  92.             List<ApplicationUser> users = await GetItemAsync<List<ApplicationUser>>("api/users");
  93.             if (users != null)
  94.             {
  95.                 DateTime lastUpdate = users.Max(x => x.ModifiedDateTime);
  96.                 List<ApplicationUser> usersToUpdate = ApplicationUserRepository.GetUsers().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
  97.                 foreach (ApplicationUser userToUpdate in usersToUpdate)
  98.                 {
  99.                     await Client.PostAsJsonAsync("api/users", userToUpdate);
  100.                 }
  101.             }
  102.  
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement