public async Task <IActionResult> Get(BlogParameters bLogParameters) { _logger.LogInformation("获取数据中..."); var blogList = await _blogRepository.GetBlogsByParams(bLogParameters); // throw new Exception("测试异常抛出"); var blogResources = _mapper.Map <IEnumerable <Blog>, IEnumerable <BlogResource> >(blogList); var previousPageLink = blogList.HasPrevious ? CreatePostUri(bLogParameters, PaginationResourceUriType.PreviousPage) : null; var nextPageLink = blogList.HasNext ? CreatePostUri(bLogParameters, PaginationResourceUriType.NextPage) : null; var meta = new { blogList.PageIndex, blogList.PageSize, blogList.PageCount, blogList.TotalItemsCount, previousPageLink, nextPageLink }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(meta, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() })); return(Ok(blogResources)); }
public async Task <IEnumerable <Blog> > GetAllBlogs(BlogParameters blogParameters, bool trackChanges) { return(await _context.Blogs .Skip((blogParameters.PageNumber - 1) *blogParameters.PageSize) .Take(blogParameters.PageSize) .ToListAsync()); }
private string CreatePostUri(BlogParameters parameters, PaginationResourceUriType uriType) { switch (uriType) { case PaginationResourceUriType.PreviousPage: var previousParameters = new { pageIndex = parameters.PageIndex - 1, pageSize = parameters.PageSize, orderBy = parameters.OrderBy, fields = parameters.Fields }; return(_urlHelper.Link("GetBlogs", previousParameters)); case PaginationResourceUriType.NextPage: var nextParameters = new { pageIndex = parameters.PageIndex + 1, pageSize = parameters.PageSize, orderBy = parameters.OrderBy, fields = parameters.Fields }; return(_urlHelper.Link("GetBlogs", nextParameters)); default: var currentParameters = new { pageIndex = parameters.PageIndex, pageSize = parameters.PageSize, orderBy = parameters.OrderBy, fields = parameters.Fields }; return(_urlHelper.Link("GetBlogs", currentParameters)); } }
public async Task <IActionResult> GetBlogs([FromQuery] BlogParameters blogParameters) { var dbblogs = await _repository.GetAllBlogs(blogParameters, false); var blogdtos = _mapper.Map <IEnumerable <BlogOutputDTO> >(dbblogs); return(Ok(blogdtos)); }
/// <summary> /// Overload to take the setup attributes and blog id so that tests can /// be ran against them without needing it scan for the blog controller /// </summary> /// <param name="instanceName">The name of the blog controller that inherits the base class</param> /// <param name="setupAttributes">The collection of attributes that setup the blog</param> /// <param name="seoAttributes">The collection of seo attributes for the blog</param> /// <param name="blogs">The dictionary to add the blog to once it is set up</param> /// <returns></returns> public Boolean Register(String instanceName, BlogSetupAttribute[] setupAttributes, BlogSEOAttribute[] seoAttributes, ref Dictionary <String, IBlog> blogs) { // Define the result as failed by default Boolean result = false; // Check to see if the blogs dictionary has been instantiated (is not from the startup routine) blogs = blogs ?? new Dictionary <string, IBlog>(); // If the base controller hasn't been set up yet the go gather the information it // needs to connect to the blogs that have been assigned for it to manage if (!blogs.ContainsKey(instanceName)) { try { // Get all of the blog setup attributes for this controller / blog combination if (setupAttributes.Length > 0) { // Get the blog Id from the calling class custom parameters String blogId = setupAttributes[0].BlogId; // Work out and create an instance of the correct data provider IBlogDataProvider blogDataProvider = (new BlogDataProviderFactory()).Get(setupAttributes[0].Provider); if (blogDataProvider != null) { blogDataProvider.ConnectionString = new BlogDataProviderConnectionString(setupAttributes[0].ProviderConnectionString); // Initialise the data provider blogDataProvider.Initialise(); // Get all of the blog setup attributes for this controller / blog combination BlogSEOSettings blogSEOSettings = (seoAttributes.Length > 0) ? seoAttributes[0].SEOSettings : new BlogSEOSettings() { }; // Construct the parameters for setting up the blog IBlogParameters blogParameters = new BlogParameters() { Id = blogId, Provider = blogDataProvider, SEOSettings = blogSEOSettings }; // Assign the instantiated blog class to the static array of blogs blogs[instanceName] = new Blog(blogParameters); // Success result = true; } } } catch (Exception ex) { // Tell the caller we could not initialise the blog controller throw BlogException.Passthrough(ex, new NotInitialisedBlogException(ex)); } } // Return if it was successful return(result); }