добавлен фильтр сообщений по тексту

This commit is contained in:
Фролов 2021-08-09 15:09:46 +05:00
parent 9e69ff4ce7
commit a14e5134bf
3 changed files with 26 additions and 14 deletions

View File

@ -6,7 +6,7 @@ namespace AsbCloudApp.Services
{ {
public interface IMessageService public interface IMessageService
{ {
PaginationContainer<MessageDto> GetMessages(int idWell, IEnumerable<int> categoryids = null, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32); PaginationContainer<MessageDto> GetMessages(int idWell, IEnumerable<int> categoryids = null, DateTime begin = default, DateTime end = default, string searchString = default, int skip = 0, int take = 32);
DatesRangeDto GetMessagesDatesRange(int idWell); DatesRangeDto GetMessagesDatesRange(int idWell);
void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos); void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos);
} }

View File

@ -25,7 +25,14 @@ namespace AsbCloudInfrastructure.Services
cacheTUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db); cacheTUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
} }
public PaginationContainer<MessageDto> GetMessages(int idWell, IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32) public PaginationContainer<MessageDto> GetMessages(
int idWell,
IEnumerable<int> categoryids = default,
DateTime begin = default,
DateTime end = default,
string searchString = default,
int skip = 0,
int take = 32)
{ {
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell); var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
if (telemetryId is null) if (telemetryId is null)
@ -36,15 +43,17 @@ namespace AsbCloudInfrastructure.Services
if (!events.Any()) if (!events.Any())
return null; return null;
var messages = from m in db.Messages var messages = db.Messages.Where(m => m.IdTelemetry == telemetryId);
where m.IdTelemetry == telemetryId
select m;
if ((categoryids != default) && (categoryids.Any())) if((categoryids?.Any() == true) || !string.IsNullOrEmpty(searchString))
{ {
var eventIds = from e in events if (!string.IsNullOrEmpty(searchString))
where categoryids.ToList().Contains(e.IdCategory) events = events.Where(e => e.MessageTemplate.Contains(searchString, StringComparison.OrdinalIgnoreCase));
select e.IdEvent;
if (categoryids?.Any() == true)
events = events.Where(e => categoryids.ToList().Contains(e.IdCategory));
var eventIds = events.Select(e=> e.IdEvent);
if (!eventIds.Any()) if (!eventIds.Any())
return null; return null;
@ -52,8 +61,6 @@ namespace AsbCloudInfrastructure.Services
messages = messages.Where(m => eventIds.Contains(m.IdEvent)); messages = messages.Where(m => eventIds.Contains(m.IdEvent));
} }
var result = new PaginationContainer<MessageDto>() { Skip = skip, Take = take };
messages = messages.OrderByDescending(m => m.Date); messages = messages.OrderByDescending(m => m.Date);
if (begin != default) if (begin != default)
@ -62,7 +69,12 @@ namespace AsbCloudInfrastructure.Services
if (end != default) if (end != default)
messages = messages.Where(m => m.Date <= end); messages = messages.Where(m => m.Date <= end);
result.Count = messages.Count(); var result = new PaginationContainer<MessageDto>
{
Skip = skip,
Take = take,
Count = messages.Count()
};
if (skip > 0) if (skip > 0)
messages = messages.Skip(skip); messages = messages.Skip(skip);

View File

@ -32,7 +32,7 @@ namespace AsbCloudWebApi.Controllers
[HttpGet] [HttpGet]
[Route("{idWell}/message")] [Route("{idWell}/message")]
[ProducesResponseType(typeof(PaginationContainer<MessageDto>), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(PaginationContainer<MessageDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetMessage(int idWell, int skip = 0, int take = 32, [FromQuery] IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default) public IActionResult GetMessage(int idWell, int skip = 0, int take = 32, [FromQuery] IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, string searchString = default)
{ {
if (take > 1024) if (take > 1024)
return BadRequest("limit mast be less then 1024"); return BadRequest("limit mast be less then 1024");
@ -40,7 +40,7 @@ namespace AsbCloudWebApi.Controllers
if (begin > DateTime.Now) if (begin > DateTime.Now)
begin = default; begin = default;
var result = messageService.GetMessages(idWell, categoryids, begin, end, skip, take); var result = messageService.GetMessages(idWell, categoryids, begin, end, searchString, skip, take);
if (result is null || result.Count == 0) if (result is null || result.Count == 0)
return NoContent(); return NoContent();