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
{
/// The model for cached mod data.
internal class CachedMod
{
/*********
** Accessors
*********/
/****
** Tracking
****/
/// The internal MongoDB ID.
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Named per MongoDB conventions.")]
[BsonIgnoreIfDefault]
public ObjectId _id { get; set; }
/// When the data was last updated.
public DateTimeOffset LastUpdated { get; set; }
/// When the data was last requested through the web API.
public DateTimeOffset LastRequested { get; set; }
/****
** Metadata
****/
/// The mod site on which the mod is found.
public ModRepositoryKey Site { get; set; }
/// The mod's unique ID within the .
public string ID { get; set; }
/// The mod availability status on the remote site.
public RemoteModStatus FetchStatus { get; set; }
/// The error message providing more info for the , if applicable.
public string FetchError { get; set; }
/****
** Mod info
****/
/// The mod's display name.
public string Name { get; set; }
/// The mod's latest version.
public string MainVersion { get; set; }
/// The mod's latest optional or prerelease version, if newer than .
public string PreviewVersion { get; set; }
/// The URL for the mod page.
public string Url { get; set; }
/// The license URL, if available.
public string LicenseUrl { get; set; }
/// The license name, if available.
public string LicenseName { get; set; }
/*********
** Accessors
*********/
/// Construct an instance.
public CachedMod() { }
/// Construct an instance.
/// The mod site on which the mod is found.
/// The mod's unique ID within the .
/// The mod data.
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;
this.LicenseUrl = mod.LicenseUrl;
this.LicenseName = mod.LicenseName;
}
/// Get the API model for the cached data.
public ModInfoModel GetModel()
{
return new ModInfoModel(name: this.Name, version: this.MainVersion, url: this.Url, previewVersion: this.PreviewVersion)
.SetLicense(this.LicenseUrl, this.LicenseName)
.SetError(this.FetchStatus, this.FetchError);
}
}
}