Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Atles.Core/Results/Types/FailureType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Atles.Core.Results.Types;

public enum FailureType { Error, NotFound, NullArgument }
public enum FailureType { Error, NotFound, NullArgument, Unauthorized }
73 changes: 36 additions & 37 deletions src/Atles.Models/Public/ForumPageModel.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
using Atles.Domain;

namespace Atles.Models.Public
namespace Atles.Models.Public;

public class ForumPageModel
{
public class ForumPageModel
{
public ForumModel Forum { get; set; } = new();
public ForumModel Forum { get; set; } = new();

public PaginatedData<TopicModel> Topics { get; set; } = new();
public PaginatedData<TopicModel> Topics { get; set; } = new();

public bool CanRead { get; set; }
public bool CanStart { get; set; }
public bool CanRead { get; set; }
public bool CanStart { get; set; }

public class ForumModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
}
public class ForumModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
}

public class TopicModel
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public int TotalReplies { get; set; }
public Guid UserId { get; set; }
public string UserDisplayName { get; set; }
public DateTime TimeStamp { get; set; }
public string GravatarHash { get; set; }
public DateTime MostRecentTimeStamp { get; set; }
public Guid MostRecentUserId { get; set; }
public string MostRecentUserDisplayName { get; set; }
public bool Pinned { get; set; }
public bool Locked { get; set; }
public bool HasAnswer { get; set; }
public IList<ReactionModel> Reactions { get; set; } = new List<ReactionModel>();
}
public class TopicModel
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public int TotalReplies { get; set; }
public Guid UserId { get; set; }
public string UserDisplayName { get; set; }
public DateTime TimeStamp { get; set; }
public string GravatarHash { get; set; }
public DateTime MostRecentTimeStamp { get; set; }
public Guid MostRecentUserId { get; set; }
public string MostRecentUserDisplayName { get; set; }
public bool Pinned { get; set; }
public bool Locked { get; set; }
public bool HasAnswer { get; set; }
public IList<ReactionModel> Reactions { get; set; } = new List<ReactionModel>();
}

public class ReactionModel
{
public PostReactionType Type { get; set; }
public int Count { get; set; }
}
public class ReactionModel
{
public PostReactionType Type { get; set; }
public int Count { get; set; }
}
}
15 changes: 7 additions & 8 deletions src/Atles.Models/Public/PermissionModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Atles.Domain;

namespace Atles.Models.Public
namespace Atles.Models.Public;

public class PermissionModel
{
public class PermissionModel
{
public PermissionType Type { get; set; }
public bool AllUsers { get; set; }
public bool RegisteredUsers { get; set; }
public IList<string> Roles { get; set; } = new List<string>();
}
public PermissionType Type { get; set; }
public bool AllUsers { get; set; }
public bool RegisteredUsers { get; set; }
public IList<string> Roles { get; set; } = new List<string>();
}
104 changes: 61 additions & 43 deletions src/Atles.Queries.Handlers/Public/GetForumPageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,79 @@
using System.Threading.Tasks;
using Atles.Core;
using Atles.Core.Queries;
using Atles.Core.Queries;
using Atles.Core.Results;
using Atles.Core.Results.Types;
using Atles.Data;
using Atles.Domain;
using Atles.Models.Public;
using Atles.Queries.Handlers.Public.Services;
using Atles.Queries.Public;
using Microsoft.EntityFrameworkCore;

namespace Atles.Queries.Handlers.Public
namespace Atles.Queries.Handlers.Public;

public class GetForumPageHandler : IQueryHandler<GetForumPage, ForumPageModel>
{
public class GetForumPageHandler : IQueryHandler<GetForumPage, ForumPageModel>
private readonly AtlesDbContext _dbContext;
private readonly IForumPageTopicsService _forumPageTopicsService;
private readonly ISecurityService _securityService;
private readonly IPermissionsService _permissionsService;

public GetForumPageHandler(
AtlesDbContext dbContext,
IForumPageTopicsService forumPageTopicsService,
ISecurityService securityService,
IPermissionsService permissionsService)
{
private readonly AtlesDbContext _dbContext;
private readonly IDispatcher _dispatcher;
_dbContext = dbContext;
_forumPageTopicsService = forumPageTopicsService;
_securityService = securityService;
_permissionsService = permissionsService;
}

public GetForumPageHandler(AtlesDbContext dbContext, IDispatcher sender)
public async Task<QueryResult<ForumPageModel>> Handle(GetForumPage query)
{
var forum = await _dbContext.Forums
.Include(x => x.Category)
.FirstOrDefaultAsync(x =>
x.Slug == query.Slug &&
x.Category.SiteId == query.SiteId &&
x.Status == ForumStatusType.Published);

if (forum == null)
{
_dbContext = dbContext;
_dispatcher = sender;
return new Failure(FailureType.NotFound, "Forum", $"Forum with slug {query.Slug} not found.");
}

public async Task<QueryResult<ForumPageModel>> Handle(GetForumPage query)
var topics = await _forumPageTopicsService.GetForumPageTopics(new GetForumPageTopics
{
var forum = await _dbContext.Forums
.Include(x => x.Category)
.FirstOrDefaultAsync(x =>
x.Slug == query.Slug &&
x.Category.SiteId == query.SiteId &&
x.Status == ForumStatusType.Published);

if (forum == null)
{
return null;
}

// TODO: To be moved to a service
var queryResult = await _dispatcher.Get(new GetForumPageTopics
{
ForumId = forum.Id,
Options = query.Options
});
var topics = queryResult.AsT0;

var result = new ForumPageModel
ForumId = forum.Id,
Options = query.Options
});

var result = new ForumPageModel
{
Forum = new ForumPageModel.ForumModel
{
Forum = new ForumPageModel.ForumModel
{
Id = forum.Id,
Name = forum.Name,
Description = forum.Description,
Slug = forum.Slug
},
Topics = topics
};

return result;
Id = forum.Id,
Name = forum.Name,
Description = forum.Description,
Slug = forum.Slug
},
Topics = topics
};

var permissions = await _permissionsService.GetPermissions(query.SiteId, forum.Id, null);

var canViewForum = _securityService.HasPermission(PermissionType.ViewForum, permissions);
var canViewTopics = _securityService.HasPermission(PermissionType.ViewTopics, permissions);

if (!canViewForum || !canViewTopics)
{
return new Failure(FailureType.Unauthorized, "Forum", $"User can't access forum with slug {query.Slug}.");
}

result.CanRead = _securityService.HasPermission(PermissionType.Read, permissions);
result.CanStart = _securityService.HasPermission(PermissionType.Start, permissions) /*TODO: && !CurrentUser.IsSuspended*/;

return result;
}
}
73 changes: 6 additions & 67 deletions src/Atles.Queries.Handlers/Public/GetForumPageTopicsHandler.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,23 @@
using Atles.Core.Extensions;
using Atles.Core.Queries;
using Atles.Core.Queries;
using Atles.Core.Results;
using Atles.Data;
using Atles.Domain;
using Atles.Models;
using Atles.Models.Public;
using Atles.Queries.Handlers.Public.Services;
using Atles.Queries.Public;
using Microsoft.EntityFrameworkCore;

