diff --git a/NzbDrone.Api/AutomapperBootstraper.cs b/NzbDrone.Api/AutomapperBootstraper.cs deleted file mode 100644 index a949de131..000000000 --- a/NzbDrone.Api/AutomapperBootstraper.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using AutoMapper; -using NzbDrone.Api.Calendar; -using NzbDrone.Api.Episodes; -using NzbDrone.Api.History; -using NzbDrone.Api.Missing; -using NzbDrone.Api.Qualities; -using NzbDrone.Api.Resolvers; -using NzbDrone.Api.Series; -using NzbDrone.Core.Datastore; -using NzbDrone.Core.Qualities; -using NzbDrone.Core.Tv; - -namespace NzbDrone.Api -{ - public static class AutomapperBootstraper - { - - public static void InitializeAutomapper() - { - //QualityProfiles - Mapper.CreateMap() - .ForMember(dest => dest.Qualities, - opt => opt.ResolveUsing().FromMember(src => src.Allowed)); - - Mapper.CreateMap() - .ForMember(dest => dest.Allowed, - opt => opt.ResolveUsing().FromMember(src => src.Qualities)); - - Mapper.CreateMap() - .ForMember(dest => dest.Allowed, opt => opt.Ignore()); - - //QualitySize - Mapper.CreateMap() - .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityId)); - - Mapper.CreateMap() - .ForMember(dest => dest.QualityId, opt => opt.MapFrom(src => src.Id)); - - //Episode - Mapper.CreateMap(); - - //Episode Paging - Mapper.CreateMap, PagingResource>(); - - //History - Mapper.CreateMap(); - Mapper.CreateMap, PagingResource>(); - } - } -} \ No newline at end of file diff --git a/NzbDrone.Api/Calendar/CalendarModule.cs b/NzbDrone.Api/Calendar/CalendarModule.cs index 9af32a597..e4ed0f560 100644 --- a/NzbDrone.Api/Calendar/CalendarModule.cs +++ b/NzbDrone.Api/Calendar/CalendarModule.cs @@ -1,15 +1,12 @@ using System; using System.Collections.Generic; -using AutoMapper; -using Nancy; using NzbDrone.Api.Episodes; -using NzbDrone.Api.Extensions; using NzbDrone.Api.Mapping; using NzbDrone.Core.Tv; namespace NzbDrone.Api.Calendar { - public class CalendarModule : NzbDroneApiModule + public class CalendarModule : NzbDroneRestModule { private readonly IEpisodeService _episodeService; @@ -17,10 +14,11 @@ namespace NzbDrone.Api.Calendar : base("/calendar") { _episodeService = episodeService; - Get["/"] = x => GetEpisodesBetweenStartAndEndDate(); + + GetResourceAll = GetPaged; } - private Response GetEpisodesBetweenStartAndEndDate() + private List GetPaged() { var start = DateTime.Today.AddDays(-1); var end = DateTime.Today.AddDays(7); @@ -30,10 +28,11 @@ namespace NzbDrone.Api.Calendar if (queryStart.HasValue) start = DateTime.Parse(queryStart.Value); - if(queryEnd.HasValue) end = DateTime.Parse(queryEnd.Value); + if (queryEnd.HasValue) end = DateTime.Parse(queryEnd.Value); var episodes = _episodeService.EpisodesBetweenDates(start, end); - return episodes.InjectTo>().AsResponse(); + + return ToListResource(() => episodes); } } -} \ No newline at end of file +} diff --git a/NzbDrone.Api/Config/SettingsModule.cs b/NzbDrone.Api/Config/SettingsModule.cs index 47158667d..0c12cd5a7 100644 --- a/NzbDrone.Api/Config/SettingsModule.cs +++ b/NzbDrone.Api/Config/SettingsModule.cs @@ -29,12 +29,12 @@ namespace NzbDrone.Api.Config private NamingConfigResource UpdateNamingConfig(NamingConfigResource resource) { - return Apply(_namingConfigService.Save, resource); + return ToResource(_namingConfigService.Save, resource); } private NamingConfigResource GetNamingConfig() { - return Apply(_namingConfigService.GetConfig); + return ToResource(_namingConfigService.GetConfig); } } diff --git a/NzbDrone.Api/Episodes/EpisodeModule.cs b/NzbDrone.Api/Episodes/EpisodeModule.cs index 017d61479..5dda07586 100644 --- a/NzbDrone.Api/Episodes/EpisodeModule.cs +++ b/NzbDrone.Api/Episodes/EpisodeModule.cs @@ -1,13 +1,10 @@ using System.Collections.Generic; -using System.Linq; -using AutoMapper; -using Nancy; -using NzbDrone.Api.Extensions; +using NzbDrone.Api.REST; using NzbDrone.Core.Tv; namespace NzbDrone.Api.Episodes { - public class EpisodeModule : NzbDroneApiModule + public class EpisodeModule : NzbDroneRestModule { private readonly IEpisodeService _episodeService; @@ -15,16 +12,26 @@ namespace NzbDrone.Api.Episodes : base("/episodes") { _episodeService = episodeService; - Get["/"] = x => GetEpisodesForSeries(); + + GetResourceAll = GetEpisodes; } - private Response GetEpisodesForSeries() + private List GetEpisodes() { - var seriesId = (int)Request.Query.SeriesId; - var seasonNumber = (int)Request.Query.SeasonNumber; + var seriesId = (int?)Request.Query.SeriesId; + var seasonNumber = (int?)Request.Query.SeasonNumber; - var episodes = _episodeService.GetEpisodesBySeason(seriesId, seasonNumber); - return Mapper.Map, List>(episodes).AsResponse(); + if (seriesId == null) + { + throw new BadRequestException("seriesId is missing"); + } + + if (seasonNumber == null) + { + return ToListResource(() => _episodeService.GetEpisodeBySeries(seriesId.Value)); + } + + return ToListResource(() => _episodeService.GetEpisodesBySeason(seriesId.Value, seasonNumber.Value)); } } } \ No newline at end of file diff --git a/NzbDrone.Api/History/HistoryModule.cs b/NzbDrone.Api/History/HistoryModule.cs index e46f253ba..9069f2930 100644 --- a/NzbDrone.Api/History/HistoryModule.cs +++ b/NzbDrone.Api/History/HistoryModule.cs @@ -1,15 +1,5 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using AutoMapper; -using Nancy; -using NzbDrone.Api.Episodes; -using NzbDrone.Api.Extensions; -using NzbDrone.Core.Datastore; +using NzbDrone.Core.Datastore; using NzbDrone.Core.History; -using NzbDrone.Core.Qualities; -using NzbDrone.Core.Tv; namespace NzbDrone.Api.History { diff --git a/NzbDrone.Api/NancyBootstrapper.cs b/NzbDrone.Api/NancyBootstrapper.cs index 4907ff34b..01128eff9 100644 --- a/NzbDrone.Api/NancyBootstrapper.cs +++ b/NzbDrone.Api/NancyBootstrapper.cs @@ -33,7 +33,6 @@ namespace NzbDrone.Api protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) { _logger.Info("Starting NzbDrone API"); - AutomapperBootstraper.InitializeAutomapper(); container.Resolve().Register(); container.Resolve().Register(pipelines); diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index cf590269a..50a14ff40 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -55,10 +55,6 @@ - - False - ..\packages\AutoMapper.2.2.1\lib\net40\AutoMapper.dll - False ..\packages\FluentValidation.4.0.0.0\lib\Net40\FluentValidation.dll @@ -93,7 +89,6 @@ - @@ -126,7 +121,6 @@ - @@ -152,9 +146,6 @@ - - - diff --git a/NzbDrone.Api/NzbDroneApiModule.cs b/NzbDrone.Api/NzbDroneApiModule.cs index fdc2a46f7..ad8131487 100644 --- a/NzbDrone.Api/NzbDroneApiModule.cs +++ b/NzbDrone.Api/NzbDroneApiModule.cs @@ -1,5 +1,4 @@ using Nancy; -using Nancy.Security; namespace NzbDrone.Api { diff --git a/NzbDrone.Api/NzbDroneRestModule.cs b/NzbDrone.Api/NzbDroneRestModule.cs index 4b07c11d1..b46857972 100644 --- a/NzbDrone.Api/NzbDroneRestModule.cs +++ b/NzbDrone.Api/NzbDroneRestModule.cs @@ -22,26 +22,26 @@ namespace NzbDrone.Api } - protected TResource Apply(Func function, TResource resource) where TModel : ModelBase, new() + protected TResource ToResource(Func function, TResource resource) where TModel : ModelBase, new() { var model = resource.InjectTo(); function(model); return model.InjectTo(); } - protected List ApplyToList(Func> function) where TModel : ModelBase, new() + protected List ToListResource(Func> function) where TModel : ModelBase, new() { var modelList = function(); return modelList.InjectTo>(); } - protected TResource Apply(Func function) where TModel : ModelBase, new() + protected TResource ToResource(Func function) where TModel : ModelBase, new() { var modelList = function(); return modelList.InjectTo(); } - protected TResource Apply(Func action, int id) where TModel : ModelBase, new() + protected TResource ToResource(Func action, int id) where TModel : ModelBase, new() { var model = action(id); return model.InjectTo(); diff --git a/NzbDrone.Api/Qualities/QualityProfileResource.cs b/NzbDrone.Api/Qualities/QualityProfileResource.cs index cc3582ded..f757421db 100644 --- a/NzbDrone.Api/Qualities/QualityProfileResource.cs +++ b/NzbDrone.Api/Qualities/QualityProfileResource.cs @@ -1,17 +1,18 @@ using System; using System.Collections.Generic; +using NzbDrone.Api.REST; namespace NzbDrone.Api.Qualities { - public class QualityProfileResource + public class QualityProfileResource : RestResource { public Int32 Id { get; set; } public String Name { get; set; } public Int32 Cutoff { get; set; } - public List Qualities { get; set; } + public List Qualities { get; set; } } - public class QualityProfileType + public class QualityResource : RestResource { public Int32 Id { get; set; } public Int32 Weight { get; set; } diff --git a/NzbDrone.Api/Qualities/QualityProfilesModule.cs b/NzbDrone.Api/Qualities/QualityProfilesModule.cs index 03b0dfcee..835e364e5 100644 --- a/NzbDrone.Api/Qualities/QualityProfilesModule.cs +++ b/NzbDrone.Api/Qualities/QualityProfilesModule.cs @@ -1,60 +1,58 @@ using System.Collections.Generic; -using AutoMapper; -using Nancy; -using NzbDrone.Api.Extensions; using NzbDrone.Core.Qualities; +using NzbDrone.Api.Mapping; namespace NzbDrone.Api.Qualities { - public class QualityProfilesModule : NzbDroneApiModule + public class QualityProfilesModule : NzbDroneRestModule { private readonly QualityProfileService _qualityProvider; public QualityProfilesModule(QualityProfileService qualityProvider) - : base("/QualityProfiles") + : base("/qualityProfiles") { _qualityProvider = qualityProvider; - Get["/"] = x => OnGet(); - Get["/{Id}"] = x => OnGet((int)x.Id); - Put["/"] = x => OnPut(); - Delete["/{Id}"] = x => OnDelete((int)x.Id); + + GetResourceAll = GetAll; + + GetResourceById = GetById; + + UpdateResource = Update; + + CreateResource = Create; + + DeleteResource = DeleteProfile; + } - private Response OnGet() + private QualityProfileResource Create(QualityProfileResource resource) { - var profiles = _qualityProvider.All(); - return Mapper.Map, List>(profiles).AsResponse(); + var model = resource.InjectTo(); + model = _qualityProvider.Add(model); + return GetById(model.Id); } - private Response OnGet(int id) - { - var profile = _qualityProvider.Get(id); - return Mapper.Map(profile).AsResponse(); - } - - private Response OnPost() - { - var request = Request.Body.FromJson(); - var profile = Mapper.Map(request); - request.Id = _qualityProvider.Add(profile).Id; - - return request.AsResponse(); - } - - //Update - private Response OnPut() - { - var request = Request.Body.FromJson(); - var profile = Mapper.Map(request); - _qualityProvider.Update(profile); - - return request.AsResponse(); - } - - private Response OnDelete(int id) + private void DeleteProfile(int id) { _qualityProvider.Delete(id); - return new Response(); } + + private QualityProfileResource Update(QualityProfileResource resource) + { + var model = resource.InjectTo(); + _qualityProvider.Update(model); + return GetById(resource.Id); + } + + private QualityProfileResource GetById(int id) + { + return ToResource(() => _qualityProvider.Get(id)); + } + + private List GetAll() + { + return ToListResource(_qualityProvider.All); + } + } } \ No newline at end of file diff --git a/NzbDrone.Api/Qualities/QualitySizeModule.cs b/NzbDrone.Api/Qualities/QualitySizeModule.cs index 36d67e6a6..c5a66c5fd 100644 --- a/NzbDrone.Api/Qualities/QualitySizeModule.cs +++ b/NzbDrone.Api/Qualities/QualitySizeModule.cs @@ -1,49 +1,39 @@ using System.Collections.Generic; -using System.Linq; -using AutoMapper; -using Nancy; -using NzbDrone.Api.Extensions; using NzbDrone.Core.Qualities; +using NzbDrone.Api.Mapping; namespace NzbDrone.Api.Qualities { - public class QualitySizeModule : NzbDroneApiModule + public class QualitySizeModule : NzbDroneRestModule { private readonly QualitySizeService _qualityTypeProvider; public QualitySizeModule(QualitySizeService qualityTypeProvider) - : base("/qualitysizes") { _qualityTypeProvider = qualityTypeProvider; - Get["/"] = x => GetQualityType(); - Get["/{id}"] = x => GetQualityType(x.Id); - Put["/"] = x => PutQualityType(); + GetResourceAll = GetAll; + + GetResourceById = GetById; + + UpdateResource = Update; } - private Response PutQualityType() + private QualitySizeResource Update(QualitySizeResource resource) { - var model = Request.Body.FromJson(); - - var type = Mapper.Map(model); - _qualityTypeProvider.Update(type); - - return model.AsResponse(); - + var model = resource.InjectTo(); + _qualityTypeProvider.Update(model); + return GetById(resource.Id); } - private Response GetQualityType(int id) + private QualitySizeResource GetById(int id) { - var type = _qualityTypeProvider.Get(id); - return Mapper.Map(type).AsResponse(); + return ToResource(() => _qualityTypeProvider.Get(id)); } - private Response GetQualityType() + private List GetAll() { - var types = _qualityTypeProvider.All().Where(qualityType => qualityType.QualityId != 0 && qualityType.QualityId != 10).ToList(); - var responseModel = Mapper.Map, List>(types); - - return responseModel.AsResponse(); + return ToListResource(_qualityTypeProvider.All); } } } \ No newline at end of file diff --git a/NzbDrone.Api/REST/RestModule.cs b/NzbDrone.Api/REST/RestModule.cs index 4bfaf77c4..b3dbb3b4f 100644 --- a/NzbDrone.Api/REST/RestModule.cs +++ b/NzbDrone.Api/REST/RestModule.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using AutoMapper; using FluentValidation; using Nancy; using NzbDrone.Api.Extensions; @@ -189,17 +188,17 @@ namespace NzbDrone.Api.REST private PagingResource ReadPagingResourceFromRequest() { int pageSize; - Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.PageSize), out pageSize); + Int32.TryParse(Request.Query.PageSize.ToString(), out pageSize); if (pageSize == 0) pageSize = 10; - + int page; - Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.Page), out page); + Int32.TryParse(Request.Query.Page.ToString(), out page); if (page == 0) page = 1; - var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey); + var sortKey = Request.Query.SortKey.ToString(); if (String.IsNullOrEmpty(sortKey)) sortKey = "AirDate"; - var sortDirection = PrimitiveExtensions.ToNullSafeString(Request.Query.SortDir) + var sortDirection = Request.Query.SortDir.ToString() .Equals("Asc", StringComparison.InvariantCultureIgnoreCase) ? SortDirection.Ascending : SortDirection.Descending; diff --git a/NzbDrone.Api/Resolvers/AllowedToQualitiesResolver.cs b/NzbDrone.Api/Resolvers/AllowedToQualitiesResolver.cs deleted file mode 100644 index e708ca534..000000000 --- a/NzbDrone.Api/Resolvers/AllowedToQualitiesResolver.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using AutoMapper; -using NzbDrone.Api.Qualities; -using NzbDrone.Core.Qualities; - -namespace NzbDrone.Api.Resolvers -{ - public class AllowedToQualitiesResolver : ValueResolver, List> - { - protected override List ResolveCore(List source) - { - var qualities = Mapper.Map, List>(Quality.All().Where(q => q.Id > 0).ToList()); - - qualities.ForEach(quality => - { - quality.Allowed = source.SingleOrDefault(q => q.Id == quality.Id) != null; - }); - - return qualities; - } - } -} diff --git a/NzbDrone.Api/Resolvers/NullableDatetimeToString.cs b/NzbDrone.Api/Resolvers/NullableDatetimeToString.cs deleted file mode 100644 index ae9e3f483..000000000 --- a/NzbDrone.Api/Resolvers/NullableDatetimeToString.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using AutoMapper; - -namespace NzbDrone.Api.Resolvers -{ - public class NullableDatetimeToString : ValueResolver - { - protected override String ResolveCore(DateTime? source) - { - if(!source.HasValue) - return String.Empty; - - return source.Value.ToString("yyyy-MM-dd"); - } - } -} diff --git a/NzbDrone.Api/Resolvers/QualitiesToAllowedResolver.cs b/NzbDrone.Api/Resolvers/QualitiesToAllowedResolver.cs deleted file mode 100644 index d3e258d2a..000000000 --- a/NzbDrone.Api/Resolvers/QualitiesToAllowedResolver.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using AutoMapper; -using NzbDrone.Api.Qualities; -using NzbDrone.Core.Qualities; - -namespace NzbDrone.Api.Resolvers -{ - public class QualitiesToAllowedResolver : ValueResolver, List> - { - protected override List ResolveCore(List source) - { - var ids = source.Where(s => s.Allowed).Select(s => s.Id).ToList(); - - var qualityTypes = new List(); - - ids.ForEach(id => - { - qualityTypes.Add(Quality.FindById(id)); - }); - - return qualityTypes; - } - } -} diff --git a/NzbDrone.Api/Resolvers/QualityTypesToIntResolver.cs b/NzbDrone.Api/Resolvers/QualityTypesToIntResolver.cs deleted file mode 100644 index d9ef2da77..000000000 --- a/NzbDrone.Api/Resolvers/QualityTypesToIntResolver.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using AutoMapper; -using NzbDrone.Core.Qualities; - -namespace NzbDrone.Api.Resolvers -{ - public class QualityTypesToIntResolver : ValueResolver - { - protected override int ResolveCore(Quality source) - { - return source.Id; - } - } -} \ No newline at end of file diff --git a/NzbDrone.Api/RootFolders/RootFolderModule.cs b/NzbDrone.Api/RootFolders/RootFolderModule.cs index c929fc1d6..3accd37df 100644 --- a/NzbDrone.Api/RootFolders/RootFolderModule.cs +++ b/NzbDrone.Api/RootFolders/RootFolderModule.cs @@ -18,12 +18,12 @@ namespace NzbDrone.Api.RootFolders private RootFolderResource CreateRootFolder(RootFolderResource rootFolderResource) { - return Apply(_rootFolderService.Add, rootFolderResource); + return ToResource(_rootFolderService.Add, rootFolderResource); } private List GetRootFolders() { - return ApplyToList(_rootFolderService.AllWithUnmappedFolders); + return ToListResource(_rootFolderService.AllWithUnmappedFolders); } private void DeleteFolder(int id) diff --git a/NzbDrone.Api/Series/SeriesModule.cs b/NzbDrone.Api/Series/SeriesModule.cs index c650b9e2b..75d43b0d5 100644 --- a/NzbDrone.Api/Series/SeriesModule.cs +++ b/NzbDrone.Api/Series/SeriesModule.cs @@ -51,7 +51,7 @@ namespace NzbDrone.Api.Series private List AllSeries() { var seriesStats = _seriesStatisticsService.SeriesStatistics(); - var seriesModels = ApplyToList(_seriesService.GetAllSeries); + var seriesModels = ToListResource(_seriesService.GetAllSeries); foreach (var s in seriesModels) { @@ -69,7 +69,7 @@ namespace NzbDrone.Api.Series private SeriesResource GetSeries(int id) { - return Apply(_seriesService.GetSeries, id); + return ToResource(_seriesService.GetSeries, id); } private SeriesResource AddSeries(SeriesResource seriesResource) @@ -79,12 +79,12 @@ namespace NzbDrone.Api.Series //(we can just create the folder and it won't blow up if it already exists) //We also need to remove any special characters from the filename before attempting to create it - return Apply(_seriesService.AddSeries, seriesResource); + return ToResource(_seriesService.AddSeries, seriesResource); } private SeriesResource UpdateSeries(SeriesResource seriesResource) { - return Apply(_seriesService.UpdateSeries, seriesResource); + return ToResource(_seriesService.UpdateSeries, seriesResource); } private void DeleteSeries(int id) diff --git a/NzbDrone.Api/packages.config b/NzbDrone.Api/packages.config index 8e94690ad..2d743b79b 100644 --- a/NzbDrone.Api/packages.config +++ b/NzbDrone.Api/packages.config @@ -1,6 +1,5 @@  - diff --git a/NzbDrone.Integration.Test/QualityProfileIntegrationTest.cs b/NzbDrone.Integration.Test/QualityProfileIntegrationTest.cs index ed21746db..d1c4cb796 100644 --- a/NzbDrone.Integration.Test/QualityProfileIntegrationTest.cs +++ b/NzbDrone.Integration.Test/QualityProfileIntegrationTest.cs @@ -8,26 +8,6 @@ namespace NzbDrone.Integration.Test [TestFixture] public class QualityProfileIntegrationTest : IntegrationTest { - [Test] - public void should_have_2_quality_profiles_initially() - { - RootFolders.All().Should().BeEmpty(); - var rootFolder = new RootFolderResource - { - Path = Directory.GetCurrentDirectory() - }; - - var postResponse = RootFolders.Post(rootFolder); - - postResponse.Id.Should().NotBe(0); - postResponse.FreeSpace.Should().NotBe(0); - - RootFolders.All().Should().OnlyContain(c => c.Id == postResponse.Id); - - RootFolders.Delete(postResponse.Id); - - RootFolders.All().Should().BeEmpty(); - } } } \ No newline at end of file