From 6bce1d933097a8a8fd9f60dc3d6063bac8b651b4 Mon Sep 17 00:00:00 2001 From: Riccardo Usardi Date: Sun, 20 Jul 2025 11:49:20 +0200 Subject: [PATCH] api --- uWine.Api/Builder/Servicies.cs | 2 +- uWine.Api/Controllers/AreaController.cs | 6 +- uWine.Api/Controllers/OwnerController.cs | 58 +++++++++++++++++++ uWine.Api/Controllers/TestController.cs | 12 ++-- uWine.Api/Controllers/UserController.cs | 36 ++++++++++++ uWine.Api/Controllers/VineyardController.cs | 29 ++++++++++ uWine.Api/Program.cs | 31 ++++++++-- .../Api01 (192.168.0.8).pubxml | 2 +- .../Api01 (192.168.0.8).pubxml.user | 4 +- 9 files changed, 167 insertions(+), 13 deletions(-) create mode 100644 uWine.Api/Controllers/OwnerController.cs diff --git a/uWine.Api/Builder/Servicies.cs b/uWine.Api/Builder/Servicies.cs index 9400abf..2202a79 100644 --- a/uWine.Api/Builder/Servicies.cs +++ b/uWine.Api/Builder/Servicies.cs @@ -52,7 +52,7 @@ namespace uWine.Api.Builder builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); - builder.Services.AddTransient(); + builder.Services.AddTransient(); return builder; } diff --git a/uWine.Api/Controllers/AreaController.cs b/uWine.Api/Controllers/AreaController.cs index 963110d..106e0c3 100644 --- a/uWine.Api/Controllers/AreaController.cs +++ b/uWine.Api/Controllers/AreaController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; + using uWine.Core.Domain.Entities; using uWine.Core.Infrastructure.Services; @@ -35,13 +36,16 @@ namespace uWine.Api.Controllers } [HttpGet("get-area")] + [ProducesResponseType(typeof(Dictionary), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public IActionResult GetArea(int vineyardId) { try { var area = _coordinatesService.GetArea(vineyardId); if (area == null || area.Count == 0) - return NotFound("No area found for the given vineyard ID"); + return NotFound(); return Ok(area); } diff --git a/uWine.Api/Controllers/OwnerController.cs b/uWine.Api/Controllers/OwnerController.cs new file mode 100644 index 0000000..0c4e500 --- /dev/null +++ b/uWine.Api/Controllers/OwnerController.cs @@ -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 }); + } + } + } +} diff --git a/uWine.Api/Controllers/TestController.cs b/uWine.Api/Controllers/TestController.cs index 81aa111..cb8afc0 100644 --- a/uWine.Api/Controllers/TestController.cs +++ b/uWine.Api/Controllers/TestController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using System.Security.Claims; +using uWine.Core.Domain.Entities; using uWine.Core.Infrastructure.Services; namespace uWine.Api.Controllers @@ -13,8 +14,8 @@ namespace uWine.Api.Controllers private readonly AuthenticationService _authenticationService; private readonly UserService _userService; - public TestController(IConfiguration configuration, - OwnerService ownerService, + public TestController(IConfiguration configuration, + OwnerService ownerService, AuthenticationService authenticationService, UserService userService) { @@ -25,6 +26,9 @@ namespace uWine.Api.Controllers } [HttpGet("cklogin")] + [ProducesResponseType(typeof(( int userId, string TUserId, string role, User user)), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public IActionResult test() { try @@ -37,9 +41,9 @@ namespace uWine.Api.Controllers return Ok(new { userId, TUserId, role, user }); } - catch + catch(Exception ex) { - return Unauthorized(new { status = 401 }); + return Unauthorized(new { status = 401, exeptiom = ex }); } } diff --git a/uWine.Api/Controllers/UserController.cs b/uWine.Api/Controllers/UserController.cs index ced5f2d..e331ae9 100644 --- a/uWine.Api/Controllers/UserController.cs +++ b/uWine.Api/Controllers/UserController.cs @@ -55,6 +55,42 @@ namespace uWine.Api.Controllers 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) { diff --git a/uWine.Api/Controllers/VineyardController.cs b/uWine.Api/Controllers/VineyardController.cs index 9df8802..1020e59 100644 --- a/uWine.Api/Controllers/VineyardController.cs +++ b/uWine.Api/Controllers/VineyardController.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc; +using NetTopologySuite.Geometries; +using uWine.Core.Domain.Entities; using uWine.Core.Infrastructure.Services; namespace uWine.Api.Controllers @@ -31,6 +33,9 @@ namespace uWine.Api.Controllers } [HttpGet("get-owner-vineyards")] + [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public IActionResult OwnerVineyards() { try @@ -45,5 +50,29 @@ namespace uWine.Api.Controllers 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}"); + } + } } } diff --git a/uWine.Api/Program.cs b/uWine.Api/Program.cs index a931485..f5da4fa 100644 --- a/uWine.Api/Program.cs +++ b/uWine.Api/Program.cs @@ -27,20 +27,43 @@ namespace uWine.Api 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(); var app = builder.Build(); // Configure the HTTP request pipeline. - if (app.Environment.IsDevelopment()) - { + //if (app.Environment.IsDevelopment()) + //{ app.UseSwagger(); app.UseSwaggerUI(); - } + //} + app.UseCors("AllowAll"); app.UseAuthorization(); - app.MapControllers(); app.Run(); diff --git a/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml b/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml index e7de124..9602e18 100644 --- a/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml +++ b/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml @@ -10,7 +10,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. Release Any CPU FileSystem - \\192.168.0.8\deploy\Api + \\192.168.0.9\deploy\Api FileSystem <_TargetId>Folder diff --git a/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml.user b/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml.user index 7af2d46..dde9657 100644 --- a/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml.user +++ b/uWine.Api/Properties/PublishProfiles/Api01 (192.168.0.8).pubxml.user @@ -4,8 +4,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - <_PublishTargetUrl>\\192.168.0.8\deploy\Api - True|2025-07-09T20:17:50.4482481Z||;False|2025-07-09T22:16:08.6429078+02:00||; + <_PublishTargetUrl>\\192.168.0.9\deploy\Api + 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||; \ No newline at end of file