Add LicenseDto and update LicensesController to use DTO for license operations

This commit is contained in:
2025-11-14 04:51:12 +00:00
parent 9d146eb151
commit 5e3be9cbb0
2 changed files with 98 additions and 43 deletions

View File

@@ -0,0 +1,48 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2021 Natalia Portillo
*******************************************************************************/
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
namespace Marechai.Data.Dtos;
public class LicenseDto : BaseDto<int>
{
[JsonPropertyName("name")] [Required] public string Name { get; set; }
[JsonPropertyName("spdx")] public string? SPDX { get; set; }
[JsonPropertyName("fsf_approved")]
[Required]
public bool FsfApproved { get; set; }
[JsonPropertyName("osi_approved")]
[Required]
public bool OsiApproved { get; set; }
[JsonPropertyName("link")] public string? Link { get; set; }
[JsonPropertyName("text")] public string? Text { get; set; }
}

View File

@@ -27,6 +27,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Marechai.Data.Dtos;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
@@ -43,8 +44,10 @@ public class LicensesController(MarechaiContext context) : ControllerBase
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public Task<List<License>> GetAsync() => context.Licenses.OrderBy(l => l.Name)
.Select(l => new License
public Task<List<LicenseDto>> GetAsync()
{
return context.Licenses.OrderBy(l => l.Name)
.Select(l => new LicenseDto
{
FsfApproved = l.FsfApproved,
Id = l.Id,
@@ -54,13 +57,16 @@ public class LicensesController(MarechaiContext context) : ControllerBase
SPDX = l.SPDX
})
.ToListAsync();
}
[HttpGet("{id:int}")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public Task<License> GetAsync(int id) => context.Licenses.Where(l => l.Id == id)
.Select(l => new License
public Task<LicenseDto> GetAsync(int id)
{
return context.Licenses.Where(l => l.Id == id)
.Select(l => new LicenseDto
{
FsfApproved = l.FsfApproved,
Id = l.Id,
@@ -71,6 +77,7 @@ public class LicensesController(MarechaiContext context) : ControllerBase
Text = l.Text
})
.FirstOrDefaultAsync();
}
[HttpPut("{id:int}")]
[Authorize(Roles = "Admin,UberAdmin")]
@@ -78,12 +85,12 @@ public class LicensesController(MarechaiContext context) : ControllerBase
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> UpdateAsync(int id, [FromBody] License viewModel)
public async Task<ActionResult> UpdateAsync(int id, [FromBody] LicenseDto viewModel)
{
string userId = User.FindFirstValue(ClaimTypes.Sid);
var userId = User.FindFirstValue(ClaimTypes.Sid);
if (userId is null) return Unauthorized();
License model = await context.Licenses.FindAsync(viewModel.Id);
var model = await context.Licenses.FindAsync(viewModel.Id);
if (model is null) return NotFound();
@@ -104,9 +111,9 @@ public class LicensesController(MarechaiContext context) : ControllerBase
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<int>> CreateAsync([FromBody] License viewModel)
public async Task<ActionResult<int>> CreateAsync([FromBody] LicenseDto viewModel)
{
string userId = User.FindFirstValue(ClaimTypes.Sid);
var userId = User.FindFirstValue(ClaimTypes.Sid);
if (userId is null) return Unauthorized();
@@ -134,10 +141,10 @@ public class LicensesController(MarechaiContext context) : ControllerBase
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> DeleteAsync(int id)
{
string userId = User.FindFirstValue(ClaimTypes.Sid);
var userId = User.FindFirstValue(ClaimTypes.Sid);
if (userId is null) return Unauthorized();
License item = await context.Licenses.FindAsync(id);
var item = await context.Licenses.FindAsync(id);
if (item is null) return NotFound();