Commit 0
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user