Setup list operations to use Get and FromQuery

This commit is contained in:
Steven Hildreth 2018-11-18 10:40:52 -06:00
parent 27744337da
commit e150e51891
17 changed files with 119 additions and 52 deletions

View file

@ -8,6 +8,8 @@ using Roadie.Api.Services;
using Roadie.Library.Identity;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace Roadie.Api.Controllers
@ -51,11 +53,21 @@ namespace Roadie.Api.Controllers
if (!loginResult.Succeeded)
{
return BadRequest();
}
// If successful login return OK with generated token
}
var user = await userManager.FindByNameAsync(model.Username);
var now = DateTime.UtcNow;
user.LastLogin = now;
user.LastUpdated = now;
await userManager.UpdateAsync(user);
var t = await this.tokenService.GenerateToken(user, this.userManager);
return Ok(t);
return Ok(new
{
Id = user.RoadieId,
Username = user.UserName,
user.Email,
user.LastLogin,
Token = t
});
}
catch (Exception ex)
{

View file

@ -16,7 +16,7 @@ using models = Roadie.Library.Models;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("artist")]
[Route("artists")]
[ApiController]
[Authorize]
public class ArtistController : EntityControllerBase
@ -53,9 +53,9 @@ namespace Roadie.Api.Controllers
return Ok(result);
}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request)
public async Task<IActionResult> List([FromQuery]PagedRequest request)
{
var result = await this.ArtistService.List(roadieUser: await this.CurrentUserModel(),
request: request);

View file

@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("bookmark")]
[Route("bookmarks")]
[ApiController]
[Authorize]
public class BookmarkController : EntityControllerBase
@ -55,9 +55,9 @@ namespace Roadie.Api.Controllers
// return Ok(result);
//}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request)
public async Task<IActionResult> List([FromQuery]PagedRequest request)
{
var result = await this.BookmarkService.List(roadieUser: await this.CurrentUserModel(),
request: request);

View file

@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("collection")]
[Route("collections")]
[ApiController]
[Authorize]
public class CollectionController : EntityControllerBase
@ -55,9 +55,9 @@ namespace Roadie.Api.Controllers
// return Ok(result);
//}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request)
public async Task<IActionResult> List([FromQuery]PagedRequest request)
{
var result = await this.CollectionService.List(roadieUser: await this.CurrentUserModel(),
request: request);

View file

@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("genre")]
[Route("genres")]
[ApiController]
[Authorize]
public class GenreController : EntityControllerBase
@ -55,9 +55,9 @@ namespace Roadie.Api.Controllers
// return Ok(result);
//}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request)
public async Task<IActionResult> List([FromQuery]PagedRequest request)
{
var result = await this.GenreService.List(roadieUser: await this.CurrentUserModel(),
request: request);

View file

@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("image")]
[Route("images")]
[ApiController]
// [Authorize]
public class ImageController : EntityControllerBase

View file

@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("label")]
[Route("labels")]
[ApiController]
[Authorize]
public class LabelController : EntityControllerBase
@ -55,9 +55,9 @@ namespace Roadie.Api.Controllers
// return Ok(result);
//}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request)
public async Task<IActionResult> List([FromQuery]PagedRequest request)
{
var result = await this.LabelService.List(roadieUser: await this.CurrentUserModel(),
request: request);

View file

@ -15,7 +15,7 @@ using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("playactivity")]
[Route("playactivities")]
[ApiController]
[Authorize]
public class PlayActivityController : EntityControllerBase
@ -29,9 +29,9 @@ namespace Roadie.Api.Controllers
this.PlayActivityService = playActivityService;
}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> PlayActivity(PagedRequest request)
public async Task<IActionResult> PlayActivity([FromQuery]PagedRequest request)
{
var result = await this.PlayActivityService.List(request);
if (!result.IsSuccess)
@ -41,9 +41,9 @@ namespace Roadie.Api.Controllers
return Ok(result);
}
[HttpPost("{userId}")]
[HttpGet("{userId}")]
[ProducesResponseType(200)]
public async Task<IActionResult> PlayActivity([FromBody]PagedRequest request, Guid userId)
public async Task<IActionResult> PlayActivity([FromQuery]PagedRequest request, Guid userId)
{
var user = this.UserManager.Users.FirstOrDefault(x => x.RoadieId == userId);
if (user == null)

View file

@ -10,7 +10,7 @@ using Roadie.Library.Identity;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("playlist")]
[Route("playlists")]
[ApiController]
[Authorize]
public class PlaylistController : EntityControllerBase

View file

