Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 35 additions & 41 deletions src/GuruPR.API/Controllers/v1/AccountController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using Asp.Versioning;

using GuruPR.Application.Interfaces.Application;
using GuruPR.Application.Interfaces.Infrastructure;
using GuruPR.Domain.Requests;
using GuruPR.Extensions;
using GuruPR.Infrastructure.Identity.Constants;
using GuruPR.Application.Features.Account.Commands.ConfirmEmail;
using GuruPR.Application.Features.Account.Commands.ExternalLogin.Google.GoogleCallback;
using GuruPR.Application.Features.Account.Commands.ExternalLogin.Google.GoogleLogin;
using GuruPR.Application.Features.Account.Commands.Login;
using GuruPR.Application.Features.Account.Commands.Logout;
using GuruPR.Application.Features.Account.Commands.RefreshToken;
using GuruPR.Application.Features.Account.Commands.Register;
using GuruPR.Application.Features.Users.Dtos;
using GuruPR.Application.Features.Users.Queries.GetCurrentUser;

using MediatR;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -18,47 +24,37 @@ namespace GuruPR.Controllers.v1;
public class AccountController : ControllerBase
{
private readonly ILogger<AccountController> _logger;
private readonly IAccountService _accountService;
private readonly IExternalAuthService _externalAuthService;
private readonly IMediator _mediator;

public AccountController(ILogger<AccountController> logger,
IAccountService accountService,
IExternalAuthService externalAuthService)
IMediator mediator)
{
_logger = logger;
_accountService = accountService;
_externalAuthService = externalAuthService;
_mediator = mediator;
}

[HttpPost("register")]
[AllowAnonymous]
public async Task<IActionResult> RegisterAsync([FromBody] RegisterRequest registerRequest)
public async Task<IActionResult> RegisterAsync([FromBody] RegisterCommand registerCommand)
{
await _accountService.RegisterAsync(registerRequest);
await _mediator.Send(registerCommand);

return Ok("Registration succeeded.");
}

[HttpPost("login")]
[AllowAnonymous]
public async Task<IActionResult> LoginAsync([FromBody] LoginRequest loginRequest)
public async Task<IActionResult> LoginAsync([FromBody] LoginCommand loginCommand)
{
await _accountService.LoginAsync(loginRequest);
await _mediator.Send(loginCommand);

return Ok("Login was successful.");
}

[HttpPost("refresh")]
public async Task<IActionResult> RefreshTokenAsync([FromBody] RefreshRequest refreshRequest)
public async Task<IActionResult> RefreshTokenAsync([FromBody] RefreshTokenCommand refreshTokenCommand)
{
var userId = User.GetClaimValue(JwtClaimTypes.Subject);

if (userId == null)
{
return Unauthorized("Invalid token or missing subject claim.");
}

await _accountService.RefreshTokenAsync(userId, refreshRequest.RefreshToken);
await _mediator.Send(refreshTokenCommand);

return Ok("Token refresh has succeeded.");
}
Expand All @@ -67,26 +63,16 @@ public async Task<IActionResult> RefreshTokenAsync([FromBody] RefreshRequest ref
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmailAsync(string userId, string token)
{
try
{
await _accountService.ConfirmEmailAsync(userId, token);
var redirectUrl = await _accountService.GetEmailConfirmationRedirectUrlAsync(true);

return Redirect(redirectUrl);
}
catch (Exception)
{
var redirectUrl = await _accountService.GetEmailConfirmationRedirectUrlAsync(false);

return Redirect(redirectUrl);
}
await _mediator.Send(new ConfirmEmailCommand(userId, token));

return Ok("Email confirmation succeeded.");
}

[HttpGet("login/google")]
[AllowAnonymous]
public async Task<IActionResult> GoogleLoginAsync([FromQuery] string? returnUrl)
{
var challengeResult = await _externalAuthService.InitiateGoogleLoginAsync(returnUrl, HttpContext);
var challengeResult = await _mediator.Send(new GoogleLoginCommand(returnUrl));

return challengeResult;
}
Expand All @@ -95,16 +81,24 @@ public async Task<IActionResult> GoogleLoginAsync([FromQuery] string? returnUrl)
[AllowAnonymous]
public async Task<IActionResult> GoogleLoginCallbackAsync([FromQuery] string returnUrl)
{
var redirectUrl = await _externalAuthService.HandleGoogleCallbackAsync(returnUrl, HttpContext);
var redirectUrl = await _mediator.Send(new GoogleCallbackCommand(returnUrl));

return Redirect(redirectUrl);
}

[HttpPost("logout")]
public async Task<IActionResult> LogoutAsync([FromBody] LogoutRequest logoutRequest)
public async Task<IActionResult> LogoutAsync([FromBody] LogoutCommand logoutCommand)
{
await _accountService.LogoutAsync(logoutRequest.UserId, logoutRequest.RefreshToken);
await _mediator.Send(logoutCommand);

return Ok("Logout has succeeded.");
}

[HttpGet("me")]
public async Task<ActionResult<UserDto>> GetCurrentUserAsync()
{
var userDto = await _mediator.Send(new GetCurrentUserQuery());

return Ok(userDto);
}
}
64 changes: 30 additions & 34 deletions src/GuruPR.API/Controllers/v1/AgentController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Asp.Versioning;

