api
This commit is contained in:
1 parent
3a1a0ca7ef
commit
6bce1d9330
9 files changed
+167
-13
No files matched your search
@@ -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<int, Coordinates>), 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);
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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<Vineyard>), 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user