summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Caching/Mods
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework/Caching/Mods')
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs97
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs26
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs96
3 files changed, 219 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs b/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs
new file mode 100644
index 00000000..fe8a7a1f
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Caching/Mods/CachedMod.cs
@@ -0,0 +1,97 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
+using StardewModdingAPI.Toolkit.Framework.UpdateData;
+using StardewModdingAPI.Web.Framework.ModRepositories;
+
+namespace StardewModdingAPI.Web.Framework.Caching.Mods
+{
+ /// <summary>The model for cached mod data.</summary>
+ internal class CachedMod
+ {
+ /*********
+ ** Accessors
+ *********/
+ /****
+ ** Tracking
+ ****/
+ /// <summary>The internal MongoDB ID.</summary>
+ [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Named per MongoDB conventions.")]
+ [BsonIgnoreIfDefault]
+ public ObjectId _id { get; set; }
+
+ /// <summary>When the data was last updated.</summary>
+ public DateTimeOffset LastUpdated { get; set; }
+
+ /// <summary>When the data was last requested through the web API.</summary>
+ public DateTimeOffset LastRequested { get; set; }
+
+ /****
+ ** Metadata
+ ****/
+ /// <summary>The mod site on which the mod is found.</summary>
+ public ModRepositoryKey Site { get; set; }
+
+ /// <summary>The mod's unique ID within the <see cref="Site"/>.</summary>
+ public string ID { get; set; }
+
+ /// <summary>The mod availability status on the remote site.</summary>
+ public RemoteModStatus FetchStatus { get; set; }
+
+ /// <summary>The error message providing more info for the <see cref="FetchStatus"/>, if applicable.</summary>
+ public string FetchError { get; set; }
+
+
+ /****
+ ** Mod info
+ ****/
+ /// <summary>The mod's display name.</summary>
+ public string Name { get; set; }
+
+ /// <summary>The mod's latest version.</summary>
+ public string MainVersion { get; set; }
+
+ /// <summary>The mod's latest optional or prerelease version, if newer than <see cref="MainVersion"/>.</summary>
+ public string PreviewVersion { get; set; }
+
+ /// <summary>The URL for the mod page.</summary>
+ public string Url { get; set; }
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>Construct an instance.</summary>
+ public CachedMod() { }
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="site">The mod site on which the mod is found.</param>
+ /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
+ /// <param name="mod">The mod data.</param>
+ public CachedMod(ModRepositoryKey site, string id, ModInfoModel mod)
+ {
+ // tracking
+ this.LastUpdated = DateTimeOffset.UtcNow;
+ this.LastRequested = DateTimeOffset.UtcNow;
+
+ // metadata
+ this.Site = site;
+ this.ID = id;
+ this.FetchStatus = mod.Status;
+ this.FetchError = mod.Error;
+
+ // mod info
+ this.Name = mod.Name;
+ this.MainVersion = mod.Version;
+ this.PreviewVersion = mod.PreviewVersion;
+ this.Url = mod.Url;
+ }
+
+ /// <summary>Get the API model for the cached data.</summary>
+ public ModInfoModel GetModel()
+ {
+ return new ModInfoModel(name: this.Name, version: this.MainVersion, previewVersion: this.PreviewVersion, url: this.Url).WithError(this.FetchStatus, this.FetchError);
+ }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
new file mode 100644
index 00000000..23929d1d
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
@@ -0,0 +1,26 @@
+using StardewModdingAPI.Toolkit.Framework.UpdateData;
+using StardewModdingAPI.Web.Framework.ModRepositories;
+
+namespace StardewModdingAPI.Web.Framework.Caching.Mods
+{
+ /// <summary>Encapsulates logic for accessing the mod data cache.</summary>
+ internal interface IModCacheRepository : ICacheRepository
+ {
+ /*********
+ ** Methods
+ *********/
+ /// <summary>Get the cached mod data.</summary>
+ /// <param name="site">The mod site to search.</param>
+ /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
+ /// <param name="mod">The fetched mod.</param>
+ /// <param name="markRequested">Whether to update the mod's 'last requested' date.</param>
+ bool TryGetMod(ModRepositoryKey site, string id, out CachedMod mod, bool markRequested = true);
+
+ /// <summary>Save data fetched for a mod.</summary>
+ /// <param name="site">The mod site on which the mod is found.</param>
+ /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
+ /// <param name="mod">The mod data.</param>
+ /// <param name="cachedMod">The stored mod record.</param>
+ void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod);
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs
new file mode 100644
index 00000000..d8ad7d21
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs
@@ -0,0 +1,96 @@
+using System;
+using MongoDB.Driver;
+using StardewModdingAPI.Toolkit.Framework.UpdateData;
+using StardewModdingAPI.Web.Framework.ModRepositories;
+
+namespace StardewModdingAPI.Web.Framework.Caching.Mods
+{
+ /// <summary>Encapsulates logic for accessing the mod data cache.</summary>
+ internal class ModCacheRepository : BaseCacheRepository, IModCacheRepository
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>The collection for cached mod data.</summary>
+ private readonly IMongoCollection<CachedMod> Mods;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="database">The authenticated MongoDB database.</param>
+ public ModCacheRepository(IMongoDatabase database)
+ {
+ // get collections
+ this.Mods = database.GetCollection<CachedMod>("mods");
+
+ // add indexes if needed
+ this.Mods.Indexes.CreateOne(new CreateIndexModel<CachedMod>(Builders<CachedMod>.IndexKeys.Ascending(p => p.ID).Ascending(p => p.Site)));
+ }
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get the cached mod data.</summary>
+ /// <param name="site">The mod site to search.</param>
+ /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
+ /// <param name="mod">The fetched mod.</param>
+ /// <param name="markRequested">Whether to update the mod's 'last requested' date.</param>
+ public bool TryGetMod(ModRepositoryKey site, string id, out CachedMod mod, bool markRequested = true)
+ {
+ // get mod
+ id = this.NormaliseId(id);
+ mod = this.Mods.Find(entry => entry.ID == id && entry.Site == site).FirstOrDefault();
+ if (mod == null)
+ return false;
+
+ // bump 'last requested'
+ if (markRequested)
+ {
+ mod.LastRequested = DateTimeOffset.UtcNow;
+ mod = this.SaveMod(mod);
+ }
+
+ return true;
+ }
+
+ /// <summary>Save data fetched for a mod.</summary>
+ /// <param name="site">The mod site on which the mod is found.</param>
+ /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
+ /// <param name="mod">The mod data.</param>
+ /// <param name="cachedMod">The stored mod record.</param>
+ public void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod)
+ {
+ id = this.NormaliseId(id);
+
+ cachedMod = this.SaveMod(new CachedMod(site, id, mod));
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Save data fetched for a mod.</summary>
+ /// <param name="mod">The mod data.</param>
+ public CachedMod SaveMod(CachedMod mod)
+ {
+ string id = this.NormaliseId(mod.ID);
+
+ this.Mods.ReplaceOne(
+ entry => entry.ID == id && entry.Site == mod.Site,
+ mod,
+ new UpdateOptions { IsUpsert = true }
+ );
+
+ return mod;
+ }
+
+ /// <summary>Normalise a mod ID for case-insensitive search.</summary>
+ /// <param name="id">The mod ID.</param>
+ public string NormaliseId(string id)
+ {
+ return id.Trim().ToLower();
+ }
+ }
+}