mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add LicenseDto and update LicensesController to use DTO for license operations
This commit is contained in:
48
Marechai.Data/Dtos/LicenseDto.cs
Normal file
48
Marechai.Data/Dtos/LicenseDto.cs
Normal 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; }
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Marechai.Data.Dtos;
|
||||||
using Marechai.Database.Models;
|
using Marechai.Database.Models;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
@@ -43,8 +44,10 @@ public class LicensesController(MarechaiContext context) : ControllerBase
|
|||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public Task<List<License>> GetAsync() => context.Licenses.OrderBy(l => l.Name)
|
public Task<List<LicenseDto>> GetAsync()
|
||||||
.Select(l => new License
|
{
|
||||||
|
return context.Licenses.OrderBy(l => l.Name)
|
||||||
|
.Select(l => new LicenseDto
|
||||||
{
|
{
|
||||||
FsfApproved = l.FsfApproved,
|
FsfApproved = l.FsfApproved,
|
||||||
Id = l.Id,
|
Id = l.Id,
|
||||||
@@ -54,13 +57,16 @@ public class LicensesController(MarechaiContext context) : ControllerBase
|
|||||||
SPDX = l.SPDX
|
SPDX = l.SPDX
|
||||||
})
|
})
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet("{id:int}")]
|
[HttpGet("{id:int}")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public Task<License> GetAsync(int id) => context.Licenses.Where(l => l.Id == id)
|
public Task<LicenseDto> GetAsync(int id)
|
||||||
.Select(l => new License
|
{
|
||||||
|
return context.Licenses.Where(l => l.Id == id)
|
||||||
|
.Select(l => new LicenseDto
|
||||||
{
|
{
|
||||||
FsfApproved = l.FsfApproved,
|
FsfApproved = l.FsfApproved,
|
||||||
Id = l.Id,
|
Id = l.Id,
|
||||||
@@ -71,6 +77,7 @@ public class LicensesController(MarechaiContext context) : ControllerBase
|
|||||||
Text = l.Text
|
Text = l.Text
|
||||||
})
|
})
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPut("{id:int}")]
|
[HttpPut("{id:int}")]
|
||||||
[Authorize(Roles = "Admin,UberAdmin")]
|
[Authorize(Roles = "Admin,UberAdmin")]
|
||||||
@@ -78,14 +85,14 @@ public class LicensesController(MarechaiContext context) : ControllerBase
|
|||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[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();
|
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();
|
if (model is null) return NotFound();
|
||||||
|
|
||||||
model.FsfApproved = viewModel.FsfApproved;
|
model.FsfApproved = viewModel.FsfApproved;
|
||||||
model.Link = viewModel.Link;
|
model.Link = viewModel.Link;
|
||||||
@@ -104,11 +111,11 @@ public class LicensesController(MarechaiContext context) : ControllerBase
|
|||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[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();
|
if (userId is null) return Unauthorized();
|
||||||
|
|
||||||
var model = new License
|
var model = new License
|
||||||
{
|
{
|
||||||
@@ -134,12 +141,12 @@ public class LicensesController(MarechaiContext context) : ControllerBase
|
|||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
public async Task<ActionResult> DeleteAsync(int id)
|
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();
|
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();
|
if (item is null) return NotFound();
|
||||||
|
|
||||||
context.Licenses.Remove(item);
|
context.Licenses.Remove(item);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user