76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
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);
|
|
}
|
|
} |