Endpoint
namespace Serene.Northwind.Endpoints
{
using Serenity.Data;
using Serenity.Reporting;
using Serenity.Services;
using Serenity.Web;
using System;
using System.Data;
using System.Web.Mvc;
using MyRepository = Repositories.CustomerRepository;
using MyRow = Entities.CustomerRow;
[RoutePrefix("Services/Northwind/Customer"), Route("{action}")]
[ConnectionKey(typeof(MyRow)), ServiceAuthorize(typeof(MyRow))]
public class CustomerController : ServiceEndpoint
{
[HttpPost, AuthorizeCreate(typeof(MyRow))]
public SaveResponse Create(IUnitOfWork uow, SaveRequest<MyRow> request)
{
return new MyRepository().Create(uow, request);
}
[HttpPost, AuthorizeUpdate(typeof(MyRow))]
public SaveResponse Update(IUnitOfWork uow, SaveRequest<MyRow> request)
{
return new MyRepository().Update(uow, request);
}
[HttpPost, AuthorizeDelete(typeof(MyRow))]
public DeleteResponse Delete(IUnitOfWork uow, DeleteRequest request)
{
return new MyRepository().Delete(uow, request);
}
public GetNextNumberResponse GetNextNumber(IDbConnection connection, GetNextNumberRequest request)
{
return new MyRepository().GetNextNumber(connection, request);
}
public RetrieveResponse<MyRow> Retrieve(IDbConnection connection, RetrieveRequest request)
{
return new MyRepository().Retrieve(connection, request);
}
public ListResponse<MyRow> List(IDbConnection connection, ListRequest request)
{
return new MyRepository().List(connection, request);
}
public FileContentResult ListExcel(IDbConnection connection, ListRequest request)
{
var data = List(connection, request).Entities;
var report = new DynamicDataReport(data, request.IncludeColumns, typeof(Columns.CustomerColumns));
var bytes = new ReportRepository().Render(report);
return ExcelContentResult.Create(bytes, "CustomerList_" +
DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xlsx");
}
}
}
Repository
namespace Serene.Northwind.Repositories
{
using Serenity.Data;
using Serenity.Services;
using System;
using System.Data;
using MyRow = Entities.CustomerRow;
public class CustomerRepository
{
private static MyRow.RowFields fld { get { return MyRow.Fields; } }
public SaveResponse Create(IUnitOfWork uow, SaveRequest<MyRow> request)
{
return new MySaveHandler().Process(uow, request, SaveRequestType.Create);
}
public SaveResponse Update(IUnitOfWork uow, SaveRequest<MyRow> request)
{
return new MySaveHandler().Process(uow, request, SaveRequestType.Update);
}
public DeleteResponse Delete(IUnitOfWork uow, DeleteRequest request)
{
return new MyDeleteHandler().Process(uow, request);
}
public UndeleteResponse Undelete(IUnitOfWork uow, UndeleteRequest request)
{
return new MyUndeleteHandler().Process(uow, request);
}
public RetrieveResponse<MyRow> Retrieve(IDbConnection connection, RetrieveRequest request)
{
return new MyRetrieveHandler().Process(connection, request);
}
public ListResponse<MyRow> List(IDbConnection connection, ListRequest request)
{
return new MyListHandler().Process(connection, request);
}
public GetNextNumberResponse GetNextNumber(IDbConnection connection, GetNextNumberRequest request)
{
return GetNextNumberHelper.GetNextNumber(connection, request, fld.CustomerID);
}
private class MySaveHandler : SaveRequestHandler<MyRow> { }
private class MyDeleteHandler : DeleteRequestHandler<MyRow>
{
protected override void ExecuteDelete()
{
try
{
base.ExecuteDelete();
}
catch (Exception e)
{
SqlExceptionHelper.HandleDeleteForeignKeyException(e);
throw;
}
}
}
private class MyUndeleteHandler : UndeleteRequestHandler<MyRow> { }
private class MyRetrieveHandler : RetrieveRequestHandler<MyRow> { }
private class MyListHandler : ListRequestHandler<MyRow> { }
}
}
Get Number Helper.
namespace Serene
{
using Serenity.Data;
using System.Data;
using System.Linq;
public static class GetNextNumberHelper
{
public static GetNextNumberResponse GetNextNumber(IDbConnection connection, GetNextNumberRequest request,
Field field)
{
var prefix = request.Prefix ?? "";
var max = connection.Query<string>(new SqlQuery()
.From(field.Fields)
.Select(Sql.Max(field.Expression))
.Where(
field.StartsWith(prefix) &&
field >= prefix.PadRight(request.Length, '0') &&
field <= prefix.PadRight(request.Length, '9')))
.FirstOrDefault();
var response = new GetNextNumberResponse();
long l;
response.Number = max == null ||
!long.TryParse(max.Substring(prefix.Length), out l) ? 1 : l + 1;
response.Serial = prefix + response.Number.ToString()
.PadLeft(request.Length - prefix.Length, '0');
return response;
}
}
}
This is custom endpoint and also display chart method.
namespace Serene.BasicSamples.Endpoints
{
using Northwind.Entities;
using Serenity.Data;
using Serenity.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
[ServiceAuthorize, RoutePrefix("Services/BasicSamples/BasicSamples"), Route("{action}")]
[ConnectionKey(typeof(OrderRow))]
public class BasicSamplesController : ServiceEndpoint
{
public OrdersByShipperResponse OrdersByShipper(IDbConnection connection, OrdersByShipperRequest request)
{
var fld = OrderRow.Fields;
var year = DateTime.Today.Year;
var startOfMonth = new DateTime(year, DateTime.Today.Month, 1);
var startingFrom = startOfMonth.AddMonths(-11);
var response = new OrdersByShipperResponse();
var shippers = connection.List<ShipperRow>(q => q.SelectTableFields().OrderBy(ShipperRow.Fields.CompanyName));
response.ShipperKeys = shippers.Select(x => "s" + x.ShipperID.Value).ToList();
response.ShipperLabels = shippers.Select(x => x.CompanyName).ToList();
var monthExpr = "DATEPART(MONTH, " + fld.OrderDate.Expression + ")";
var byMonth = connection.Query(
new SqlQuery()
.From(fld)
.Select(monthExpr, "Month")
.Select(Sql.Count(), "Count")
.Select(fld.ShipVia, "ShipVia")
.GroupBy(monthExpr)
.GroupBy(fld.ShipVia)
.Where(
fld.OrderDate.IsNotNull() &
fld.ShipVia.IsNotNull() &
fld.OrderDate < startOfMonth.AddMonths(1) &
fld.OrderDate >= startingFrom))
.ToDictionary(x => new Tuple<int, int>((int)x.Month, (int)x.ShipVia), x => (int)x.Count);
response.Values = new List<Dictionary<string, object>>();
var month = startingFrom.Month;
int mc;
for (var i = 0; i < 12; i++)
{
var d = new Dictionary<string, object>();
d["Month"] = new DateTime(1999, month, 1)
.ToString("MMM");
foreach (var p in shippers)
d["s" + p.ShipperID] = byMonth.TryGetValue(
new Tuple<int, int>(month, p.ShipperID.Value), out mc) ? mc : 0;
response.Values.Add(d);
if (++month > 12)
month = 1;
}
return response;
}
public ServiceResponse OrderBulkAction(IUnitOfWork uow, OrderBulkActionRequest request)
{
request.CheckNotNull();
var random = new Random();
// fail randomly with 3 percent chance
if (random.Next(100) < 3)
throw new ValidationError("Failed randomly!");
foreach (var x in request.OrderIDs)
Thread.Sleep(random.Next(400) + 100);
return new ServiceResponse();
}
}
}