SceneMapping added to Services API

pull/4/head
Mark McDowall 12 years ago committed by kay.one
parent 4176e7e9e4
commit 27afb2402a

@ -3,11 +3,14 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Web; using System.Web;
using AutoMapper;
using Autofac; using Autofac;
using MongoDB.Bson;
using NLog; using NLog;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.Bootstrappers.Autofac; using Nancy.Bootstrappers.Autofac;
using NzbDrone.Services.Api.Extensions; using NzbDrone.Services.Api.Extensions;
using NzbDrone.Services.Api.SceneMapping;
namespace NzbDrone.Services.Api namespace NzbDrone.Services.Api
{ {
@ -37,5 +40,19 @@ namespace NzbDrone.Services.Api
return NancyInternalConfiguration.WithOverrides(c => c.Serializers.Add(typeof(NancyJsonSerializer))); return NancyInternalConfiguration.WithOverrides(c => c.Serializers.Add(typeof(NancyJsonSerializer)));
} }
} }
protected override void ApplicationStartup(ILifetimeScope container, IPipelines pipelines)
{
InitializeAutomapper();
}
public static void InitializeAutomapper()
{
Mapper.CreateMap<SceneMappingModel, SceneMappingJsonModel>()
.ForMember(dest => dest.MapId, opt => opt.MapFrom(src => src.MapId.ToString()));
Mapper.CreateMap<SceneMappingJsonModel, SceneMappingModel>()
.ForMember(dest => dest.MapId, opt => opt.MapFrom(src => ObjectId.Parse(src.MapId)));
}
} }
} }

@ -14,10 +14,8 @@ namespace NzbDrone.Services.Api.DailySeries
[BsonId] [BsonId]
public Int32 Id { get; set; } public Int32 Id { get; set; }
[BsonElement("t")]
public String Title { get; set; } public String Title { get; set; }
[BsonElement("p")]
public Boolean Public { get; set; } public Boolean Public { get; set; }
} }
} }

@ -9,12 +9,12 @@ namespace NzbDrone.Services.Api.DailySeries
{ {
public class DailySeriesModule : NancyModule public class DailySeriesModule : NancyModule
{ {
private readonly DailySeriesRepository _dailySeriesProvider; private readonly DailySeriesRepository _dailySeriesRepository;
public DailySeriesModule(DailySeriesRepository dailySeriesProvider) public DailySeriesModule(DailySeriesRepository dailySeriesRepository)
: base("/dailyseries") : base("/dailyseries")
{ {
_dailySeriesProvider = dailySeriesProvider; _dailySeriesRepository = dailySeriesRepository;
Get["/"] = x => OnGet(); Get["/"] = x => OnGet();
Get["/all"] = x => OnGet(); Get["/all"] = x => OnGet();
@ -24,12 +24,14 @@ namespace NzbDrone.Services.Api.DailySeries
private Response OnGet() private Response OnGet()
{ {
return _dailySeriesProvider.Public().AsResponse(); return _dailySeriesRepository.Public().AsResponse();
} }
private Response OnGet(int seriesId) private Response OnGet(int seriesId)
{ {
return _dailySeriesProvider.IsDaily(seriesId).AsResponse(); var result = _dailySeriesRepository.Find(seriesId) != null;
return result.AsResponse();
} }
} }
} }

@ -31,10 +31,10 @@ namespace NzbDrone.Services.Api.DailySeries
return _mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).Find(query).ToList(); return _mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).Find(query).ToList();
} }
public Boolean IsDaily(int seriesId) public DailySeriesModel Find(int seriesId)
{ {
var query = Query<DailySeriesModel>.EQ(d => d.Id, seriesId); var query = Query<DailySeriesModel>.EQ(d => d.Id, seriesId);
return _mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).Count(query) > 0; return _mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).FindOne(query);
} }
public void Insert(DailySeriesModel dailySeries) public void Insert(DailySeriesModel dailySeries)

