From b7575e05c1655e00c2250e947b15a1648c2bbe48 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 30 Jan 2013 18:02:13 -0800 Subject: [PATCH] Json.net instead of ServiceStack More DailySeries repo work done --- NzbDrone.Services.Api/Bootstrapper.cs | 4 +-- .../DailySeries/DailySeriesModule.cs | 6 ++-- ...esProvider.cs => DailySeriesRepository.cs} | 24 ++++++++++++-- NzbDrone.Services.Api/Datastore/Connection.cs | 3 +- .../Extensions/NancyJsonSerializer.cs | 27 +++++++++++++++ .../RequestExtensions.cs} | 15 ++++----- .../Extensions/Serializer.cs | 33 +++++++++++++++++++ .../NancyExtensions/ServiceStackSerializer.cs | 27 --------------- .../NzbDrone.Services.Api.csproj | 14 ++++---- .../SceneMapping/SceneMappingModel.cs | 33 +++++++++++++++++++ NzbDrone.Services.Api/Web.config | 3 ++ NzbDrone.Services.Api/packages.config | 2 +- NzbDrone.sln.DotSettings | 2 ++ 13 files changed, 142 insertions(+), 51 deletions(-) rename NzbDrone.Services.Api/DailySeries/{DailySeriesProvider.cs => DailySeriesRepository.cs} (52%) create mode 100644 NzbDrone.Services.Api/Extensions/NancyJsonSerializer.cs rename NzbDrone.Services.Api/{NancyExtensions/JsonExtensions.cs => Extensions/RequestExtensions.cs} (61%) create mode 100644 NzbDrone.Services.Api/Extensions/Serializer.cs delete mode 100644 NzbDrone.Services.Api/NancyExtensions/ServiceStackSerializer.cs create mode 100644 NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs diff --git a/NzbDrone.Services.Api/Bootstrapper.cs b/NzbDrone.Services.Api/Bootstrapper.cs index 8d9c2895e..f55e22f2d 100644 --- a/NzbDrone.Services.Api/Bootstrapper.cs +++ b/NzbDrone.Services.Api/Bootstrapper.cs @@ -7,7 +7,7 @@ using Autofac; using NLog; using Nancy.Bootstrapper; using Nancy.Bootstrappers.Autofac; -using NzbDrone.Services.Api.NancyExtensions; +using NzbDrone.Services.Api.Extensions; namespace NzbDrone.Services.Api { @@ -34,7 +34,7 @@ namespace NzbDrone.Services.Api { get { - return NancyInternalConfiguration.WithOverrides(c => c.Serializers.Add(typeof(ServiceStackSerializer))); + return NancyInternalConfiguration.WithOverrides(c => c.Serializers.Add(typeof(NancyJsonSerializer))); } } } diff --git a/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs b/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs index 4e7c957a6..fea1978f8 100644 --- a/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs +++ b/NzbDrone.Services.Api/DailySeries/DailySeriesModule.cs @@ -3,15 +3,15 @@ using System.Collections.Generic; using System.Linq; using System.Web; using Nancy; -using NzbDrone.Services.Api.NancyExtensions; +using NzbDrone.Services.Api.Extensions; namespace NzbDrone.Services.Api.DailySeries { public class DailySeriesModule : NancyModule { - private readonly DailySeriesProvider _dailySeriesProvider; + private readonly DailySeriesRepository _dailySeriesProvider; - public DailySeriesModule(DailySeriesProvider dailySeriesProvider) + public DailySeriesModule(DailySeriesRepository dailySeriesProvider) : base("/dailyseries") { _dailySeriesProvider = dailySeriesProvider; diff --git a/NzbDrone.Services.Api/DailySeries/DailySeriesProvider.cs b/NzbDrone.Services.Api/DailySeries/DailySeriesRepository.cs similarity index 52% rename from NzbDrone.Services.Api/DailySeries/DailySeriesProvider.cs rename to NzbDrone.Services.Api/DailySeries/DailySeriesRepository.cs index 36f00c117..1c24c1647 100644 --- a/NzbDrone.Services.Api/DailySeries/DailySeriesProvider.cs +++ b/NzbDrone.Services.Api/DailySeries/DailySeriesRepository.cs @@ -7,16 +7,16 @@ using MongoDB.Driver.Builders; namespace NzbDrone.Services.Api.DailySeries { - public class DailySeriesProvider + public class DailySeriesRepository { private readonly MongoDatabase _mongoDb; - public DailySeriesProvider(MongoDatabase mongoDb) + public DailySeriesRepository(MongoDatabase mongoDb) { _mongoDb = mongoDb; } - public DailySeriesProvider() + public DailySeriesRepository() { } @@ -36,5 +36,23 @@ namespace NzbDrone.Services.Api.DailySeries var query = Query.EQ(d => d.Id, seriesId); return _mongoDb.GetCollection(DailySeriesModel.CollectionName).Count(query) > 0; } + + public void Insert(DailySeriesModel dailySeries) + { + _mongoDb.GetCollection(DailySeriesModel.CollectionName).Insert(dailySeries); + } + + public void Delete(int seriesId) + { + var query = Query.EQ(d => d.Id, seriesId); + _mongoDb.GetCollection(DailySeriesModel.CollectionName).Remove(query); + } + + public void TogglePublic(int seriesId, bool status) + { + var query = Query.EQ(d => d.Id, seriesId); + var update = Update.Set(d => d.Public, status); + _mongoDb.GetCollection(DailySeriesModel.CollectionName).Update(query, update); + } } } \ No newline at end of file diff --git a/NzbDrone.Services.Api/Datastore/Connection.cs b/NzbDrone.Services.Api/Datastore/Connection.cs index 312214cec..3234fc252 100644 --- a/NzbDrone.Services.Api/Datastore/Connection.cs +++ b/NzbDrone.Services.Api/Datastore/Connection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Linq; using System.Web; using MongoDB.Driver; @@ -10,7 +11,7 @@ namespace NzbDrone.Services.Api.Datastore { public MongoDatabase GetMainDb() { - var db = GetMongoDb("mongodb://nzbdrone:h53huDrAzufRe8a3@ds035147.mongolab.com:35147/?safe=true;wtimeoutMS=2000", "services-dev"); + var db = GetMongoDb(ConfigurationManager.ConnectionStrings["MongoLab"].ConnectionString, "services-dev"); return db; } diff --git a/NzbDrone.Services.Api/Extensions/NancyJsonSerializer.cs b/NzbDrone.Services.Api/Extensions/NancyJsonSerializer.cs new file mode 100644 index 000000000..38658c9bc --- /dev/null +++ b/NzbDrone.Services.Api/Extensions/NancyJsonSerializer.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Nancy; +using Newtonsoft.Json; + +namespace NzbDrone.Services.Api.Extensions +{ + public class NancyJsonSerializer : ISerializer + { + public readonly static NancyJsonSerializer Instance = new NancyJsonSerializer(); + + public bool CanSerialize(string contentType) + { + return true; + } + + public void Serialize(string contentType, TModel model, Stream outputStream) + { + var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream)); + Serializer.Instance.Serialize(jsonTextWriter, model); + jsonTextWriter.Flush(); + } + + public IEnumerable Extensions { get; private set; } + } +} \ No newline at end of file diff --git a/NzbDrone.Services.Api/NancyExtensions/JsonExtensions.cs b/NzbDrone.Services.Api/Extensions/RequestExtensions.cs similarity index 61% rename from NzbDrone.Services.Api/NancyExtensions/JsonExtensions.cs rename to NzbDrone.Services.Api/Extensions/RequestExtensions.cs index 4855c2a1c..dc2892c0e 100644 --- a/NzbDrone.Services.Api/NancyExtensions/JsonExtensions.cs +++ b/NzbDrone.Services.Api/Extensions/RequestExtensions.cs @@ -1,25 +1,24 @@ -using System; -using System.Collections.Generic; -using System.IO; +using System.IO; using System.Linq; -using System.Web; using Nancy; using Nancy.Responses; -using ServiceStack.Text; +using Newtonsoft.Json; -namespace NzbDrone.Services.Api.NancyExtensions +namespace NzbDrone.Services.Api.Extensions { public static class JsonExtensions { public static T FromJson(this Stream body) { var reader = new StreamReader(body, true); - return JsonSerializer.DeserializeFromReader(reader); + body.Position = 0; + var value = reader.ReadToEnd(); + return JsonConvert.DeserializeObject(value, Serializer.Settings); } public static JsonResponse AsResponse(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK) { - var jsonResponse = new JsonResponse(model, new ServiceStackSerializer()) { StatusCode = statusCode }; + var jsonResponse = new JsonResponse(model, new NancyJsonSerializer()) { StatusCode = statusCode }; return jsonResponse; } } diff --git a/NzbDrone.Services.Api/Extensions/Serializer.cs b/NzbDrone.Services.Api/Extensions/Serializer.cs new file mode 100644 index 000000000..d990596e4 --- /dev/null +++ b/NzbDrone.Services.Api/Extensions/Serializer.cs @@ -0,0 +1,33 @@ +using System.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace NzbDrone.Services.Api.Extensions +{ + public static class Serializer + { + static Serializer() + { + Settings = new JsonSerializerSettings + { + DateTimeZoneHandling = DateTimeZoneHandling.Utc, + NullValueHandling = NullValueHandling.Ignore, + Formatting = Formatting.None, + DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate + }; + + Instance = new JsonSerializer + { + DateTimeZoneHandling = Settings.DateTimeZoneHandling, + NullValueHandling = NullValueHandling.Ignore, + Formatting = Formatting.None, + DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, + ContractResolver = new CamelCasePropertyNamesContractResolver() + }; + } + + public static JsonSerializerSettings Settings { get; private set; } + + public static JsonSerializer Instance { get; private set; } + } +} \ No newline at end of file diff --git a/NzbDrone.Services.Api/NancyExtensions/ServiceStackSerializer.cs b/NzbDrone.Services.Api/NancyExtensions/ServiceStackSerializer.cs deleted file mode 100644 index 175db50b3..000000000 --- a/NzbDrone.Services.Api/NancyExtensions/ServiceStackSerializer.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Web; -using Nancy; -using ServiceStack.Text; - -namespace NzbDrone.Services.Api.NancyExtensions -{ - public class ServiceStackSerializer : ISerializer - { - public readonly static ServiceStackSerializer Instance = new ServiceStackSerializer(); - - public bool CanSerialize(string contentType) - { - return true; - } - - public void Serialize(string contentType, TModel model, Stream outputStream) - { - JsonSerializer.SerializeToStream(model, outputStream); - } - - public IEnumerable Extensions { get; private set; } - } -} \ No newline at end of file diff --git a/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj b/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj index de18bf2c4..994612bb5 100644 --- a/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj +++ b/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj @@ -62,12 +62,12 @@ ..\packages\Nancy.Hosting.Aspnet.0.15.3\lib\net40\Nancy.Hosting.Aspnet.dll + + ..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll + ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll - - ..\packages\ServiceStack.Text.3.9.33\lib\net35\ServiceStack.Text.dll - @@ -93,11 +93,13 @@ - + - - + + + + diff --git a/NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs b/NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs new file mode 100644 index 000000000..22a3c44e6 --- /dev/null +++ b/NzbDrone.Services.Api/SceneMapping/SceneMappingModel.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using MongoDB.Bson.Serialization.Attributes; +using Newtonsoft.Json; + + +namespace NzbDrone.Services.Api.SceneMapping +{ + public class SceneMappingModel + { + public const string CollectionName = "SceneMappings"; + + [BsonId] + public String Id { get; set; } + + [BsonElement("ct")] + public string CleanTitle { get; set; } + + [BsonElement("si")] + [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; } + } +} \ No newline at end of file diff --git a/NzbDrone.Services.Api/Web.config b/NzbDrone.Services.Api/Web.config index 9166b6a1b..251003427 100644 --- a/NzbDrone.Services.Api/Web.config +++ b/NzbDrone.Services.Api/Web.config @@ -4,6 +4,9 @@ http://go.microsoft.com/fwlink/?LinkId=169433 --> + + + diff --git a/NzbDrone.Services.Api/packages.config b/NzbDrone.Services.Api/packages.config index 1c011ee9b..e714240a5 100644 --- a/NzbDrone.Services.Api/packages.config +++ b/NzbDrone.Services.Api/packages.config @@ -5,6 +5,6 @@ + - \ No newline at end of file diff --git a/NzbDrone.sln.DotSettings b/NzbDrone.sln.DotSettings index 676ad420d..0c6ea2ed2 100644 --- a/NzbDrone.sln.DotSettings +++ b/NzbDrone.sln.DotSettings @@ -5,6 +5,8 @@ C:\Dropbox\Git\NzbDrone\NzbDrone.sln.DotSettings True 1 + True + 2 Backbone model 5 True