diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 00:47:46 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 00:47:46 -0400 |
commit | 2c02dfe45a6d1252bfef557db8f39f97e57d3d19 (patch) | |
tree | 4fb8b5759d56ba6a62a452e09180ede280228b3b /src/StardewModdingAPI.Web/Controllers | |
parent | edbc3ef3c08909486d6c4b8797f0e3c2934854fa (diff) | |
download | SMAPI-2c02dfe45a6d1252bfef557db8f39f97e57d3d19.tar.gz SMAPI-2c02dfe45a6d1252bfef557db8f39f97e57d3d19.tar.bz2 SMAPI-2c02dfe45a6d1252bfef557db8f39f97e57d3d19.zip |
rewrite to make update-check logic vendor-agnostic (#336)
Diffstat (limited to 'src/StardewModdingAPI.Web/Controllers')
-rw-r--r-- | src/StardewModdingAPI.Web/Controllers/CheckController.cs | 71 |
1 files changed, 60 insertions, 11 deletions
diff --git a/src/StardewModdingAPI.Web/Controllers/CheckController.cs b/src/StardewModdingAPI.Web/Controllers/CheckController.cs index 9ec068cb..2a346cf3 100644 --- a/src/StardewModdingAPI.Web/Controllers/CheckController.cs +++ b/src/StardewModdingAPI.Web/Controllers/CheckController.cs @@ -1,7 +1,9 @@ +using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using StardewModdingAPI.Web.Framework; +using StardewModdingAPI.Web.Framework.ModRepositories; using StardewModdingAPI.Web.Models; namespace StardewModdingAPI.Web.Controllers @@ -12,27 +14,74 @@ namespace StardewModdingAPI.Web.Controllers public class CheckController : Controller { /********* + ** Properties + *********/ + /// <summary>The mod repositories which provide mod metadata.</summary> + private readonly IDictionary<string, IModRepository> Repositories = + new IModRepository[] + { + new NexusRepository() + } + .ToDictionary(p => p.VendorKey, StringComparer.CurrentCultureIgnoreCase); + + + /********* ** Public methods *********/ /// <summary>Fetch version metadata for the given mods.</summary> - /// <param name="mods">The mods for which to fetch update metadata.</param> + /// <param name="search">The mod update search criteria.</param> [HttpPost] - public async Task<ModGenericModel[]> Post([FromBody] ModSearchModel[] mods) + public async Task<ModGenericModel[]> Post([FromBody] ModSearchModel search) { - using (NexusModsClient client = new NexusModsClient()) + IList<ModGenericModel> result = new List<ModGenericModel>(); + + foreach (string modKey in search.ModKeys) { - List<ModGenericModel> result = new List<ModGenericModel>(); + // parse mod key + if (!this.TryParseModKey(modKey, out string vendorKey, out string modID)) + { + result.Add(new ModGenericModel(modKey, "The mod key isn't in a valid format. It should contain the mod repository key and mod ID like 'Nexus:541'.")); + continue; + } - foreach (ModSearchModel mod in mods) + // get matching repository + if (!this.Repositories.TryGetValue(vendorKey, out IModRepository repository)) { - if (mod.NexusID.HasValue) - result.Add(await client.GetModInfoAsync(mod.NexusID.Value)); - else - result.Add(new ModGenericModel(null, mod.NexusID ?? 0)); + result.Add(new ModGenericModel(modKey, "There's no mod repository matching this namespaced mod ID.")); + continue; } - return result.ToArray(); + // fetch mod info + result.Add(await repository.GetModInfoAsync(modID)); } + + return result.ToArray(); + } + + + /********* + ** Private methods + *********/ + /// <summary>Parse a namespaced mod ID.</summary> + /// <param name="raw">The raw mod ID to parse.</param> + /// <param name="vendorKey">The parsed vendor key.</param> + /// <param name="modID">The parsed mod ID.</param> + /// <returns>Returns whether the value could be parsed.</returns> + private bool TryParseModKey(string raw, out string vendorKey, out string modID) + { + // split parts + string[] parts = raw?.Split(':'); + if (parts == null || parts.Length != 2) + { + vendorKey = null; + modID = null; + return false; + } + + // parse + vendorKey = parts[0]; + modID = parts[1]; + return true; } } } |