Basic implementation of updating which shop is active.
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
using BodyshopUploader.Utils.Growls;
|
||||
using GraphQL.Client;
|
||||
using GraphQL.Common.Request;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -18,9 +20,38 @@ namespace BodyshopUploader.ViewModels
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
logger.Trace("Main VM constructed.");
|
||||
|
||||
BackgroundWorker bw = new BackgroundWorker();
|
||||
bw.DoWork += bw_InitVm;
|
||||
bw.WorkerReportsProgress = true;
|
||||
bw.ProgressChanged += Bw_ProgressChanged;
|
||||
bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
|
||||
bw.RunWorkerAsync();
|
||||
|
||||
Growler = new GrowlNotification(this);
|
||||
Utils.JobProcessingQueue.SetGrowler(Growler);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
||||
{
|
||||
((BackgroundWorker)sender).Dispose();
|
||||
//Progress = 0;
|
||||
logger.Trace("BW Completed.");
|
||||
}
|
||||
|
||||
private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
||||
{
|
||||
Progress = e.ProgressPercentage;
|
||||
}
|
||||
|
||||
private void bw_InitVm(object sender, DoWorkEventArgs e)
|
||||
{
|
||||
BackgroundWorker _callingThread = sender as BackgroundWorker;
|
||||
_callingThread.ReportProgress(1);
|
||||
|
||||
//Restore list of paths, convert to monitor object.
|
||||
List<string> listOfPaths = Properties.Settings.Default.MonitoringPaths;
|
||||
if (listOfPaths == null) listOfPaths = new List<string>();
|
||||
@@ -29,9 +60,19 @@ namespace BodyshopUploader.ViewModels
|
||||
MonitoringPaths.Add(new Models.Monitor() { FilePath = p });
|
||||
}
|
||||
MonitoringPaths.CollectionChanged += MonitoringPathsChanged;
|
||||
logger.Trace("Main VM Created.");
|
||||
_callingThread.ReportProgress(30);
|
||||
|
||||
//Cannot use await.
|
||||
LoadBodyshopData().Wait(); ;
|
||||
|
||||
|
||||
_callingThread.ReportProgress(80);
|
||||
|
||||
logger.Debug("VM Init Complete");
|
||||
_callingThread.ReportProgress(100);
|
||||
}
|
||||
|
||||
|
||||
private void MonitoringPathsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
logger.Warn("TODO: Change monitoring lifecycles for folder watchers.");
|
||||
@@ -83,6 +124,84 @@ namespace BodyshopUploader.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public async Task LoadBodyshopData()
|
||||
{
|
||||
var r = new GraphQLRequest
|
||||
{
|
||||
Query = @"query QUERY_BODYSHOPS {
|
||||
bodyshops {
|
||||
shopname
|
||||
id
|
||||
associations {
|
||||
id
|
||||
active
|
||||
}
|
||||
}
|
||||
}"
|
||||
};
|
||||
|
||||
using (var g = Utils.GraphQL.CreateGQLClient())
|
||||
{
|
||||
logger.Trace("Firing GQL Query: {0}", r.ToString());
|
||||
var graphQLResponse = await g.PostAsync(r);
|
||||
if (graphQLResponse.Errors == null)
|
||||
{
|
||||
logger.Trace("GQL Response: {0}", graphQLResponse.Data.bodyshops);
|
||||
var p = graphQLResponse.Data.bodyshops;
|
||||
ShopData = graphQLResponse.Data.bodyshops.ToObject<ObservableCollection<Bodyshop>>();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error("Error querying bodyshop data.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetActiveBodyshop(Bodyshop b)
|
||||
{
|
||||
foreach (var s in ShopData)
|
||||
{
|
||||
if (s.Id == ActiveShop.Id) s.AssociationActive = true;
|
||||
else s.AssociationActive = false;
|
||||
|
||||
|
||||
var r = new GraphQLRequest
|
||||
{
|
||||
Query = @"
|
||||
mutation UPDATE_ASSOCIATION($assocId: uuid, $assocActive: Boolean) {
|
||||
update_associations(where: {id: {_eq: $assocId}}, _set: {active: $assocActive}) {
|
||||
returning {
|
||||
id
|
||||
active
|
||||
}
|
||||
}
|
||||
}
|
||||
",
|
||||
Variables = new
|
||||
{
|
||||
assocId = s.AssociationId,
|
||||
assocActive = s.AssociationActive
|
||||
}
|
||||
};
|
||||
|
||||
using (var g = Utils.GraphQL.CreateGQLClient())
|
||||
{
|
||||
logger.Trace("Firing GQL Query: {0}", r.ToString());
|
||||
var graphQLResponse = await g.PostAsync(r);
|
||||
if (graphQLResponse.Errors == null)
|
||||
{
|
||||
logger.Trace("GQL Response: {0}", graphQLResponse.Data);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error("Error mutating data.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task TestGql()
|
||||
{
|
||||
Notification _n = new Notification()
|
||||
@@ -98,20 +217,18 @@ namespace BodyshopUploader.ViewModels
|
||||
var r = new GraphQLRequest
|
||||
{
|
||||
Query = @"
|
||||
query {
|
||||
jobs {
|
||||
id
|
||||
est_number
|
||||
ro_number
|
||||
}
|
||||
}
|
||||
"
|
||||
query QUERY_BODYSHOPS {
|
||||
bodyshops {
|
||||
shopname
|
||||
id
|
||||
}
|
||||
}"
|
||||
};
|
||||
|
||||
using (var g = Utils.GraphQL.CreateGQLClient())
|
||||
{
|
||||
var graphQLResponse = await g.PostAsync(r);
|
||||
logger.Info(graphQLResponse.Data.jobs);
|
||||
logger.Info("GQL Response: {0}", graphQLResponse.Data.bodyshops);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user