Skip to content

Commit

Permalink
(#77): Manage synonyms for alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jibedoubleve committed Jun 19, 2023
1 parent a5c9666 commit 3dcc7ea
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 452 deletions.
43 changes: 17 additions & 26 deletions src/Lanceur.Core/Models/AliasQueryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,19 @@ public static class AliasQueryResultMixin
{
#region Methods

public static AliasQueryResult Duplicate(this AliasQueryResult @this)

public static string[] GetNames(this AliasQueryResult @this)
{
return new AliasQueryResult
{
Parameters = @this.Parameters,
Count = @this.Count,
FileName = @this.FileName,
Icon = @this.Icon,
Notes = @this.Notes,
Name = $"Duplicate of {@this.Name}",
RunAs = @this.RunAs,
WorkingDirectory = @this.WorkingDirectory,
StartMode = @this.StartMode,
Query = @this.Query,
// In case it's already a duplicate of a duplicate,
// go recursively all the way down to the original
DuplicateOf = @this.DuplicateOf ?? @this.Id
};
if (@this is null) { return Array.Empty<string>(); }
if (@this.Synonyms is null) { return Array.Empty<string>(); }

return
@this.Synonyms
.Split(",")
.Select(n => n.Trim())
.ToArray();
}

public static bool HasChangedName(this AliasQueryResult @this) => @this.Name?.ToLower() != @this.OldName.ToLower();

#endregion Methods
}

Expand All @@ -54,12 +45,6 @@ public override string Description
set => Notes = value;
}

/// <summary>
/// If this <see cref="AliasQueryResult"/> is a ducplicate from another
/// alias, this contains its ID. Otherwise contains <c>NULL</c>
/// </summary>
public long? DuplicateOf { get; set; }

public string FileName
{
get => _fileName;
Expand All @@ -82,13 +67,19 @@ public override bool IsElevated

public StartMode StartMode { get; set; } = StartMode.Default;

/// <summary>
/// Get or set a string representing all the name this alias has.
/// It should be a coma separated list of names.
/// </summary>
public string Synonyms { get; set; }

public string WorkingDirectory { get; set; }

#endregion Properties

#region Methods

public static AliasQueryResult FromName(string aliasName) => new AliasQueryResult() { Name = aliasName };
public static AliasQueryResult FromName(string aliasName) => new AliasQueryResult() { Name = aliasName, Synonyms = aliasName };

#endregion Methods
}
Expand Down
33 changes: 33 additions & 0 deletions src/Lanceur.Core/Models/ExistingNameResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Lanceur.Core.Models
{
/// <summary>
/// Represents the response when checks whether the names specified is alias names
/// </summary>
public record ExistingNameResponse
{
#region Constructors

public ExistingNameResponse(string[] existingNames)
{
ExistingNames = existingNames is null
? Array.Empty<string>()
: existingNames;
}

#endregion Constructors

#region Properties

/// <summary>
/// List of existing names. Empty if no names exists
/// </summary>
public string[] ExistingNames { get; }

/// <summary>
/// <c>True</c> if at least one of the names to check already exist; otherwise <c>False</c>
/// </summary>
public bool Exists => ExistingNames?.Any() ?? false;

#endregion Properties
}
}
22 changes: 3 additions & 19 deletions src/Lanceur.Core/Models/QueryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public abstract class QueryResult : ObservableModel
{
#region Fields

private string _name = string.Empty;
private string _oldName;

private object _thumbnail;

#endregion Fields
Expand All @@ -30,6 +27,8 @@ public abstract class QueryResult : ObservableModel
public int Count { get; set; } = 0;
public virtual string Description { get; set; }

public string Name { get; set; }

/// <summary>
/// Fall back for <see cref="Thumbnail"/>. This property is expected to
/// contain information to display an icon (or whatever) in the UI
Expand All @@ -48,21 +47,6 @@ public abstract class QueryResult : ObservableModel
/// </summary>
public virtual bool IsResult => true;

public string Name
{
get => _name;
set
{
// First time you set the name of the QueryResult, it'll save
// the value. Now, OldName will have the value of the first time
// it was set.
if (value != null && _oldName == null) { _oldName = value.ToLower(); }
Set(ref _name, value);
}
}

public string OldName => _oldName;

public Cmdline Query { get; set; } = Cmdline.Empty;

/// <summary>
Expand All @@ -82,7 +66,7 @@ public object Thumbnail

#region Methods

public override int GetHashCode() => (Count, Description, Icon, Id, IsResult, Name, OldName, Query, Thumbnail).GetHashCode();
public override int GetHashCode() => (Count, Description, Icon, Id, IsResult, Name, Query, Thumbnail).GetHashCode();

public virtual string ToQuery() => $"{Name}";

Expand Down
10 changes: 9 additions & 1 deletion src/Lanceur.Core/Repositories/IDbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public interface IDbRepository
{
#region Methods

/// <summary>
/// Checks whether the specified names exists in the database for the specified session
/// </summary>
/// <param name="names"></param>
/// <param name="idSession"></param>
/// <returns></returns>
public ExistingNameResponse CheckNamesExist(string[] names, long? idSession = null);

/// <summary>
/// Get all the aliases
/// </summary>
Expand Down Expand Up @@ -66,7 +74,7 @@ public interface IDbRepository
IEnumerable<DataPoint<DateTime, double>> GetUsage(Per per, long? idSession = null);

/// <summary>
/// Hydrate the macro with its <c>id</c> and <c>count</c>. This method will try to find the
/// Hydrate the macro with its <c>id</c> and <c>count</c>. This method will try to find the
/// macro by using its name that should be something like '@it_s_name@'
/// </summary>
/// <param name="alias">Macro to hydrate</param>
Expand Down
Loading

0 comments on commit 3dcc7ea

Please sign in to comment.