Lots of changes to get code ready to add library.

This commit is contained in:
Joseph Milazzo 2020-12-17 11:27:19 -06:00
parent 67b97b3be2
commit d5eed4e85d
20 changed files with 570 additions and 3 deletions

View file

@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.DTOs;
using API.Entities;
using API.Extensions;
using API.Interfaces;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace API.Controllers
{
public class LibraryController : BaseApiController
{
private readonly DataContext _context;
private readonly IDirectoryService _directoryService;
private readonly ILibraryRepository _libraryRepository;
private readonly ILogger<LibraryController> _logger;
private readonly IUserRepository _userRepository;
public LibraryController(DataContext context, IDirectoryService directoryService,
ILibraryRepository libraryRepository, ILogger<LibraryController> logger, IUserRepository userRepository)
{
_context = context;
_directoryService = directoryService;
_libraryRepository = libraryRepository;
_logger = logger;
_userRepository = userRepository;
}
/// <summary>
/// Returns a list of directories for a given path. If path is empty, returns root drives.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
[HttpGet("list")]
public ActionResult<IEnumerable<string>> GetDirectories(string path)
{
// TODO: We need some sort of validation other than our auth layer
_logger.Log(LogLevel.Debug, "Listing Directories for " + path);
if (string.IsNullOrEmpty(path))
{
return Ok(Directory.GetLogicalDrives());
}
if (!Directory.Exists(@path)) return BadRequest("This is not a valid path");
return Ok(_directoryService.ListDirectory(path));
}
[HttpGet]
public async Task<ActionResult<IEnumerable<LibraryDto>>> GetLibraries()
{
return Ok(await _libraryRepository.GetLibrariesAsync());
}
}
}

View file

@ -1,10 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.DTOs;
using API.Entities;
using API.Extensions;
using API.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace API.Controllers
{
@ -12,11 +16,13 @@ namespace API.Controllers
{
private readonly DataContext _context;
private readonly IUserRepository _userRepository;
private readonly ILibraryRepository _libraryRepository;
public UsersController(DataContext context, IUserRepository userRepository)
public UsersController(DataContext context, IUserRepository userRepository, ILibraryRepository libraryRepository)
{
_context = context;
_userRepository = userRepository;
_libraryRepository = libraryRepository;
}
[HttpGet]
@ -24,5 +30,41 @@ namespace API.Controllers
{
return Ok(await _userRepository.GetMembersAsync());
}
[HttpPost("add-library")]
public async Task<ActionResult> AddLibrary(CreateLibraryDto createLibraryDto)
{
//_logger.Log(LogLevel.Debug, "Creating a new " + createLibraryDto.Type + " library");
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
if (await _libraryRepository.LibraryExists(createLibraryDto.Name))
{
return BadRequest("Library name already exists. Please choose a unique name to the server.");
}
// TODO: We probably need to clean the folders before we insert
var library = new Library()
{
Name = createLibraryDto.Name,
Type = createLibraryDto.Type,
//Folders = createLibraryDto.Folders
};
user.Libraries.Add(library);
_userRepository.Update(user);
if (await _userRepository.SaveAllAsync())
{
return Ok();
}
return BadRequest("Not implemented");
}
}
}