From 8156aeb495221db4877711f53f48657131cba6d6 Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Sun, 20 Dec 2020 17:47:18 -0600 Subject: [PATCH] Some api work --- API/Controllers/LibraryController.cs | 42 +++++++++++++++++++- API/Controllers/UsersController.cs | 18 ++++++--- API/Controllers/WeatherForecastController.cs | 39 ------------------ API/DTOs/MemberDto.cs | 2 +- API/DTOs/UpdateLibraryDto.cs | 11 +++++ API/Data/LibraryRepository.cs | 8 ++++ API/Helpers/AutoMapperProfiles.cs | 9 ++++- API/Interfaces/ILibraryRepository.cs | 2 + API/WeatherForecast.cs | 15 ------- 9 files changed, 82 insertions(+), 64 deletions(-) delete mode 100644 API/Controllers/WeatherForecastController.cs create mode 100644 API/DTOs/UpdateLibraryDto.cs delete mode 100644 API/WeatherForecast.cs 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 _logger; private readonly IUserRepository _userRepository; + private readonly IMapper _mapper; public LibraryController(DataContext context, IDirectoryService directoryService, - ILibraryRepository libraryRepository, ILogger logger, IUserRepository userRepository) + ILibraryRepository libraryRepository, ILogger logger, IUserRepository userRepository, + IMapper mapper) { _context = context; _directoryService = directoryService; _libraryRepository = libraryRepository; _logger = logger; _userRepository = userRepository; + _mapper = mapper; } /// @@ -60,6 +63,43 @@ namespace API.Controllers { return Ok(await _libraryRepository.GetLibrariesAsync()); } + + + // Do I need this method? + // [HttpGet("library/{username}")] + // public async Task>> 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> 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(); + + foreach (var selectedLibrary in updateLibraryDto.SelectedLibraries) + { + user.Libraries.Add(_mapper.Map(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 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() { user } }; + library.Folders = createLibraryDto.Folders.Select(x => new FolderPath + { + Path = x, + Library = library + }).ToList(); + user.Libraries ??= new List(); // 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 _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet] - public IEnumerable 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 Libraries { get; set; } + public IEnumerable 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 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> GetLibrariesForUserAsync(AppUser user) + { + return await _context.Library.Where(library => library.AppUsers.Contains(user)) + .Include(l => l.Folders) + .ProjectTo(_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(); + CreateMap(); + CreateMap() .ForMember(dest => dest.Folders, opt => opt.MapFrom(src => src.Folders.Select(x => x.Path).ToList())); + + CreateMap() + .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 /// /// Task LibraryExists(string libraryName); + + Task> 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; } - } -}