Files
gameauthapi/Services/PlayerService.cs
T
2025-05-08 09:29:20 -05:00

45 lines
1.3 KiB
C#

// Services/PlayerService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using GameAuthApi.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
namespace GameAuthApi.Services;
public class PlayerService
{
private readonly IMongoCollection<Player> _players;
public PlayerService(IConfiguration config)
{
var client = new MongoClient(config["MongoDB:ConnectionString"]);
var db = client.GetDatabase(config["MongoDB:DatabaseName"]);
_players = db.GetCollection<Player>("Players");
}
public async Task<Player> GetByUserIdAsync(string userId) =>
await _players.Find(p => p.UserId == userId).FirstOrDefaultAsync();
public async Task<List<Player>> GetByLocationIdAsync(string locationId)
{
return await _players.Find(p => p.LocationId == locationId).ToListAsync();
}
public async Task<Player> GetByIdAsync(string id) =>
await _players.Find(p => p.Id == id).FirstOrDefaultAsync();
public async Task CreateAsync(Player player) =>
await _players.InsertOneAsync(player);
public async Task UpdateAsync(Player player)
{
await _players.ReplaceOneAsync(p => p.Id == player.Id, player);
}
public async Task DeleteAsync(string id)
{
await _players.DeleteOneAsync(p => p.Id == id);
}
}