Refactored some code.
This commit is contained in:
1 parent
ba04d31c3e
commit
0ff7edfed6
4 files changed
+31
-16
No files matched your search
@@ -1,12 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using PcgsInvUi.Models;
|
||||
|
||||
namespace PcgsInvUi.Services;
|
||||
|
||||
public class CoinCollection {
|
||||
public IEnumerable<Coin> GetItems() => new[] {
|
||||
new Coin(),
|
||||
new Coin(),
|
||||
new Coin(),
|
||||
};
|
||||
}
|
||||
@@ -76,11 +76,12 @@ public class CoinDatabase {
|
||||
/// <summary>
|
||||
/// Atttempts to initialize the PCGS API client.
|
||||
/// </summary>
|
||||
/// <param name=apiKey>The API key to verify.</param>
|
||||
/// <returns>A Task with a return value that returns true if the client intializes properly.</returns>
|
||||
public async Task<bool> TryInitApiClient(string apiKey) {
|
||||
Console.Write("Attempting to initialize API client with key param... ");
|
||||
_pcgsClient = await PcgsClient.CreateClient(apiKey);
|
||||
// TODO Check if the API key is valid and if it is, then add to the database.
|
||||
// Check if the API key is valid and if it is, then add to the database.
|
||||
if (_pcgsClient is null) {
|
||||
IsClientConnected = false;
|
||||
Console.WriteLine("Failed. API key was invalid.");
|
||||
@@ -150,6 +151,9 @@ public class CoinDatabase {
|
||||
Console.WriteLine("API table initialized.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a table to store the collection.
|
||||
/// </summary>
|
||||
public void CreateCollection(string collectionName) {
|
||||
// Create a table to store the collection.
|
||||
if (!IsConnected) {
|
||||
@@ -436,3 +440,7 @@ public class CoinDatabase {
|
||||
return actualGrade;
|
||||
}
|
||||
}
|
||||
|
||||
public class NotConnectedToDatabaseException : Exception {
|
||||
public NotConnectedToDatabaseException() : base("Not connected to database.") { }
|
||||
}
|
||||
@@ -31,7 +31,6 @@ public class PcgsClient : ReactiveObject {
|
||||
/// </summary>
|
||||
private PcgsClient(string apiKey) {
|
||||
_apiKey = apiKey;
|
||||
// "E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -47,6 +46,10 @@ public class PcgsClient : ReactiveObject {
|
||||
return testClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a request to the API to verify the credentials.
|
||||
/// </summary>
|
||||
/// <returns>A boolean indicating whether or not the credentials are valid.</returns>
|
||||
public async Task<bool> VerifyCredentials() {
|
||||
// Create a way to check if the api key given is valid
|
||||
// I think the best way to do this is to make a request to the api and see if it returns the expected response.
|
||||
@@ -55,6 +58,13 @@ public class PcgsClient : ReactiveObject {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the coin information for a given PCGS number and grade.
|
||||
/// </summary>
|
||||
/// <param name="pcgsNumber">The PCGS number of the coin.</param>
|
||||
/// <param name="grade">The grade of the coin.</param>
|
||||
/// <param name="plusGrade">Whether or not the coin is a plus grade.</param>
|
||||
/// <returns>A tuple containing the error type and the coin information.</returns>
|
||||
public async Task<(ErrorType, Coin?)> GetCoinFactsByGrade(int pcgsNumber, string grade, bool plusGrade = false) {
|
||||
using (HttpClient client = new HttpClient()) {
|
||||
var gradeParam = GetGradeFromString(grade);
|
||||
@@ -77,15 +87,25 @@ public class PcgsClient : ReactiveObject {
|
||||
return (ErrorType.ApiKeyInvalid, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Utility function to parse the grade number from the grade string passed from the front end.
|
||||
/// </summary>
|
||||
/// <param name="grade">The grade string.</param>
|
||||
/// <returns>The grade number.</returns>
|
||||
private int GetGradeFromString(string grade) {
|
||||
var actualGrade = int.Parse(grade.Substring(grade.Length - 2)); // Throws an exception if the grade doesn't exist.
|
||||
Console.WriteLine($"Grade is {actualGrade}");
|
||||
return actualGrade;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the json response from the api.
|
||||
/// </summary>
|
||||
/// <param name="json">The json response.</param>
|
||||
/// <returns>A tuple containing an error type and the coin information.</returns>
|
||||
private (ErrorType, Coin?) ParseJsonResponse(string json) {
|
||||
Console.WriteLine(json);
|
||||
// TODO Check for errors in the response
|
||||
// Check for errors in the response
|
||||
if (json.Contains("\"ServerMessage\":\"No data found\"")) return (ErrorType.NoCoinFound, null);
|
||||
if (json.Contains("\"IsValidRequest\":false")) return (ErrorType.InvalidRequestFormat, null);
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using ReactiveUI;
|
||||
using System.Reactive;
|
||||
using System;
|
||||
|
||||
namespace PcgsInvUi.ViewModels;
|
||||
|
||||
|
||||
Reference in new issue
Block a user