@ -46,6 +46,9 @@
<Reference Include="Autofac.Configuration"> <Reference Include="Autofac.Configuration">
<HintPath>..\packages\Autofac.2.6.3.862\lib\NET40\Autofac.Configuration.dll</HintPath> <HintPath>..\packages\Autofac.2.6.3.862\lib\NET40\Autofac.Configuration.dll</HintPath>
</Reference> </Reference>
<Reference Include="AutoMapper">
<HintPath>..\packages\AutoMapper.2.2.0\lib\net40\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="MongoDB.Bson"> <Reference Include="MongoDB.Bson">
<HintPath>..\packages\mongocsharpdriver.1.7\lib\net35\MongoDB.Bson.dll</HintPath> <HintPath>..\packages\mongocsharpdriver.1.7\lib\net35\MongoDB.Bson.dll</HintPath>
@ -99,7 +102,10 @@
<Compile Include="Extensions\RequestExtensions.cs" /> <Compile Include="Extensions\RequestExtensions.cs" />
<Compile Include="Extensions\Serializer.cs" /> <Compile Include="Extensions\Serializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SceneMapping\SceneMappingJsonModel.cs" />
<Compile Include="SceneMapping\SceneMappingModel.cs" /> <Compile Include="SceneMapping\SceneMappingModel.cs" />
<Compile Include="SceneMapping\SceneMappingModule.cs" />
<Compile Include="SceneMapping\SceneMappingRepository.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="packages.config" /> <Content Include="packages.config" />
@ -110,6 +116,9 @@
<DependentUpon>Web.config</DependentUpon> <DependentUpon>Web.config</DependentUpon>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Resolvers\" />
</ItemGroup>
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

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

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -13,21 +14,19 @@ namespace NzbDrone.Services.Api.SceneMapping
public const string CollectionName = "SceneMappings"; public const string CollectionName = "SceneMappings";
[BsonId] [BsonId]
public String Id { get; set; } public ObjectId MapId { get; set; }
[BsonElement("ct")]
public string CleanTitle { get; set; } public string CleanTitle { get; set; }
[BsonElement("si")] [JsonProperty(PropertyName = "Id")]
[JsonProperty(PropertyName = "id")]
public int SeriesId { get; set; } public int SeriesId { get; set; }
[BsonElement("sn")]
[JsonProperty(PropertyName = "Title")] [JsonProperty(PropertyName = "Title")]
public string SceneName { get; set; } public string SceneName { get; set; }
[BsonElement("s")]
[JsonProperty(PropertyName = "Season")] [JsonProperty(PropertyName = "Season")]
public int SeasonNumber { get; set; } public int SeasonNumber { get; set; }
public Boolean Public { get; set; }
} }
} }

@ -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<SceneMappingModel>, List<SceneMappingJsonModel>>(mappings).AsResponse();
}
private Response OnGet(string mapId)
{
var mapping = _sceneMappingRepository.Find(ObjectId.Parse(mapId));
return Mapper.Map<SceneMappingModel, SceneMappingJsonModel>(mapping).AsResponse();
}
}
}

@ -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<SceneMappingModel> All()
{
return _mongoDb.GetCollection<SceneMappingModel>(SceneMappingModel.CollectionName).FindAll().ToList();
}
public List<SceneMappingModel> Public()
{
var query = Query<SceneMappingModel>.EQ(d => d.Public, true);
return _mongoDb.GetCollection<SceneMappingModel>(SceneMappingModel.CollectionName).Find(query).ToList();
}
public SceneMappingModel Find(ObjectId mapId)
{
var query = Query<SceneMappingModel>.EQ(d => d.MapId, mapId);
return _mongoDb.GetCollection<SceneMappingModel>(SceneMappingModel.CollectionName).FindOne(query);
}
public ObjectId Insert(SceneMappingModel mapping)
{
mapping.MapId = ObjectId.GenerateNewId();
_mongoDb.GetCollection<SceneMappingModel>(SceneMappingModel.CollectionName).Insert(mapping);
return mapping.MapId;
}
public void Delete(ObjectId mapId)
{
var query = Query<SceneMappingModel>.EQ(d => d.MapId, mapId);
_mongoDb.GetCollection<SceneMappingModel>(SceneMappingModel.CollectionName).Remove(query);
}
public void TogglePublic(ObjectId mapId, bool status)
{
var query = Query<SceneMappingModel>.EQ(d => d.MapId, mapId);
var update = Update<SceneMappingModel>.Set(d => d.Public, status);
_mongoDb.GetCollection<SceneMappingModel>(SceneMappingModel.CollectionName).Update(query, update);
}
}
}

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Autofac" version="2.6.3.862" targetFramework="net40" /> <package id="Autofac" version="2.6.3.862" targetFramework="net40" />
<package id="AutoMapper" version="2.2.0" targetFramework="net40" />
<package id="mongocsharpdriver" version="1.7" targetFramework="net40" /> <package id="mongocsharpdriver" version="1.7" targetFramework="net40" />
<package id="Nancy" version="0.15.3" targetFramework="net40" /> <package id="Nancy" version="0.15.3" targetFramework="net40" />
<package id="Nancy.Bootstrappers.Autofac" version="0.15.3" targetFramework="net40" /> <package id="Nancy.Bootstrappers.Autofac" version="0.15.3" targetFramework="net40" />

Loading…
Cancel
Save