PIAPS_CW/RestAPI/Controllers/UserController.cs

163 lines
4.6 KiB
C#
Raw Normal View History

using BusinessLogic.BusinessLogic;
2024-06-19 22:30:34 +04:00
using BusinessLogic.Tools.Mail.MailTemplates;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Exceptions;
using Contracts.SearchModels;
using Contracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace RestAPI.Controllers
{
2024-06-19 22:30:34 +04:00
[Route("[controller]/[action]")]
[ApiController]
public class UserController
{
private readonly ILogger _logger;
private readonly IUserLogic _userLogic;
2024-06-19 22:30:34 +04:00
public UserController(ILogger<UserController> logger, IUserLogic userLogic)
{
_userLogic = userLogic;
_logger = logger;
}
2024-06-19 22:30:34 +04:00
[HttpPost]
public IResult Login([FromBody] UserData data)
{
try
{
var res = _userLogic.Login(data.email, data.password);
return Results.Ok(res);
}
catch (ElementNotFoundException ex)
{
_logger.LogInformation(ex, "User not found");
return Results.NoContent();
}
catch (AccountException ex)
{
_logger.LogWarning(ex, "Wrong login data");
return Results.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error get user");
return Results.Problem(ex.Message);
}
}
2024-06-22 18:42:11 +04:00
[HttpPost]
public IResult VerifyCode([FromBody] VerifyCodeData data)
{
try
{
var res = _userLogic.VerifyCode(data.code);
return Results.Ok(res);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error verify code");
return Results.Problem(ex.Message);
}
}
2024-06-19 22:30:34 +04:00
[HttpPost]
public IResult Registration([FromBody] UserBindingModel model)
{
try
{
var res = _userLogic.Create(model);
return Results.Ok(res);
}
catch (AccountException ex)
{
_logger.LogWarning(ex, "Wrong registration data");
return Results.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error create user");
return Results.Problem(ex.Message);
}
}
2024-06-19 22:30:34 +04:00
[HttpGet]
public IResult Get([FromQuery] UserSearchModel model)
{
try
{
var res = _userLogic.ReadElement(model);
return Results.Ok(res);
}
catch (ElementNotFoundException ex)
{
_logger.LogInformation(ex, "User not found");
return Results.NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error get user");
return Results.Problem(ex.Message);
}
}
2024-06-19 22:30:34 +04:00
[HttpPatch]
public IResult Update([FromBody] UserBindingModel model)
{
try
{
var res = _userLogic.Update(model);
return Results.Ok(res);
}
catch (AccountException ex)
{
_logger.LogWarning(ex, "Wrong update data");
return Results.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error update user");
return Results.Problem(ex.Message);
}
}
2024-06-19 22:30:34 +04:00
[HttpDelete]
public IResult Delete(Guid id)
{
try
{
var res = _userLogic.Delete(new() { Id = id });
return Results.Ok(res);
}
catch (ElementNotFoundException ex)
{
_logger.LogInformation(ex, "User not found");
return Results.NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error delete user");
return Results.Problem(ex.Message);
}
}
[HttpPatch]
public void UpdateBonus([FromBody] BonusUpdateBindingModel model)
{
try
{
_userLogic.UpdateBonus(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error update purchase");
return;
}
}
}
2024-06-19 22:30:34 +04:00
public record class UserData(string email, string password);
2024-06-22 18:42:11 +04:00
public record class VerifyCodeData(string code);
}