This commit is contained in:
96
BimAI.API/Controllers/AuthController.cs
Normal file
96
BimAI.API/Controllers/AuthController.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System.Security.Claims;
|
||||
using BimAI.API.Services;
|
||||
using BimAI.Application.DTOModels;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BimAI.API.Controllers;
|
||||
|
||||
public class AuthController(
|
||||
GoogleAuthService googleAuthService,
|
||||
JwtTokenService jwtTokenService,
|
||||
ILogger<AuthController> logger)
|
||||
: ControllerBase
|
||||
{
|
||||
[HttpPost("google")]
|
||||
public async Task<IActionResult> GoogleAuth([FromBody] GoogleAuthRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.IdToken))
|
||||
{
|
||||
return BadRequest(new GoogleAuthResponse
|
||||
{
|
||||
Success = false,
|
||||
Error = "IdToken is required"
|
||||
});
|
||||
}
|
||||
|
||||
var (isValid, user, error) = await googleAuthService.ValidateGoogleTokenAsync(request.IdToken);
|
||||
|
||||
if (!isValid || user == null)
|
||||
{
|
||||
var statusCode = error switch
|
||||
{
|
||||
"User not authorized to access this application" => 403,
|
||||
"User account is not active" => 403,
|
||||
"Invalid Google token" => 401,
|
||||
_ => 401
|
||||
};
|
||||
|
||||
return StatusCode(statusCode, new GoogleAuthResponse
|
||||
{
|
||||
Success = false,
|
||||
Error = error ?? "Authentication failed"
|
||||
});
|
||||
}
|
||||
|
||||
var jwt = jwtTokenService.GenerateToken(user);
|
||||
|
||||
return Ok(new GoogleAuthResponse
|
||||
{
|
||||
Success = true,
|
||||
Token = jwt,
|
||||
User = new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Email = user.Email,
|
||||
FullName = user.FullName,
|
||||
IsActive = user.IsActive,
|
||||
LastLoginAt = user.LastLoginAt
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error during Google authentication");
|
||||
return StatusCode(500, new GoogleAuthResponse
|
||||
{
|
||||
Success = false,
|
||||
Error = "Internal server error"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
public IActionResult GetCurrentUser()
|
||||
{
|
||||
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier);
|
||||
var emailClaim = User.FindFirst(ClaimTypes.Email);
|
||||
var nameClaim = User.FindFirst(ClaimTypes.Name);
|
||||
|
||||
if (userIdClaim == null || emailClaim == null || nameClaim == null)
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
return Ok(new UserDto
|
||||
{
|
||||
Id = Guid.Parse(userIdClaim.Value),
|
||||
Email = emailClaim.Value,
|
||||
FullName = nameClaim.Value,
|
||||
IsActive = true,
|
||||
});
|
||||
}
|
||||
}
|
||||
71
BimAI.API/Controllers/ProductsController.cs
Normal file
71
BimAI.API/Controllers/ProductsController.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using BimAI.Application.DTOModels;
|
||||
using BimAI.Application.DTOModels.Common;
|
||||
using BimAI.Infrastructure.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BimAI.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ProductsController(BimAIDbContext context) : ControllerBase
|
||||
{
|
||||
private readonly BimAIDbContext _context = context;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts([FromQuery] ProductFilterRequest request)
|
||||
{
|
||||
var query = _context.Products.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Search))
|
||||
{
|
||||
var searchTerm = request.Search.ToLower();
|
||||
query = query.Where(x =>
|
||||
x.Name.ToLower().Contains(searchTerm) ||
|
||||
(x.Code != null && x.Code.ToLower().Contains(searchTerm)) ||
|
||||
(x.Ean != null && x.Ean.ToLower().Contains(searchTerm))
|
||||
);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||
{
|
||||
query = query.Where(x => x.Name.ToLower().Contains(request.Name.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Code))
|
||||
{
|
||||
query = query.Where(x => x.Code != null && x.Code.ToLower().Contains(request.Code.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Ean))
|
||||
{
|
||||
query = query.Where(x => x.Ean != null && x.Ean.ToLower().Contains(request.Ean.ToLower()));
|
||||
}
|
||||
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Name)
|
||||
.Skip((request.Page -1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.Select(x => new ProductDto
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Code = x.Code ?? string.Empty,
|
||||
Ean = x.Ean ?? string.Empty,
|
||||
StockAddresses = x.StockAddresses ?? string.Empty,
|
||||
CreatedAt = x.CreatedAt,
|
||||
UpdatedAt = x.UpdatedAt
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return Ok(new PagedResult<ProductDto>
|
||||
{
|
||||
Items = items,
|
||||
TotalCount = totalCount,
|
||||
Page = request.Page,
|
||||
PageSize = request.PageSize,
|
||||
});
|
||||
}
|
||||
}
|
||||
16
BimAI.API/Controllers/SyncController.cs
Normal file
16
BimAI.API/Controllers/SyncController.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using BimAI.Infrastructure.Sync;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BimAI.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class SyncController(ProductSyncService productSyncService) : ControllerBase
|
||||
{
|
||||
[HttpPost("run-product-sync")]
|
||||
public async Task<IActionResult> RunProductSync()
|
||||
{
|
||||
await productSyncService.RunAsync();
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user