Skip to content

Commit

Permalink
More updates to queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlrhodes committed Mar 28, 2017
1 parent a21c500 commit eb0f3ed
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/blog/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async Task<IActionResult> EditFeature(FeatureViewModel model)
public async Task<IActionResult> ManageFeatures()
{
AdminManageFeaturesViewModel amfvm = new AdminManageFeaturesViewModel();
amfvm.Features = await _context.Features.ToListAsync();
amfvm.Features = await _context.Features.AsNoTracking().ToListAsync();
return View(amfvm);
}

Expand Down Expand Up @@ -159,7 +159,7 @@ public async Task<IActionResult> EditCategory(CategoryViewModel model)
public async Task<IActionResult> ManageCategories()
{
AdminManageCategoriesViewModel amcvm = new AdminManageCategoriesViewModel();
amcvm.Categories = await _context.Categories.ToListAsync();
amcvm.Categories = await _context.Categories.AsNoTracking().ToListAsync();
return View(amcvm);
}

Expand All @@ -176,13 +176,13 @@ public async Task<IActionResult> DeleteCategory(long id)
public async Task<IActionResult> ManagePosts()
{
AdminManagePostsViewModel mpvm = new AdminManagePostsViewModel();
mpvm.posts = await _context.BlogPosts.Include(a => a.Author).ToListAsync();
mpvm.posts = await _context.BlogPosts.AsNoTracking().Include(a => a.Author).ToListAsync();
return View(mpvm);
}

public async Task<IActionResult> ManageUsers()
{
return View(await _userManager.Users.Include(u => u.Logins).Include(u => u.Author).ToListAsync());
return View(await _userManager.Users.AsNoTracking().Include(u => u.Logins).Include(u => u.Author).ToListAsync());
}

public ViewResult CreateUser() => View();
Expand Down Expand Up @@ -270,7 +270,7 @@ public async Task<IActionResult> DeleteUser(string id)

