Added mongodb as database

This commit is contained in:
AeonLucid
2021-11-01 00:53:04 +01:00
parent 5f4e81afa4
commit 505ae8b2f8
15 changed files with 263 additions and 15 deletions
@@ -0,0 +1,20 @@
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Prospect.Server.Api.Config;
namespace Prospect.Server.Api.Services.Database
{
public abstract class BaseDbService<T>
{
protected BaseDbService(IOptions<DatabaseSettings> options, string collection)
{
var settings = options.Value;
var client = new MongoClient(settings.ConnectionString);
var database = client.GetDatabase(settings.DatabaseName);
Collection = database.GetCollection<T>(collection);
}
protected IMongoCollection<T> Collection { get; }
}
}
@@ -0,0 +1,38 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Prospect.Server.Api.Config;
using Prospect.Server.Api.Services.Database.Models;
namespace Prospect.Server.Api.Services.Database
{
public class DbEntityService : BaseDbService<PlayFabEntity>
{
public DbEntityService(IOptions<DatabaseSettings> settings) : base(settings, nameof(DbEntityService))
{
}
public async Task<PlayFabEntity> FindAsync(string userId)
{
return await Collection.Find(user => user.UserId == userId).FirstOrDefaultAsync();
}
private async Task<PlayFabEntity> CreateAsync(string userId)
{
var user = new PlayFabEntity
{
UserId = userId
};
await Collection.InsertOneAsync(user);
return user;
}
public async Task<PlayFabEntity> FindOrCreateAsync(string userId)
{
return await FindAsync(userId) ??
await CreateAsync(userId);
}
}
}
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Prospect.Server.Api.Config;
using Prospect.Server.Api.Services.Database.Models;
namespace Prospect.Server.Api.Services.Database
{
public class DbUserService : BaseDbService<PlayFabUser>
{
public DbUserService(IOptions<DatabaseSettings> settings) : base(settings, nameof(PlayFabUser))
{
}
public async Task<PlayFabUser> FindAsync(PlayFabUserAuthType type, string key)
{
return await Collection.Find(user => user.Auth.Any(auth =>
auth.Type == type &&
auth.Key == key)).FirstOrDefaultAsync();
}
private async Task<PlayFabUser> CreateAsync(PlayFabUserAuthType type, string key)
{
var user = new PlayFabUser
{
DisplayName = "Unknown",
Auth = new List<PlayFabUserAuth>
{
new PlayFabUserAuth
{
Type = type,
Key = key
}
}
};
await Collection.InsertOneAsync(user);
return user;
}
public async Task<PlayFabUser> FindOrCreateAsync(PlayFabUserAuthType type, string key)
{
return await FindAsync(type, key) ??
await CreateAsync(type, key);
}
}
}
@@ -0,0 +1,23 @@
using System;
using System.Security.Cryptography;
using MongoDB.Bson.Serialization;
namespace Prospect.Server.Api.Services.Database.Generator
{
public class PlayFabIdGenerator : IIdGenerator
{
private static readonly RNGCryptoServiceProvider Random = new RNGCryptoServiceProvider();
public object GenerateId(object container, object document)
{
Span<byte> data = stackalloc byte[8];
Random.GetBytes(data);
return Convert.ToHexString(data);
}
public bool IsEmpty(object id)
{
return id is not string idStr || string.IsNullOrEmpty(idStr);
}
}
}
@@ -0,0 +1,18 @@
using MongoDB.Bson.Serialization.Attributes;
using Prospect.Server.Api.Services.Database.Generator;
namespace Prospect.Server.Api.Services.Database.Models
{
/// <summary>
/// title_player_account
/// </summary>
public class PlayFabEntity
{
[BsonId(IdGenerator = typeof(PlayFabIdGenerator))]
public string Id { get; set; }
[BsonRequired]
[BsonElement("UserId")]
public string UserId { get; set; }
}
}
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
using Prospect.Server.Api.Services.Database.Generator;
namespace Prospect.Server.Api.Services.Database.Models
{
/// <summary>
/// master_player_account
/// </summary>
public class PlayFabUser
{
[BsonId(IdGenerator = typeof(PlayFabIdGenerator))]
public string Id { get; set; }
[BsonRequired]
[BsonElement("DisplayName")]
public string DisplayName { get; set; }
[BsonRequired]
[BsonElement("Auth")]
public List<PlayFabUserAuth> Auth { get; set; }
}
}
@@ -0,0 +1,13 @@
using MongoDB.Bson.Serialization.Attributes;
namespace Prospect.Server.Api.Services.Database.Models
{
public class PlayFabUserAuth
{
[BsonElement("Type")]
public PlayFabUserAuthType Type { get; set; }
[BsonElement("Key")]
public string Key { get; set; }
}
}
@@ -0,0 +1,8 @@
namespace Prospect.Server.Api.Services.Database.Models
{
public enum PlayFabUserAuthType
{
Epic,
Steam
}
}