yavsc/src/Yavsc.Org/Services/BlogSpotService.cs

184 lines
5.9 KiB
C#
Raw Normal View History

2025-06-29 16:12:16 +01:00
using System.Diagnostics;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
2025-06-29 18:44:35 +01:00
using Yavsc;
2025-06-29 16:12:16 +01:00
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.Models.Blog;
using Yavsc.Server.Exceptions;
using Yavsc.Server.Helpers;
2025-06-29 16:12:16 +01:00
using Yavsc.ViewModels.Auth;
public class BlogSpotService
{
private readonly ApplicationDbContext _context;
private readonly IAuthorizationService _authorizationService;
public BlogSpotService(ApplicationDbContext context,
IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
_context = context;
}
2026-02-22 19:54:10 +00:00
public BlogPost Create(string userId, BlogPost post)
2025-06-29 16:12:16 +01:00
{
_context.BlogSpot.Add(post);
_context.SaveChanges(userId);
return post;
}
2025-07-07 07:49:18 +01:00
public async Task<BlogPostEditViewModel> GetPostForEdition(ClaimsPrincipal user, long blogPostId)
2025-06-29 16:12:16 +01:00
{
var blog = await _context.BlogSpot.Include(x => x.Author).Include(x => x.ACL).SingleAsync(m => m.Id == blogPostId);
2026-02-22 19:54:10 +00:00
var auth = await _authorizationService.AuthorizeAsync(user, blog, new EditPermission());
2025-06-29 16:12:16 +01:00
if (!auth.Succeeded)
{
throw new AuthorizationFailureException(auth);
2026-02-22 19:54:10 +00:00
}
var pub = await _context.blogSpotPublications.AnyAsync(x => x.BlogpostId == blog.Id);
return new BlogPostEditViewModel(blog, pub);
2025-06-29 16:12:16 +01:00
}
public async Task<BlogPost> Details(ClaimsPrincipal user, long blogPostId)
{
BlogPost blog = await _context.BlogSpot
.Include(p => p.Author)
.Include(p => p.Tags)
.Include(p => p.Comments)
.Include(p => p.ACL)
.SingleAsync(m => m.Id == blogPostId);
if (blog == null)
{
return null;
}
var auth = await _authorizationService.AuthorizeAsync(user, blog, new ReadPermission());
if (!auth.Succeeded)
{
throw new AuthorizationFailureException(auth);
}
foreach (var c in blog.Comments)
{
c.Author = _context.Users.First(u => u.Id == c.AuthorId);
}
return blog;
}
public async Task Modify(ClaimsPrincipal user, BlogPostEditViewModel blogEdit)
{
var blog = _context.BlogSpot.SingleOrDefault(b => b.Id == blogEdit.Id);
Debug.Assert(blog != null);
var auth = await _authorizationService.AuthorizeAsync(user, blog, new EditPermission());
if (!auth.Succeeded)
{
throw new AuthorizationFailureException(auth);
}
2026-02-22 19:59:33 +00:00
blog.Article = blogEdit.Article;
2025-06-29 16:12:16 +01:00
blog.Title = blogEdit.Title;
blog.Photo = blogEdit.Photo;
blog.ACL = blogEdit.ACL;
// saves the change
_context.Update(blog);
2025-06-29 18:44:35 +01:00
var publication = await _context.blogSpotPublications.SingleOrDefaultAsync
2026-02-22 19:54:10 +00:00
(p => p.BlogpostId == blogEdit.Id);
2025-06-29 18:44:35 +01:00
if (publication != null)
{
if (!blogEdit.Publish)
{
_context.blogSpotPublications.Remove(publication);
}
}
else
{
if (blogEdit.Publish)
{
_context.blogSpotPublications.Add(
new BlogSpotPublication
{
BlogpostId = blogEdit.Id
}
);
}
}
2025-06-29 16:12:16 +01:00
_context.SaveChanges(user.GetUserId());
}
2025-07-10 08:38:38 +01:00
public async Task<IEnumerable<IBlogPost>> Index(ClaimsPrincipal user, string id, int skip = 0, int take = 25)
2025-06-29 16:12:16 +01:00
{
2025-06-29 18:44:35 +01:00
IEnumerable<IBlogPost> posts;
2025-06-29 16:12:16 +01:00
if (user.Identity.IsAuthenticated)
{
string viewerId = user.GetUserId();
2025-06-29 18:44:35 +01:00
long[] userCircles = await _context.Circle.Include(c => c.Members).
2025-06-29 16:12:16 +01:00
Where(c => c.Members.Any(m => m.MemberId == viewerId))
.Select(c => c.Id).ToArrayAsync();
posts = _context.BlogSpot
.Include(b => b.Author)
.Include(p => p.ACL)
.Include(p => p.Tags)
.Include(p => p.Comments)
2025-06-29 18:44:35 +01:00
.Where(p => p.ACL == null
|| p.ACL.Count == 0
2025-06-29 16:12:16 +01:00
|| (p.AuthorId == viewerId)
2025-06-29 18:44:35 +01:00
|| (userCircles != null &&
p.ACL.Any(a => userCircles.Contains(a.CircleId)))
2025-06-29 16:12:16 +01:00
);
}
else
{
2025-06-29 18:44:35 +01:00
posts = _context.blogSpotPublications
2025-06-29 16:12:16 +01:00
.Include(p => p.BlogPost)
.Include(b => b.BlogPost.Author)
.Include(p => p.BlogPost.ACL)
.Include(p => p.BlogPost.Tags)
.Include(p => p.BlogPost.Comments)
2025-06-29 18:44:35 +01:00
.Where(p => p.BlogPost.ACL == null
|| p.BlogPost.ACL.Count == 0)
.Select(p => p.BlogPost).ToArray();
2025-06-29 16:12:16 +01:00
}
2025-07-10 08:38:38 +01:00
var data = posts.OrderByDescending(p => p.DateModified);
return data;
2025-06-29 16:12:16 +01:00
}
public async Task Delete(ClaimsPrincipal user, long id)
{
var uid = user.GetUserId();
BlogPost blog = _context.BlogSpot.Single(m => m.Id == id);
_context.BlogSpot.Remove(blog);
_context.SaveChanges(user.GetUserId());
}
public async Task<IEnumerable<BlogPost>> UserPosts(
string posterName,
string? readerId,
int pageLen = 10,
int pageNum = 0)
{
string? posterId = (await _context.Users.SingleOrDefaultAsync(u => u.UserName == posterName))?.Id ?? null;
if (posterId == null) return Array.Empty<BlogPost>();
return _context.UserPosts(posterId, readerId);
}
2025-07-10 08:38:38 +01:00
public object? GetTitle(string title)
2025-06-29 16:12:16 +01:00
{
return _context.BlogSpot.Include(
b => b.Author
).Where(x => x.Title == title).OrderByDescending(
x => x.DateCreated
).ToList();
}
2025-07-07 07:49:18 +01:00
public async Task<BlogPost?> GetBlogPostAsync(long value)
{
return await _context.BlogSpot
.Include(b => b.Author)
.Include(b => b.ACL)
.SingleOrDefaultAsync(x => x.Id == value);
}
2025-07-10 08:38:38 +01:00
2025-06-29 16:12:16 +01:00
}