Files
2025-05-01 07:20:41 +09:00

135 lines
3.8 KiB
C#

using MongoDB.Driver;
using ServerBase;
using ServerCore;
using BEACON_GUID = System.String;
using META_ID = System.UInt32;
using USER_GUID = System.String;
using ITEM_GUID = System.String;
namespace GameServer;
public class BeaconShopRepository : MongoDbRepository<BeaconShopMongoDoc>
{
private const string CollectionName = "BeaconShop";
public BeaconShopRepository(IMongoClient mongoClient, MongoDbConf settings) :
base(mongoClient, settings.DatabaseName, CollectionName)
{
}
public async Task<(Result, List<BeaconShopMongoDoc>?)> get(int tagId)
{
var result = new Result();
var err_msg = string.Empty;
var builder = Builders<BeaconShopMongoDoc>.Filter;
var filter = builder.Eq(x => x.TagId, tagId);
try
{
var findData = await m_collection.FindAsync(filter);
return (result, findData.ToList());
}
catch (Exception ex)
{
err_msg = $"MongoDB Write Exception Error: {ex.Message}";
result.setFail(ServerErrorCode.BeaconShopFailedGetBoardItem, err_msg);
return (result, null);
}
}
public async Task<Result> insert(BeaconShopMongoDoc beaconShopEntity)
{
var result = new Result();
var err_msg = string.Empty;
try
{
await m_collection.InsertOneAsync(beaconShopEntity);
}
catch (Exception ex)
{
err_msg = $"MongoDB Write Exception Error: {ex.Message}";
result.setFail(ServerErrorCode.BeaconShopFailedRegisterBoard, err_msg);
Log.getLogger().error(err_msg);
return result;
}
return result;
}
public async Task<(Result, BeaconShopMongoDoc?)> updateAmount(string id, int amount)
{
var result = new Result();
var err_msg = string.Empty;
try
{
var filter = Builders<BeaconShopMongoDoc>.Filter
.Eq(x => x.ItemGuid, id);
var update = Builders<BeaconShopMongoDoc>.Update
.Inc(x => x.Amount, amount);
var options = new FindOneAndUpdateOptions<BeaconShopMongoDoc>
{
ReturnDocument = ReturnDocument.After,
};
var updated = await m_collection.FindOneAndUpdateAsync(filter, update, options);
if (updated == null)
{
err_msg = $"MongoDB is already empty. Error";
result.setFail(ServerErrorCode.BeaconShopFailedToFindOrUpdate, err_msg);
return (result, null);
}
if (updated.Amount == 0)
{
return (await delete(id), updated);
}
return (result, updated);
}
catch (Exception ex)
{
err_msg = $"MongoDB Write Exception Error: {ex.Message}";
result.setFail(ServerErrorCode.BeaconShopDbException, err_msg);
return (result, null);
}
}
public async Task<Result> delete(ITEM_GUID item_guid)
{
var result = new Result();
var err_msg = string.Empty;
var builder = Builders<BeaconShopMongoDoc>.Filter;
var filter = builder.Eq(x => x.ItemGuid, item_guid);
try
{
var db_result = await m_collection.DeleteOneAsync(filter);
if (db_result.DeletedCount == 0)
{
result.setFail(ServerErrorCode.BeaconShopNotFoundItemFromBoard, err_msg);
return result;
}
}
catch (Exception ex)
{
err_msg = $"MongoDB Delete Exception Error : {ex.Message}";
result.setFail(ServerErrorCode.BeaconShopFailedDeleteBoard, err_msg);
return result;
}
return result;
}
}