@ -16,7 +16,7 @@ using models = Roadie.Library.Models;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("release")]
[Route("releases")]
[ApiController]
[Authorize]
public class ReleaseController : EntityControllerBase
@ -53,9 +53,9 @@ namespace Roadie.Api.Controllers
return Ok(result);
}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request, string inc)
public async Task<IActionResult> List([FromQuery]PagedRequest request, string inc)
{
var result = await this.ReleaseService.List(user: await this.CurrentUserModel(),
request: request);

View file

@ -16,7 +16,7 @@ using models = Roadie.Library.Models;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("track")]
[Route("tracks")]
[ApiController]
[Authorize]
public class TrackController : EntityControllerBase
@ -53,9 +53,9 @@ namespace Roadie.Api.Controllers
return Ok(result);
}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request, string inc)
public async Task<IActionResult> List([FromQuery]PagedRequest request, string inc)
{
var result = await this.TrackService.List(roadieUser: await this.CurrentUserModel(),
request: request);

View file

@ -8,14 +8,15 @@ using Roadie.Library.Caching;
using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("user")]
[Route("users")]
[ApiController]
[Authorize]
// [Authorize]
public class UserController : EntityControllerBase
{
private IUserService UserService { get; }
@ -55,9 +56,10 @@ namespace Roadie.Api.Controllers
// return Ok(result);
//}
[HttpPost]
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List(PagedRequest request)
public async Task<IActionResult> List([FromQuery]PagedRequest request)
{
var result = await this.UserService.List(request);
if (!result.IsSuccess)
@ -66,5 +68,11 @@ namespace Roadie.Api.Controllers
}
return Ok(result);
}
public class PagingParams
{
public int Page { get; set; } = 1;
public int Limit { get; set; } = 5;
}
}
}

View file

@ -11,7 +11,7 @@ namespace Roadie.Api.Services
public HttpContext(IUrlHelper urlHelper)
{
this.BaseUrl = $"{ urlHelper.ActionContext.HttpContext.Request.Scheme}://{ urlHelper.ActionContext.HttpContext.Request.Host }";
this.ImageBaseUrl = $"{ this.BaseUrl}/image";
this.ImageBaseUrl = $"{ this.BaseUrl}/images";
}
}
}

View file

@ -38,14 +38,6 @@ namespace Roadie.Api
this._configuration = configuration;
this._loggerFactory = loggerFactory;
//TypeAdapterConfig<Roadie.Library.Data.Release, Roadie.Library.Models.Releases.ReleaseList>
// .NewConfig()
// .Map(rml => rml.Artist.Value,
// src => src.Artist.RoadieId)
// .Map(rml => rml.Artist.Text,
// src => src.Artist.Name)
// .Compile();
TypeAdapterConfig<Roadie.Library.Data.Image, Roadie.Library.Models.Image>
.NewConfig()
.Map(i => i.ArtistId,
@ -65,12 +57,18 @@ namespace Roadie.Api
app.UseDeveloperExceptionPage();
}
app.UseCors("Cors");
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseAuthentication();
//app.UseSwagger();
//app.UseSwaggerUI(c =>
//{
// c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyComics API v1");
// c.SwaggerEndpoint("/swagger/swagger.json", "Roadie API");
// c.RoutePrefix = string.Empty;
//});
app.UseSignalR(routes =>

View file

@ -20,7 +20,7 @@ namespace Roadie.Library.Models.Pagination
}
}
public int? Page { get; set; }
public int? Page { get; set; } = 1;
public int PageValue
{
get
@ -30,7 +30,7 @@ namespace Roadie.Library.Models.Pagination
}
public string Sort { get; set; }
public string Order { get; set; }
public int? Limit { get; set; }
public int? Limit { get; set; } = 10;
public int LimitValue
{
get
@ -84,7 +84,7 @@ namespace Roadie.Library.Models.Pagination
return result.ToString();
}
public bool FilterOnlyMissing { get; set; }
public bool? FilterOnlyMissing { get; set; }
public Guid? FilterToArtistId { get; set; }

View file

@ -1,22 +1,64 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Roadie.Library
{
[Serializable]
public class AppException : Exception
{
public AppException() : base()
{
}
public AppException(string message) : base(message)
{
}
public AppException(string message, params object[] args)
: base(String.Format(CultureInfo.CurrentCulture, message, args))
{
}
}
[Serializable]
public class OperationResult<T>
{
private List<Exception> _errors;
private List<string> _messages;
public Dictionary<string, object> AdditionalData { get; set; }
/// <summary>
/// Client friendly exceptions
/// </summary>
[JsonProperty("errors")]
public IEnumerable<AppException> AppExceptions
{
get
{
if (this.Errors == null || !this.Errors.Any())
{
return null;
}
return this.Errors.Select(x => new AppException(x.Message));
}
}
public T Data { get; set; }
/// <summary>
/// Server side visible exceptions
/// </summary>
[JsonIgnore]
public IEnumerable<Exception> Errors { get; set; }
public bool IsSuccess { get; set; }
[JsonIgnore]
public bool IsNotFoundResult { get; set; }
public bool IsSuccess { get; set; }
public IEnumerable<string> Messages
{
get
@ -77,7 +119,7 @@ namespace Roadie.Library
{
if (exception != null)
{
if(this._errors == null)
if (this._errors == null)
{
this._errors = new List<Exception>();
}
@ -89,7 +131,7 @@ namespace Roadie.Library
{
if (!string.IsNullOrEmpty(message))
{
if(this._messages == null)
if (this._messages == null)
{
this._messages = new List<string>();
}

View file

@ -5,8 +5,15 @@ using System.Text;
namespace Roadie.Library.Utility
{
/// <summary>
/// Helper to work with M3U files
/// <seealso cref="https://en.wikipedia.org/wiki/M3U"/>
/// </summary>
public static class M3uHelper
{
/// <summary>
/// For the given collection of tracks generate a M3U file in a single string
/// </summary>
public static string M3uContentForTracks(IEnumerable<TrackList> tracks)
{
var result = new List<string>