factorisation
This commit is contained in:
parent
74a82a6e48
commit
c8df1f40e8
2 changed files with 28 additions and 19 deletions
|
|
@ -12,6 +12,7 @@ using Yavsc.Models;
|
||||||
using Yavsc.ViewModels.Auth;
|
using Yavsc.ViewModels.Auth;
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
using Yavsc.Models.Blog;
|
using Yavsc.Models.Blog;
|
||||||
|
using Yavsc.Helpers;
|
||||||
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
||||||
|
|
||||||
namespace Yavsc.Controllers
|
namespace Yavsc.Controllers
|
||||||
|
|
@ -20,8 +21,6 @@ namespace Yavsc.Controllers
|
||||||
{
|
{
|
||||||
ILogger _logger;
|
ILogger _logger;
|
||||||
private ApplicationDbContext _context;
|
private ApplicationDbContext _context;
|
||||||
|
|
||||||
private SiteSettings _siteSettings;
|
|
||||||
private IAuthorizationService _authorizationService;
|
private IAuthorizationService _authorizationService;
|
||||||
public BlogspotController(
|
public BlogspotController(
|
||||||
ApplicationDbContext context,
|
ApplicationDbContext context,
|
||||||
|
|
@ -33,15 +32,14 @@ namespace Yavsc.Controllers
|
||||||
_context = context;
|
_context = context;
|
||||||
_logger = loggerFactory.CreateLogger<AccountController>();
|
_logger = loggerFactory.CreateLogger<AccountController>();
|
||||||
_authorizationService = authorizationService;
|
_authorizationService = authorizationService;
|
||||||
_siteSettings = siteSettings.Value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: Blog
|
// GET: Blog
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
public IActionResult Index(string id, int skip=0, int maxLen=25)
|
public async Task<IActionResult> Index(string id, int skip=0, int maxLen=25)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(id))
|
if (!string.IsNullOrEmpty(id))
|
||||||
return UserPosts(id);
|
return await UserPosts(id);
|
||||||
string uid = User.GetUserId();
|
string uid = User.GetUserId();
|
||||||
long[] usercircles = _context.Circle.Include(c=>c.Members).Where(c=>c.Members.Any(m=>m.MemberId == uid))
|
long[] usercircles = _context.Circle.Include(c=>c.Members).Where(c=>c.Members.Any(m=>m.MemberId == uid))
|
||||||
.Select(c=>c.Id).ToArray();
|
.Select(c=>c.Id).ToArray();
|
||||||
|
|
@ -82,21 +80,10 @@ namespace Yavsc.Controllers
|
||||||
|
|
||||||
[Route("/Blog/{id?}")]
|
[Route("/Blog/{id?}")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
public IActionResult UserPosts(string id)
|
public async Task<IActionResult> UserPosts(string id)
|
||||||
{
|
{
|
||||||
|
string posterId = (await _context.Users.SingleOrDefaultAsync(u=>u.UserName == id))?.Id ?? null ;
|
||||||
if (string.IsNullOrEmpty(id)) return Index(null);
|
var result = _context.UserPosts(posterId, User.Identity.Name);
|
||||||
var uid = User.GetUserId();
|
|
||||||
long[] usercircles = _context.Circle.Include(c=>c.Members).Where(c=>c.Members.Any(m=>m.MemberId == uid))
|
|
||||||
.Select(c=>c.Id).ToArray();
|
|
||||||
var result = (User.IsSignedIn())?
|
|
||||||
_context.Blogspot.Include(
|
|
||||||
b => b.Author
|
|
||||||
).Include(p=>p.ACL).Where(x => x.Author.UserName == id && (x.Visible && (x.ACL.Count==0 || x.ACL.Any(a=> usercircles.Contains(a.CircleId))))):
|
|
||||||
_context.Blogspot.Include(
|
|
||||||
b => b.Author
|
|
||||||
).Where(x => x.Author.UserName == id && x.Visible);
|
|
||||||
// BlogIndexKey
|
|
||||||
return View("Index", result.OrderByDescending(p => p.DateCreated).ToList().GroupBy(p=> p.Title ));
|
return View("Index", result.OrderByDescending(p => p.DateCreated).ToList().GroupBy(p=> p.Title ));
|
||||||
}
|
}
|
||||||
// GET: Blog/Details/5
|
// GET: Blog/Details/5
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
|
using Yavsc.Models.Blog;
|
||||||
|
|
||||||
namespace Yavsc.Helpers
|
namespace Yavsc.Helpers
|
||||||
{
|
{
|
||||||
|
|
@ -21,5 +24,24 @@ namespace Yavsc.Helpers
|
||||||
var avatar = user.UserName;
|
var avatar = user.UserName;
|
||||||
return $"/Avatars/{avatar}{imgFmt}.png";
|
return $"/Avatars/{avatar}{imgFmt}.png";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<BlogPost> UserPosts(this ApplicationDbContext dbContext, string posterId, string readerId)
|
||||||
|
{
|
||||||
|
long[] readerCirclesMemberships = dbContext.Circle.Include(c=>c.Members).Where(c=>c.Members.Any(m=>m.MemberId == readerId))
|
||||||
|
.Select(c=>c.Id).ToArray();
|
||||||
|
var result = (readerId!=null)
|
||||||
|
?
|
||||||
|
dbContext.Blogspot.Include(
|
||||||
|
b => b.Author
|
||||||
|
).Include(p=>p.ACL).Where(x => x.Author.Id == posterId &&
|
||||||
|
(x.Visible &&
|
||||||
|
(x.ACL.Count==0 || x.ACL.Any(a=> readerCirclesMemberships.Contains(a.CircleId)))))
|
||||||
|
:
|
||||||
|
dbContext.Blogspot.Include(
|
||||||
|
b => b.Author
|
||||||
|
).Where(x => x.Author.Id == posterId && x.Visible);
|
||||||
|
// BlogIndexKey
|
||||||
|
return result.OrderByDescending(p => p.DateCreated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue