♻️ Finish centerlizing the data models

This commit is contained in:
2025-09-27 15:14:05 +08:00
parent e70d8371f8
commit 9ce31c4dd8
167 changed files with 780 additions and 42880 deletions

View File

@@ -93,7 +93,7 @@ public class PublisherController(
public class PublisherMemberRequest
{
[Required] public long RelatedUserId { get; set; }
[Required] public PublisherMemberRole Role { get; set; }
[Required] public Shared.Models.PublisherMemberRole Role { get; set; }
}
[HttpPost("invites/{name}")]
@@ -226,7 +226,7 @@ public class PublisherController(
.FirstOrDefaultAsync();
var accountId = Guid.Parse(currentUser.Id);
if (member is null) return NotFound("Member was not found");
if (!await ps.IsMemberWithRole(publisher.Id, accountId, PublisherMemberRole.Manager))
if (!await ps.IsMemberWithRole(publisher.Id, accountId, Shared.Models.PublisherMemberRole.Manager))
return StatusCode(403, "You need at least be a manager to remove members from this publisher.");
db.PublisherMembers.Remove(member);
@@ -428,7 +428,7 @@ public class PublisherController(
.Where(m => m.PublisherId == publisher.Id)
.FirstOrDefaultAsync();
if (member is null) return StatusCode(403, "You are not even a member of the targeted publisher.");
if (member.Role < PublisherMemberRole.Manager)
if (member.Role < Shared.Models.PublisherMemberRole.Manager)
return StatusCode(403, "You need at least be the manager to update the publisher profile.");
if (request.Name is not null) publisher.Name = request.Name;
@@ -532,7 +532,7 @@ public class PublisherController(
.Where(m => m.PublisherId == publisher.Id)
.FirstOrDefaultAsync();
if (member is null) return StatusCode(403, "You are not even a member of the targeted publisher.");
if (member.Role < PublisherMemberRole.Owner)
if (member.Role < Shared.Models.PublisherMemberRole.Owner)
return StatusCode(403, "You need to be the owner to delete the publisher.");
var publisherResourceId = $"publisher:{publisher.Id}";
@@ -659,7 +659,7 @@ public class PublisherController(
.FirstOrDefaultAsync();
if (publisher is null) return NotFound();
var feature = new PublisherFeature
var feature = new SnPublisherFeature
{
PublisherId = publisher.Id,
Flag = request.Flag,

View File

@@ -160,7 +160,7 @@ public class PublisherService(
{
var publisher = new SnPublisher
{
Type = PublisherType.Individual,
Type = Shared.Models.PublisherType.Individual,
Name = name ?? account.Name,
Nick = nick ?? account.Nick,
Bio = bio ?? account.Profile.Bio,
@@ -171,15 +171,15 @@ public class PublisherService(
? null
: SnCloudFileReferenceObject.FromProtoValue(account.Profile.Background)),
AccountId = Guid.Parse(account.Id),
Members = new List<SnPublisherMember>
{
Members =
[
new()
{
AccountId = Guid.Parse(account.Id),
Role = PublisherMemberRole.Owner,
Role = Shared.Models.PublisherMemberRole.Owner,
JoinedAt = Instant.FromDateTimeUtc(DateTime.UtcNow)
}
}
]
};
db.Publishers.Add(publisher);
@@ -224,7 +224,7 @@ public class PublisherService(
{
var publisher = new SnPublisher
{
Type = PublisherType.Organizational,
Type = Shared.Models.PublisherType.Organizational,
Name = name ?? realm.Slug,
Nick = nick ?? realm.Name,
Bio = bio ?? realm.Description,
@@ -236,7 +236,7 @@ public class PublisherService(
new()
{
AccountId = Guid.Parse(account.Id),
Role = PublisherMemberRole.Owner,
Role = Shared.Models.PublisherMemberRole.Owner,
JoinedAt = Instant.FromDateTimeUtc(DateTime.UtcNow)
}
}
@@ -332,7 +332,7 @@ public class PublisherService(
if (featureFlag == null)
{
featureFlag = new PublisherFeature
featureFlag = new SnPublisherFeature
{
PublisherId = publisherId,
Flag = flag,
@@ -368,7 +368,7 @@ public class PublisherService(
return isEnabled.Value;
}
public async Task<bool> IsMemberWithRole(Guid publisherId, Guid accountId, PublisherMemberRole requiredRole)
public async Task<bool> IsMemberWithRole(Guid publisherId, Guid accountId, Shared.Models.PublisherMemberRole requiredRole)
{
var member = await db.Publishers
.Where(p => p.Id == publisherId)
@@ -377,7 +377,7 @@ public class PublisherService(
return member != null && member.Role >= requiredRole;
}
public async Task<SnPublisherMember> LoadMemberAccount(SnPublisherMember member)
{
var account = await accountsHelper.GetAccount(member.AccountId);
@@ -390,11 +390,11 @@ public class PublisherService(
var accountIds = members.Select(m => m.AccountId).ToList();
var accounts = (await accountsHelper.GetAccountBatch(accountIds)).ToDictionary(a => Guid.Parse(a.Id), a => a);
return members.Select(m =>
return [.. members.Select(m =>
{
if (accounts.TryGetValue(m.AccountId, out var account))
m.Account = SnAccount.FromProtoValue(account);
return m;
}).ToList();
})];
}
}

View File

@@ -2,6 +2,7 @@ using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Grpc.Core;
using Microsoft.EntityFrameworkCore;
using PublisherMemberRole = DysonNetwork.Shared.Models.PublisherMemberRole;
namespace DysonNetwork.Sphere.Publisher;
@@ -27,7 +28,7 @@ public class PublisherServiceGrpc(PublisherService service, AppDatabase db)
}
if (p is null) throw new RpcException(new Status(StatusCode.NotFound, "Publisher not found"));
return new GetPublisherResponse { Publisher = p.ToProto(db) };
return new GetPublisherResponse { Publisher = p.ToProto() };
}
public override async Task<ListPublishersResponse> GetPublisherBatch(
@@ -42,7 +43,7 @@ public class PublisherServiceGrpc(PublisherService service, AppDatabase db)
if (ids.Count == 0) return new ListPublishersResponse();
var list = await db.Publishers.Where(p => ids.Contains(p.Id)).ToListAsync();
var resp = new ListPublishersResponse();
resp.Publishers.AddRange(list.Select(p => p.ToProto(db)));
resp.Publishers.AddRange(list.Select(p => p.ToProto()));
return resp;
}
@@ -63,7 +64,7 @@ public class PublisherServiceGrpc(PublisherService service, AppDatabase db)
var list = await query.ToListAsync();
var resp = new ListPublishersResponse();
resp.Publishers.AddRange(list.Select(p => p.ToProto(db)));
resp.Publishers.AddRange(list.Select(p => p.ToProto()));
return resp;
}

View File

@@ -1,6 +1,5 @@
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Sphere.Post;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@@ -51,7 +50,7 @@ public class PublisherSubscriptionController(
[HttpPost("{name}/subscribe")]
[Authorize]
public async Task<ActionResult<PublisherSubscription>> Subscribe(
public async Task<ActionResult<SnPublisherSubscription>> Subscribe(
string name,
[FromBody] SubscribeRequest request)
{
@@ -104,7 +103,7 @@ public class PublisherSubscriptionController(
/// <returns>List of active subscriptions</returns>
[HttpGet("subscriptions")]
[Authorize]
public async Task<ActionResult<List<PublisherSubscription>>> GetCurrentSubscriptions()
public async Task<ActionResult<List<SnPublisherSubscription>>> GetCurrentSubscriptions()
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();

View File

@@ -38,7 +38,7 @@ public class PublisherSubscriptionService(
/// <param name="accountId">The account ID</param>
/// <param name="publisherId">The publisher ID</param>
/// <returns>The subscription or null if not found</returns>
public async Task<PublisherSubscription?> GetSubscriptionAsync(Guid accountId, Guid publisherId)
public async Task<SnPublisherSubscription?> GetSubscriptionAsync(Guid accountId, Guid publisherId)
{
return await db.PublisherSubscriptions
.Include(ps => ps.Publisher)
@@ -137,7 +137,7 @@ public class PublisherSubscriptionService(
/// </summary>
/// <param name="accountId">The account ID</param>
/// <returns>A list of active subscriptions</returns>
public async Task<List<PublisherSubscription>> GetAccountSubscriptionsAsync(Guid accountId)
public async Task<List<SnPublisherSubscription>> GetAccountSubscriptionsAsync(Guid accountId)
{
return await db.PublisherSubscriptions
.Include(ps => ps.Publisher)
@@ -150,7 +150,7 @@ public class PublisherSubscriptionService(
/// </summary>
/// <param name="publisherId">The publisher ID</param>
/// <returns>A list of active subscriptions</returns>
public async Task<List<PublisherSubscription>> GetPublisherSubscribersAsync(Guid publisherId)
public async Task<List<SnPublisherSubscription>> GetPublisherSubscribersAsync(Guid publisherId)
{
return await db.PublisherSubscriptions
.Where(ps => ps.PublisherId == publisherId && ps.Status == PublisherSubscriptionStatus.Active)
@@ -164,7 +164,7 @@ public class PublisherSubscriptionService(
/// <param name="publisherId">The publisher ID</param>
/// <param name="tier">Optional subscription tier</param>
/// <returns>The created subscription</returns>
public async Task<PublisherSubscription> CreateSubscriptionAsync(
public async Task<SnPublisherSubscription> CreateSubscriptionAsync(
Guid accountId,
Guid publisherId,
int tier = 0
@@ -187,7 +187,7 @@ public class PublisherSubscriptionService(
}
// Create a new subscription
var subscription = new PublisherSubscription
var subscription = new SnPublisherSubscription
{
AccountId = accountId,
PublisherId = publisherId,