Skip to content

Commit

Permalink
Upgrade to v6 + .NET 6
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 9, 2023
1 parent fda1bb0 commit 3ba6ad5
Show file tree
Hide file tree
Showing 34 changed files with 516 additions and 644 deletions.
21 changes: 10 additions & 11 deletions src/Northwind.ServiceInterface/CachedServices.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Northwind.ServiceModel;
using ServiceStack;

namespace Northwind.ServiceInterface
namespace Northwind.ServiceInterface;

[CacheResponse(Duration = 60 * 60, MaxAge = 30 * 60)]
public class CachedServices : Service
{
[CacheResponse(Duration = 60 * 60, MaxAge = 30 * 60)]
public class CachedServices : Service
{
public object Get(CachedGetAllCustomers request) =>
Gateway.Send(new GetAllCustomers());
public object Get(CachedGetAllCustomers request) =>
Gateway.Send(new GetAllCustomers());

public object Get(CachedGetCustomerDetails request) =>
Gateway.Send(new GetCustomerDetails { Id = request.Id });
public object Get(CachedGetCustomerDetails request) =>
Gateway.Send(new GetCustomerDetails { Id = request.Id });

public object Get(CachedGetOrders request) =>
Gateway.Send(new GetOrders { CustomerId = request.CustomerId, Page = request.Page });
}
public object Get(CachedGetOrders request) =>
Gateway.Send(new GetOrders { CustomerId = request.CustomerId, Page = request.Page });
}
30 changes: 14 additions & 16 deletions src/Northwind.ServiceInterface/CustomerDetailsService.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
using System;
using System.Net;
using Northwind.ServiceModel;
using Northwind.ServiceModel.Types;
using ServiceStack;
using ServiceStack.OrmLite;

namespace Northwind.ServiceInterface
namespace Northwind.ServiceInterface;

public class CustomerDetailsService : Service
{
public class CustomerDetailsService : Service
public CustomerDetailsResponse Get(GetCustomerDetails request)
{
public CustomerDetailsResponse Get(GetCustomerDetails request)
{
var customer = Db.SingleById<Customer>(request.Id);
if (customer == null)
throw new HttpError(HttpStatusCode.NotFound,
new ArgumentException("Customer does not exist: " + request.Id));
var customer = Db.SingleById<Customer>(request.Id);
if (customer == null)
throw new HttpError(HttpStatusCode.NotFound,
new ArgumentException("Customer does not exist: " + request.Id));

var ordersResponse = base.Gateway.Send(new GetOrders { CustomerId = customer.Id });
return new CustomerDetailsResponse
{
Customer = customer,
Orders = ordersResponse.Results,
};
}
var ordersResponse = base.Gateway.Send(new GetOrders { CustomerId = customer.Id });
return new CustomerDetailsResponse
{
Customer = customer,
Orders = ordersResponse.Results,
};
}
}
13 changes: 6 additions & 7 deletions src/Northwind.ServiceInterface/CustomersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
using ServiceStack;
using ServiceStack.OrmLite;

namespace Northwind.ServiceInterface
namespace Northwind.ServiceInterface;

public class CustomersService : Service
{
public class CustomersService : Service
{
public object Get(GetAllCustomers request) =>
new CustomersResponse { Results = Db.Select<Customer>() };
}
}
public object Get(GetAllCustomers request) =>
new CustomersResponse { Results = Db.Select<Customer>() };
}
17 changes: 4 additions & 13 deletions src/Northwind.ServiceInterface/Northwind.ServiceInterface.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Northwind.ServiceInterface</AssemblyName>
<PackageId>Northwind.ServiceInterface</PackageId>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Northwind.ServiceModel/Northwind.ServiceModel.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ServiceStack.Server" Version="5.*" />
<PackageReference Include="ServiceStack" Version="5.*" />
<PackageReference Include="ServiceStack.Interfaces" Version="5.*" />
<PackageReference Include="ServiceStack.Text" Version="5.*" />
<PackageReference Include="ServiceStack.Client" Version="5.*" />
<PackageReference Include="ServiceStack.Common" Version="5.*" />
<PackageReference Include="ServiceStack.OrmLite" Version="5.*" />
<PackageReference Include="ServiceStack.Server" Version="6.*" />
</ItemGroup>

</Project>
45 changes: 22 additions & 23 deletions src/Northwind.ServiceInterface/OrdersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,34 @@
using ServiceStack;
using ServiceStack.OrmLite;

