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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,11 +27,11 @@ namespace GroupService.WebApi.Application.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 +39,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);
|
||||
|
||||
@@ -24,13 +24,17 @@ namespace GroupService.WebApi.Application.GroupMember
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
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()));
|
||||
@@ -78,14 +82,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +98,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)));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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