fix: align backend APIs and upload flow
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using GroupService.Domain.Events;
|
||||
using IM.Commons.IntegrationEvents;
|
||||
using MassTransit;
|
||||
using MediatR;
|
||||
|
||||
namespace GroupService.WebApi.Application.EventHandler
|
||||
{
|
||||
public class GroupMemberLeftHandler(IPublishEndpoint endpoint)
|
||||
: INotificationHandler<GroupMemberLeftDomainEvent>
|
||||
{
|
||||
public Task Handle(GroupMemberLeftDomainEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
return endpoint.Publish(
|
||||
new GroupMemberLeftEvent(notification.Member.UserId, notification.Member.GroupId),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper;
|
||||
using GroupService.Domain.IReposities;
|
||||
using GroupService.WebApi.Application.Dtos;
|
||||
using IM.Commons;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GroupService.WebApi.Application.Group
|
||||
{
|
||||
@@ -9,29 +10,33 @@ namespace GroupService.WebApi.Application.Group
|
||||
{
|
||||
private readonly IGroupReposity reposity;
|
||||
private readonly IGroupMemberReposity memberReposity;
|
||||
private readonly IMapper mapper;
|
||||
private readonly IMapper mapper; private readonly IM.InitCommon.Management.RuntimePolicy runtime; private readonly global::GroupService.Infrastructure.GroupDbContext db;
|
||||
|
||||
public GroupService(IGroupReposity reposity, IGroupMemberReposity memberReposity, IMapper mapper)
|
||||
public GroupService(IGroupReposity reposity, IGroupMemberReposity memberReposity, IMapper mapper, IM.InitCommon.Management.RuntimePolicy runtime, global::GroupService.Infrastructure.GroupDbContext db)
|
||||
{
|
||||
this.reposity = reposity;
|
||||
this.memberReposity = memberReposity;
|
||||
this.mapper = mapper;
|
||||
this.mapper = mapper; this.runtime = runtime; this.db = db;
|
||||
}
|
||||
|
||||
public async Task<Result<GroupResponse>> CreateAsync(GroupCreateCommand command)
|
||||
{
|
||||
var policy = runtime.Current;
|
||||
if (policy.CreatedGroupLimit > 0 && await db.Groups.CountAsync(x => x.GroupMaster == command.GroupMasterId) >= policy.CreatedGroupLimit)
|
||||
return Result.Fail<GroupResponse>(ResultCode.PERMISSION_DENIED, "创建群组数量已达到平台上限");
|
||||
var group = new Domain.Entities.Group(command.GroupMasterId, command.Name);
|
||||
group.Update(null, (Domain.Enums.GroupAuthorityType)policy.DefaultJoinAuthority, null, null);
|
||||
reposity.Create(group);
|
||||
return Result<GroupResponse>.Success(mapper.Map<GroupResponse>(group));
|
||||
}
|
||||
|
||||
public async Task<Result<List<GroupResponse>>> GetAllAsync(Guid userId)
|
||||
{
|
||||
var groups = await reposity.FindByMasterIdAsync(userId);
|
||||
var groups = await reposity.FindByMemberIdAsync(userId);
|
||||
return Result<List<GroupResponse>>.Success(mapper.Map<List<GroupResponse>>(groups));
|
||||
}
|
||||
|
||||
public async Task<Result<GroupResponse>> GetByIdAsync(Guid groupId)
|
||||
public async Task<Result<GroupResponse>> GetByIdAsync(Guid groupId, Guid userId)
|
||||
{
|
||||
var group = await reposity.FindByIdAsync(groupId);
|
||||
if (group is null)
|
||||
@@ -39,9 +44,36 @@ namespace GroupService.WebApi.Application.Group
|
||||
return Result<GroupResponse>.Fail(ResultCode.GROUP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (!await memberReposity.CheckMemberExistAsync(groupId, userId))
|
||||
{
|
||||
return Result<GroupResponse>.Fail(ResultCode.PERMISSION_DENIED);
|
||||
}
|
||||
|
||||
return Result<GroupResponse>.Success(mapper.Map<GroupResponse>(group));
|
||||
}
|
||||
|
||||
public async Task<Result<object>> DissolveAsync(Guid groupId, Guid userId)
|
||||
{
|
||||
var group = await reposity.FindByIdAsync(groupId);
|
||||
if (group is null)
|
||||
{
|
||||
return Result.Fail(ResultCode.GROUP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (group.GroupMaster != userId)
|
||||
{
|
||||
return Result.Fail(ResultCode.PERMISSION_DENIED);
|
||||
}
|
||||
|
||||
var members = await memberReposity.FindByGroupIdAsync(groupId);
|
||||
foreach (var member in members)
|
||||
{
|
||||
member.Leave();
|
||||
}
|
||||
group.SoftDelete();
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result<GroupResponse>> UpdateAsync(GroupUpdateCommand command)
|
||||
{
|
||||
var group = await reposity.FindByIdAsync(command.GroupId);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper;
|
||||
using GroupService.Domain.IReposities;
|
||||
using GroupService.Domain.ValueObjects;
|
||||
using GroupService.WebApi.Application.Dtos;
|
||||
@@ -46,7 +46,7 @@ namespace GroupService.WebApi.Application.GroupInvitation
|
||||
return Result.Success(mapper.Map<GroupInvitationResponse>(existing));
|
||||
}
|
||||
|
||||
var group = await groupReposity.FindByIdAsync(groupId);
|
||||
var group = await groupReposity.FindByIdAsync(groupId); if (group?.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组不可用或已被封禁");
|
||||
|
||||
var userProfile = new UserProfile()
|
||||
{
|
||||
@@ -75,7 +75,7 @@ namespace GroupService.WebApi.Application.GroupInvitation
|
||||
|
||||
public async Task<Result<object>> CreateBatchAsync(Guid operatorId, List<Guid> userIds, Guid groupId)
|
||||
{
|
||||
var group = await groupReposity.FindByIdAsync(groupId);
|
||||
var group = await groupReposity.FindByIdAsync(groupId); if (group?.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组不可用或已被封禁");
|
||||
if (group is null)
|
||||
return Result.Fail(ResultCode.GROUP_NOT_FOUND);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper;
|
||||
using GroupService.Domain;
|
||||
using GroupService.Domain.IReposities;
|
||||
using GroupService.WebApi.Application.Dtos;
|
||||
@@ -13,24 +13,28 @@ namespace GroupService.WebApi.Application.GroupMember
|
||||
private readonly IGroupReposity groupReposity;
|
||||
private readonly GroupMemberDomainService service;
|
||||
private readonly IIdentityIntegrationService idService;
|
||||
private IMapper mapper;
|
||||
private IMapper mapper; private readonly IM.InitCommon.Management.RuntimePolicy runtime;
|
||||
|
||||
public GroupMemberService(IGroupMemberReposity reposity, IGroupReposity groupReposity, GroupMemberDomainService service, IIdentityIntegrationService idService, IMapper mapper)
|
||||
public GroupMemberService(IGroupMemberReposity reposity, IGroupReposity groupReposity, GroupMemberDomainService service, IIdentityIntegrationService idService, IMapper mapper, IM.InitCommon.Management.RuntimePolicy runtime)
|
||||
{
|
||||
this.reposity = reposity;
|
||||
this.groupReposity = groupReposity;
|
||||
this.service = service;
|
||||
this.idService = idService;
|
||||
this.mapper = mapper;
|
||||
this.mapper = mapper; this.runtime = runtime;
|
||||
}
|
||||
|
||||
public async Task<Result<List<GroupMemberResponse>>> GetByGroupIdAsync(Guid groupId)
|
||||
public async Task<Result<List<GroupMemberResponse>>> GetByGroupIdAsync(Guid groupId, Guid userId)
|
||||
{
|
||||
var group = await groupReposity.FindByIdAsync(groupId);
|
||||
if (group is null)
|
||||
{
|
||||
return Result<List<GroupMemberResponse>>.Fail(ResultCode.GROUP_NOT_FOUND);
|
||||
}
|
||||
if (!await reposity.CheckMemberExistAsync(groupId, userId))
|
||||
{
|
||||
return Result<List<GroupMemberResponse>>.Fail(ResultCode.PERMISSION_DENIED);
|
||||
}
|
||||
var members = await reposity.FindByGroupIdAsync(groupId);
|
||||
|
||||
return Result<List<GroupMemberResponse>>.Success(mapper.Map<List<GroupMemberResponse>>(members.ToList()));
|
||||
@@ -45,6 +49,9 @@ namespace GroupService.WebApi.Application.GroupMember
|
||||
return Result<GroupMemberResponse>.Fail(ResultCode.GROUP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (group.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组已被封禁,不能加入");
|
||||
var limit = runtime.Current.GroupMemberLimit;
|
||||
if (limit > 0 && (await reposity.FindByGroupIdAsync(groupId)).Count() >= limit) throw new IM.DomainCommons.DomainException("群成员数量已达到平台上限");
|
||||
var userRes = await idService.FindUserByIdAsync(userId);
|
||||
if (!userRes.Succeeded)
|
||||
{
|
||||
@@ -64,7 +71,7 @@ namespace GroupService.WebApi.Application.GroupMember
|
||||
|
||||
public async Task<Result<bool>> CheckMemberAsync(Guid groupId, Guid userId)
|
||||
{
|
||||
var exist = await reposity.CheckMemberExistAsync(groupId, userId);
|
||||
var group = await groupReposity.FindByIdAsync(groupId); var exist = group?.Status == Domain.Enums.GroupState.Normal && await reposity.CheckMemberExistAsync(groupId, userId);
|
||||
|
||||
return Result.Success(exist);
|
||||
}
|
||||
@@ -78,14 +85,32 @@ namespace GroupService.WebApi.Application.GroupMember
|
||||
}
|
||||
|
||||
var operatorMember = await reposity.FindOneByGroupIdAndUserIdAsync(member.GroupId, operatorId);
|
||||
if (operatorMember is null || operatorMember.Role == Domain.Enums.GroupMemberRole.Normal)
|
||||
if (operatorMember is null || operatorMember.Id == member.Id ||
|
||||
member.Role == Domain.Enums.GroupMemberRole.Master ||
|
||||
operatorMember.Role <= member.Role)
|
||||
{
|
||||
return Result.Fail(ResultCode.PERMISSION_DENIED);
|
||||
}
|
||||
|
||||
member.SoftDelete();
|
||||
member.Leave();
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result<object>> LeaveAsync(Guid groupId, Guid userId)
|
||||
{
|
||||
var member = await reposity.FindOneByGroupIdAndUserIdAsync(groupId, userId);
|
||||
if (member is null)
|
||||
{
|
||||
return Result.Fail(ResultCode.GROUP_MEMBER_NOT_FOUNT);
|
||||
}
|
||||
if (member.Role == Domain.Enums.GroupMemberRole.Master)
|
||||
{
|
||||
return Result.Fail<object>(ResultCode.PERMISSION_DENIED, "群主不能直接退群,请使用解散群接口");
|
||||
}
|
||||
|
||||
member.Leave();
|
||||
return Result.Success();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper;
|
||||
using GroupService.Domain.Entities;
|
||||
using GroupService.Domain.IReposities;
|
||||
using GroupService.Domain.ValueObjects;
|
||||
@@ -33,6 +33,8 @@ namespace GroupService.WebApi.Application.GroupRequest
|
||||
return Result.Fail<GroupRequestResponse>(ResultCode.GROUP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (group.Status != Domain.Enums.GroupState.Normal || group.Authority == Domain.Enums.GroupAuthorityType.NOT_ALLOWED_TO_JOIN)
|
||||
return Result.Fail<GroupRequestResponse>(ResultCode.PERMISSION_DENIED, "群组当前不允许加入");
|
||||
var user = await idService.FindUserByIdAsync(userId);
|
||||
|
||||
var groupProfile = new GroupProfile()
|
||||
@@ -98,7 +100,7 @@ namespace GroupService.WebApi.Application.GroupRequest
|
||||
}
|
||||
public async Task<Result<List<GroupRequestResponse>>> GetListAsync(Guid userId)
|
||||
{
|
||||
var list = await reposity.FindByUserIdAsync(userId);
|
||||
var list = await reposity.FindVisibleToUserAsync(userId);
|
||||
return Result.Success(mapper.Map<List<GroupRequestResponse>>(list.ToList()));
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,8 @@ namespace GroupService.WebApi.Controllers.Group
|
||||
[ProducesDefaultResponseType(typeof(Result<GroupResponse>))]
|
||||
public async Task<IActionResult> GetOne(Guid groupId)
|
||||
{
|
||||
return Ok(await service.GetByIdAsync(groupId));
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
return Ok(await service.GetByIdAsync(groupId, Guid.Parse(userId)));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -49,5 +50,13 @@ namespace GroupService.WebApi.Controllers.Group
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
return Ok(await service.UpdateAsync(new GroupUpdateCommand(Guid.Parse(userId), request.GroupId, request.Avatar, request.GroupName, request.Description)));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[UnitOfWork(typeof(GroupDbContext))]
|
||||
public async Task<IActionResult> Dissolve([FromQuery] Guid groupId)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
return Ok(await service.DissolveAsync(groupId, Guid.Parse(userId)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using GroupService.Infrastructure;
|
||||
using GroupService.WebApi.Application.GroupMember;
|
||||
using IM.ASPNETCore;
|
||||
using IM.Commons;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
@@ -11,25 +13,36 @@ namespace GroupService.WebApi.Controllers.GroupMember
|
||||
public class GroupMemberController : ControllerBase
|
||||
{
|
||||
private readonly GroupMemberService service;
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
public GroupMemberController(GroupMemberService service)
|
||||
public GroupMemberController(GroupMemberService service, IConfiguration configuration)
|
||||
{
|
||||
this.service = service;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> CheckMember(Guid userId, Guid groupId)
|
||||
{
|
||||
var expectedKey = configuration["InternalApiKey"];
|
||||
var suppliedKey = Request.Headers["X-Internal-Api-Key"].ToString();
|
||||
if (string.IsNullOrWhiteSpace(expectedKey) || suppliedKey != expectedKey)
|
||||
{
|
||||
return Unauthorized(Result.Fail(ResultCode.AUTH_FAILED));
|
||||
}
|
||||
return Ok(await service.CheckMemberAsync(groupId, userId));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> List(Guid groupId)
|
||||
{
|
||||
return Ok(await service.GetByGroupIdAsync(groupId));
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
return Ok(await service.GetByGroupIdAsync(groupId, Guid.Parse(userId)));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
[UnitOfWork(typeof(GroupDbContext))]
|
||||
public async Task<IActionResult> Delete([FromQuery] Guid memberId)
|
||||
{
|
||||
@@ -37,6 +50,15 @@ namespace GroupService.WebApi.Controllers.GroupMember
|
||||
return Ok(await service.DeleteAsync(memberId, Guid.Parse(userId)));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
[UnitOfWork(typeof(GroupDbContext))]
|
||||
public async Task<IActionResult> Leave([FromQuery] Guid groupId)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
return Ok(await service.LeaveAsync(groupId, Guid.Parse(userId)));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using GroupService.Infrastructure;
|
||||
using GroupService.Domain.Enums;
|
||||
using IM.InitCommon.Management;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GroupService.WebApi.Controllers;
|
||||
[ApiController, Route("internal/management")]
|
||||
public sealed class ManagementController(GroupDbContext db) : ControllerBase
|
||||
{
|
||||
[HttpGet("summary")] public async Task<object> Summary() => new { total = await db.Groups.CountAsync() };
|
||||
[HttpGet("list")] public async Task<object> List(string? q, string? status, int page = 1, int size = 8)
|
||||
{
|
||||
page = Math.Max(1, page); size = Math.Clamp(size, 1, 100);
|
||||
var query = db.Groups.AsNoTracking().Where(x => q == null || x.Name.Contains(q) || x.Id.ToString() == q || x.GroupMaster.ToString() == q || db.GroupMembers.Any(m => m.GroupId == x.Id && m.UserId == x.GroupMaster && m.GroupNickName.Contains(q)));
|
||||
if (!string.IsNullOrEmpty(status)) query = query.Where(x => x.Status == (status == "封禁" ? GroupState.Blocked : GroupState.Normal));
|
||||
return new { items = await query.OrderByDescending(x => x.CreationTime).Skip((page - 1) * size).Take(size).Select(x => new { x.Id, x.Name, ownerId = x.GroupMaster, ownerName = db.GroupMembers.Where(m => m.GroupId == x.Id && m.UserId == x.GroupMaster).Select(m => m.GroupNickName).FirstOrDefault(), memberCount = db.GroupMembers.Count(m => m.GroupId == x.Id), status = x.Status == GroupState.Blocked ? "封禁" : "正常", createdAt = x.CreationTime }).ToListAsync(), total = await query.CountAsync(), page, size };
|
||||
}
|
||||
[HttpGet("detail/{id:guid}")] public async Task<IActionResult> Detail(Guid id)
|
||||
{
|
||||
var g = await db.Groups.AsNoTracking().SingleOrDefaultAsync(x => x.Id == id); if (g is null) return NotFound();
|
||||
return Ok(new { g.Id, g.Name, ownerId = g.GroupMaster, g.Announcement, authority = g.Authority, memberCount = await db.GroupMembers.CountAsync(x => x.GroupId == id), status = g.Status == GroupState.Blocked ? "封禁" : "正常", createdAt = g.CreationTime });
|
||||
}
|
||||
[HttpGet("members/{id:guid}")] public async Task<object> Members(Guid id, string? q, int page = 1, int size = 8)
|
||||
{
|
||||
page = Math.Max(1, page); size = Math.Clamp(size, 1, 100); var query = db.GroupMembers.AsNoTracking().Where(x => x.GroupId == id && (q == null || x.GroupNickName.Contains(q) || x.UserId.ToString() == q));
|
||||
return new { items = await query.OrderBy(x => x.UserId).Skip((page - 1) * size).Take(size).Select(x => new { id = x.UserId, name = x.GroupNickName, x.Role }).ToListAsync(), total = await query.CountAsync(), page, size };
|
||||
}
|
||||
[HttpGet("user/{id:guid}/groups")] public async Task<object> UserGroups(Guid id) => await db.Groups.Where(x => db.GroupMembers.Any(m => m.GroupId == x.Id && m.UserId == id)).Select(x => new { x.Id, x.Name, status = x.Status == GroupState.Blocked ? "封禁" : "正常" }).Take(100).ToListAsync();
|
||||
[HttpGet("access/{id:guid}/{userId:guid}")] public async Task<object> Access(Guid id, Guid userId) => new { exists = await db.Groups.AnyAsync(x => x.Id == id), enabled = await db.Groups.AnyAsync(x => x.Id == id && x.Status == GroupState.Normal), member = await db.GroupMembers.AnyAsync(x => x.GroupId == id && x.UserId == userId) };
|
||||
[HttpPost("action")] public async Task<IActionResult> Action(InternalAction command, CancellationToken ct)
|
||||
{
|
||||
if (command.Action is not "封禁" and not "解封") return BadRequest();
|
||||
return Ok(await ReceiptStore.Execute(db, command, async () => {
|
||||
var g = await db.Groups.SingleOrDefaultAsync(x => x.Id == command.TargetId, ct) ?? throw new InvalidOperationException("群组不存在");
|
||||
var before = g.Status == GroupState.Blocked ? "封禁" : "正常"; if (command.Action == "封禁") g.Ban(notify: false); else g.Unban();
|
||||
return new ActionReceipt(g.Name, before, command.Action == "封禁" ? "封禁" : "正常");
|
||||
}, ct));
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using IM.InitCommon.Management;
|
||||
|
||||
using IM.InitCommon;
|
||||
|
||||
@@ -19,6 +20,7 @@ namespace GroupService.WebApi
|
||||
builder.ConfigExtraServices();
|
||||
|
||||
var app = builder.Build();
|
||||
if (app.ApplyMigrationsIfRequested(args)) return;
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
@@ -28,6 +30,7 @@ namespace GroupService.WebApi
|
||||
}
|
||||
|
||||
app.UseAppDefault();
|
||||
app.MapManagementHealth();
|
||||
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"InternalApiKey": "development-only-change-me",
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
@@ -5,5 +5,6 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"InternalApiKey": ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user