diff --git a/src/Yavsc.Abstract/Blogspot/IBlog.cs b/src/Yavsc.Abstract/Blogspot/IBlog.cs index c9d7666d..7c7b2bff 100644 --- a/src/Yavsc.Abstract/Blogspot/IBlog.cs +++ b/src/Yavsc.Abstract/Blogspot/IBlog.cs @@ -5,8 +5,8 @@ namespace Yavsc { public interface IBlogPostPayLoad { - string Content { get; set; } - string Photo { get; set; } + string? Content { get; set; } + string? Photo { get; set; } } public interface IBlogPost :IBlogPostPayLoad, ITrackedEntity, IIdentified, ITitle diff --git a/src/Yavsc.Server/Helpers/UserHelpers.cs b/src/Yavsc.Server/Helpers/UserHelpers.cs index 666d6ba8..20d74897 100644 --- a/src/Yavsc.Server/Helpers/UserHelpers.cs +++ b/src/Yavsc.Server/Helpers/UserHelpers.cs @@ -28,7 +28,7 @@ namespace Yavsc.Helpers { if (readerId == null) { - var userPosts = dbContext.blogspotPublications.Include( + var userPosts = dbContext.blogSpotPublications.Include( b => b.BlogPost ).Where(x => x.BlogPost.AuthorId == posterId) .Select(x=>x.BlogPost).ToArray(); diff --git a/src/Yavsc.Server/Models/ApplicationDbContext.cs b/src/Yavsc.Server/Models/ApplicationDbContext.cs index 7385a6c5..0c9efb4d 100644 --- a/src/Yavsc.Server/Models/ApplicationDbContext.cs +++ b/src/Yavsc.Server/Models/ApplicationDbContext.cs @@ -300,7 +300,7 @@ namespace Yavsc.Models public DbSet Scopes { get; set; } - public DbSet blogspotPublications{ get; set; } + public DbSet blogSpotPublications{ get; set; } } } diff --git a/src/Yavsc.Server/Models/Blog/BlogPost.cs b/src/Yavsc.Server/Models/Blog/BlogPost.cs index 8e300bf7..1daeee83 100644 --- a/src/Yavsc.Server/Models/Blog/BlogPost.cs +++ b/src/Yavsc.Server/Models/Blog/BlogPost.cs @@ -14,7 +14,8 @@ using Yavsc.ViewModels.Blog; namespace Yavsc.Models.Blog { - public class BlogPost : BlogPostInputViewModel, IBlogPost, ICircleAuthorized, ITaggable, IIdentified + public class BlogPost : BlogPostBase, + IBlogPost, ICircleAuthorized, ITaggable { [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Display(Name="Identifiant du post")] diff --git a/src/Yavsc.Server/Models/Blog/BlogspotPublication.cs b/src/Yavsc.Server/Models/Blog/BlogspotPublication.cs index 9df4729c..bcae5221 100644 --- a/src/Yavsc.Server/Models/Blog/BlogspotPublication.cs +++ b/src/Yavsc.Server/Models/Blog/BlogspotPublication.cs @@ -4,7 +4,7 @@ using Yavsc.Models.Blog; namespace Yavsc.Models { - public class BlogspotPublication + public class BlogSpotPublication { [Key] public long BlogpostId { get; set; } diff --git a/src/Yavsc.Server/Services/BlogSpotService.cs b/src/Yavsc.Server/Services/BlogSpotService.cs index e12f228b..91f7bbd7 100644 --- a/src/Yavsc.Server/Services/BlogSpotService.cs +++ b/src/Yavsc.Server/Services/BlogSpotService.cs @@ -3,10 +3,8 @@ using System.Diagnostics; using System.Security.Claims; using Microsoft.AspNetCore.Authorization; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.Differencing; using Microsoft.EntityFrameworkCore; -using Org.BouncyCastle.Tls; +using Yavsc; using Yavsc.Helpers; using Yavsc.Models; using Yavsc.Models.Blog; @@ -27,7 +25,7 @@ public class BlogSpotService _context = context; } - public BlogPost Create(string userId, BlogPostInputViewModel blogInput) + public BlogPost Create(string userId, BlogPostBase blogInput) { BlogPost post = new BlogPost { @@ -91,17 +89,38 @@ public class BlogSpotService blog.ACL = blogEdit.ACL; // saves the change _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()); } - public async Task>> IndexByTitle(ClaimsPrincipal user, string id, int skip = 0, int take = 25) + public async Task>> IndexByTitle(ClaimsPrincipal user, string id, int skip = 0, int take = 25) { - IEnumerable posts; + IEnumerable posts; if (user.Identity.IsAuthenticated) { 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)) .Select(c => c.Id).ToArrayAsync(); @@ -110,20 +129,24 @@ public class BlogSpotService .Include(p => p.ACL) .Include(p => p.Tags) .Include(p => p.Comments) - .Where(p => (p.ACL.Count == 0) + .Where(p => p.ACL == null + || p.ACL.Count == 0 || (p.AuthorId == viewerId) - || (usercircles != null && p.ACL.Any(a => usercircles.Contains(a.CircleId))) + || (userCircles != null && + p.ACL.Any(a => userCircles.Contains(a.CircleId))) ); } else { - posts = _context.blogspotPublications + posts = _context.blogSpotPublications .Include(p => p.BlogPost) .Include(b => b.BlogPost.Author) .Include(p => p.BlogPost.ACL) .Include(p => p.BlogPost.Tags) .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); diff --git a/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostInputViewModel.cs b/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostBase.cs similarity index 77% rename from src/Yavsc.Server/ViewModels/BlogSpot/BlogPostInputViewModel.cs rename to src/Yavsc.Server/ViewModels/BlogSpot/BlogPostBase.cs index 526f52e1..24644a5e 100644 --- a/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostInputViewModel.cs +++ b/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostBase.cs @@ -5,8 +5,8 @@ using Yavsc.Models.Access; namespace Yavsc.ViewModels.Blog { - public class BlogPostInputViewModel -{ + public class BlogPostBase + { [StringLength(1024)] public string? Photo { get; set; } @@ -18,9 +18,10 @@ namespace Yavsc.ViewModels.Blog public string? Content { get; set; } [InverseProperty("Target")] - [Display(Name="Liste de contrôle d'accès")] - public virtual List? ACL { get; set; } + [Display(Name = "Liste de contrôle d'accès")] + public virtual List? ACL { get; set; } + public bool Publish { get; set; } } } diff --git a/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostEdit.cs b/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostEdit.cs index 26b3ba2b..1c84934c 100644 --- a/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostEdit.cs +++ b/src/Yavsc.Server/ViewModels/BlogSpot/BlogPostEdit.cs @@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations; namespace Yavsc.ViewModels.Blog; -public class BlogPostEditViewModel : BlogPostInputViewModel +public class BlogPostEditViewModel : BlogPostBase { [Required] diff --git a/src/Yavsc/Controllers/Communicating/BlogspotController.cs b/src/Yavsc/Controllers/Communicating/BlogspotController.cs index 96dbc898..4e4fea91 100644 --- a/src/Yavsc/Controllers/Communicating/BlogspotController.cs +++ b/src/Yavsc/Controllers/Communicating/BlogspotController.cs @@ -88,7 +88,7 @@ namespace Yavsc.Controllers [Authorize()] public IActionResult Create(string title) { - var result = new BlogPostInputViewModel + var result = new BlogPostBase { Title = title }; @@ -98,7 +98,7 @@ namespace Yavsc.Controllers // POST: Blog/Create [HttpPost, Authorize, ValidateAntiForgeryToken] - public IActionResult Create(BlogPostInputViewModel blogInput) + public IActionResult Create(BlogPostBase blogInput) { if (ModelState.IsValid) { diff --git a/src/Yavsc/Views/Blogspot/Create.cshtml b/src/Yavsc/Views/Blogspot/Create.cshtml index 4739ea9c..2625cdc9 100644 --- a/src/Yavsc/Views/Blogspot/Create.cshtml +++ b/src/Yavsc/Views/Blogspot/Create.cshtml @@ -1,4 +1,4 @@ -@model Yavsc.ViewModels.Blog.BlogPostInputViewModel +@model Yavsc.ViewModels.Blog.BlogPostBase @{ ViewData["Title"] = "Blog post edition"; @@ -47,6 +47,7 @@ +
@@ -56,6 +57,7 @@
+
@@ -63,6 +65,13 @@
+
+ +
+ +
+
+
diff --git a/src/Yavsc/Views/Blogspot/Edit.cshtml b/src/Yavsc/Views/Blogspot/Edit.cshtml index 1e24a005..68160839 100644 --- a/src/Yavsc/Views/Blogspot/Edit.cshtml +++ b/src/Yavsc/Views/Blogspot/Edit.cshtml @@ -97,6 +97,13 @@
+
+ +
+ +
+
+