namespace Atles.Queries.Handlers.Public;

public class GetForumPageTopicsHandler : IQueryHandler<GetForumPageTopics, PaginatedData<ForumPageModel.TopicModel>>
{
private readonly AtlesDbContext _dbContext;
private readonly IForumPageTopicsService _forumPageTopicsService;

public GetForumPageTopicsHandler(AtlesDbContext dbContext)
public GetForumPageTopicsHandler(IForumPageTopicsService forumPageTopicsService)
{
_dbContext = dbContext;
_forumPageTopicsService = forumPageTopicsService;
}

public async Task<QueryResult<PaginatedData<ForumPageModel.TopicModel>>> Handle(GetForumPageTopics query)
{
var topicsQuery = _dbContext.Posts
.Include(x => x.PostReactionSummaries)
.Include(x => x.CreatedByUser)
.Include(x => x.LastReply).ThenInclude(x => x.CreatedByUser)
.Where(x =>
x.TopicId == null &&
x.ForumId == query.ForumId &&
x.Status == PostStatusType.Published);

if (!string.IsNullOrWhiteSpace(query.Options.Search))
{
topicsQuery = topicsQuery
.Where(x => x.Title.Contains(query.Options.Search) || x.Content.Contains(query.Options.Search));
}

var topics = await topicsQuery
.OrderByDescending(x => x.Pinned)
.ThenByDescending(x => x.LastReply != null ? x.LastReply.CreatedOn : x.CreatedOn)
.Skip(query.Options.Skip)
.Take(query.Options.PageSize)
.ToListAsync();

var items = topics.Select(topic => new ForumPageModel.TopicModel
{
Id = topic.Id,
Title = topic.Title,
Slug = topic.Slug,
TotalReplies = topic.RepliesCount,
UserId = topic.CreatedByUser.Id,
UserDisplayName = topic.CreatedByUser.DisplayName,
TimeStamp = topic.CreatedOn,
GravatarHash = topic.CreatedByUser.Email.ToGravatarEmailHash(),
MostRecentUserId = topic.LastReply?.CreatedBy ?? topic.CreatedBy,
MostRecentUserDisplayName = topic.LastReply?.CreatedByUser?.DisplayName ?? topic.CreatedByUser.DisplayName,
MostRecentTimeStamp = topic.LastReply?.CreatedOn ?? topic.CreatedOn,
Pinned = topic.Pinned,
Locked = topic.Locked,
HasAnswer = topic.HasAnswer,
Reactions = topic.PostReactionSummaries.Select(x => new ForumPageModel.ReactionModel { Type = x.Type, Count = x.Count }).ToList()
})
.ToList();

var totalRecordsQuery = _dbContext.Posts
.Where(x =>
x.TopicId == null &&
x.ForumId == query.ForumId &&
x.Status == PostStatusType.Published);

if (!string.IsNullOrWhiteSpace(query.Options.Search))
{
totalRecordsQuery = totalRecordsQuery
.Where(x => x.Title.Contains(query.Options.Search) || x.Content.Contains(query.Options.Search));
}

var totalRecords = await totalRecordsQuery.CountAsync();

var result = new PaginatedData<ForumPageModel.TopicModel>(items, totalRecords, query.Options.PageSize);

return result;
return await _forumPageTopicsService.GetForumPageTopics(query);
}
}
2 changes: 1 addition & 1 deletion src/Atles.Queries.Handlers/Public/GetPermissionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<QueryResult<IList<PermissionModel>>> Handle(GetPermissions que
return await BuildPermissionModels(query.SiteId, permission.Id);
}
}
else if(query.PermissionSetId != null)
else if (query.PermissionSetId != null)
{
return await BuildPermissionModels(query.SiteId, query.PermissionSetId.Value);
}
Expand Down
Loading