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

View File

@@ -193,7 +193,8 @@ namespace AyaNova.Api.Controllers
if (!ModelState.IsValid) if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState)); 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 //build query
//select id,serial,customerid from aunit where aunit.tags @> array['red','blue'::varchar(255)] AND customerid=19 //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) if (searchParam.restrictToCustomerId != null && searchParam.restrictToCustomerId != 0)
sbQuery.Append($" and customerid={searchParam.restrictToCustomerId}"); 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>(); List<InternalUnitListForSorting> slist = new List<InternalUnitListForSorting>();
using (var cmd = ct.Database.GetDbConnection().CreateCommand()) using (var cmd = ct.Database.GetDbConnection().CreateCommand())
{ {
@@ -239,19 +224,16 @@ namespace AyaNova.Api.Controllers
{ {
while (dr.Read()) 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(sorted.Select(z => new NameIdItem { Id = z.unitid, Name = $"{z.serial} {z.customername}" }).ToList()));
return Ok(ApiOkResponse.Response(ret.OrderBy(z => z.Name)));
} }
public record UnitListByTagParams(List<string> tags, long? restrictToCustomerId); public record UnitListByTagParams(List<string> tags, long? restrictToCustomerId);
private record InternalUnitListForSorting(string customername, string serial, long unitid); private record InternalUnitListForSorting(string customername, string serial, long unitid);
// public record UnitListByTagParams(char[,] tags, long? restrictToCustomerId);
//------------ //------------