Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Some api work

Joseph Milazzo8156aeb49522
diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs
index 5ceaf1690..2ff43ec52 100644
--- a/API/Controllers/LibraryController.cs
+++ b/API/Controllers/LibraryController.cs
@@ -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");
+ }
diff --git a/API/Controllers/UsersController.cs b/API/Controllers/UsersController.cs
index b0ac10ff2..67039cce2 100644
--- a/API/Controllers/UsersController.cs
+++ b/API/Controllers/UsersController.cs
@@ -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");
}
+
+
}
}
\ No newline at end of file
diff --git a/API/Controllers/WeatherForecastController.cs b/API/Controllers/WeatherForecastController.cs
deleted file mode 100644
index 28c1c42a8..000000000
--- a/API/Controllers/WeatherForecastController.cs
+++ /dev/null
@@ -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();
- }
- }
-}
diff --git a/API/DTOs/MemberDto.cs b/API/DTOs/MemberDto.cs
index 26e299a7d..38ecbfcc3 100644
--- a/API/DTOs/MemberDto.cs
+++ b/API/DTOs/MemberDto.cs
@@ -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; }
}
}
\ No newline at end of file
diff --git a/API/DTOs/UpdateLibraryDto.cs b/API/DTOs/UpdateLibraryDto.cs
new file mode 100644
index 000000000..c664b1df6
--- /dev/null
+++ b/API/DTOs/UpdateLibraryDto.cs
@@ -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; }
+ }
+}
\ No newline at end of file
diff --git a/API/Data/LibraryRepository.cs b/API/Data/LibraryRepository.cs
index d63b588e9..68e6371bd 100644
--- a/API/Data/LibraryRepository.cs
+++ b/API/Data/LibraryRepository.cs
@@ -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();
+ }
}
}
\ No newline at end of file
diff --git a/API/Helpers/AutoMapperProfiles.cs b/API/Helpers/AutoMapperProfiles.cs
index 9e98f2ee0..4e2f2a100 100644
--- a/API/Helpers/AutoMapperProfiles.cs
+++ b/API/Helpers/AutoMapperProfiles.cs
@@ -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));
}
}
}
\ No newline at end of file
diff --git a/API/Interfaces/ILibraryRepository.cs b/API/Interfaces/ILibraryRepository.cs
index 625fba35e..3f929efda 100644
--- a/API/Interfaces/ILibraryRepository.cs
+++ b/API/Interfaces/ILibraryRepository.cs
@@ -16,5 +16,7 @@ namespace API.Interfaces
/// <param name="libraryName"></param>
/// <returns></returns>
Task<bool> LibraryExists(string libraryName);
+
+ Task<IEnumerable<LibraryDto>> GetLibrariesForUserAsync(AppUser user);
}
}
\ No newline at end of file
diff --git a/API/WeatherForecast.cs b/API/WeatherForecast.cs
deleted file mode 100644
index 6f8943b65..000000000
--- a/API/WeatherForecast.cs
+++ /dev/null
@@ -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; }
- }
-}