【发布时间】:2022-01-23 00:13:03
【问题描述】:
我正在寻找处理每个用户的项目可见性的最佳或好方法。 其实我有以下实体...
public class Post
{
public Guid Id {get;set;}
public string Content {get; set;}
public Guid VisibilityId {get; set;}
public Visibility Visibility {get; set;}
}
public class Visibility
{
public Guid Id {get; set;}
public string Name {get; set;}
}
public class AllowedList
{
public Guid PostId {get; set;}
public Guid UserId {get; set;}
}
我为可见性实体添加了以下查找
Public, OnlyMe, Custom
我想在案例中为用户显示帖子项目
- “公开”在这种情况下,所有用户都允许显示该帖子。
- “自定义”在这种情况下,如果用户存在于 AllowedList 中,那么他们可以显示它。
- “OnlyMe”在这种情况下,创建的用户只能显示该帖子。
因此,基于该验收标准,我创建了以下代码。
/*
1. PublicId => 1,
2. OnlyMeId => 2,
3. CustomId => 3
*/
public IQueryable<Post> GetPosts(ApplicationContext context ,ICurrentUser user)
{
var userId = user.Id;
var posts = context.Posts.Where(p => p.VisibilityId == 1 || (p.VisibilityId == 2 && p.CreatedBy == userId) || context.AllowedList.Any(a => a.PostId == p.Id && a.UserId == userId));
return posts; // return as IQueryble of Posts
}
这是我的解决方案,但我认为这不是针对这种情况的正确或好的解决方案。
【问题讨论】:
-
当范围变量有意义时,请不要使用丢弃 (
_)。在这里它们特别令人困惑,因为您还使用__。那么,你不认为它是正确的,请详细说明。 -
@GertArnold 完成,我编辑了问题。
标签: entity-framework asp.net-core asp.net-web-api entity-framework-core