Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MSApi.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- namespace MSApi.DAL
- {
- public class Synchronizer
- {
- private static HttpClient Client;
- public Synchronizer()
- {
- Client = new HttpClient();
- Client.BaseAddress = new Uri("http://localhost:65208/");
- Client.DefaultRequestHeaders.Accept.Clear();
- Client.DefaultRequestHeaders.Accept.Add(
- new MediaTypeWithQualityHeaderValue("application/json"));
- }
- private static async Task<T> GetItemAsync<T>(string path) where T : class
- {
- T item = null;
- HttpResponseMessage response = await Client.GetAsync(path);
- if (response.IsSuccessStatusCode)
- {
- item = await response.Content.ReadAsAsync<T>();
- }
- return item;
- }
- public async Task RunAsync()
- {
- List<Book> books = await GetItemAsync<List<Book>>("api/books");
- if (books != null)
- {
- DateTime lastUpdate = books.Max(x => x.ModifiedDateTime);
- List<Book> booksToUpdate = BookRepository.GetBooks().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
- foreach (Book bookToUpdate in booksToUpdate)
- {
- await Client.PostAsJsonAsync("api/books", bookToUpdate);
- }
- }
- List<Category> categories = await GetItemAsync<List<Category>>("api/categories");
- if (categories != null)
- {
- DateTime lastUpdate = categories.Max(x => x.ModifiedDateTime);
- List<Category> categoriesToUpdate = CategoryRepository.GetCategories().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
- foreach (Category categoryToUpdate in categoriesToUpdate)
- {
- await Client.PostAsJsonAsync("api/categories", categoryToUpdate);
- }
- }
- List<Location> locations = await GetItemAsync<List<Location>>("api/locations");
- if (locations != null)
- {
- DateTime lastUpdate = locations.Max(x => x.ModifiedDateTime);
- List<Location> locationsToUpdate = LocationRepository.GetLocations().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
- foreach (Location locationToUpdate in locationsToUpdate)
- {
- await Client.PostAsJsonAsync("api/locations", locationToUpdate);
- }
- }
- List<Order> orders = await GetItemAsync<List<Order>>("api/orders");
- if (orders != null)
- {
- DateTime lastUpdate = orders.Max(x => x.ModifiedDateTime);
- List<Order> ordersToUpdate = OrderRepository.GetOrders().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
- foreach (Order orderToUpdate in ordersToUpdate)
- {
- await Client.PostAsJsonAsync("api/orders", orderToUpdate);
- }
- }
- List<Stock> stocks = await GetItemAsync<List<Stock>>("api/stocks");
- if (stocks != null)
- {
- DateTime lastUpdate = stocks.Max(x => x.ModifiedDateTime);
- List<Stock> stocksToUpdate = StockRepository.GetStocks().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
- foreach (Stock stockToUpdate in stocksToUpdate)
- {
- await Client.PostAsJsonAsync("api/stocks", stockToUpdate);
- }
- }
- List<ApplicationUser> users = await GetItemAsync<List<ApplicationUser>>("api/users");
- if (users != null)
- {
- DateTime lastUpdate = users.Max(x => x.ModifiedDateTime);
- List<ApplicationUser> usersToUpdate = ApplicationUserRepository.GetUsers().Where(x => x.ModifiedDateTime > lastUpdate).ToList();
- foreach (ApplicationUser userToUpdate in usersToUpdate)
- {
- await Client.PostAsJsonAsync("api/users", userToUpdate);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement