using DbController; using System.Globalization; using System.Text; using Tabletop.Core.Filters; using Tabletop.Core.Models; namespace Tabletop.Core.Services { public class GamemodeService : IModelService { public async Task CreateAsync(Gamemode input, IDbController dbController, CancellationToken cancellationToken = default) { string sql = $@"INSERT INTO Gamemodes ( ) VALUES ( ); {dbController.GetLastIdSql()}"; input.GamemodeId = await dbController.GetFirstAsync(sql, input.GetParameters(), cancellationToken); foreach (var description in input.Description) { sql = @"INSERT INTO GamemodeDescription ( GamemodeId, Code, Name, Description, Mechanic ) VALUES ( @GAMEMODE_ID, @CODE, @NAME, @DESCRIPTION, @MECHANIC )"; var parameters = new { GAMEMODE_ID = input.GamemodeId, CODE = description.Code, NAME = description.Name, DESCRIPTION = description.Description }; await dbController.QueryAsync(sql, parameters, cancellationToken); } } public async Task DeleteAsync(Gamemode input, IDbController dbController, CancellationToken cancellationToken = default) { string sql = "DELETE FROM Gamemodes WHERE GamemodeId = @GAMEMODE_ID"; await dbController.QueryAsync(sql, new { GAMEMODE_ID = input.GamemodeId }, cancellationToken); } public async Task GetAsync(int gamemodeId, IDbController dbController, CancellationToken cancellationToken = default) { string sql = @"SELECT * FROM Gamemodes WHERE GamemodeId = @GAMEMODE_ID"; var gamemode = await dbController.GetFirstAsync(sql, new { GAMEMODE_ID = gamemodeId }, cancellationToken); return gamemode; } public static async Task> GetAllAsync(IDbController dbController, CancellationToken cancellationToken = default) { string sql = "SELECT * FROM Gamemodes"; var list = await dbController.SelectDataAsync(sql, cancellationToken: cancellationToken); await LoadGamemodeDescriptionsAsync(list, dbController, cancellationToken); return list; } public async Task UpdateAsync(Gamemode input, IDbController dbController, CancellationToken cancellationToken = default) { string sql; //string sql = @"UPDATE Gamemodes SET // Image = @IMAGE // WHERE GamemodeId = @GAMEMODE_ID"; //await dbController.QueryAsync(sql, input.GetParameters(), cancellationToken); foreach (var description in input.Description) { sql = @"UPDATE GamemodeDescription SET Name = @NAME, Description = @DESCRIPTION, Mechanic = @MECHANIC WHERE GamemodeId = @GAMEMODE_ID AND Code = @CODE"; var parameters = new { GAMEMODE_ID = input.GamemodeId, CODE = description.Code, NAME = description.Name, DESCRIPTION = description.Description, MECHANIC = description.Mechanic }; await dbController.QueryAsync(sql, parameters, cancellationToken); } } public async Task> GetAsync(GamemodeFilter filter, IDbController dbController, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); StringBuilder sqlBuilder = new(); sqlBuilder.AppendLine("SELECT gd.Name, g.* " + "FROM GamemodeDescription gd " + "INNER JOIN Gamemodes g " + "ON (g.GamemodeId = gd.GamemodeId) " + "WHERE 1 = 1"); sqlBuilder.AppendLine(GetFilterWhere(filter)); sqlBuilder.AppendLine(@" AND Code = @CULTURE"); sqlBuilder.AppendLine(@$" ORDER BY Name ASC "); sqlBuilder.AppendLine(dbController.GetPaginationSyntax(filter.PageNumber, filter.Limit)); // Zum Debuggen schreiben wir den Wert einmal als Variabel string sql = sqlBuilder.ToString(); List list = await dbController.SelectDataAsync(sql, GetFilterParameter(filter), cancellationToken); await LoadGamemodeDescriptionsAsync(list, dbController, cancellationToken); return list; } public async Task GetTotalAsync(GamemodeFilter filter, IDbController dbController, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); StringBuilder sqlBuilder = new(); sqlBuilder.AppendLine("SELECT COUNT(*) AS record_count FROM GamemodeDescription WHERE 1 = 1 "); sqlBuilder.AppendLine(GetFilterWhere(filter)); sqlBuilder.AppendLine(@" AND Code = @CULTURE"); string sql = sqlBuilder.ToString(); int result = await dbController.GetFirstAsync(sql, GetFilterParameter(filter), cancellationToken); return result; } public string GetFilterWhere(GamemodeFilter filter) { StringBuilder sqlBuilder = new(); if (!string.IsNullOrWhiteSpace(filter.SearchPhrase)) { sqlBuilder.AppendLine(@" AND (UPPER(Name) LIKE @SEARCHPHRASE)"); } string sql = sqlBuilder.ToString(); return sql; } public Dictionary GetFilterParameter(GamemodeFilter filter) { return new Dictionary { { "SEARCHPHRASE", $"%{filter.SearchPhrase}%" }, { "CULTURE", CultureInfo.CurrentCulture.Name } }; } private static async Task LoadGamemodeDescriptionsAsync(List list, IDbController dbController, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (list.Count != 0) { IEnumerable gamemodeIds = list.Select(x => x.GamemodeId); string sql = $"SELECT * FROM GamemodeDescription WHERE GamemodeId IN ({string.Join(",", gamemodeIds)})"; List descriptions = await dbController.SelectDataAsync(sql, null, cancellationToken); foreach (var gamemode in list) { gamemode.Description = [.. descriptions.Where(x => x.GamemodeId == gamemode.GamemodeId)]; } } } } }