public async Task <IEnumerable <MessageDto> > GetMessagesAsync(MessageSearchFilterDto filter) { return(await this.dbContext .Messages .Where(x => x.StartDate <= DateTime.UtcNow && (x.EndDate >= DateTime.UtcNow || x.EndDate == null)) .FilterMessages(filter) .ProjectTo <MessageDto>(this.mapper.ConfigurationProvider) .OrderBy(x => x.StartDate) .ThenBy(x => x.EndDate) .ToListAsync()); }
public static IQueryable <Message> FilterMessages(this IQueryable <Message> messages, MessageSearchFilterDto filter) { if (!string.IsNullOrEmpty(filter.Content)) { messages = messages.Where(x => x.Content.Contains(filter.Content)); } if (!string.IsNullOrEmpty(filter.StartDate.ToString())) { messages = messages.Where(x => x.StartDate == filter.StartDate); } if (!string.IsNullOrEmpty(filter.EndDate.ToString())) { messages = messages.Where(x => x.EndDate == filter.EndDate); } if (filter.Status.HasValue) { messages = messages.Where(x => x.Status == filter.Status.Value); } return(messages); }
public async Task <IEnumerable <MessageDto> > GetMessages([FromQuery] MessageSearchFilterDto filter) { return(await this.messageService.GetMessagesAsync(filter)); }