add openhack files

This commit is contained in:
Ryan Peters
2022-11-03 16:41:13 -04:00
commit b2c9f7e29f
920 changed files with 118861 additions and 0 deletions

View 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;
}
}
}

View 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);
}
}

View 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;
}
}
}

View 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();
}
}
}

View 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;
}
}
}

View 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;
}
}
}