namespace Northwind.ServiceInterface
namespace Northwind.ServiceInterface;

public class OrdersService : Service
{
public class OrdersService : Service
{
private const int PageCount = 20;
private const int PageCount = 20;

public OrdersResponse Get(GetOrders request)
{
var orders = request.CustomerId.IsNullOrEmpty()
? Db.Select(Db.From<Order>().OrderByDescending(o => o.OrderDate))
.Skip((request.Page.GetValueOrDefault(1) - 1) * PageCount)
.Take(PageCount)
.ToList()
: Db.Select<Order>(x => x.CustomerId == request.CustomerId);
public OrdersResponse Get(GetOrders request)
{
var orders = request.CustomerId.IsNullOrEmpty()
? Db.Select(Db.From<Order>().OrderByDescending(o => o.OrderDate))
.Skip((request.Page.GetValueOrDefault(1) - 1) * PageCount)
.Take(PageCount)
.ToList()
: Db.Select<Order>(x => x.CustomerId == request.CustomerId);

if (orders.Count == 0)
return new OrdersResponse();
if (orders.Count == 0)
return new OrdersResponse();

var orderDetails = Db.Select<OrderDetail>(detail => Sql.In(detail.OrderId, orders.ConvertAll(x => x.Id)));
var orderDetails = Db.Select<OrderDetail>(detail => Sql.In(detail.OrderId, orders.ConvertAll(x => x.Id)));

var orderDetailsLookup = orderDetails.ToLookup(o => o.OrderId);
var orderDetailsLookup = orderDetails.ToLookup(o => o.OrderId);

var customerOrders = orders.ConvertAll(o => new CustomerOrder
{
Order = o,
OrderDetails = orderDetailsLookup[o.Id].ToList()
});
var customerOrders = orders.ConvertAll(o => new CustomerOrder
{
Order = o,
OrderDetails = orderDetailsLookup[o.Id].ToList()
});

return new OrdersResponse { Results = customerOrders };
}
return new OrdersResponse { Results = customerOrders };
}
}
19 changes: 0 additions & 19 deletions src/Northwind.ServiceInterface/Properties/AssemblyInfo.cs

This file was deleted.

88 changes: 39 additions & 49 deletions src/Northwind.ServiceInterface/VCardFormat.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,57 @@
using Northwind.ServiceModel;
using ServiceStack;
using ServiceStack.Web;
using System;
using System.IO;

namespace Northwind.ServiceInterface;

namespace Northwind.ServiceInterface
using ServiceModel.Types;

public class VCardFormat
{
using ServiceModel.Types;
private const string VCardContentType = "text/x-vcard";

public class VCardFormat
public static void Register(IAppHost appHost)
{
private const string VCardContentType = "text/x-vcard";
appHost.ContentTypes.Register(VCardContentType, SerializeToStream, DeserializeFromStream);

public static void Register(IAppHost appHost)
appHost.GlobalResponseFilters.Add((req, res, dto) =>
{
appHost.ContentTypes.Register(VCardContentType, SerializeToStream, DeserializeFromStream);

appHost.GlobalResponseFilters.Add((req, res, dto) =>
if (req.ResponseContentType == VCardContentType)
{
if (req.ResponseContentType == VCardContentType)
{
res.AddHeader(HttpHeaders.ContentDisposition,
string.Format("attachment;filename={0}.vcf", req.OperationName));
}
});
}

public static void SerializeToStream(IRequest req, object response, Stream stream)
{
var customerResponse = response as CustomerDetailsResponse;
using (var sw = new StreamWriter(stream))
{
if (customerResponse != null)
{
WriteCustomer(sw, customerResponse.Customer);
}
var customers = response as CustomersResponse;
if (customers != null)
{
customers.Results.ForEach(x => WriteCustomer(sw, x));
}
res.AddHeader(HttpHeaders.ContentDisposition,
string.Format("attachment;filename={0}.vcf", req.OperationName));
}
}
});
}

public static void WriteCustomer(StreamWriter sw, Customer customer)
public static void SerializeToStream(IRequest req, object response, Stream stream)
{
var customerResponse = response as CustomerDetailsResponse;
using var sw = new StreamWriter(stream);
if (customerResponse != null)
{
sw.WriteLine("BEGIN:VCARD");
sw.WriteLine("VERSION:2.1");
sw.WriteLine("FN:" + customer.ContactName);
sw.WriteLine("ORG:" + customer.CompanyName);
sw.WriteLine("TITLE:" + customer.ContactTitle);
sw.WriteLine("EMAIL;TYPE=PREF,INTERNET:" + customer.Email);
sw.WriteLine("TEL;HOME;VOICE:" + customer.Phone);
sw.WriteLine("TEL;WORK;FAX:" + customer.Fax);
sw.WriteLine("ADR;TYPE=HOME;"
+ new[] { customer.Address, customer.City, customer.PostalCode }.Join(";"));
sw.WriteLine("END:VCARD");
WriteCustomer(sw, customerResponse.Customer);
}

public static object DeserializeFromStream(Type type, Stream stream)
if (response is CustomersResponse customers)
{
throw new NotImplementedException();
customers.Results.ForEach(x => WriteCustomer(sw, x));
}
}
}

