Some api work
This commit is contained in:
parent
b6e0e05205
commit
8156aeb495
9 changed files with 82 additions and 64 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue