removed automapper.

This commit is contained in:
Keivan Beigi 2013-05-30 17:12:20 -07:00
parent 5b2410da3f
commit bdf86aa4ba
21 changed files with 100 additions and 282 deletions

View File

@ -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<QualityProfile, QualityProfileResource>()
.ForMember(dest => dest.Qualities,
opt => opt.ResolveUsing<AllowedToQualitiesResolver>().FromMember(src => src.Allowed));
Mapper.CreateMap<QualityProfileResource, QualityProfile>()
.ForMember(dest => dest.Allowed,
opt => opt.ResolveUsing<QualitiesToAllowedResolver>().FromMember(src => src.Qualities));
Mapper.CreateMap<Quality, QualityProfileType>()
.ForMember(dest => dest.Allowed, opt => opt.Ignore());
//QualitySize
Mapper.CreateMap<QualitySize, QualitySizeResource>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityId));
Mapper.CreateMap<QualitySizeResource, QualitySize>()
.ForMember(dest => dest.QualityId, opt => opt.MapFrom(src => src.Id));
//Episode
Mapper.CreateMap<Episode, EpisodeResource>();
//Episode Paging
Mapper.CreateMap<PagingSpec<Episode>, PagingResource<EpisodeResource>>();
//History
Mapper.CreateMap<Core.History.History, HistoryResource>();
Mapper.CreateMap<PagingSpec<Core.History.History>, PagingResource<HistoryResource>>();
}
}
}

View File

@ -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<EpisodeResource>
{
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<EpisodeResource> 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<List<EpisodeResource>>().AsResponse();
return ToListResource(() => episodes);
}
}
}

View File

@ -29,12 +29,12 @@ namespace NzbDrone.Api.Config
private NamingConfigResource UpdateNamingConfig(NamingConfigResource resource)
{
return Apply<NamingConfig>(_namingConfigService.Save, resource);
return ToResource<NamingConfig>(_namingConfigService.Save, resource);
}
private NamingConfigResource GetNamingConfig()
{
return Apply(_namingConfigService.GetConfig);
return ToResource(_namingConfigService.GetConfig);
}
}

View File

@ -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<EpisodeResource>
{
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<EpisodeResource> 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<Episode>, List<EpisodeResource>>(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));
}
}
}

View File

@ -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
{

View File

@ -33,7 +33,6 @@ namespace NzbDrone.Api
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
_logger.Info("Starting NzbDrone API");
AutomapperBootstraper.InitializeAutomapper();
container.Resolve<DatabaseTarget>().Register();
container.Resolve<IEnableBasicAuthInNancy>().Register(pipelines);

View File

@ -55,10 +55,6 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoMapper, Version=2.2.1.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\AutoMapper.2.2.1\lib\net40\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="FluentValidation, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FluentValidation.4.0.0.0\lib\Net40\FluentValidation.dll</HintPath>
@ -93,7 +89,6 @@
<Compile Include="Authentication\AuthenticationService.cs" />
<Compile Include="Authentication\EnableBasicAuthInNancy.cs" />
<Compile Include="Authentication\NzbDroneUser.cs" />
<Compile Include="AutomapperBootstraper.cs" />
<Compile Include="Calendar\CalendarModule.cs" />
<Compile Include="ClientSchema\SchemaDeserializer.cs" />
<Compile Include="ClientSchema\FieldDefinitionAttribute.cs" />
@ -126,7 +121,6 @@
<Compile Include="Notifications\NotificationResource.cs" />
<Compile Include="NzbDroneRestModule.cs" />
<Compile Include="PagingResource.cs" />
<Compile Include="Resolvers\NullableDatetimeToString.cs" />
<Compile Include="REST\BadRequestException.cs" />
<Compile Include="REST\ResourceValidator.cs" />
<Compile Include="REST\RestModule.cs" />
@ -152,9 +146,6 @@
<Compile Include="Qualities\QualitySizeResource.cs" />
<Compile Include="Qualities\QualitySizeModule.cs" />
<Compile Include="Extensions\RequestExtensions.cs" />
<Compile Include="Resolvers\AllowedToQualitiesResolver.cs" />
<Compile Include="Resolvers\QualitiesToAllowedResolver.cs" />
<Compile Include="Resolvers\QualityTypesToIntResolver.cs" />
<Compile Include="Config\SettingsModule.cs" />
<Compile Include="SignalR\BasicResourceConnection.cs" />
<Compile Include="SignalR\Serializer.cs" />

View File

@ -1,5 +1,4 @@
using Nancy;
using Nancy.Security;
namespace NzbDrone.Api
{

View File

@ -22,26 +22,26 @@ namespace NzbDrone.Api
}
protected TResource Apply<TModel>(Func<TModel, TModel> function, TResource resource) where TModel : ModelBase, new()
protected TResource ToResource<TModel>(Func<TModel, TModel> function, TResource resource) where TModel : ModelBase, new()
{
var model = resource.InjectTo<TModel>();
function(model);
return model.InjectTo<TResource>();
}
protected List<TResource> ApplyToList<TModel>(Func<IEnumerable<TModel>> function) where TModel : ModelBase, new()
protected List<TResource> ToListResource<TModel>(Func<IEnumerable<TModel>> function) where TModel : ModelBase, new()
{
var modelList = function();
return modelList.InjectTo<List<TResource>>();
}
protected TResource Apply<TModel>(Func<TModel> function) where TModel : ModelBase, new()
protected TResource ToResource<TModel>(Func<TModel> function) where TModel : ModelBase, new()
{
var modelList = function();
return modelList.InjectTo<TResource>();
}
protected TResource Apply<TModel>(Func<int, TModel> action, int id) where TModel : ModelBase, new()
protected TResource ToResource<TModel>(Func<int, TModel> action, int id) where TModel : ModelBase, new()
{
var model = action(id);
return model.InjectTo<TResource>();

View File

@ -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<QualityProfileType> Qualities { get; set; }
public List<QualityResource> Qualities { get; set; }
}
public class QualityProfileType
public class QualityResource : RestResource
{
public Int32 Id { get; set; }
public Int32 Weight { get; set; }

View File

@ -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<QualityProfileResource>
{
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<QualityProfile>, List<QualityProfileResource>>(profiles).AsResponse();
var model = resource.InjectTo<QualityProfile>();
model = _qualityProvider.Add(model);
return GetById(model.Id);
}
private Response OnGet(int id)
{
var profile = _qualityProvider.Get(id);
return Mapper.Map<QualityProfile, QualityProfileResource>(profile).AsResponse();
}
private Response OnPost()
{
var request = Request.Body.FromJson<QualityProfileResource>();
var profile = Mapper.Map<QualityProfileResource, QualityProfile>(request);
request.Id = _qualityProvider.Add(profile).Id;
return request.AsResponse();
}
//Update
private Response OnPut()
{
var request = Request.Body.FromJson<QualityProfileResource>();
var profile = Mapper.Map<QualityProfileResource, QualityProfile>(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<QualityProfile>();
_qualityProvider.Update(model);
return GetById(resource.Id);
}
private QualityProfileResource GetById(int id)
{
return ToResource(() => _qualityProvider.Get(id));
}
private List<QualityProfileResource> GetAll()
{
return ToListResource(_qualityProvider.All);
}
}
}

View File

@ -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<QualitySizeResource>
{
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<QualitySizeResource>();
var type = Mapper.Map<QualitySizeResource, QualitySize>(model);
_qualityTypeProvider.Update(type);
return model.AsResponse();
var model = resource.InjectTo<QualitySize>();
_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<QualitySize, QualitySizeResource>(type).AsResponse();
return ToResource(() => _qualityTypeProvider.Get(id));
}
private Response GetQualityType()
private List<QualitySizeResource> GetAll()
{
var types = _qualityTypeProvider.All().Where(qualityType => qualityType.QualityId != 0 && qualityType.QualityId != 10).ToList();
var responseModel = Mapper.Map<List<QualitySize>, List<QualitySizeResource>>(types);
return responseModel.AsResponse();
return ToListResource(_qualityTypeProvider.All);
}
}
}

