From 27afb2402addb90d4e1427b6caa5cb2c87c6838e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 31 Jan 2013 20:18:11 -0800 Subject: [PATCH] SceneMapping added to Services API --- NzbDrone.Services.Api/Bootstrapper.cs | 17 +++++ .../DailySeries/DailySeriesModel.cs | 2 - .../DailySeries/DailySeriesModule.cs | 12 ++-- .../DailySeries/DailySeriesRepository.cs | 4 +- .../NzbDrone.Services.Api.csproj | 9 +++ .../SceneMapping/SceneMappingJsonModel.cs | 29 +++++++++ .../SceneMapping/SceneMappingModel.cs | 11 ++-- .../SceneMapping/SceneMappingModule.cs | 38 +++++++++++ .../SceneMapping/SceneMappingRepository.cs | 63 +++++++++++++++++++ NzbDrone.Services.Api/packages.config | 1 + 10 files changed, 171 insertions(+), 15 deletions(-) create mode 100644 NzbDrone.Services.Api/SceneMapping/SceneMappingJsonModel.cs create mode 100644 NzbDrone.Services.Api/SceneMapping/SceneMappingModule.cs create mode 100644 NzbDrone.Services.Api/SceneMapping/SceneMappingRepository.cs diff --git a/NzbDrone.Services.Api/Bootstrapper.cs b/NzbDrone.Services.Api/Bootstrapper.cs index f55e22f2d..6af7608f9 100644 --- a/NzbDrone.Services.Api/Bootstrapper.cs +++ b/NzbDrone.Services.Api/Bootstrapper.cs @@ -3,11 +3,14 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web; +using AutoMapper; using Autofac; +using MongoDB.Bson; using NLog; using Nancy.Bootstrapper; using Nancy.Bootstrappers.Autofac; using NzbDrone.Services.Api.Extensions; +using NzbDrone.Services.Api.SceneMapping; namespace NzbDrone.Services.Api { @@ -37,5 +40,19 @@ namespace NzbDrone.Services.Api return NancyInternalConfiguration.WithOverrides(c => c.Serializers.Add(typeof(NancyJsonSerializer))); } } + + protected override void ApplicationStartup(ILifetimeScope container, IPipelines pipelines) + { + InitializeAutomapper(); + } + + public static void InitializeAutomapper() + { + Mapper.CreateMap() + .ForMember(dest => dest.MapId, opt => opt.MapFrom(src => src.MapId.ToString())); + + Mapper.CreateMap() + .ForMember(dest => dest.MapId, opt => opt.MapFrom(src => ObjectId.Parse(src.MapId))); + } } } \ No newline at end of file diff --git a/NzbDrone.Services.Api/DailySeries/DailySeriesModel.cs b/NzbDrone.Services.Api/DailySeries/DailySeriesModel.cs index 41dab258d..9c6ca51fe 100644 --- a/NzbDrone.Services.Api/DailySeries/DailySeriesModel.cs +++ b/NzbDrone.Services.Api/DailySeries/DailySeriesModel.cs @@ -14,10 +14,8 @@ namespace NzbDrone.Services.Api.DailySeries [BsonId] public Int32 Id { get; set; } - [BsonElement("t")] public String Title { get; set; } - [BsonElement("p")] public Boolean Public { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs b/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs index fea1978f8..3cfa6465d 100644 --- a/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs +++ b/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs @@ -9,12 +9,12 @@ namespace NzbDrone.Services.Api.DailySeries { public class DailySeriesModule : NancyModule { - private readonly DailySeriesRepository _dailySeriesProvider; + private readonly DailySeriesRepository _dailySeriesRepository; - public DailySeriesModule(DailySeriesRepository dailySeriesProvider) + public DailySeriesModule(DailySeriesRepository dailySeriesRepository) : base("/dailyseries") { - _dailySeriesProvider = dailySeriesProvider; + _dailySeriesRepository = dailySeriesRepository; Get["/"] = x => OnGet(); Get["/all"] = x => OnGet(); @@ -24,12 +24,14 @@ namespace NzbDrone.Services.Api.DailySeries private Response OnGet() { - return _dailySeriesProvider.Public().AsResponse(); + return _dailySeriesRepository.Public().AsResponse(); } private Response OnGet(int seriesId) { - return _dailySeriesProvider.IsDaily(seriesId).AsResponse(); + var result = _dailySeriesRepository.Find(seriesId) != null; + + return result.AsResponse(); } } } \ No newline at end of file diff --git a/NzbDrone.Services.Api/DailySeries/DailySeriesRepository.cs b/NzbDrone.Services.Api/DailySeries/DailySeriesRepository.cs index 1c24c1647..9c5c8d0ee 100644 --- a/NzbDrone.Services.Api/DailySeries/DailySeriesRepository.cs +++ b/NzbDrone.Services.Api/DailySeries/DailySeriesRepository.cs @@ -31,10 +31,10 @@ namespace NzbDrone.Services.Api.DailySeries return _mongoDb.GetCollection(DailySeriesModel.CollectionName).Find(query).ToList(); } - public Boolean IsDaily(int seriesId) + public DailySeriesModel Find(int seriesId) { var query = Query.EQ(d => d.Id, seriesId); - return _mongoDb.GetCollection(DailySeriesModel.CollectionName).Count(query) > 0; + return _mongoDb.GetCollection(DailySeriesModel.CollectionName).FindOne(query); } public void Insert(DailySeriesModel dailySeries) diff --git a/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj b/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj index 994612bb5..d135f28af 100644 --- a/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj +++ b/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj @@ -46,6 +46,9 @@ ..\packages\Autofac.2.6.3.862\lib\NET40\Autofac.Configuration.dll + + ..\packages\AutoMapper.2.2.0\lib\net40\AutoMapper.dll + ..\packages\mongocsharpdriver.1.7\lib\net35\MongoDB.Bson.dll @@ -99,7 +102,10 @@ + + + @@ -110,6 +116,9 @@ Web.config + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/NzbDrone.Services.Api/SceneMapping/SceneMappingJsonModel.cs b/NzbDrone.Services.Api/SceneMapping/SceneMappingJsonModel.cs new file mode 100644 index 000000000..c16b82f5d --- /dev/null +++ b/NzbDrone.Services.Api/SceneMapping/SceneMappingJsonModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using Newtonsoft.Json; + + +namespace NzbDrone.Services.Api.SceneMapping +{ + public class SceneMappingJsonModel + { + public String MapId { get; set; } + + public string CleanTitle { get; set; } + + [JsonProperty(PropertyName = "Id")] + public int SeriesId { get; set; } + + [JsonProperty(PropertyName = "Title")] + public string SceneName { get; set; } + + [JsonProperty(PropertyName = "Season")] + public int SeasonNumber { get; set; } + + public Boolean Public { get; set; } + } +} \ No newline at end of file diff --git a/NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs b/NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs index 22a3c44e6..4f808df8b 100644 --- a/NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs +++ b/NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Web; +using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using Newtonsoft.Json; @@ -13,21 +14,19 @@ namespace NzbDrone.Services.Api.SceneMapping public const string CollectionName = "SceneMappings"; [BsonId] - public String Id { get; set; } + public ObjectId MapId { get; set; } - [BsonElement("ct")] public string CleanTitle { get; set; } - [BsonElement("si")] - [JsonProperty(PropertyName = "id")] + [JsonProperty(PropertyName = "Id")] public int SeriesId { get; set; } - [BsonElement("sn")] [JsonProperty(PropertyName = "Title")] public string SceneName { get; set; } - [BsonElement("s")] [JsonProperty(PropertyName = "Season")] public int SeasonNumber { get; set; } + + public Boolean Public { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Services.Api/SceneMapping/SceneMappingModule.cs b/NzbDrone.Services.Api/SceneMapping/SceneMappingModule.cs new file mode 100644 index 000000000..cc603daae --- /dev/null +++ b/NzbDrone.Services.Api/SceneMapping/SceneMappingModule.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using AutoMapper; +using MongoDB.Bson; +using Nancy; +using NzbDrone.Services.Api.Extensions; + +namespace NzbDrone.Services.Api.SceneMapping +{ + public class SceneMappingModule : NancyModule + { + private readonly SceneMappingRepository _sceneMappingRepository; + + public SceneMappingModule(SceneMappingRepository sceneMappingRepository) + : base("/scenemapping") + { + _sceneMappingRepository = sceneMappingRepository; + + Get["/"] = x => OnGet(); + Get["/active"] = x => OnGet(); + Get["/{MapId}"] = x => OnGet(x.MapId); + } + + private Response OnGet() + { + var mappings = _sceneMappingRepository.Public(); + return Mapper.Map, List>(mappings).AsResponse(); + } + + private Response OnGet(string mapId) + { + var mapping = _sceneMappingRepository.Find(ObjectId.Parse(mapId)); + return Mapper.Map(mapping).AsResponse(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Services.Api/SceneMapping/SceneMappingRepository.cs b/NzbDrone.Services.Api/SceneMapping/SceneMappingRepository.cs new file mode 100644 index 000000000..4b74a215a --- /dev/null +++ b/NzbDrone.Services.Api/SceneMapping/SceneMappingRepository.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDB.Driver.Builders; +using NzbDrone.Services.Api.DailySeries; + +namespace NzbDrone.Services.Api.SceneMapping +{ + public class SceneMappingRepository + { + private readonly MongoDatabase _mongoDb; + + public SceneMappingRepository(MongoDatabase mongoDb) + { + _mongoDb = mongoDb; + } + + public SceneMappingRepository() + { + } + + public List All() + { + return _mongoDb.GetCollection(SceneMappingModel.CollectionName).FindAll().ToList(); + } + + public List Public() + { + var query = Query.EQ(d => d.Public, true); + return _mongoDb.GetCollection(SceneMappingModel.CollectionName).Find(query).ToList(); + } + + public SceneMappingModel Find(ObjectId mapId) + { + var query = Query.EQ(d => d.MapId, mapId); + return _mongoDb.GetCollection(SceneMappingModel.CollectionName).FindOne(query); + } + + public ObjectId Insert(SceneMappingModel mapping) + { + mapping.MapId = ObjectId.GenerateNewId(); + _mongoDb.GetCollection(SceneMappingModel.CollectionName).Insert(mapping); + + return mapping.MapId; + } + + public void Delete(ObjectId mapId) + { + var query = Query.EQ(d => d.MapId, mapId); + _mongoDb.GetCollection(SceneMappingModel.CollectionName).Remove(query); + } + + public void TogglePublic(ObjectId mapId, bool status) + { + var query = Query.EQ(d => d.MapId, mapId); + var update = Update.Set(d => d.Public, status); + _mongoDb.GetCollection(SceneMappingModel.CollectionName).Update(query, update); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Services.Api/packages.config b/NzbDrone.Services.Api/packages.config index e714240a5..e0acc6a7f 100644 --- a/NzbDrone.Services.Api/packages.config +++ b/NzbDrone.Services.Api/packages.config @@ -1,6 +1,7 @@  +