초기커밋
This commit is contained in:
119
UGQDataAccess/Repository/AdminAccountRepository.cs
Normal file
119
UGQDataAccess/Repository/AdminAccountRepository.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Driver;
|
||||
using SharpCompress.Common;
|
||||
using UGQDatabase.Models;
|
||||
using UGQDataAccess.Settings;
|
||||
using ServerCommon.UGQ;
|
||||
using System.Drawing;
|
||||
using UGQDataAccess.Repository.Models;
|
||||
using UGQDataAccess.Repository.Query;
|
||||
|
||||
namespace UGQDataAccess.Repository;
|
||||
|
||||
|
||||
public class AdminAccountRepository : BaseRepository<AdminAccountEntity>
|
||||
{
|
||||
private const string CollectionName = "AdminAccount";
|
||||
|
||||
public AdminAccountRepository(IMongoClient mongoClient, IOptions<UGQDatabaseSettings> settings) :
|
||||
base(mongoClient, settings.Value.DatabaseName, CollectionName)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<AdminAccountEntity?> get(string username)
|
||||
{
|
||||
var filter = Builders<AdminAccountEntity>.Filter
|
||||
.Eq(x => x.Username, username);
|
||||
|
||||
return await Collection.Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<AdminAccountEntity> signup(string username, string password, UGQAccountRole role)
|
||||
{
|
||||
var filter = Builders<AdminAccountEntity>.Filter
|
||||
.Eq(x => x.Username, username);
|
||||
|
||||
var update = Builders<AdminAccountEntity>.Update
|
||||
.SetOnInsert(x => x.Username, username)
|
||||
.SetOnInsert(x => x.Password, password)
|
||||
.SetOnInsert(x => x.Role, role)
|
||||
.SetOnInsert(x => x.UpdatedAt, DateTime.UtcNow)
|
||||
.SetOnInsert(x => x.CreatedAt, DateTime.UtcNow);
|
||||
|
||||
var option = new FindOneAndUpdateOptions<AdminAccountEntity>
|
||||
{
|
||||
IsUpsert = true,
|
||||
ReturnDocument = ReturnDocument.After
|
||||
};
|
||||
|
||||
return await Collection.FindOneAndUpdateAsync(filter, update, option);
|
||||
}
|
||||
|
||||
public async Task<AdminAccountEntity> changePassword(string username, string newPassword)
|
||||
{
|
||||
var filter = Builders<AdminAccountEntity>.Filter
|
||||
.Eq(x => x.Username, username);
|
||||
|
||||
var update = Builders<AdminAccountEntity>.Update
|
||||
.Set(x => x.Password, newPassword)
|
||||
.Set(x => x.UpdatedAt, DateTime.UtcNow);
|
||||
|
||||
var option = new FindOneAndUpdateOptions<AdminAccountEntity>
|
||||
{
|
||||
IsUpsert = true,
|
||||
ReturnDocument = ReturnDocument.After
|
||||
};
|
||||
|
||||
return await Collection.FindOneAndUpdateAsync(filter, update, option);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<AdminAccountEntity?> saveRefreshToken(string id, string refreshToken, DateTime? refreshTokenExpryTime)
|
||||
{
|
||||
var filter = Builders<AdminAccountEntity>.Filter
|
||||
.Eq(x => x.Id, id);
|
||||
|
||||
var updateBuilder = Builders<AdminAccountEntity>.Update;
|
||||
var update = Builders<AdminAccountEntity>.Update
|
||||
.Set(x => x.UpdatedAt, DateTime.UtcNow)
|
||||
.Set(x => x.RefreshToken, refreshToken);
|
||||
|
||||
if (refreshTokenExpryTime != null)
|
||||
update = updateBuilder.Combine(update, updateBuilder.Set(x => x.RefreshTokenExpiryTime, refreshTokenExpryTime));
|
||||
|
||||
var options = new FindOneAndUpdateOptions<AdminAccountEntity>
|
||||
{
|
||||
ReturnDocument = ReturnDocument.After,
|
||||
};
|
||||
|
||||
var updated = await Collection.FindOneAndUpdateAsync(filter, update, options);
|
||||
return updated;
|
||||
}
|
||||
|
||||
public async Task<AdminAccountEntity?> deleteRefreshToken(string username)
|
||||
{
|
||||
var filter = Builders<AdminAccountEntity>.Filter
|
||||
.Eq(x => x.Username, username);
|
||||
|
||||
var updateBuilder = Builders<AdminAccountEntity>.Update;
|
||||
var update = Builders<AdminAccountEntity>.Update
|
||||
.Set(x => x.UpdatedAt, DateTime.UtcNow)
|
||||
.Set(x => x.RefreshToken, "")
|
||||
.Set(x => x.RefreshTokenExpiryTime, DateTime.Now);
|
||||
|
||||
var options = new FindOneAndUpdateOptions<AdminAccountEntity>
|
||||
{
|
||||
ReturnDocument = ReturnDocument.After,
|
||||
};
|
||||
|
||||
var updated = await Collection.FindOneAndUpdateAsync(filter, update, options);
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user