using AutoMapper;
using GuruPR.Application.Common.Models;
using GuruPR.Application.Features.Agents.Commands.CreateAgent;
using GuruPR.Application.Features.Agents.Commands.DeleteAgent;
using GuruPR.Application.Features.Agents.Commands.UpdateAgent;
using GuruPR.Application.Features.Agents.Dtos;
using GuruPR.Application.Features.Agents.Queries.GetAgentById;
using GuruPR.Application.Features.Agents.Queries.GetAgents;

using GuruPR.Application.Dtos.Agent;
using GuruPR.Application.Interfaces.Application;
using GuruPR.Extensions;
using GuruPR.Infrastructure.Identity.Constants;
using MediatR;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -19,64 +22,57 @@ namespace GuruPR.Controllers.v1;
public class AgentController : ControllerBase
{
private readonly ILogger<AgentController> _logger;
private readonly IMapper _mapper;
private readonly IAgentService _agentService;
private readonly IMediator _mediator;

public AgentController(ILogger<AgentController> logger,
IMapper mapper,
IAgentService agentService)
IMediator mediator)
{
_logger = logger;
_mapper = mapper;
_agentService = agentService;
_mediator = mediator;
}

[HttpGet]
public async Task<IActionResult> GetAllAgentsAsync()
public async Task<ActionResult<PaginatedList<AgentDto>>> GetAllAgentsAsync([FromQuery] GetAgentsQuery getAgentsQuery)
{
var agents = await _agentService.GetAllAgentsAsync();
var agentDtos = _mapper.Map<IEnumerable<AgentDto>>(agents);
var agents = await _mediator.Send(getAgentsQuery);

return Ok(agentDtos);
return Ok(agents);
}

[HttpGet("{agentId}", Name = "GetAgentById")]
public async Task<IActionResult> GetAgentByIdAsync(string agentId)
public async Task<ActionResult<AgentDto>> GetAgentByIdAsync([FromRoute] string agentId)
{
var agent = await _agentService.GetAgentByIdAsync(agentId);
var getAgentByIdQuery = new GetAgentByIdQuery(agentId);
var agent = await _mediator.Send(getAgentByIdQuery);

return Ok(agent);
}

[HttpPost]
public async Task<IActionResult> CreateAgentAsync([FromBody] CreateAgentRequest createAgentRequest)
public async Task<IActionResult> CreateAgentAsync([FromBody] CreateAgentCommand createAgentCommand)
{
var userId = User.GetClaimValue(JwtClaimTypes.Name);
if (userId == null)
{
return Unauthorized("Invalid token or missing subject claim.");
}
var agent = await _mediator.Send(createAgentCommand);

var agent = await _agentService.CreateAgentAsync(createAgentRequest, userId);
var agentDto = _mapper.Map<AgentDto>(agent);

return CreatedAtRoute("GetAgentById", new { agentId = agent.Id }, agentDto);
return CreatedAtRoute("GetAgentById", new { agentId = agent.Id }, agent);
}

[HttpPut("{agentId}")]
public async Task<IActionResult> UpdateAgentAsync([FromBody] UpdateAgentRequest updateAgentRequest, string agentId)
public async Task<ActionResult<AgentDto>> UpdateAgentAsync([FromRoute] string agentId, [FromBody] UpdateAgentCommand updateAgentCommand)
{
var agent = await _agentService.UpdateAgentAsync(agentId, updateAgentRequest);
var agentDto = _mapper.Map<AgentDto>(agent);
updateAgentCommand.Id = agentId;

var agent = await _mediator.Send(updateAgentCommand);

return Ok(agentDto);
return Ok(agent);
}

[HttpDelete("{agentId}")]
public async Task<IActionResult> DeleteAgentAsync(string agentId)
public async Task<IActionResult> DeleteAgentAsync([FromRoute] string agentId)
{
var result = await _agentService.DeleteAgentAsync(agentId);
var deleteAgentCommand = new DeleteAgentCommand(agentId);

await _mediator.Send(deleteAgentCommand);

return result ? NoContent() : NotFound();
return NoContent();
}
}
Loading