This commit is contained in:
2021-07-06 18:02:47 +00:00
parent 37e0089624
commit 47a9b5153c

View File

@@ -193,8 +193,9 @@ namespace AyaNova.Api.Controllers
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
List<NameIdItem> ret = new List<NameIdItem>();
if (searchParam.tags.Count == 0) //note: this error only applies to api users so not translated
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "tags", "tags are required"));
//build query
//select id,serial,customerid from aunit where aunit.tags @> array['red','blue'::varchar(255)] AND customerid=19
/*
@@ -214,22 +215,6 @@ namespace AyaNova.Api.Controllers
if (searchParam.restrictToCustomerId != null && searchParam.restrictToCustomerId != 0)
sbQuery.Append($" and customerid={searchParam.restrictToCustomerId}");
// else
// {
// //include customer
// //select id,serial,customerid from aunit where aunit.tags @> array['red','blue'::varchar(255)] AND customerid=19
// var unitz = await ct.Unit.AsNoTracking().Where(z => z.Active == true).Where(z => searchParam.tags.All(y => (z.Tags.Contains(y)))).Select(z => new { z.CustomerId, z.Serial }).ToListAsync();
// foreach (var u in unitz)
// {
// var cviz = await ct.Customer.AsNoTracking().Where(z => z.Id == u.CustomerId).Select(z => z.Name).FirstOrDefaultAsync();
// ret.Add(new NameIdItem { Name = $"{cviz} - {u.Serial}" });
// }
// return Ok(ApiOkResponse.Response(ret.OrderBy(z => z.Name)));
// }
List<InternalUnitListForSorting> slist = new List<InternalUnitListForSorting>();
using (var cmd = ct.Database.GetDbConnection().CreateCommand())
{
@@ -238,20 +223,17 @@ namespace AyaNova.Api.Controllers
using (var dr = await cmd.ExecuteReaderAsync())
{
while (dr.Read())
{
slist.Add(new InternalUnitListForSorting( dr.GetString(2), dr.GetString(1) , dr.GetInt64(0)));
}
{
slist.Add(new InternalUnitListForSorting(dr.GetString(2), dr.GetString(1), dr.GetInt64(0)));
}
}
}
var sorted=slist.OrderBy(z=>z.customername).ThenBy(z=>z.serial);
var sorted = slist.OrderBy(z => z.customername).ThenBy(z => z.serial);
ret.AddRange(sorted.Select(z=>new NameIdItem{Id=z.unitid, Name=$"{z.serial} - {z.customername}"}).ToList());
return Ok(ApiOkResponse.Response(ret.OrderBy(z => z.Name)));
return Ok(ApiOkResponse.Response(sorted.Select(z => new NameIdItem { Id = z.unitid, Name = $"{z.serial} {z.customername}" }).ToList()));
}
public record UnitListByTagParams(List<string> tags, long? restrictToCustomerId);
private record InternalUnitListForSorting(string customername, string serial, long unitid);
// public record UnitListByTagParams(char[,] tags, long? restrictToCustomerId);
//------------