Implemented theme import and export
This commit is contained in:
@@ -11,7 +11,7 @@ using Moonlight.Shared.Http.Requests.Admin.Themes;
|
||||
using Moonlight.Shared.Http.Responses;
|
||||
using Moonlight.Shared.Http.Responses.Admin.Themes;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
namespace Moonlight.Api.Http.Controllers.Admin.Themes;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/themes")]
|
||||
@@ -0,0 +1,76 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Mappers;
|
||||
using Moonlight.Api.Models;
|
||||
using Moonlight.Shared;
|
||||
using Moonlight.Shared.Http.Responses.Admin.Themes;
|
||||
using VYaml.Serialization;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin.Themes;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/themes")]
|
||||
[Authorize(Policy = Permissions.Themes.View)]
|
||||
public class TransferController : Controller
|
||||
{
|
||||
private readonly DatabaseRepository<Theme> ThemeRepository;
|
||||
|
||||
public TransferController(DatabaseRepository<Theme> themeRepository)
|
||||
{
|
||||
ThemeRepository = themeRepository;
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}/export")]
|
||||
public async Task<ActionResult> ExportAsync([FromRoute] int id)
|
||||
{
|
||||
var theme = await ThemeRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (theme == null)
|
||||
return Problem("No theme with that id found", statusCode: 404);
|
||||
|
||||
var yml = YamlSerializer.Serialize(new ThemeTransferModel()
|
||||
{
|
||||
Name = theme.Name,
|
||||
Author = theme.Author,
|
||||
CssContent = theme.CssContent,
|
||||
Version = theme.Version
|
||||
});
|
||||
|
||||
return File(yml.ToArray(), "text/yaml", $"{theme.Name}.yml");
|
||||
}
|
||||
|
||||
[HttpPost("import")]
|
||||
public async Task<ActionResult<ThemeDto>> ImportAsync()
|
||||
{
|
||||
var themeToImport = await YamlSerializer.DeserializeAsync<ThemeTransferModel>(Request.Body);
|
||||
|
||||
var existingTheme = await ThemeRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(x => x.Name == themeToImport.Name && x.Author == themeToImport.Author);
|
||||
|
||||
if (existingTheme == null)
|
||||
{
|
||||
var finalTheme = await ThemeRepository.AddAsync(new Theme()
|
||||
{
|
||||
Name = themeToImport.Name,
|
||||
Author = themeToImport.Author,
|
||||
CssContent = themeToImport.CssContent,
|
||||
Version = themeToImport.Version
|
||||
});
|
||||
|
||||
return ThemeMapper.ToDto(finalTheme);
|
||||
}
|
||||
|
||||
existingTheme.CssContent = themeToImport.CssContent;
|
||||
existingTheme.Version = themeToImport.Version;
|
||||
|
||||
await ThemeRepository.UpdateAsync(existingTheme);
|
||||
|
||||
return ThemeMapper.ToDto(existingTheme);
|
||||
}
|
||||
}
|
||||
12
Moonlight.Api/Models/ThemeTransferModel.cs
Normal file
12
Moonlight.Api/Models/ThemeTransferModel.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using VYaml.Annotations;
|
||||
|
||||
namespace Moonlight.Api.Models;
|
||||
|
||||
[YamlObject]
|
||||
public partial class ThemeTransferModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Version { get; set; }
|
||||
public string Author { get; set; }
|
||||
public string CssContent { get; set; }
|
||||
}
|
||||
@@ -28,6 +28,7 @@
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1"/>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0"/>
|
||||
<PackageReference Include="Riok.Mapperly" Version="4.3.1-next.0"/>
|
||||
<PackageReference Include="VYaml" Version="1.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user