refact program.cs vineyardController, service e repo
This commit is contained in:
1 parent
a373201ca4
commit
3a1a0ca7ef
17 files changed
+769
-306
No files matched your search
@@ -0,0 +1,124 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Text;
|
||||
using uWine.Core.Domain.Repositories;
|
||||
using uWine.Core.Infrastructure.Repository;
|
||||
using uWine.Core.Infrastructure.Services;
|
||||
|
||||
namespace uWine.Api.Builder
|
||||
{
|
||||
public class Servicies
|
||||
{
|
||||
|
||||
public static WebApplicationBuilder? AddAuthentiction(WebApplicationBuilder? builder)
|
||||
{
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = jwtSettings["Issuer"],
|
||||
ValidAudience = jwtSettings["Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["Secret"]))
|
||||
};
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static WebApplicationBuilder? AddRepository(WebApplicationBuilder? builder)
|
||||
{
|
||||
builder.Services.AddTransient<IAuthenticationRepository, MariaDbAuthenticationRepository>();
|
||||
builder.Services.AddTransient<IOwnerRepository, MariadbOwnerRepository>();
|
||||
builder.Services.AddTransient<IAddressRepository, MariaDbAddressRepository>();
|
||||
builder.Services.AddTransient<IContactRepository, MariaDbContactRepository>();
|
||||
builder.Services.AddTransient<IVineyardRepository, MariaDbVineyardRepository>();
|
||||
builder.Services.AddTransient<IUserRepository, MariaDbUserRepository>();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static WebApplicationBuilder? AddServices(WebApplicationBuilder? builder)
|
||||
{
|
||||
builder.Services.AddTransient<AuthenticationService>();
|
||||
builder.Services.AddTransient<OwnerService>();
|
||||
builder.Services.AddTransient<UserService>();
|
||||
builder.Services.AddTransient<CoordinatesService>();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static WebApplicationBuilder? AddSwagger(WebApplicationBuilder? builder)
|
||||
{
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Il tuo API", Version = "v1" });
|
||||
|
||||
// 1. Configurazione per JWT Bearer Token
|
||||
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
{
|
||||
Description = "Inserisci il JWT Token nel campo 'Value' con il prefisso 'Bearer '. Esempio: 'Bearer your_token_here'",
|
||||
Name = "Authorization",
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Scheme = "Bearer"
|
||||
});
|
||||
|
||||
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer"
|
||||
}
|
||||
},
|
||||
new string[] {}
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Configurazione per Basic Authentication
|
||||
c.AddSecurityDefinition("Basic", new OpenApiSecurityScheme
|
||||
{
|
||||
Description = "Basic authentication header",
|
||||
Name = "Authorization",
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "basic"
|
||||
});
|
||||
|
||||
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Basic"
|
||||
}
|
||||
},
|
||||
new string[] {}
|
||||
}
|
||||
});
|
||||
|
||||
// Opzionale: per i commenti XML della documentazione
|
||||
// var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
// var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
||||
// c.IncludeXmlComments(xmlPath);
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,14 +18,14 @@ namespace uWine.Api.Controllers
|
||||
}
|
||||
|
||||
[HttpPost("save-area")]
|
||||
public IActionResult SaveArea([FromBody] Dictionary<int,Coordinates> coordinates, int vineyardId)
|
||||
public IActionResult SaveArea([FromBody] Dictionary<int, Coordinates> coordinates, int vineyardId)
|
||||
{
|
||||
if (coordinates == null)
|
||||
return BadRequest("Invalid coordinates");
|
||||
|
||||
try
|
||||
{
|
||||
_coordinatesService.SaveArea(vineyardId ,coordinates);
|
||||
_coordinatesService.SaveArea(vineyardId, coordinates);
|
||||
return Ok(new { status = "Area set successfully" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -33,5 +33,22 @@ namespace uWine.Api.Controllers
|
||||
return StatusCode(500, new { error = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("get-area")]
|
||||
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 Ok(area);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, new { error = ex.Message });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using uWine.Core.Infrastructure.Services;
|
||||
|
||||
namespace uWine.Api.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("/[controller]")]
|
||||
public class VineyardController : Controller
|
||||
{
|
||||
public VineyardService _vineyardService;
|
||||
public VineyardController(VineyardService vineyardService)
|
||||
{
|
||||
_vineyardService = vineyardService;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("get-user-vineyards")]
|
||||
public IActionResult UserVineyards()
|
||||
{
|
||||
try
|
||||
{
|
||||
var UserId = int.Parse(User.FindFirst("userId")?.Value);
|
||||
var list = _vineyardService.User(UserId);
|
||||
|
||||
return Ok(list);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Unauthorized(new { status = 401 });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("get-owner-vineyards")]
|
||||
public IActionResult OwnerVineyards()
|
||||
{
|
||||
try
|
||||
{
|
||||
var UserId = int.Parse(User.FindFirst("userId")?.Value);
|
||||
var list = _vineyardService.Owner(UserId);
|
||||
|
||||
return Ok(list);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Unauthorized(new { status = 401 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-29
@@ -1,6 +1,7 @@
|
||||
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Text;
|
||||
using uWine.Core.Domain.Repositories;
|
||||
using uWine.Core.Infrastructure.Repository;
|
||||
@@ -14,42 +15,18 @@ namespace uWine.Api
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = jwtSettings["Issuer"],
|
||||
ValidAudience = jwtSettings["Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["Secret"]))
|
||||
};
|
||||
});
|
||||
builder = Builder.Servicies.AddAuthentiction(builder);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
//builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddTransient<IAuthenticationRepository, MariaDbAuthenticationRepository>();
|
||||
builder.Services.AddTransient<IOwnerRepository, MariadbOwnerRepository>();
|
||||
builder.Services.AddTransient<IAddressRepository, MariaDbAddressRepository>();
|
||||
builder.Services.AddTransient<IContactRepository, MariaDbContactRepository>();
|
||||
builder.Services.AddTransient<IVineyardRepository, MariaDbVineyardRepository>();
|
||||
builder.Services.AddTransient<IUserRepository, MariaDbUserRepository>();
|
||||
builder = Builder.Servicies.AddSwagger(builder);
|
||||
builder = Builder.Servicies.AddRepository(builder);
|
||||
builder = Builder.Servicies.AddServices(builder);
|
||||
|
||||
|
||||
builder.Services.AddTransient<AuthenticationService>();
|
||||
builder.Services.AddTransient<OwnerService>();
|
||||
builder.Services.AddTransient<UserService>();
|
||||
//builder.Services.AddSingleton<MariaDbServerRepository>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
Reference in new issue
Block a user