diff --git a/NzbDrone.Api/AppHost.cs b/NzbDrone.Api/AppHost.cs index b036eef6f..e01030a7b 100644 --- a/NzbDrone.Api/AppHost.cs +++ b/NzbDrone.Api/AppHost.cs @@ -27,8 +27,10 @@ namespace NzbDrone.Api SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api" }); Routes - .Add("{ApiKey}/qualityprofiles") - .Add("{ApiKey}/qualityprofiles/{Id}"); + .Add("/qualityprofiles") + .Add("/qualityprofiles/{Id}"); + + Bootstrapper.Initialize(); } } } \ No newline at end of file diff --git a/NzbDrone.Api/Bootstrapper.cs b/NzbDrone.Api/Bootstrapper.cs new file mode 100644 index 000000000..06d6eb9de --- /dev/null +++ b/NzbDrone.Api/Bootstrapper.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using AutoMapper; +using NzbDrone.Api.QualityProfiles; +using NzbDrone.Core.Repository.Quality; + +namespace NzbDrone.Api +{ + public static class Bootstrapper + { + public static void Initialize() + { + Mapper.CreateMap() + .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId)); + + Mapper.CreateMap() + .ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id)); + } + } +} diff --git a/NzbDrone.Api/Filters/ValidApiRequestAttribute.cs b/NzbDrone.Api/Filters/ValidApiRequestAttribute.cs index 61def76af..3b15ffbd5 100644 --- a/NzbDrone.Api/Filters/ValidApiRequestAttribute.cs +++ b/NzbDrone.Api/Filters/ValidApiRequestAttribute.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using Ninject; using NzbDrone.Api.Exceptions; +using NzbDrone.Api.Helpers; using NzbDrone.Core.Providers.Core; using ServiceStack.ServiceHost; using ServiceStack.ServiceInterface; @@ -21,10 +22,10 @@ namespace NzbDrone.Api.Filters public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto) { //Verify the API Key here - var apikey = ((IApiRequest)requestDto).ApiKey; + var apikey = req.GetApiKey(); - if (String.IsNullOrWhiteSpace(apikey)) - throw new InvalidApiKeyException(); + //if (String.IsNullOrWhiteSpace(apikey)) + //throw new InvalidApiKeyException(); } public IHasRequestFilter Copy() diff --git a/NzbDrone.Api/Helpers/HttpRequestExtensions.cs b/NzbDrone.Api/Helpers/HttpRequestExtensions.cs new file mode 100644 index 000000000..430feef3d --- /dev/null +++ b/NzbDrone.Api/Helpers/HttpRequestExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ServiceStack.Common.Web; +using ServiceStack.ServiceHost; + +namespace NzbDrone.Api.Helpers +{ + public static class HttpRequestExtensions + { + public static string GetApiKey(this IHttpRequest httpReq) + { + var auth = httpReq.Headers[HttpHeaders.Authorization]; + if (auth == null) return null; + + var split = auth.Split(' '); + + if (split.Count() != 2) + return null; + + if (!split[0].Equals("APIKEY", StringComparison.InvariantCultureIgnoreCase)) + return null; + + return split[1]; + } + } +} diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index 5571af429..3e1579fb4 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -50,6 +50,9 @@ MinimumRecommendedRules.ruleset + + ..\packages\AutoMapper.2.2.0\lib\net40\AutoMapper.dll + False ..\packages\Ninject.3.0.1.10\lib\net40\Ninject.dll @@ -91,11 +94,14 @@ + + + diff --git a/NzbDrone.Api/QualityProfiles/QualityProfileModel.cs b/NzbDrone.Api/QualityProfiles/QualityProfileModel.cs new file mode 100644 index 000000000..7f2f75f81 --- /dev/null +++ b/NzbDrone.Api/QualityProfiles/QualityProfileModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NzbDrone.Core.Repository.Quality; + +namespace NzbDrone.Api.QualityProfiles +{ + public class QualityProfileModel + { + public Int32 Id { get; set; } + public String Name { get; set; } + public QualityTypes Cutoff { get; set; } + public List Allowed { get; set; } + } +} diff --git a/NzbDrone.Api/QualityProfiles/QualityProfileService.cs b/NzbDrone.Api/QualityProfiles/QualityProfileService.cs index 7c4da157e..a80c1ace0 100644 --- a/NzbDrone.Api/QualityProfiles/QualityProfileService.cs +++ b/NzbDrone.Api/QualityProfiles/QualityProfileService.cs @@ -1,13 +1,16 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; +using AutoMapper; using Ninject; using NzbDrone.Api.Filters; using NzbDrone.Core.Providers; +using NzbDrone.Core.Repository.Quality; using ServiceStack.ServiceInterface; namespace NzbDrone.Api.QualityProfiles { [ValidApiRequest] - public class QualityProfileService : RestServiceBase + public class QualityProfileService : RestServiceBase { private readonly QualityProvider _qualityProvider; @@ -21,32 +24,39 @@ namespace NzbDrone.Api.QualityProfiles { } - public override object OnGet(QualityProfileRequest request) + public override object OnGet(QualityProfileModel request) { if (request.Id == 0) { var profiles = _qualityProvider.All(); - return new { Profiles = profiles }; + return Mapper.Map, IEnumerable>(profiles); } var profile = _qualityProvider.Get(request.Id); - return profile; + return Mapper.Map(profile); } - //public override object OnPost(Todo todo) - //{ - // return Repository.Store(todo); - //} - - //public override object OnPut(Todo todo) - //{ - // return Repository.Store(todo); - //} - - //public override object OnDelete(Todo request) - //{ - // Repository.DeleteById(request.Id); - // return null; - //} + public override object OnPost(QualityProfileModel data) + { + var profile = Mapper.Map(data); + _qualityProvider.Update(profile); + + return data; + } + + public override object OnPut(QualityProfileModel data) + { + var profile = Mapper.Map(data); + data.Id = _qualityProvider.Add(profile); + + return data; + } + + public override object OnDelete(QualityProfileModel data) + { + _qualityProvider.Delete(data.Id); + + return "ok"; + } } } \ No newline at end of file diff --git a/NzbDrone.Api/packages.config b/NzbDrone.Api/packages.config index c6dffb0d9..b382dd46c 100644 --- a/NzbDrone.Api/packages.config +++ b/NzbDrone.Api/packages.config @@ -1,5 +1,6 @@  + diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 227096a4e..ad200824a 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -203,7 +203,6 @@ ..\packages\EntityFramework.SqlServerCompact.4.1.8482.2\lib\System.Data.SqlServerCe.Entity.dll - diff --git a/packages/AutoMapper.2.2.0/AutoMapper.2.2.0.nupkg b/packages/AutoMapper.2.2.0/AutoMapper.2.2.0.nupkg new file mode 100644 index 000000000..1dbc52621 Binary files /dev/null and b/packages/AutoMapper.2.2.0/AutoMapper.2.2.0.nupkg differ diff --git a/packages/AutoMapper.2.2.0/AutoMapper.2.2.0.nuspec b/packages/AutoMapper.2.2.0/AutoMapper.2.2.0.nuspec new file mode 100644 index 000000000..25c20ca60 --- /dev/null +++ b/packages/AutoMapper.2.2.0/AutoMapper.2.2.0.nuspec @@ -0,0 +1,15 @@ + + + + AutoMapper + 2.2.0 + Jimmy Bogard + Jimmy Bogard + https://github.com/AutoMapper/AutoMapper/blob/master/LICENSE.txt + http://automapper.org/ + https://s3.amazonaws.com/automapper/icon.png + false + A convention-based object-object mapper. AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer. + A convention-based object-object mapper + + \ No newline at end of file diff --git a/packages/AutoMapper.2.2.0/lib/net40/AutoMapper.dll b/packages/AutoMapper.2.2.0/lib/net40/AutoMapper.dll new file mode 100644 index 000000000..d1207464a Binary files /dev/null and b/packages/AutoMapper.2.2.0/lib/net40/AutoMapper.dll differ diff --git a/packages/AutoMapper.2.2.0/lib/net40/AutoMapper.xml b/packages/AutoMapper.2.2.0/lib/net40/AutoMapper.xml new file mode 100644 index 000000000..a5c98208f --- /dev/null +++ b/packages/AutoMapper.2.2.0/lib/net40/AutoMapper.xml @@ -0,0 +1,15 @@ + + + + AutoMapper + + + + + This expression visitor will replace an input parameter by another one + + see http://stackoverflow.com/questions/4601844/expression-tree-copy-or-convert + + + +