add openhack files
This commit is contained in:
42
support/tripviewer/web/Models/Stores/BaseStore.cs
Normal file
42
support/tripviewer/web/Models/Stores/BaseStore.cs
Normal file
@ -0,0 +1,42 @@
|
||||
namespace Simulator.DataStore.Stores
|
||||
{
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
public class BaseStore
|
||||
//<T> : IBaseStore<T> where T : class, IBaseDataObject, new()
|
||||
{
|
||||
public string EndPoint { get; set; }
|
||||
public HttpClient Client { get; set; }
|
||||
public DateTime DateTime { get; set; }
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
|
||||
public BaseStore(IHttpClientFactory clientFactory)
|
||||
{
|
||||
_clientFactory = clientFactory;
|
||||
}
|
||||
|
||||
public Task InitializeStore(string EndPoint)
|
||||
{
|
||||
Client = _clientFactory.CreateClient();
|
||||
Client.BaseAddress = new Uri(EndPoint);
|
||||
Client.DefaultRequestHeaders.Accept.Clear();
|
||||
|
||||
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
DateTime = DateTime.UtcNow;
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
22
support/tripviewer/web/Models/Stores/IBaseStore.cs
Normal file
22
support/tripviewer/web/Models/Stores/IBaseStore.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Simulator.DataStore.Stores
|
||||
{
|
||||
public interface IBaseStore<T>
|
||||
{
|
||||
Task InitializeStoreAsync();
|
||||
|
||||
Task<T> GetItemAsync(string id);
|
||||
|
||||
Task<IEnumerable<T>> GetItemsAsync();
|
||||
|
||||
Task<bool> CreateItemAsync(T item);
|
||||
|
||||
Task<bool> UpdateItemAsync(T item);
|
||||
|
||||
Task<bool> DeleteItemAsync(T item);
|
||||
}
|
||||
}
|
71
support/tripviewer/web/Models/Stores/PoiStore.cs
Normal file
71
support/tripviewer/web/Models/Stores/PoiStore.cs
Normal file
@ -0,0 +1,71 @@
|
||||
namespace Simulator.DataStore.Stores
|
||||
{
|
||||
using Simulator.DataObjects;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class PoiStore : BaseStore, IBaseStore<Poi>
|
||||
{
|
||||
|
||||
public PoiStore(string EndPoint)
|
||||
{
|
||||
base.InitializeStore(EndPoint);
|
||||
}
|
||||
|
||||
public async Task<Poi> GetItemAsync(string id)
|
||||
{
|
||||
Poi poi = null;
|
||||
HttpResponseMessage response = await Client.GetAsync($"/api/poi/{id}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
poi = await response.Content.ReadAsAsync<Poi>();
|
||||
}
|
||||
return poi;
|
||||
}
|
||||
|
||||
public async Task<List<Poi>> GetItemsAsync()
|
||||
{
|
||||
List<Poi> trips = null;
|
||||
HttpResponseMessage response = await Client.GetAsync("api/poi/");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
trips = await response.Content.ReadAsAsync<List<Poi>>();
|
||||
}
|
||||
return trips;
|
||||
}
|
||||
|
||||
public async Task<Poi> CreateItemAsync(Poi item)
|
||||
{
|
||||
HttpResponseMessage response = await Client.PostAsJsonAsync<Poi>("api/poi", item);
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
item = await response.Content.ReadAsAsync<Poi>();
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateItemAsync(Poi item)
|
||||
{
|
||||
|
||||
HttpResponseMessage response = await Client.PatchAsJsonAsync($"api/poi/{item.Id}", item);
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (response.IsSuccessStatusCode)
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteItemAsync(Poi item)
|
||||
{
|
||||
HttpResponseMessage response = await Client.DeleteAsync($"api/poi/{item.Id}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (response.IsSuccessStatusCode)
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
81
support/tripviewer/web/Models/Stores/TripPointStore.cs
Normal file
81
support/tripviewer/web/Models/Stores/TripPointStore.cs
Normal file
@ -0,0 +1,81 @@
|
||||
namespace Simulator.DataStore.Stores
|
||||
{
|
||||
using Simulator.DataObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class TripPointStore : BaseStore//, IBaseStore<TripPoint>
|
||||
{
|
||||
public TripPointStore(IHttpClientFactory clientFactory, string EndPoint) : base(clientFactory)
|
||||
{
|
||||
base.InitializeStore(EndPoint);
|
||||
}
|
||||
|
||||
public async Task<TripPoint> GetItemAsync(string id)
|
||||
{
|
||||
//Deviating implemetnation because of complxity of the nested API
|
||||
TripPoint tripPoint = null;
|
||||
HttpResponseMessage response = await Client.GetAsync($"/api/trips/{id}/trippoints/{id}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
tripPoint = await response.Content.ReadAsAsync<TripPoint>();
|
||||
}
|
||||
return tripPoint;
|
||||
}
|
||||
|
||||
public async Task<TripPoint> GetItemAsync(TripPoint item)
|
||||
{
|
||||
//Deviating implemetnation because of complxity of the nested API
|
||||
TripPoint tripPoint = null;
|
||||
HttpResponseMessage response = await Client.GetAsync($"/api/trips/{item.TripId}/trippoints/{item.Id}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
tripPoint = await response.Content.ReadAsAsync<TripPoint>();
|
||||
}
|
||||
return tripPoint;
|
||||
}
|
||||
|
||||
public Task<List<TripPoint>> GetItemsAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<List<TripPoint>> GetItemsAsync(Trip item)
|
||||
{
|
||||
List<TripPoint> tripPoints = null;
|
||||
HttpResponseMessage response = await Client.GetAsync($"api/trips/{item.Id}/trippoints");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
tripPoints = await response.Content.ReadAsAsync<List<TripPoint>>();
|
||||
}
|
||||
return tripPoints;
|
||||
}
|
||||
|
||||
public async Task<TripPoint> CreateItemAsync(TripPoint item)
|
||||
{
|
||||
string apiPath = $"api/trips/{item.TripId.ToString()}/trippoints";
|
||||
HttpResponseMessage response = await Client.PostAsJsonAsync<TripPoint>(apiPath, item);
|
||||
response.EnsureSuccessStatusCode();
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
|
||||
item = await response.Content.ReadAsAsync<TripPoint>();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
public Task<bool> UpdateItemAsync(TripPoint item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> DeleteItemAsync(TripPoint item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
44
support/tripviewer/web/Models/Stores/TripStore.cs
Normal file
44
support/tripviewer/web/Models/Stores/TripStore.cs
Normal file
@ -0,0 +1,44 @@
|
||||
namespace Simulator.DataStore.Stores
|
||||
{
|
||||
using Simulator.DataObjects;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class TripStore : BaseStore//, IBaseStore<Trip>
|
||||
{
|
||||
|
||||
|
||||
public TripStore(IHttpClientFactory clientFactory, string EndPoint) : base(clientFactory)
|
||||
{
|
||||
base.InitializeStore(EndPoint);
|
||||
}
|
||||
|
||||
public async Task<Trip> GetItemAsync(string id)
|
||||
{
|
||||
Trip trip = null;
|
||||
HttpResponseMessage response = await Client.GetAsync($"/api/trips/{id}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
trip = await response.Content.ReadAsAsync<Trip>();
|
||||
}
|
||||
return trip;
|
||||
}
|
||||
|
||||
public async Task<List<Trip>> GetItemsAsync()
|
||||
{
|
||||
List<Trip> trips = null;
|
||||
HttpResponseMessage response = await Client.GetAsync("api/trips/");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
trips = await response.Content.ReadAsAsync<List<Trip>>();
|
||||
}
|
||||
return trips;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
56
support/tripviewer/web/Models/Stores/UserStore.cs
Normal file
56
support/tripviewer/web/Models/Stores/UserStore.cs
Normal file
@ -0,0 +1,56 @@
|
||||
namespace Simulator.DataStore.Stores
|
||||
{
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using Simulator.DataObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class UserStore : BaseStore//, IBaseStore<User>
|
||||
{
|
||||
private readonly IConfiguration Configuration;
|
||||
|
||||
public UserStore(IHttpClientFactory clientFactory, string EndPoint, IConfiguration configuration) : base(clientFactory)
|
||||
{
|
||||
base.InitializeStore(EndPoint);
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task<User> GetItemAsync(string id)
|
||||
{
|
||||
User user = null;
|
||||
// var baseUrl = Configuration.GetValue<string>("USER_ROOT_URL");
|
||||
HttpResponseMessage response = await Client.GetAsync($"/api/user/{id}");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
response.Content.Headers.ContentType.MediaType = "application/json";
|
||||
user = await response.Content.ReadAsAsync<User>();
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<List<User>> GetItemsAsync()
|
||||
{
|
||||
List<User> users = null;
|
||||
HttpResponseMessage response = await Client.GetAsync("/api/user/");
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var contents = await response.Content.ReadAsStreamAsync();
|
||||
|
||||
var serializer = new JsonSerializer();
|
||||
|
||||
using (var sr = new StreamReader(contents))
|
||||
using (var jsonTextReader = new JsonTextReader(sr))
|
||||
{
|
||||
users = serializer.Deserialize<List<User>>(jsonTextReader);
|
||||
}
|
||||
}
|
||||
return users;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user