Files
promiscuity/microservices/AuthApi/Services/UserService.cs
T
admin 18c3d6fc5d
Deploy Promiscuity Auth API / deploy (push) Successful in 1m28s
Deploy Promiscuity Character API / deploy (push) Successful in 1m24s
Deploy Promiscuity Crafting API / deploy (push) Successful in 1m20s
Deploy Promiscuity Inventory API / deploy (push) Successful in 1m24s
Deploy Promiscuity Locations API / deploy (push) Successful in 1m25s
Deploy Promiscuity Mail API / deploy (push) Successful in 1m20s
Deploy Promiscuity World API / deploy (push) Successful in 1m25s
k8s smoke test / test (push) Successful in 20s
Add self-service profile and account deletion APIs
2026-07-19 13:39:24 -05:00

41 lines
1.4 KiB
C#

using AuthApi.Models;
using MongoDB.Driver;
namespace AuthApi.Services;
public class UserService
{
private readonly IMongoCollection<User> _col;
public UserService(IConfiguration cfg)
{
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
var client = new MongoClient(cs);
var db = client.GetDatabase(dbName);
_col = db.GetCollection<User>("Users");
var keys = Builders<User>.IndexKeys.Ascending(u => u.Username);
_col.Indexes.CreateOne(new CreateIndexModel<User>(keys, new CreateIndexOptions { Unique = true }));
}
public async Task<User?> GetByUsernameAsync(string username) =>
await _col.Find(u => u.Username == username).FirstOrDefaultAsync();
public async Task<User?> GetByIdAsync(string id) =>
await _col.Find(u => u.Id == id).FirstOrDefaultAsync();
public async Task<User?> GetByEmailAsync(string email) =>
await _col.Find(u => u.Email == email).FirstOrDefaultAsync();
public Task CreateAsync(User user) => _col.InsertOneAsync(user);
public Task UpdateAsync(User user) =>
_col.ReplaceOneAsync(u => u.Id == user.Id, user);
public Task DeleteAsync(string id) => _col.DeleteOneAsync(u => u.Id == id);
public Task<List<User>> GetAllAsync() =>
_col.Find(FilterDefinition<User>.Empty).ToListAsync();
}