Skip to content

Commit

Permalink
fix: incorrect projection of query from LINQ
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Nov 5, 2023
1 parent 84e83eb commit 60ae56f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions App/Modules/Cyan/Data/Repositories/PluginRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,19 @@ public async Task<Result<IEnumerable<PluginVersionPrincipal>>> GetAllVersion(IEn
: c.Or(x => x.Plugin.Name == r.Name && x.Plugin.User.Username == r.Username)
);

query = query.Where(predicate)
var all = await query.Where(predicate).ToArrayAsync();
var grouped = all
.GroupBy(x => new { x.Plugin.Name, x.Plugin.User.Username })
.Select(g => g.OrderByDescending(o => o.Version).First());
.Select(g => g.OrderByDescending(o => o.Version).First())
.ToArray();

var plugins = await query.Select(x => x.ToPrincipal()).ToArrayAsync();
var plugins = grouped.Select(x => x.ToPrincipal()).ToArray();

this._logger.LogInformation("Plugin References: {@PluginReferences}", plugins.Select(x => x.Id));

if (plugins.Length != pluginRefs.Length)
{
var found = await query.Select(x => $"{x.Plugin.User.Username}/{x.Plugin.Name}:{x.Version}").ToArrayAsync();
var found = grouped.Select(x => $"{x.Plugin.User.Username}/{x.Plugin.Name}:{x.Version}").ToArray();
var search = pluginRefs.Select(x => $"{x.Username}/{x.Name}:{x.Version}");
var notFound = search.Except(found);
return new MultipleEntityNotFound("Plugins not found", typeof(PluginPrincipal), notFound.ToArray(),
Expand Down
12 changes: 7 additions & 5 deletions App/Modules/Cyan/Data/Repositories/ProcessorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,22 +415,24 @@ public async Task<Result<IEnumerable<ProcessorVersionPrincipal>>> GetAllVersion(
: c.Or(x => x.Processor.Name == r.Name && x.Processor.User.Username == r.Username)
);

query = query.Where(predicate)
var all = await query.Where(predicate).ToArrayAsync();
var grouped = all
.GroupBy(x => new { x.Processor.Name, x.Processor.User.Username })
.Select(g => g.OrderByDescending(o => o.Version).First());
.Select(g => g.OrderByDescending(o => o.Version).First())
.ToArray();

var processors = await query.Select(x => x.ToPrincipal()).ToArrayAsync();
var processors = grouped.Select(x => x.ToPrincipal()).ToArray();
this._logger.LogInformation("Processor References: {@ProcessorReferences}", processors.Select(x => x.Id));

if (processors.Length != processorRefs.Length)
{
var found = await query.Select(x => $"{x.Processor.User.Username}/{x.Processor.Name}:{x.Version}")
.ToArrayAsync();
var found = grouped.Select(x => $"{x.Processor.User.Username}/{x.Processor.Name}:{x.Version}").ToArray();
var search = processorRefs.Select(x => $"{x.Username}/{x.Name}:{x.Version}");
var notFound = search.Except(found).ToArray();
return new MultipleEntityNotFound("Processors not found", typeof(ProcessorPrincipal), notFound, found)
.ToException();
}

return processors;
}
catch (Exception e)
Expand Down

0 comments on commit 60ae56f

Please sign in to comment.