summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/ConfigModels/MongoDbConfig.cs
blob: 3c5083002140bd214d59dedbbe4edf96a735e18e (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
using System;

namespace StardewModdingAPI.Web.Framework.ConfigModels
{
    /// <summary>The config settings for mod compatibility list.</summary>
    internal class MongoDbConfig
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The MongoDB hostname.</summary>
        public string Host { get; set; }

        /// <summary>The MongoDB username (if any).</summary>
        public string Username { get; set; }

        /// <summary>The MongoDB password (if any).</summary>
        public string Password { get; set; }

        /// <summary>The database name.</summary>
        public string Database { get; set; }


        /*********
        ** Public method
        *********/
        /// <summary>Get the MongoDB connection string.</summary>
        public string GetConnectionString()
        {
            bool isLocal = this.Host == "localhost";
            bool hasLogin = !string.IsNullOrWhiteSpace(this.Username) && !string.IsNullOrWhiteSpace(this.Password);

            return $"mongodb{(isLocal ? "" : "+srv")}://"
                + (hasLogin ? $"{Uri.EscapeDataString(this.Username)}:{Uri.EscapeDataString(this.Password)}@" : "")
                + $"{this.Host}/{this.Database}?retryWrites=true&w=majority";
        }
    }
}