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 { private const string CollectionName = "BeaconShop"; public BeaconShopRepository(IMongoClient mongoClient, MongoDbConf settings) : base(mongoClient, settings.DatabaseName, CollectionName) { } public async Task<(Result, List?)> get(int tagId) { var result = new Result(); var err_msg = string.Empty; var builder = Builders.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 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.Filter .Eq(x => x.ItemGuid, id); var update = Builders.Update .Inc(x => x.Amount, amount); var options = new FindOneAndUpdateOptions { 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 delete(ITEM_GUID item_guid) { var result = new Result(); var err_msg = string.Empty; var builder = Builders.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; } }