ShowErrorWindow now actually shows the ErrorWindow
This commit is contained in:
1 parent
d667e86b1b
commit
e73f9db02c
10 files changed
+104
-30
No files matched your search
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.SQLite;
|
||||
using System.Threading.Tasks;
|
||||
using PcgsInvUi.Models;
|
||||
|
||||
namespace PcgsInvUi.Services;
|
||||
@@ -109,9 +110,10 @@ public class CoinDatabase {
|
||||
|
||||
// Execute the command.
|
||||
cmd.ExecuteNonQuery();
|
||||
Console.WriteLine("Coin inserted.");
|
||||
}
|
||||
|
||||
public async void CreateCoin(int pcgsNumber, string grade, int quantity) {
|
||||
public async Task<PcgsClient.ErrorType> CreateCoin(int pcgsNumber, string grade, int quantity) {
|
||||
Console.WriteLine("Creating coin...");
|
||||
// Create a coin by calling the API, then insert it into the database.
|
||||
// First, check if the coin already exists - if it does, just update the quantity by adding the amount entered.
|
||||
@@ -119,13 +121,21 @@ public class CoinDatabase {
|
||||
if (coin is not null) {
|
||||
coin.Quantity += quantity;
|
||||
SaveCoin("CollectionTable", coin);
|
||||
return PcgsClient.ErrorType.None;
|
||||
}
|
||||
else {
|
||||
// Build a new coin with the API and insert it.
|
||||
var newCoin = await _pcgsClient.GetCoinFactsByGrade(pcgsNumber, grade);
|
||||
|
||||
// Otherwise, build a new coin with the API and insert it.
|
||||
var apiResult = await _pcgsClient.GetCoinFactsByGrade(pcgsNumber, grade);
|
||||
if (apiResult.Item1 == PcgsClient.ErrorType.None) {
|
||||
var newCoin = apiResult.Item2;
|
||||
newCoin.Quantity = quantity;
|
||||
InsertCoin(newCoin);
|
||||
Collection.Add(newCoin);
|
||||
return PcgsClient.ErrorType.None;
|
||||
}
|
||||
|
||||
Console.WriteLine("Error getting coin from API - {0}", apiResult.Item1);
|
||||
return apiResult.Item1;
|
||||
}
|
||||
|
||||
public Coin? GetCoin(int pcgsNumber, string grade) {
|
||||
|
||||
@@ -11,6 +11,13 @@ using ReactiveUI;
|
||||
namespace PcgsInvUi.Services;
|
||||
|
||||
public class PcgsClient : ReactiveObject {
|
||||
public enum ErrorType {
|
||||
None,
|
||||
NoCoinFound,
|
||||
InvalidRequestFormat,
|
||||
ApiKeyInvalid
|
||||
}
|
||||
|
||||
public int NumApiRequests {
|
||||
get => _numApiRequests;
|
||||
set => this.RaiseAndSetIfChanged(ref _numApiRequests, value);
|
||||
@@ -26,7 +33,7 @@ public class PcgsClient : ReactiveObject {
|
||||
"E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm";
|
||||
}
|
||||
|
||||
public async Task<Coin?> GetCoinFactsByGrade(int pcgsNumber, string grade, bool plusGrade = false) {
|
||||
public async Task<(ErrorType, Coin?)> GetCoinFactsByGrade(int pcgsNumber, string grade, bool plusGrade = false) {
|
||||
Coin newCoin = null;
|
||||
using (HttpClient client = new HttpClient()) {
|
||||
var gradeParam = GetGradeFromString(grade);
|
||||
@@ -40,15 +47,12 @@ public class PcgsClient : ReactiveObject {
|
||||
|
||||
if (response.IsSuccessStatusCode) {
|
||||
string jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
newCoin = ParseJsonResponse(jsonResponse);
|
||||
Console.WriteLine("Coin created.");
|
||||
}
|
||||
else {
|
||||
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
|
||||
var parsedResponse = ParseJsonResponse(jsonResponse);
|
||||
return parsedResponse;
|
||||
}
|
||||
}
|
||||
|
||||
return newCoin;
|
||||
|
||||
return (ErrorType.ApiKeyInvalid, null);
|
||||
}
|
||||
|
||||
private int GetGradeFromString(string grade) {
|
||||
@@ -59,14 +63,14 @@ public class PcgsClient : ReactiveObject {
|
||||
|
||||
// TODO Will eventually create the other methods that are included in the Public API.
|
||||
|
||||
private Coin? ParseJsonResponse(string json) {
|
||||
private (ErrorType, Coin?) ParseJsonResponse(string json) {
|
||||
Console.WriteLine(json);
|
||||
// TODO Check for errors in the response
|
||||
if (json.Contains("\"ServerMessage\":\"No data found\"")) throw new CoinNotFoundException("No coin found with that PCGS number.");
|
||||
if (json.Contains("\"IsValidRequest\":true")) throw new InvalidRequestFormatException("Invalid request format.");
|
||||
if (json.Contains("\"ServerMessage\":\"No data found\"")) return (ErrorType.NoCoinFound, null);
|
||||
if (json.Contains("\"IsValidRequest\":false")) return (ErrorType.InvalidRequestFormat, null);
|
||||
|
||||
var newCoin = JsonSerializer.Deserialize<Coin>(json);
|
||||
return newCoin;
|
||||
return (ErrorType.None, newCoin);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user