public IActionResult EditUser(string id)
{
ApplicationUser user = _userManager.Users.Where(u => u.Id == id).Include(u => u.Author).Include(u => u.Logins).FirstOrDefault();
ApplicationUser user = _userManager.Users.AsNoTracking().Where(u => u.Id == id).Include(u => u.Author).Include(u => u.Logins).FirstOrDefault();

if (user != null)
{
Expand Down
13 changes: 10 additions & 3 deletions src/blog/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public HomeController(BlogDbContext context)
public async Task<IActionResult> Index()
{
var recentPosts = await _context.BlogPosts
.AsNoTracking()
.Where(bp => bp.Public == true && bp.PublishOn < DateTime.Now)
.OrderByDescending(bp => bp.ModifiedAt)
.ThenByDescending(bp => bp.PublishOn)
Expand All @@ -49,6 +50,7 @@ public async Task<IActionResult> Search(string searchText)
var search = "\"*" + searchText + "*\"";

var posts = await _context.BlogPosts
.AsNoTracking()
.FromSql("SELECT * from [dbo].[BlogPost] WHERE Contains((Content, Description, Title), {0})", search)
.Where(bp => bp.Public == true && bp.PublishOn < DateTime.Now)
.OrderByDescending(bp => bp.ModifiedAt)
Expand Down Expand Up @@ -97,6 +99,7 @@ public async Task<IActionResult> CategoryPosts(long? id)
if (category != null)
{
var bpcs = await _context.BlogPostCategory
.AsNoTracking()
.Where(bp => bp.CategoryId == category.Id && bp.BlogPost.Public == true && bp.BlogPost.PublishOn < DateTime.Now)
.Include(bpc => bpc.BlogPost)
.ThenInclude(bp => bp.Author)
Expand All @@ -116,13 +119,14 @@ public async Task<IActionResult> AllPosts(int? sortby = 1)
// Sort by Category
if (sortby == 1)
{
var categories = await _context.Categories.ToListAsync();
var categories = await _context.Categories.AsNoTracking().ToListAsync();

var viewCategories = new List<ViewPostsByCategory>();

foreach (var c in categories)
{
var bpcs = await _context.BlogPostCategory
.AsNoTracking()
.Where(bp => bp.CategoryId == c.Id && bp.BlogPost.Public == true && bp.BlogPost.PublishOn < DateTime.Now)
.Include(bpc => bpc.BlogPost)
.ThenInclude(bp => bp.Author)
Expand Down Expand Up @@ -151,6 +155,7 @@ public async Task<IActionResult> AllPosts(int? sortby = 1)
vm = new ViewAllPostsViewModel()
{
PostsByDate = await _context.BlogPosts
.AsNoTracking()
.Where(bp => bp.Public == true && bp.PublishOn < DateTime.Now)
.Include(bp => bp.Author)
.OrderByDescending(bp => bp.ModifiedAt)
Expand All @@ -173,6 +178,7 @@ public async Task<IActionResult> FeaturePosts(long id)
}

var bpfs = await _context.BlogPostFeature
.AsNoTracking()
.Where(bpf => bpf.FeatureId == feature.Id && bpf.BlogPost.Public == true && bpf.BlogPost.PublishOn < DateTime.Now)
.Include(bpf => bpf.BlogPost)
.ThenInclude(bp => bp.Author)
Expand Down Expand Up @@ -216,15 +222,15 @@ public async Task<IActionResult> Features()
// cant select new into a defined type so have to use anon type for the select new here due to EF Core bug
// code after is a work-around

var x = from feature in _context.Features
var x = from feature in _context.Features.AsNoTracking()
select new
{
Title = feature.Title,
Description = feature.Description,
Id = feature.Id,
BlogCount = (from posts in _context.BlogPostFeature
where posts.FeatureId == feature.Id && posts.BlogPost.Public == true && posts.BlogPost.PublishOn < DateTime.Now
select posts).Count()
select posts).AsNoTracking().Count()
};

List<FeatureViewModel> featureList = new List<FeatureViewModel>();
Expand Down Expand Up @@ -257,6 +263,7 @@ public async Task<IActionResult> BlogPost(long? id)
return NotFound();

var post = await _context.BlogPosts
.AsNoTracking()
.Include(bp => bp.Author)
.Include(bp => bp.BlogPostCategory)
.ThenInclude(bpc => bpc.Category)
Expand Down
74 changes: 39 additions & 35 deletions src/blog/Views/Author/ManagePosts.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@* Author Manage Posts Page!!!*@
@{
@using blog.Models.ViewModels.Author
@model AuthorManagePostsViewModel
@using blog.Models.ViewModels.Author
@model AuthorManagePostsViewModel
ViewData["Title"] = "Manage Posts";
Layout = "_Layout";
}
Expand All @@ -10,39 +10,43 @@

<div class="container-fluid">
<div class="col-md-12">
<h2><span class="label label-primary">Manage Posts</span></h2><br />
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Description</th>
<th>Publish Date</th>
<th>Is Public?</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var post in Model.posts)
{
var aid = $"a{post.Id}";
<tr id="@post.Id">
<td>@post.Id</td>
<td>@post.Title</td>
<td>@post.Description</td>
<td>@post.PublishOn</td>
<td>@post.Public</td>
<td>
<span class="text-nowrap">
<a class="btn btn-primary btn-sm" asp-controller="Home" asp-action="blogpost" asp-route-id="@post.Id">View</a>
<a class="btn btn-primary btn-sm" asp-controller="Author" asp-action="editpost" asp-route-id="@post.Id">Edit</a>
</span>
</td>
</tr>
}
</tbody>
</table>
<a class="btn btn-primary" asp-action="AddPost">Create New Post</a>
<div class="panel panel-default">
<div class="panel-heading"><h3>Manage Posts</h3></div>
<div class="panel-body">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Description</th>
<th>Publish Date</th>
<th>Is Public?</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var post in Model.posts)
{
var aid = $"a{post.Id}";
<tr id="@post.Id">
<td>@post.Id</td>
<td>@post.Title</td>
<td>@post.Description</td>
<td>@post.PublishOn</td>
<td>@post.Public</td>
<td>
<span class="text-nowrap">
<a class="btn btn-primary btn-sm" asp-controller="Home" asp-action="blogpost" asp-route-id="@post.Id">View</a>
<a class="btn btn-primary btn-sm" asp-controller="Author" asp-action="editpost" asp-route-id="@post.Id">Edit</a>
</span>
</td>
</tr>
}
</tbody>
</table>
<a class="btn btn-primary" asp-action="AddPost">Create New Post</a>
</div>
</div>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/blog/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</head>
<body>

<nav class="navbar navbar-default">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar1" aria-expanded="false">
Expand Down
4 changes: 2 additions & 2 deletions src/blog/Views/Shared/_LoginPartial.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<form asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
<ul class="nav navbar-nav navbar-right">
<li>
<button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button>
<button type="submit" class="btn btn-link navbar-btn navbar-link">Log off&nbsp;&nbsp;</button>
</li>
</ul>
</form>
Expand All @@ -18,6 +18,6 @@ else
{
Uri url = new Uri(String.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString));
<ul class="nav navbar-nav navbar-right">
<li><a asp-controller="Account" asp-action="Login" asp-route-returnUrl="@url">Log in</a></li>
<li><a asp-controller="Account" asp-action="Login" asp-route-returnUrl="@url">Log in&nbsp;&nbsp;</a></li>
</ul>
}
1 change: 0 additions & 1 deletion src/blog/blog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<Content Include="wwwroot\js\AddEditPostPage.js" />
<Content Include="wwwroot\js\AddEditUser.js" />
<Content Include="wwwroot\js\Common.js" />
<Content Include="wwwroot\js\PostPage.js" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/blog/wwwroot/css/main.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
body {
padding: 0;
padding-top: 70px;
}

/* Get rid of highlighting on navbar-brand*/
Expand Down

0 comments on commit eb0f3ed

Please sign in to comment.