summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs
blob: d8ad7d2196706eee4c54ff6756da4085f2f5aec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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();
        }
    }
}