public async Task <WordListGetResult> GetWordList(WordListGet request) { await wordProviderValidator.ValidateListGetModel(request); var query = from word in context.Words join cat in context.Categories on word.CategoryId equals cat.Id into categories from category in categories.DefaultIfEmpty() where word.LanguageId == request.LanguageId && (request.CategoryId == null || word.CategoryId == request.CategoryId || (request.CategoryId == 0 && word.CategoryId == null)) && (request.Value == null || request.Value == "" || word.Value.Contains(request.Value) || request.Value.Contains(word.Value)) select new WordModel { Id = word.Id, Value = word.Value, CategoryId = word.CategoryId, Category = category != null ? category.Name : null }; var count = await query.CountAsync(); if (request.OrderByDesc) { query = query.OrderByDescending(x => x.Value); } else { query = query.OrderBy(x => x.Value); } if (request.PageSize > 0) { var skipValue = (request.Page - 1) * request.PageSize; query = query.Skip(skipValue).Take(request.PageSize); } var words = await query.ToListAsync(); return(new WordListGetResult() { Count = count, Page = request.Page, PageCount = (int)Math.Ceiling((decimal)count / (decimal)request.PageSize), WordList = words }); }
public async Task ValidateListGetModel(WordListGet model) { if (model == null) { throw new ValidationFailedException("No query data provided"); } if (model.LanguageId == 0) { throw new ValidationFailedException("Must specify language"); } if (model.Page <= 0) { throw new ValidationFailedException("Page must be greater than zero"); } if (model.PageSize <= 0) { throw new ValidationFailedException("Page size must be greater than zero"); } }
public async Task <WordListGetResult> GetWordList([FromBody] WordListGet request) { return(await wordProvider.GetWordList(request)); }