Json.net instead of ServiceStack

More DailySeries repo work done
pull/23/head
Mark McDowall 12 years ago committed by kay.one
parent c511292abe
commit b7575e05c1

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

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

@ -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<DailySeriesModel>.EQ(d => d.Id, seriesId);
return _mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).Count(query) > 0;
}
public void Insert(DailySeriesModel dailySeries)
{
_mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).Insert(dailySeries);
}
public void Delete(int seriesId)
{
var query = Query<DailySeriesModel>.EQ(d => d.Id, seriesId);
_mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).Remove(query);
}
public void TogglePublic(int seriesId, bool status)
{
var query = Query<DailySeriesModel>.EQ(d => d.Id, seriesId);
var update = Update<DailySeriesModel>.Set(d => d.Public, status);
_mongoDb.GetCollection<DailySeriesModel>(DailySeriesModel.CollectionName).Update(query, update);
}
}
}

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

@ -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<TModel>(string contentType, TModel model, Stream outputStream)
{
var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream));
Serializer.Instance.Serialize(jsonTextWriter, model);
jsonTextWriter.Flush();
}
public IEnumerable<string> Extensions { get; private set; }
}
}

@ -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<T>(this Stream body)
{
var reader = new StreamReader(body, true);
return JsonSerializer.DeserializeFromReader<T>(reader);
body.Position = 0;
var value = reader.ReadToEnd();
return JsonConvert.DeserializeObject<T>(value, Serializer.Settings);
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
var jsonResponse = new JsonResponse<TModel>(model, new ServiceStackSerializer()) { StatusCode = statusCode };
var jsonResponse = new JsonResponse<TModel>(model, new NancyJsonSerializer()) { StatusCode = statusCode };
return jsonResponse;
}
}

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

@ -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<TModel>(string contentType, TModel model, Stream outputStream)
{
JsonSerializer.SerializeToStream(model, outputStream);
}
public IEnumerable<string> Extensions { get; private set; }
}
}

@ -62,12 +62,12 @@
<Reference Include="Nancy.Hosting.Aspnet">
<HintPath>..\packages\Nancy.Hosting.Aspnet.0.15.3\lib\net40\Nancy.Hosting.Aspnet.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Text">
<HintPath>..\packages\ServiceStack.Text.3.9.33\lib\net35\ServiceStack.Text.dll</HintPath>
</Reference>
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
@ -93,11 +93,13 @@
<Compile Include="Bootstrapper.cs" />
<Compile Include="DailySeries\DailySeriesModel.cs" />
<Compile Include="DailySeries\DailySeriesModule.cs" />
<Compile Include="DailySeries\DailySeriesProvider.cs" />
<Compile Include="DailySeries\DailySeriesRepository.cs" />
<Compile Include="Datastore\Connection.cs" />
<Compile Include="NancyExtensions\JsonExtensions.cs" />
<Compile Include="NancyExtensions\ServiceStackSerializer.cs" />
<Compile Include="Extensions\NancyJsonSerializer.cs" />
<Compile Include="Extensions\RequestExtensions.cs" />
<Compile Include="Extensions\Serializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SceneMapping\SceneMappingModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />

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

@ -4,6 +4,9 @@
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="MongoLab" connectionString="mongodb://nzbdrone:h53huDrAzufRe8a3@ds035147.mongolab.com:35147/?safe=true;wtimeoutMS=2000" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpHandlers>

@ -5,6 +5,6 @@
<package id="Nancy" version="0.15.3" targetFramework="net40" />
<package id="Nancy.Bootstrappers.Autofac" version="0.15.3" targetFramework="net40" />
<package id="Nancy.Hosting.Aspnet" version="0.15.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
<package id="NLog" version="2.0.0.2000" targetFramework="net40" />
<package id="ServiceStack.Text" version="3.9.33" targetFramework="net40" />
</packages>

@ -5,6 +5,8 @@
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=5C7F3FB135E52A44B9447C48B2EEEE92/AbsolutePath/@EntryValue">C:\Dropbox\Git\NzbDrone\NzbDrone.sln.DotSettings</s:String>
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File5C7F3FB135E52A44B9447C48B2EEEE92/@KeyIndexDefined">True</s:Boolean>
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File5C7F3FB135E52A44B9447C48B2EEEE92/RelativePriority/@EntryValue">1</s:Double>
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File_003A_003AC_003A_005CDropbox_005CGit_005CNzbDrone_005CNzbDrone_002Esln_002EDotSettings/@KeyIndexDefined">True</s:Boolean>
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File_003A_003AC_003A_005CDropbox_005CGit_005CNzbDrone_005CNzbDrone_002Esln_002EDotSettings/RelativePriority/@EntryValue">2</s:Double>
<s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/QuickList/=26E712D4B91E2E49A0E92C0AFE6FF57E/Entry/=38860059D7978D4DAF1997C7CBC46A78/EntryName/@EntryValue">Backbone model</s:String>
<s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/QuickList/=26E712D4B91E2E49A0E92C0AFE6FF57E/Entry/=38860059D7978D4DAF1997C7CBC46A78/Position/@EntryValue">5</s:Int64>
<s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=4F6A0CA32FE60746A73308F7E39A63C1/@KeyIndexDefined">True</s:Boolean>

Loading…
Cancel
Save