refact
All checks were successful
Dotnet build and test / log-the-inputs (push) Successful in 4s
Dotnet build and test / build (push) Successful in 2m30s

This commit is contained in:
Paul Schneider
2025-06-29 18:44:35 +01:00
parent 531797d348
commit bebca989d0
11 changed files with 66 additions and 25 deletions

View File

@ -5,8 +5,8 @@ namespace Yavsc
{ {
public interface IBlogPostPayLoad public interface IBlogPostPayLoad
{ {
string Content { get; set; } string? Content { get; set; }
string Photo { get; set; } string? Photo { get; set; }
} }
public interface IBlogPost :IBlogPostPayLoad, ITrackedEntity, IIdentified<long>, ITitle public interface IBlogPost :IBlogPostPayLoad, ITrackedEntity, IIdentified<long>, ITitle

View File

@ -28,7 +28,7 @@ namespace Yavsc.Helpers
{ {
if (readerId == null) if (readerId == null)
{ {
var userPosts = dbContext.blogspotPublications.Include( var userPosts = dbContext.blogSpotPublications.Include(
b => b.BlogPost b => b.BlogPost
).Where(x => x.BlogPost.AuthorId == posterId) ).Where(x => x.BlogPost.AuthorId == posterId)
.Select(x=>x.BlogPost).ToArray(); .Select(x=>x.BlogPost).ToArray();

View File

@ -300,7 +300,7 @@ namespace Yavsc.Models
public DbSet<Scope> Scopes { get; set; } public DbSet<Scope> Scopes { get; set; }
public DbSet<BlogspotPublication> blogspotPublications{ get; set; } public DbSet<BlogSpotPublication> blogSpotPublications{ get; set; }
} }
} }

View File

@ -14,7 +14,8 @@ using Yavsc.ViewModels.Blog;
namespace Yavsc.Models.Blog namespace Yavsc.Models.Blog
{ {
public class BlogPost : BlogPostInputViewModel, IBlogPost, ICircleAuthorized, ITaggable<long>, IIdentified<long> public class BlogPost : BlogPostBase,
IBlogPost, ICircleAuthorized, ITaggable<long>
{ {
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Name="Identifiant du post")] [Display(Name="Identifiant du post")]

View File

@ -4,7 +4,7 @@ using Yavsc.Models.Blog;
namespace Yavsc.Models namespace Yavsc.Models
{ {
public class BlogspotPublication public class BlogSpotPublication
{ {
[Key] [Key]
public long BlogpostId { get; set; } public long BlogpostId { get; set; }

View File

@ -3,10 +3,8 @@
using System.Diagnostics; using System.Diagnostics;
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Differencing;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Tls; using Yavsc;
using Yavsc.Helpers; using Yavsc.Helpers;
using Yavsc.Models; using Yavsc.Models;
using Yavsc.Models.Blog; using Yavsc.Models.Blog;
@ -27,7 +25,7 @@ public class BlogSpotService
_context = context; _context = context;
} }
public BlogPost Create(string userId, BlogPostInputViewModel blogInput) public BlogPost Create(string userId, BlogPostBase blogInput)
{ {
BlogPost post = new BlogPost BlogPost post = new BlogPost
{ {
@ -91,17 +89,38 @@ public class BlogSpotService
blog.ACL = blogEdit.ACL; blog.ACL = blogEdit.ACL;
// saves the change // saves the change
_context.Update(blog); _context.Update(blog);
var publication = await _context.blogSpotPublications.SingleOrDefaultAsync
(p=>p.BlogpostId==blogEdit.Id);
if (publication != null)
{
if (!blogEdit.Publish)
{
_context.blogSpotPublications.Remove(publication);
}
}
else
{
if (blogEdit.Publish)
{
_context.blogSpotPublications.Add(
new BlogSpotPublication
{
BlogpostId = blogEdit.Id
}
);
}
}
_context.SaveChanges(user.GetUserId()); _context.SaveChanges(user.GetUserId());
} }
public async Task<IEnumerable<IGrouping<String, BlogPost>>> IndexByTitle(ClaimsPrincipal user, string id, int skip = 0, int take = 25) public async Task<IEnumerable<IGrouping<string, IBlogPost>>> IndexByTitle(ClaimsPrincipal user, string id, int skip = 0, int take = 25)
{ {
IEnumerable<BlogPost> posts; IEnumerable<IBlogPost> posts;
if (user.Identity.IsAuthenticated) if (user.Identity.IsAuthenticated)
{ {
string viewerId = user.GetUserId(); string viewerId = user.GetUserId();
long[] usercircles = await _context.Circle.Include(c => c.Members). long[] userCircles = await _context.Circle.Include(c => c.Members).
Where(c => c.Members.Any(m => m.MemberId == viewerId)) Where(c => c.Members.Any(m => m.MemberId == viewerId))
.Select(c => c.Id).ToArrayAsync(); .Select(c => c.Id).ToArrayAsync();
@ -110,20 +129,24 @@ public class BlogSpotService
.Include(p => p.ACL) .Include(p => p.ACL)
.Include(p => p.Tags) .Include(p => p.Tags)
.Include(p => p.Comments) .Include(p => p.Comments)
.Where(p => (p.ACL.Count == 0) .Where(p => p.ACL == null
|| p.ACL.Count == 0
|| (p.AuthorId == viewerId) || (p.AuthorId == viewerId)
|| (usercircles != null && p.ACL.Any(a => usercircles.Contains(a.CircleId))) || (userCircles != null &&
p.ACL.Any(a => userCircles.Contains(a.CircleId)))
); );
} }
else else
{ {
posts = _context.blogspotPublications posts = _context.blogSpotPublications
.Include(p => p.BlogPost) .Include(p => p.BlogPost)
.Include(b => b.BlogPost.Author) .Include(b => b.BlogPost.Author)
.Include(p => p.BlogPost.ACL) .Include(p => p.BlogPost.ACL)
.Include(p => p.BlogPost.Tags) .Include(p => p.BlogPost.Tags)
.Include(p => p.BlogPost.Comments) .Include(p => p.BlogPost.Comments)
.Where(p => p.BlogPost.ACL.Count == 0).Select(p => p.BlogPost).ToArray(); .Where(p => p.BlogPost.ACL == null
|| p.BlogPost.ACL.Count == 0)
.Select(p => p.BlogPost).ToArray();
} }
var data = posts.OrderByDescending(p => p.DateCreated); var data = posts.OrderByDescending(p => p.DateCreated);

View File

@ -5,8 +5,8 @@ using Yavsc.Models.Access;
namespace Yavsc.ViewModels.Blog namespace Yavsc.ViewModels.Blog
{ {
public class BlogPostInputViewModel public class BlogPostBase
{ {
[StringLength(1024)] [StringLength(1024)]
public string? Photo { get; set; } public string? Photo { get; set; }
@ -18,9 +18,10 @@ namespace Yavsc.ViewModels.Blog
public string? Content { get; set; } public string? Content { get; set; }
[InverseProperty("Target")] [InverseProperty("Target")]
[Display(Name="Liste de contrôle d'accès")] [Display(Name = "Liste de contrôle d'accès")]
public virtual List<CircleAuthorizationToBlogPost>? ACL { get; set; } public virtual List<CircleAuthorizationToBlogPost>? ACL { get; set; }
public bool Publish { get; set; }
} }
} }

View File

@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations;
namespace Yavsc.ViewModels.Blog; namespace Yavsc.ViewModels.Blog;
public class BlogPostEditViewModel : BlogPostInputViewModel public class BlogPostEditViewModel : BlogPostBase
{ {
[Required] [Required]

View File

@ -88,7 +88,7 @@ namespace Yavsc.Controllers
[Authorize()] [Authorize()]
public IActionResult Create(string title) public IActionResult Create(string title)
{ {
var result = new BlogPostInputViewModel var result = new BlogPostBase
{ {
Title = title Title = title
}; };
@ -98,7 +98,7 @@ namespace Yavsc.Controllers
// POST: Blog/Create // POST: Blog/Create
[HttpPost, Authorize, ValidateAntiForgeryToken] [HttpPost, Authorize, ValidateAntiForgeryToken]
public IActionResult Create(BlogPostInputViewModel blogInput) public IActionResult Create(BlogPostBase blogInput)
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {

View File

@ -1,4 +1,4 @@
@model Yavsc.ViewModels.Blog.BlogPostInputViewModel @model Yavsc.ViewModels.Blog.BlogPostBase
@{ @{
ViewData["Title"] = "Blog post edition"; ViewData["Title"] = "Blog post edition";
@ -47,6 +47,7 @@
</span> </span>
</div> </div>
</div> </div>
<div class="form-group mdcoding"> <div class="form-group mdcoding">
<label asp-for="Content" class="col-md-2 control-label" ></label> <label asp-for="Content" class="col-md-2 control-label" ></label>
<div class="col-md-10"> <div class="col-md-10">
@ -56,6 +57,7 @@
</span> </span>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="ACL" class="col-md-2 control-label"></label> <label asp-for="ACL" class="col-md-2 control-label"></label>
<div class="col-md-10"> <div class="col-md-10">
@ -63,6 +65,13 @@
</div> </div>
</div> </div>
<div class="form-group">
<label asp-for="Publish" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Publish" class="form-control" />
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="col-md-offset-2 col-md-10"> <div class="col-md-offset-2 col-md-10">
<button class="btn btn-primary" >Save</button> <button class="btn btn-primary" >Save</button>

View File

@ -97,6 +97,13 @@
</div> </div>
</div> </div>
<div class="form-group">
<label asp-for="Publish" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Publish" class="form-control" />
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="col-md-offset-2 col-md-10"> <div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" /> <input type="submit" value="Save" class="btn btn-default" />