View File

@ -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<TResource> 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;

View File

@ -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<Quality>, List<QualityProfileType>>
{
protected override List<QualityProfileType> ResolveCore(List<Quality> source)
{
var qualities = Mapper.Map<List<Quality>, List<QualityProfileType>>(Quality.All().Where(q => q.Id > 0).ToList());
qualities.ForEach(quality =>
{
quality.Allowed = source.SingleOrDefault(q => q.Id == quality.Id) != null;
});
return qualities;
}
}
}

View File

@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
namespace NzbDrone.Api.Resolvers
{
public class NullableDatetimeToString : ValueResolver<DateTime?, String>
{
protected override String ResolveCore(DateTime? source)
{
if(!source.HasValue)
return String.Empty;
return source.Value.ToString("yyyy-MM-dd");
}
}
}

View File

@ -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<QualityProfileType>, List<Quality>>
{
protected override List<Quality> ResolveCore(List<QualityProfileType> source)
{
var ids = source.Where(s => s.Allowed).Select(s => s.Id).ToList();
var qualityTypes = new List<Quality>();
ids.ForEach(id =>
{
qualityTypes.Add(Quality.FindById(id));
});
return qualityTypes;
}
}
}

View File

@ -1,14 +0,0 @@
using System;
using AutoMapper;
using NzbDrone.Core.Qualities;
namespace NzbDrone.Api.Resolvers
{
public class QualityTypesToIntResolver : ValueResolver<Quality, Int32>
{
protected override int ResolveCore(Quality source)
{
return source.Id;
}
}
}

View File

@ -18,12 +18,12 @@ namespace NzbDrone.Api.RootFolders
private RootFolderResource CreateRootFolder(RootFolderResource rootFolderResource)
{
return Apply<RootFolder>(_rootFolderService.Add, rootFolderResource);
return ToResource<RootFolder>(_rootFolderService.Add, rootFolderResource);
}
private List<RootFolderResource> GetRootFolders()
{
return ApplyToList(_rootFolderService.AllWithUnmappedFolders);
return ToListResource(_rootFolderService.AllWithUnmappedFolders);
}
private void DeleteFolder(int id)

View File

@ -51,7 +51,7 @@ namespace NzbDrone.Api.Series
private List<SeriesResource> 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<Core.Tv.Series>(_seriesService.AddSeries, seriesResource);
return ToResource<Core.Tv.Series>(_seriesService.AddSeries, seriesResource);
}
private SeriesResource UpdateSeries(SeriesResource seriesResource)
{
return Apply<Core.Tv.Series>(_seriesService.UpdateSeries, seriesResource);
return ToResource<Core.Tv.Series>(_seriesService.UpdateSeries, seriesResource);
}
private void DeleteSeries(int id)

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="2.2.1" targetFramework="net40" />
<package id="FluentValidation" version="4.0.0.0" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.Core" version="1.1.1" targetFramework="net40" />
<package id="Nancy" version="0.16.1" targetFramework="net40" />

View File

@ -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();
}
}
}