public async Task <IActionResult> Get([FromQuery] LocationQueryFilter filter, [FromQuery] LocationQuerySort sort, [FromQuery] LocationQueryProjection projection, [FromQuery] LocationQueryPaging paging, [FromQuery] LocationQueryOptions options) { var validationResult = _service.ValidateGetLocations( User, filter, sort, projection, paging, options); if (!validationResult.Valid) { return(BadRequest(validationResult.Result)); } var result = await _service.QueryLocationDynamic( projection, options, filter, sort, paging); if (options.single_only) { if (result == null) { return(NotFound(new AppResultBuilder().NotFound())); } return(Ok(new AppResultBuilder().Success(result.SingleResult))); } return(Ok(new AppResultBuilder().Success(result))); }
public static DynamicSql SqlFilter( this DynamicSql query, LocationQueryFilter filter) { query = DynamicSql.DeepClone(query); var listFilters = new List <string>(); if (filter.id != null) { var paramName = query.AddAutoIncrParam(filter.id); listFilters.Add($"{nameof(Location)}.{nameof(Location.Id)}=@{paramName}"); } if (filter.name_contains != null) { var paramName = query.AddAutoIncrParam(filter.name_contains); listFilters.Add($"CHARINDEX(@{paramName}, {nameof(Location)}" + $".{nameof(Location.Name)}) > 0"); } if (filter.archived != 2) { var paramName = query.AddAutoIncrParam(filter.archived); listFilters.Add($"{nameof(Location)}.{nameof(Location.Archived)}=@{paramName}"); } if (listFilters.Any()) { var whereClause = "WHERE " + string.Join(" AND ", listFilters); query.DynamicForm = query.DynamicForm.Replace(DynamicSql.FILTER, whereClause); } return(query); }
public ValidationResult ValidateGetLocations( ClaimsPrincipal principal, LocationQueryFilter filter, LocationQuerySort sort, LocationQueryProjection projection, LocationQueryPaging paging, LocationQueryOptions options) { return(ValidationResult.Pass()); }
public PagedList <Location> GetLocations(LocationQueryFilter filters) { filters.PageNumber = filters.PageNumber == 0 ? _paginationOptions.DefaultPageNumber : filters.PageNumber; filters.PageSize = filters.PageSize == 0 ? _paginationOptions.DefaultPageSize : filters.PageSize; var locations = _unitOfWork.LocationRepository.GetAll(); if (filters.Name != null) { locations = locations.Where(x => x.Name.ToLower().Contains(filters.Name.ToLower())); } if (filters.IsRoom != null) { locations = locations.Where(x => x.IsRoom == filters.IsRoom); } var pagedLocations = PagedList <Location> .Create(locations, filters.PageNumber, filters.PageSize); return(pagedLocations); }
public IActionResult GetLocations([FromQuery] LocationQueryFilter filters) { var locations = _locationService.GetLocations(filters); var locationsDtos = _mapper.Map <IEnumerable <LocationDto> >(locations); var metadata = new Metadata { TotalCount = locations.TotalCount, PageSize = locations.PageSize, CurrentPage = locations.CurrentPage, TotalPages = locations.TotalPages, HasNextPage = locations.HasNextPage, HasPreviousPage = locations.HasPreviousPage }; var response = new ApiResponse <IEnumerable <LocationDto> >(locationsDtos) { Meta = metadata }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata)); return(Ok(response)); }
public Uri GetLocationPaginationUri(LocationQueryFilter filter, string actionUrl) { string baseUrl = $"{_baseUri}{actionUrl}"; return(new Uri(baseUrl)); }
public async Task <QueryResult <IDictionary <string, object> > > QueryLocationDynamic( LocationQueryProjection projection, LocationQueryOptions options, LocationQueryFilter filter = null, LocationQuerySort sort = null, LocationQueryPaging paging = null) { var conn = context.Database.GetDbConnection(); var openConn = conn.OpenAsync(); var query = LocationQuery.CreateDynamicSql(); #region General if (filter != null) { query = query.SqlFilter(filter); } query = query.SqlJoin(projection); DynamicSql countQuery = null; int?totalCount = null; Task <int> countTask = null; if (options.count_total) { countQuery = query.SqlCount("*"); } query = query.SqlProjectFields(projection); #endregion await openConn; if (!options.single_only) { #region List query if (sort != null) { query = query.SqlSort(sort); } if (paging != null && (!options.load_all || !LocationQueryOptions.IsLoadAllAllowed)) { query = query.SqlSelectPage(paging.page, paging.limit); } #endregion #region Count query if (options.count_total) { countTask = conn.ExecuteScalarAsync <int>( sql: countQuery.PreparedForm, param: countQuery.DynamicParameters); } #endregion } var queryResult = await conn.QueryAsync( sql : query.PreparedForm, types : query.GetTypesArr(), map : (objs) => ProcessMultiResults(query, objs), splitOn : string.Join(',', query.GetSplitOns()), param : query.DynamicParameters); if (options.single_only) { var single = queryResult.FirstOrDefault(); if (single == null) { return(null); } var singleResult = GetLocationDynamic(single, projection, options); return(new QueryResult <IDictionary <string, object> >() { SingleResult = singleResult }); } if (options.count_total) { totalCount = await countTask; } var result = GetLocationDynamic(queryResult, projection, options, totalCount); return(result); }