Files
IM_NEW/GroupService.WebApi/Controllers/GroupMember/GroupMemberController.cs
T

65 lines
2.1 KiB
C#

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;
namespace GroupService.WebApi.Controllers.GroupMember
{
[Route("api/[controller]/[action]")]
[ApiController]
public class GroupMemberController : ControllerBase
{
private readonly GroupMemberService service;
private readonly IConfiguration configuration;
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)
{
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)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
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)));
}
}
}