添加项目文件。
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using IdentityService.Domain;
|
||||
using IdentityService.WebApi.Applications.Auth;
|
||||
using IdentityService.WebApi.Applications.Dtos;
|
||||
using IdentityService.WebApi.Applications.Dtos.Common;
|
||||
using IM.Commons;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace IdentityService.WebApi.Controllers.Auth
|
||||
{
|
||||
[Route("/api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly AuthService authService;
|
||||
private readonly IIdRepository idRepository;
|
||||
|
||||
public AuthController(AuthService authService, IIdRepository idRepository)
|
||||
{
|
||||
this.authService = authService;
|
||||
this.idRepository = idRepository;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesDefaultResponseType(typeof(Result<LoginResponse>))]
|
||||
public async Task<IActionResult> Login([FromBody] LoginRequest loginRequest)
|
||||
{
|
||||
return Ok(await authService.LoginAsync(loginRequest.UserName, loginRequest.Password));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesDefaultResponseType(typeof(Result<UserResponse>))]
|
||||
public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest)
|
||||
{
|
||||
return Ok(await authService.RegisterAsync(registerRequest.UserName, registerRequest.Password, registerRequest.NickName));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesDefaultResponseType(typeof(Result<LoginResponse>))]
|
||||
public async Task<IActionResult> Refresh([FromBody] RefreshRequest refreshRequest)
|
||||
{
|
||||
return Ok(await authService.RefreshAsync(refreshRequest.RefreshToken));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace IdentityService.WebApi.Controllers.Auth
|
||||
{
|
||||
public class LoginRequest
|
||||
{
|
||||
public string UserName { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
|
||||
public class LoginRequestValidator : AbstractValidator<LoginRequest>
|
||||
{
|
||||
public LoginRequestValidator()
|
||||
{
|
||||
RuleFor(x => x.UserName)
|
||||
.NotEmpty()
|
||||
.NotNull()
|
||||
.MaximumLength(20)
|
||||
.MinimumLength(5);
|
||||
|
||||
RuleFor(x => x.Password)
|
||||
.NotEmpty()
|
||||
.NotNull()
|
||||
.MaximumLength(50)
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace IdentityService.WebApi.Controllers.Auth
|
||||
{
|
||||
public class RefreshRequest
|
||||
{
|
||||
public string RefreshToken { get; set; }
|
||||
}
|
||||
|
||||
public class RefreshTokenValidator : AbstractValidator<RefreshRequest>
|
||||
{
|
||||
public RefreshTokenValidator()
|
||||
{
|
||||
RuleFor(r => r.RefreshToken)
|
||||
.NotEmpty()
|
||||
.NotNull()
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace IdentityService.WebApi.Controllers.Auth
|
||||
{
|
||||
public class RegisterRequest
|
||||
{
|
||||
public string UserName { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string NickName { get; set; }
|
||||
}
|
||||
public class RegisterRequestValidator : AbstractValidator<RegisterRequest>
|
||||
{
|
||||
public RegisterRequestValidator()
|
||||
{
|
||||
RuleFor(r => r.UserName)
|
||||
.NotEmpty()
|
||||
.NotNull()
|
||||
.MaximumLength(20)
|
||||
.MinimumLength(5);
|
||||
|
||||
RuleFor(r => r.Password)
|
||||
.NotEmpty()
|
||||
.NotNull()
|
||||
.MinimumLength(6)
|
||||
.MaximumLength(50);
|
||||
|
||||
RuleFor(r => r.NickName)
|
||||
.NotEmpty()
|
||||
.NotNull()
|
||||
.MaximumLength(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using IdentityService.Infrastructure;
|
||||
using IdentityService.WebApi.Applications.Dtos.Common;
|
||||
using IdentityService.WebApi.Applications.User;
|
||||
using IM.ASPNETCore;
|
||||
using IM.Commons;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IdentityService.WebApi.Controllers.User
|
||||
{
|
||||
[Authorize]
|
||||
[UnitOfWork(typeof(UserDbContext))]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly UserService userService;
|
||||
public UserController(UserService userService)
|
||||
{
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesDefaultResponseType(typeof(Result<UserResponse?>))]
|
||||
public async Task<IActionResult> Me()
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var res = await userService.GetUserInfoAsync(Guid.Parse(userId));
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesDefaultResponseType(typeof(Result<UserResponse?>))]
|
||||
public async Task<IActionResult> Find(Guid userId)
|
||||
{
|
||||
return Ok(await userService.GetUserInfoAsync(userId));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesDefaultResponseType(typeof(Result<UserResponse>))]
|
||||
public async Task<IActionResult> Update([FromBody] UserUpdateRequest request)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var res = await userService.UpdateAsync(new UserUpdateCommand(Guid.Parse(userId), request.NickName, request.Region, request.Avatar, request.Description));
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesDefaultResponseType(typeof(Result<List<UserResponse>>))]
|
||||
public async Task<IActionResult> GetUsersByIds([FromBody] List<Guid> ids)
|
||||
{
|
||||
return Ok(await userService.GetUsersByIdsAsync(ids));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace IdentityService.WebApi.Controllers.User
|
||||
{
|
||||
public class UserUpdateRequest
|
||||
{
|
||||
public string? NickName { get; set; }
|
||||
public string? Region { get; set; }
|
||||
public string? Avatar { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class UserUpdateRequestValidator : AbstractValidator<UserUpdateRequest>
|
||||
{
|
||||
public UserUpdateRequestValidator()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user