This commit is contained in:
Riccardo Usardi committed 2025-07-20 11:49:20 +02:00
1 parent 3a1a0ca7ef
commit 6bce1d9330
9 files changed
+167 -13

No files matched your search

+1 -1
View File
@@ -52,7 +52,7 @@ namespace uWine.Api.Builder
builder.Services.AddTransient<AuthenticationService>(); builder.Services.AddTransient<AuthenticationService>();
builder.Services.AddTransient<OwnerService>(); builder.Services.AddTransient<OwnerService>();
builder.Services.AddTransient<UserService>(); builder.Services.AddTransient<UserService>();
builder.Services.AddTransient<CoordinatesService>(); builder.Services.AddTransient<ICoordinatesService, CoordinatesService>();
return builder; return builder;
} }
+5 -1
View File
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using uWine.Core.Domain.Entities; using uWine.Core.Domain.Entities;
using uWine.Core.Infrastructure.Services; using uWine.Core.Infrastructure.Services;
@@ -35,13 +36,16 @@ namespace uWine.Api.Controllers
} }
[HttpGet("get-area")] [HttpGet("get-area")]
[ProducesResponseType(typeof(Dictionary<int, Coordinates>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetArea(int vineyardId) public IActionResult GetArea(int vineyardId)
{ {
try try
{ {
var area = _coordinatesService.GetArea(vineyardId); var area = _coordinatesService.GetArea(vineyardId);
if (area == null || area.Count == 0) if (area == null || area.Count == 0)
return NotFound("No area found for the given vineyard ID"); return NotFound();
return Ok(area); return Ok(area);
} }
+58
View File
@@ -0,0 +1,58 @@
using Microsoft.AspNetCore.Mvc;
using uWine.Core.Domain.Entities;
using uWine.Core.Infrastructure.Services;
namespace uWine.Api.Controllers
{
[ApiController]
[Route("/[controller]")]
public class OwnerController : Controller
{
private readonly IConfiguration _configuration;
private readonly OwnerService _ownerService;
public OwnerController(IConfiguration configuration, OwnerService ownerService)
{
_configuration = configuration;
_ownerService = ownerService;
}
[HttpGet("get-owner")]
[ProducesResponseType(typeof(Owner), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult Single(int ownerId)
{
{
try
{
var owner = _ownerService.Owner(ownerId);
if (owner == null)
return NotFound(new { status = "Owner not found" });
return Ok(owner);
}
catch
{
return Unauthorized(new { status = 401 });
}
}
}
[HttpPost("save-owner")]
public ActionResult Save(Owner owner)
{
if (owner == null)
return BadRequest("Invalid owner data");
try
{
_ownerService.New(owner);
return Ok();
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
}
}
+8 -4
View File
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Security.Claims; using System.Security.Claims;
using uWine.Core.Domain.Entities;
using uWine.Core.Infrastructure.Services; using uWine.Core.Infrastructure.Services;
namespace uWine.Api.Controllers namespace uWine.Api.Controllers
@@ -13,8 +14,8 @@ namespace uWine.Api.Controllers
private readonly AuthenticationService _authenticationService; private readonly AuthenticationService _authenticationService;
private readonly UserService _userService; private readonly UserService _userService;
public TestController(IConfiguration configuration, public TestController(IConfiguration configuration,
OwnerService ownerService, OwnerService ownerService,
AuthenticationService authenticationService, AuthenticationService authenticationService,
UserService userService) UserService userService)
{ {
@@ -25,6 +26,9 @@ namespace uWine.Api.Controllers
} }
[HttpGet("cklogin")] [HttpGet("cklogin")]
[ProducesResponseType(typeof(( int userId, string TUserId, string role, User user)), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult test() public IActionResult test()
{ {
try try
@@ -37,9 +41,9 @@ namespace uWine.Api.Controllers
return Ok(new { userId, TUserId, role, user }); return Ok(new { userId, TUserId, role, user });
} }
catch catch(Exception ex)
{ {
return Unauthorized(new { status = 401 }); return Unauthorized(new { status = 401, exeptiom = ex });
} }
} }
+36
View File
@@ -55,6 +55,42 @@ namespace uWine.Api.Controllers
return Ok(new { access_token = token }); return Ok(new { access_token = token });
} }
[HttpGet("active")]
public IActionResult IsActive()
{
if (!Request.Headers.ContainsKey("Authorization"))
return Unauthorized("Authorization header missing");
var authHeader = Request.Headers["Authorization"].ToString();
if (!authHeader.StartsWith("Bearer "))
return Unauthorized("Invalid auth scheme");
var token = authHeader.Substring("Bearer ".Length).Trim();
var jwtSettings = _configuration.GetSection("JwtSettings");
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.UTF8.GetBytes(jwtSettings["Secret"]);
try
{
tokenHandler.ValidateToken(token, new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = true,
ValidIssuer = jwtSettings["Issuer"],
ValidateAudience = true,
ValidAudience = jwtSettings["Audience"],
ClockSkew = TimeSpan.Zero // No clock skew for simplicity
}, out SecurityToken validatedToken);
}
catch (Exception)
{
return Unauthorized("Invalid token");
}
return Ok(new { active = true });
}
private string GenerateJwtToken(User user) private string GenerateJwtToken(User user)
{ {
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NetTopologySuite.Geometries;
using uWine.Core.Domain.Entities;
using uWine.Core.Infrastructure.Services; using uWine.Core.Infrastructure.Services;
namespace uWine.Api.Controllers namespace uWine.Api.Controllers
@@ -31,6 +33,9 @@ namespace uWine.Api.Controllers
} }
[HttpGet("get-owner-vineyards")] [HttpGet("get-owner-vineyards")]
[ProducesResponseType(typeof(List<Vineyard>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult OwnerVineyards() public IActionResult OwnerVineyards()
{ {
try try
@@ -45,5 +50,29 @@ namespace uWine.Api.Controllers
return Unauthorized(new { status = 401 }); return Unauthorized(new { status = 401 });
} }
} }
[HttpPost("new-vineyard")]
[ProducesResponseType(typeof(Vineyard), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public IActionResult NewVineyard([FromBody] Vineyard vineyard)
{
if (vineyard == null || string.IsNullOrEmpty(vineyard.Name))
{
return BadRequest("Invalid vineyard data.");
}
try
{
var UserId = int.Parse(User.FindFirst("userId")?.Value);
vineyard.UserId = UserId;
var createdVineyard = _vineyardService.New(vineyard);
return CreatedAtAction(nameof(NewVineyard), new { id = createdVineyard.Id }, createdVineyard);
}
catch (Exception ex)
{
return BadRequest($"Error creating vineyard: {ex.Message}");
}
}
} }
} }
+27 -4
View File
@@ -27,20 +27,43 @@ namespace uWine.Api
builder = Builder.Servicies.AddServices(builder); builder = Builder.Servicies.AddServices(builder);
builder.Services.AddCors(options =>
{
//options.AddPolicy("AllowSpecificOrigin",
// policy =>
// {
// policy.WithOrigins("http://localhost:5000", "http://your_flutter_web_app_domain.com") // Sostituisci con l'URL della tua app Flutter (anche localhost se la testi in sviluppo)
// .AllowAnyMethod() // Permetti tutti i metodi (GET, POST, PUT, DELETE, OPTIONS, ecc.)
// .AllowAnyHeader(); // Permetti tutte le intestazioni, inclusa 'Authorization'
// // .AllowCredentials(); // Se la tua app Flutter invia credenziali (es. cookies, intestazioni di autorizzazione)
// });
// Puoi anche definire una policy più permissiva per lo sviluppo (NON USARE IN PRODUZIONE)
options.AddPolicy("AllowAll",
policy =>
{
policy.AllowAnyOrigin() // NON USARE IN PRODUZIONE: Permette richieste da qualsiasi origine
.AllowAnyMethod() // Permette tutti i metodi
.AllowAnyHeader(); // Permette tutte le intestazioni
// .AllowCredentials(); // Se necessario per le credenziali
});
});
//builder.Services.AddSingleton<MariaDbServerRepository>(); //builder.Services.AddSingleton<MariaDbServerRepository>();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) //if (app.Environment.IsDevelopment())
{ //{
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
} //}
app.UseCors("AllowAll");
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.Run(); app.Run();
@@ -10,7 +10,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform> <LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider> <PublishProvider>FileSystem</PublishProvider>
<PublishUrl>\\192.168.0.8\deploy\Api</PublishUrl> <PublishUrl>\\192.168.0.9\deploy\Api</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod> <WebPublishMethod>FileSystem</WebPublishMethod>
<_TargetId>Folder</_TargetId> <_TargetId>Folder</_TargetId>
<SiteUrlToLaunchAfterPublish /> <SiteUrlToLaunchAfterPublish />
@@ -4,8 +4,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
--> -->
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<_PublishTargetUrl>\\192.168.0.8\deploy\Api</_PublishTargetUrl> <_PublishTargetUrl>\\192.168.0.9\deploy\Api</_PublishTargetUrl>
<History>True|2025-07-09T20:17:50.4482481Z||;False|2025-07-09T22:16:08.6429078+02:00||;</History> <History>True|2025-07-19T10:07:37.0313367Z||;True|2025-07-18T18:33:30.0842689+02:00||;True|2025-07-18T16:39:46.3437548+02:00||;True|2025-07-18T16:18:44.3945279+02:00||;True|2025-07-12T11:19:51.8369816+02:00||;True|2025-07-12T11:19:37.9276597+02:00||;True|2025-07-12T11:13:39.8418364+02:00||;True|2025-07-12T11:08:46.5193480+02:00||;True|2025-07-11T13:40:12.6978365+02:00||;True|2025-07-11T13:34:43.3827167+02:00||;True|2025-07-11T12:05:26.3975372+02:00||;False|2025-07-11T12:04:08.1989900+02:00||;True|2025-07-11T11:08:51.8736645+02:00||;True|2025-07-09T22:17:50.4482481+02:00||;False|2025-07-09T22:16:08.6429078+02:00||;</History>
<LastFailureDetails /> <LastFailureDetails />
</PropertyGroup> </PropertyGroup>
</Project> </Project>