public static void WriteCustomer(StreamWriter sw, Customer customer)
{
sw.WriteLine("BEGIN:VCARD");
sw.WriteLine("VERSION:2.1");
sw.WriteLine("FN:" + customer.ContactName);
sw.WriteLine("ORG:" + customer.CompanyName);
sw.WriteLine("TITLE:" + customer.ContactTitle);
sw.WriteLine("EMAIL;TYPE=PREF,INTERNET:" + customer.Email);
sw.WriteLine("TEL;HOME;VOICE:" + customer.Phone);
sw.WriteLine("TEL;WORK;FAX:" + customer.Fax);
sw.WriteLine("ADR;TYPE=HOME;"
+ new[] { customer.Address, customer.City, customer.PostalCode }.Join(";"));
sw.WriteLine("END:VCARD");
}

public static object DeserializeFromStream(Type type, Stream stream) => throw new NotImplementedException();
}
26 changes: 12 additions & 14 deletions src/Northwind.ServiceModel/AutoQuery.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System.Collections.Generic;
using Northwind.ServiceModel.Types;
using Northwind.ServiceModel.Types;
using ServiceStack;

namespace Northwind.ServiceModel
namespace Northwind.ServiceModel;

[Route("/query/customers"), Tag(Tags.AutoQuery)]
public class QueryCustomers : QueryDb<Customer>
{
[Route("/query/customers"), Tag(Tags.AutoQuery)]
public class QueryCustomers : QueryDb<Customer>
{
public List<string> Ids { get; set; }
public string CountryStartsWith { get; set; }
}
public List<string> Ids { get; set; }
public string CountryStartsWith { get; set; }
}

[Route("/query/orders"), Tag(Tags.AutoQuery)]
public class QueryOrders : QueryDb<Order>
{
public decimal? Freight { get; set; }
}
[Route("/query/orders"), Tag(Tags.AutoQuery)]
public class QueryOrders : QueryDb<Order>
{
public decimal? Freight { get; set; }
}
37 changes: 18 additions & 19 deletions src/Northwind.ServiceModel/CachedOperations.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using ServiceStack;

namespace Northwind.ServiceModel
{
[Route("/cached/customers"), Tag(Tags.Cached)]
public class CachedGetAllCustomers : IGet, IReturn<CustomersResponse> {}
namespace Northwind.ServiceModel;

[Route("/cached/customers"), Tag(Tags.Cached)]
public class CachedGetAllCustomers : IGet, IReturn<CustomersResponse> {}

[Route("/cached/customers/{Id}"), Tag(Tags.Cached)]
public class CachedGetCustomerDetails : IGet, IReturn<CustomerDetailsResponse>
{
public string Id { get; set; }
}
[Route("/cached/customers/{Id}"), Tag(Tags.Cached)]
public class CachedGetCustomerDetails : IGet, IReturn<CustomerDetailsResponse>
{
public string Id { get; set; }
}

[Tag(Tags.Cached)]
[Route("/cached/orders")]
[Route("/cached/orders/page/{Page}")]
[Route("/cached/customers/{CustomerId}/orders")]
public class CachedGetOrders : IGet, IReturn<OrdersResponse>
{
public int? Page { get; set; }
public string CustomerId { get; set; }
}
}
[Tag(Tags.Cached)]
[Route("/cached/orders")]
[Route("/cached/orders/page/{Page}")]
[Route("/cached/customers/{CustomerId}/orders")]
public class CachedGetOrders : IGet, IReturn<OrdersResponse>
{
public int? Page { get; set; }
public string CustomerId { get; set; }
}
Loading

0 comments on commit 3ba6ad5

Please sign in to comment.