79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using NetTopologySuite.Geometries;
|
|
using uWine.Core.Domain.Entities;
|
|
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")]
|
|
[ProducesResponseType(typeof(List<Vineyard>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
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 });
|
|
}
|
|
}
|
|
|
|
[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}");
|
|
}
|
|
}
|
|
}
|
|
}
|