Skip to content

Commit

Permalink
(maint) Update method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
jibedoubleve committed Jan 12, 2025
1 parent 4644aef commit 141e286
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
41 changes: 15 additions & 26 deletions src/Infra/Lanceur.Infra.SQLite/DbActions/AliasDbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ insert into alias (
const string sqlSynonyms = @"insert into alias_name (id_alias, name) values (@id, @name)";
foreach (var name in csv) tx.Connection.ExecuteScalar<long>(sqlSynonyms, new { id, name });

//Create additional arguments
CreateAdditionalParameters(tx, id, additionalParameters);

alias.Id = id;
Expand All @@ -175,7 +174,7 @@ internal void CreateInvisible(IDbTransaction tx, ref QueryResult alias)
alias.Id = Create(tx, ref queryResult);
}

internal AliasQueryResult GetByIdAndName(long id, string name, IDbTransaction tx)
internal AliasQueryResult GetByIdAndName(IDbTransaction tx, long id, string name)
{
if (id <= 0) throw new ArgumentException("The id of the alias should be greater than zero.");

Expand Down Expand Up @@ -216,15 +215,15 @@ order by
/// <summary>
/// Get the first alias with the exact name.
/// </summary>
/// <param name="name">The alias' exact name to find.</param>
/// <param name="tx">Transaction to use for the query</param>
/// <param name="name">The alias' exact name to find.</param>
/// <param name="includeHidden">Indicate whether include or not hidden aliases</param>
/// <returns>The exact match or <c>null</c> if not found.</returns>
/// <remarks>
/// For optimisation reason, there's no check of doubloons. Bear UI validates and
/// forbid to insert two aliases with same name.
/// </remarks>
internal AliasQueryResult GetExact(string name, IDbTransaction tx, bool includeHidden = false)
internal AliasQueryResult GetExact(IDbTransaction tx, string name, bool includeHidden = false)
{
const string sql = $"""
select
Expand Down Expand Up @@ -266,11 +265,11 @@ order by
/// . In case of multiple aliases with same name, the one with greater counter
/// is selected.
/// </summary>
/// <param name="names">The list of names to find.</param>
/// <param name="tx">The transaction to use for the query</param>
/// <param name="names">The list of names to find.</param>
/// <param name="includeHidden">Indicate whether include or not hidden aliases</param>
/// <returns>The exact match or <c>null</c> if not found.</returns>
internal IEnumerable<AliasQueryResult> GetExact(IEnumerable<string> names, IDbTransaction tx, bool includeHidden = false)
internal IEnumerable<AliasQueryResult> GetExact(IDbTransaction tx, IEnumerable<string> names, bool includeHidden = false)
{
const string sql = $"""
select
Expand Down Expand Up @@ -302,7 +301,7 @@ order by n.name
return tx.Connection!.Query<AliasQueryResult>(sql, arguments).ToArray();
}

internal AliasUsage GetHiddenAlias(string name, IDbTransaction tx)
internal AliasUsage GetHiddenAlias(IDbTransaction tx, string name)
{
const string sql = """
select
Expand All @@ -325,7 +324,7 @@ alias a
return result;
}

internal bool HasNames(AliasQueryResult alias, IDbTransaction tx)
internal bool HasNames(IDbTransaction tx, AliasQueryResult alias)
{
const string sql = """
select count(*)
Expand All @@ -339,7 +338,7 @@ where lower(name) = @name
return count > 0;
}

internal void LogicalRemove(AliasQueryResult alias, IDbTransaction tx)
internal void LogicalRemove(IDbTransaction tx, AliasQueryResult alias)
{
const string sql = """
update alias
Expand All @@ -351,11 +350,12 @@ update alias
tx.Connection!.Execute(sql, new { deleted_at = DateTime.Now, id = alias.Id });
}

internal void LogicalRemove(IEnumerable<AliasQueryResult> aliases, IDbTransaction tx)
internal void LogicalRemove(IDbTransaction tx, IEnumerable<AliasQueryResult> aliases)
{
foreach (var alias in aliases) LogicalRemove(alias, tx);
foreach (var alias in aliases) LogicalRemove(tx, alias);
}
internal void Remove(AliasQueryResult alias, IDbTransaction tx)

internal void Remove(IDbTransaction tx, AliasQueryResult alias)
{
if (alias == null) throw new ArgumentNullException(nameof(alias), "Cannot delete NULL alias.");

Expand All @@ -365,18 +365,7 @@ internal void Remove(AliasQueryResult alias, IDbTransaction tx)
ClearAlias(tx, alias.Id);
}

internal void RemoveMany(IEnumerable<AliasQueryResult> alias, IDbTransaction tx)
{
ArgumentNullException.ThrowIfNull(alias);
var ids = alias.Select(x => x.Id).ToArray();

ClearAliasUsage(tx, ids);
ClearAliasArgument(tx, ids);
ClearAliasName(tx, ids);
ClearAlias(tx, ids);
}

internal ExistingNameResponse SelectNames(string[] names, IDbTransaction tx)
internal ExistingNameResponse SelectNames(IDbTransaction tx, string[] names)
{
const string sql = """
select an.name
Expand All @@ -391,7 +380,7 @@ where an.name in @names
return new(result);
}

internal long Update(AliasQueryResult alias, IDbTransaction tx)
internal long Update(IDbTransaction tx, AliasQueryResult alias)
{
const string updateAliasSql = """
update alias
Expand Down Expand Up @@ -434,7 +423,7 @@ update alias
return alias.Id;
}

internal IEnumerable<long> UpdateThumbnails(IEnumerable<AliasQueryResult> aliases, IDbTransaction tx)
internal IEnumerable<long> UpdateThumbnails(IDbTransaction tx, IEnumerable<AliasQueryResult> aliases)
{
const string sql = "update alias set thumbnail = @thumbnail where id = @id";
var ids = new List<long>();
Expand Down
13 changes: 11 additions & 2 deletions src/Infra/Lanceur.Infra.SQLite/DbActions/AliasSaveDbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ namespace Lanceur.Infra.SQLite.DbActions;

public class AliasSaveDbAction
{
#region Fields

private readonly IDbActionFactory _dbActionFactory;
private readonly ILogger<AliasSaveDbAction> _logger;

#endregion

#region Constructors

public AliasSaveDbAction(IDbActionFactory dbActionFactory, ILoggerFactory factory)
{
_dbActionFactory = dbActionFactory;
_logger = factory.GetLogger<AliasSaveDbAction>();
}

#endregion

#region Methods

public void SaveOrUpdate(IDbTransaction tx, ref AliasQueryResult alias)
Expand All @@ -31,14 +40,14 @@ public void SaveOrUpdate(IDbTransaction tx, ref AliasQueryResult alias)

switch (alias.Id)
{
case 0 when !action.HasNames(alias, tx):
case 0 when !action.HasNames(tx, alias):
action.Create(tx, ref alias);
_logger.LogInformation("Created new alias {AliasName}", alias.Name);
break;

case > 0:
_logger.LogInformation("Updating alias {AliasName}", alias.Name);
action.Update(alias, tx);
action.Update(tx, alias);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Infra/Lanceur.Infra.SQLite/DbActions/MacroDbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private AliasQueryResult Hydrate(IDbTransaction tx, AliasQueryResult item)
var subAliases = new List<AliasQueryResult>();

var names = item?.Parameters?.Split("@") ?? [];
var aliases = action.GetExact(names, tx).ToArray();
var aliases = action.GetExact(tx, names).ToArray();

var delay = 0;
foreach (var name in names)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal void SetUsage(IDbTransaction tx, ref QueryResult alias)
var aliasDbAction = _dbActionFactory.AliasManagement;
if (alias.Id == 0)
{
if (aliasDbAction.GetExact(alias?.Name, tx, true) is { } a)
if (aliasDbAction.GetExact(tx, alias?.Name, true) is { } a)
alias!.Id = a.Id;
else
aliasDbAction.CreateInvisible(tx, ref alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ deleted_at is null
}

/// <inheritdoc />
public AliasQueryResult GetByIdAndName(long id, string name) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.GetByIdAndName(id, name, tx));
public AliasQueryResult GetByIdAndName(long id, string name) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.GetByIdAndName(tx, id, name));

/// <inheritdoc />
public IEnumerable<SelectableAliasQueryResult> GetDeletedAlias()
Expand Down Expand Up @@ -186,7 +186,7 @@ order by file_name
}

/// <inheritdoc />
public AliasQueryResult GetExact(string name) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.GetExact(name, tx));
public AliasQueryResult GetExact(string name) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.GetExact(tx, name));

/// <inheritdoc />
public IEnumerable<string> GetExistingAliases(IEnumerable<string> aliasesToCheck, long idAlias)
Expand Down Expand Up @@ -322,15 +322,15 @@ group by
}

/// <inheritdoc />
public void Remove(AliasQueryResult alias) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.LogicalRemove(alias, tx));
public void Remove(AliasQueryResult alias) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.LogicalRemove(tx, alias));

/// <inheritdoc />
public void Remove(IEnumerable<AliasQueryResult> aliases) => Db.WithinTransaction(
tx =>
{
var list = aliases as AliasQueryResult[] ?? aliases.ToArray();
_logger.LogInformation("Removing {Length} alias(es)", list.Length);
_dbActionFactory.AliasManagement.LogicalRemove(list, tx);
_dbActionFactory.AliasManagement.LogicalRemove(tx, list);
}
);

Expand Down Expand Up @@ -358,14 +358,14 @@ update alias
public IEnumerable<AliasQueryResult> SearchAliasWithAdditionalParameters(string criteria) => Db.WithinTransaction(tx => _dbActionFactory.SearchManagement.SearchAliasWithAdditionalParameters(tx, criteria));

/// <inheritdoc />
public ExistingNameResponse SelectNames(string[] names) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.SelectNames(names, tx));
public ExistingNameResponse SelectNames(string[] names) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.SelectNames(tx, names));

/// <inheritdoc />
public void SetHiddenAliasUsage(QueryResult alias)
{
ArgumentNullException.ThrowIfNull(alias);

var usage = Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.GetHiddenAlias(alias.Name, tx));
var usage = Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.GetHiddenAlias(tx, alias.Name));
if (usage is null) return;

alias.Id = usage.Id;
Expand Down Expand Up @@ -396,7 +396,7 @@ public void SetUsage(QueryResult alias) => Db.WithinTransaction(
public void Update(IEnumerable<AliasQueryResult> aliases) => Db.WithinTransaction(_dbActionFactory.SaveManagement.Update, aliases);

/// <inheritdoc />
public void UpdateThumbnails(IEnumerable<AliasQueryResult> aliases) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.UpdateThumbnails(aliases, tx));
public void UpdateThumbnails(IEnumerable<AliasQueryResult> aliases) => Db.WithinTransaction(tx => _dbActionFactory.AliasManagement.UpdateThumbnails(tx, aliases));

#endregion
}
10 changes: 5 additions & 5 deletions src/Tests/Lanceur.Tests/BusinessLogic/AliasManagementShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void CreateAliasInvisibleUsingService()
c.WithinTransaction(tx => action.CreateInvisible(tx, ref alias1));

// ACT
var sut = c.WithinTransaction(tx => action.GetExact(aliasName, tx, true));
var sut = c.WithinTransaction(tx => action.GetExact(tx, aliasName, true));

//ASSERT
sut.Should().NotBeNull();
Expand Down Expand Up @@ -267,7 +267,7 @@ public void FindExact()
var c = new DbSingleConnectionManager(connection);

// ACT
var found = c.WithinTransaction(tx => action.GetExact("noname", tx));
var found = c.WithinTransaction(tx => action.GetExact(tx, "noname"));

// ASSERT
using (new AssertionScope())
Expand All @@ -293,7 +293,7 @@ public void FindNoExact()
var c = new DbSingleConnectionManager(connection);

// ACT
var found = c.WithinTransaction(tx => action.GetExact("nothing", tx));
var found = c.WithinTransaction(tx => action.GetExact(tx, "nothing"));

// ASSERT
using (new AssertionScope()) { found.Should().BeNull(); }
Expand All @@ -314,7 +314,7 @@ public void RemoveAlias()
tx =>
{
action.Create(tx, ref alias);
action.Remove(alias, tx);
action.Remove(tx, alias);
}
);

Expand Down Expand Up @@ -373,7 +373,7 @@ public void RemoveAliasWithDbActionSql()
var action = BuildAliasDbAction(connection);

// ACT
c.WithinTransaction(tx => action.Remove(new() { Id = 256 }, tx));
c.WithinTransaction(tx => action.Remove(tx, new() { Id = 256 }));

using (new AssertionScope())
{
Expand Down

0 comments on commit 141e286

Please sign in to comment.