mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Some api work
This commit is contained in:
parent
b6e0e05205
commit
8156aeb495
@ -23,15 +23,18 @@ namespace API.Controllers
|
||||
private readonly ILibraryRepository _libraryRepository;
|
||||
private readonly ILogger<LibraryController> _logger;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public LibraryController(DataContext context, IDirectoryService directoryService,
|
||||
ILibraryRepository libraryRepository, ILogger<LibraryController> logger, IUserRepository userRepository)
|
||||
ILibraryRepository libraryRepository, ILogger<LibraryController> logger, IUserRepository userRepository,
|
||||
IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_directoryService = directoryService;
|
||||
_libraryRepository = libraryRepository;
|
||||
_logger = logger;
|
||||
_userRepository = userRepository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -60,6 +63,43 @@ namespace API.Controllers
|
||||
{
|
||||
return Ok(await _libraryRepository.GetLibrariesAsync());
|
||||
}
|
||||
|
||||
|
||||
// Do I need this method?
|
||||
// [HttpGet("library/{username}")]
|
||||
// public async Task<ActionResult<IEnumerable<LibraryDto>>> GetLibrariesForUser(string username)
|
||||
// {
|
||||
// _logger.LogDebug("Method hit");
|
||||
// var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
|
||||
//
|
||||
// if (user == null) return BadRequest("Could not validate user");
|
||||
//
|
||||
// return Ok(await _libraryRepository.GetLibrariesForUserAsync(user));
|
||||
// }
|
||||
|
||||
[HttpPut("update-for")]
|
||||
public async Task<ActionResult<MemberDto>> UpdateLibrary(UpdateLibraryDto updateLibraryDto)
|
||||
{
|
||||
// TODO: Only admins can do this
|
||||
var user = await _userRepository.GetUserByUsernameAsync(updateLibraryDto.Username);
|
||||
|
||||
if (user == null) return BadRequest("Could not validate user");
|
||||
if (!user.IsAdmin) return Unauthorized("Only admins are permitted");
|
||||
|
||||
user.Libraries = new List<Library>();
|
||||
|
||||
foreach (var selectedLibrary in updateLibraryDto.SelectedLibraries)
|
||||
{
|
||||
user.Libraries.Add(_mapper.Map<Library>(selectedLibrary));
|
||||
}
|
||||
|
||||
if (await _userRepository.SaveAllAsync())
|
||||
{
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
return BadRequest("Not Implemented");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
@ -9,7 +8,6 @@ using API.Extensions;
|
||||
using API.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Controllers
|
||||
{
|
||||
@ -36,6 +34,8 @@ namespace API.Controllers
|
||||
[HttpPost("add-library")]
|
||||
public async Task<ActionResult> AddLibrary(CreateLibraryDto createLibraryDto)
|
||||
{
|
||||
// NOTE: I think we should move this into library controller because it gets added to all admins
|
||||
|
||||
//_logger.Log(LogLevel.Debug, "Creating a new " + createLibraryDto.Type + " library");
|
||||
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
|
||||
|
||||
@ -50,16 +50,20 @@ namespace API.Controllers
|
||||
// TODO: We probably need to clean the folders before we insert
|
||||
var library = new Library
|
||||
{
|
||||
Name = createLibraryDto.Name,
|
||||
Name = createLibraryDto.Name, // TODO: Ensure code handles Library name always being lowercase
|
||||
Type = createLibraryDto.Type,
|
||||
//Folders = createLibraryDto.Folders
|
||||
AppUsers = new List<AppUser>() { user }
|
||||
};
|
||||
|
||||
library.Folders = createLibraryDto.Folders.Select(x => new FolderPath
|
||||
{
|
||||
Path = x,
|
||||
Library = library
|
||||
}).ToList();
|
||||
|
||||
user.Libraries ??= new List<Library>(); // If user is null, then set it
|
||||
|
||||
user.Libraries.Add(library);
|
||||
//_userRepository.Update(user);
|
||||
|
||||
if (await _userRepository.SaveAllAsync())
|
||||
{
|
||||
@ -68,5 +72,7 @@ namespace API.Controllers
|
||||
|
||||
return BadRequest("Not implemented");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
var rng = new Random();
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateTime.Now.AddDays(index),
|
||||
TemperatureC = rng.Next(-20, 55),
|
||||
Summary = Summaries[rng.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
@ -15,6 +15,6 @@ namespace API.DTOs
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime LastActive { get; set; }
|
||||
public bool IsAdmin { get; set; }
|
||||
public IEnumerable<Library> Libraries { get; set; }
|
||||
public IEnumerable<LibraryDto> Libraries { get; set; }
|
||||
}
|
||||
}
|
11
API/DTOs/UpdateLibraryDto.cs
Normal file
11
API/DTOs/UpdateLibraryDto.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace API.DTOs
|
||||
{
|
||||
// NOTE: Should this be a Record? https://www.youtube.com/watch?v=9Byvwa9yF-I
|
||||
public class UpdateLibraryDto
|
||||
{
|
||||
public string Username { get; init; }
|
||||
public IEnumerable<LibraryDto> SelectedLibraries { get; init; }
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
@ -41,5 +42,12 @@ namespace API.Data
|
||||
{
|
||||
return await _context.Library.AnyAsync(x => x.Name == libraryName);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<LibraryDto>> GetLibrariesForUserAsync(AppUser user)
|
||||
{
|
||||
return await _context.Library.Where(library => library.AppUsers.Contains(user))
|
||||
.Include(l => l.Folders)
|
||||
.ProjectTo<LibraryDto>(_mapper.ConfigurationProvider).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
using AutoMapper;
|
||||
@ -9,11 +10,15 @@ namespace API.Helpers
|
||||
{
|
||||
public AutoMapperProfiles()
|
||||
{
|
||||
CreateMap<AppUser, MemberDto>();
|
||||
CreateMap<LibraryDto, Library>();
|
||||
|
||||
CreateMap<Library, LibraryDto>()
|
||||
.ForMember(dest => dest.Folders,
|
||||
opt =>
|
||||
opt.MapFrom(src => src.Folders.Select(x => x.Path).ToList()));
|
||||
|
||||
CreateMap<AppUser, MemberDto>()
|
||||
.AfterMap((ps, pst, context) => context.Mapper.Map(ps.Libraries, pst.Libraries));
|
||||
}
|
||||
}
|
||||
}
|
@ -16,5 +16,7 @@ namespace API.Interfaces
|
||||
/// <param name="libraryName"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> LibraryExists(string libraryName);
|
||||
|
||||
Task<IEnumerable<LibraryDto>> GetLibrariesForUserAsync(AppUser user);
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace API
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string Summary { get; set; }
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user