Commit 609ef6a6 authored by Andrey Shvydky's avatar Andrey Shvydky 🍻

Methods Save and SaveAsync added to DataApiController and ODataControllerBase...

Methods Save and SaveAsync added to DataApiController and ODataControllerBase to allow save overrides.
parent 2e695389
using Breeze.Data;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using System.Linq.Dynamic;
using System.Reflection;
using System.Security;
using log4net;
using System.Threading.Tasks;
using System.Web.Http;
namespace Breeze.CMS.Controllers {
public abstract class DataApiController<TEntity, TModel, TKey> : ApiController
......@@ -54,7 +49,7 @@ namespace Breeze.CMS.Controllers {
var entity = Repository.Insert(new TEntity());
UpdateEntity(entity, model);
Repository.Save();
Save();
return CreatedAtRoute("DefaultApi", new { id = GetEntityId(entity) }, ConvertEntityToModel(entity));
}
......@@ -71,7 +66,7 @@ namespace Breeze.CMS.Controllers {
return StatusCode(HttpStatusCode.Forbidden);
UpdateEntity(entity, model);
Repository.Save();
Save();
return StatusCode(HttpStatusCode.NoContent);
}
......@@ -84,11 +79,19 @@ namespace Breeze.CMS.Controllers {
return StatusCode(HttpStatusCode.Forbidden);
Repository.Remove(entity);
Repository.Save();
Save();
return Ok(ConvertEntityToModel(entity));
}
protected virtual void Save() {
Repository.Save();
}
protected virtual Task<int> SaveAsync() {
return Repository.SaveAsync();
}
protected virtual Expression<Func<TEntity, TModel>> SearchSelectExpression {
get {
if (_defaultSelectExpression == null) {
......
using Breeze.Data;
using log4net;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using System.Linq.Dynamic;
using System.Reflection;
using System.Security;
using System.Web.OData;
using System.Threading.Tasks;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;
using log4net;
using System.Web.Http;
using System.Web.OData;
namespace Breeze.CMS.Controllers {
public abstract class ODataControllerBase<TEntity, TModel, TKey> : ODataController
......@@ -66,7 +61,7 @@ namespace Breeze.CMS.Controllers {
var entity = Repository.Insert(new TEntity());
UpdateEntity(entity, model);
await Repository.SaveAsync();
await SaveAsync();
return Created(ConvertEntityToModel(entity));
}
......@@ -87,7 +82,7 @@ namespace Breeze.CMS.Controllers {
UpdateEntity(entity, model);
await Repository.SaveAsync();
await SaveAsync();
return Updated(ConvertEntityToModel(entity));
}
......@@ -119,7 +114,7 @@ namespace Breeze.CMS.Controllers {
UpdateEntity(entity, fullModel);
await Repository.SaveAsync();
await SaveAsync();
return Updated(fullModel);
}
......@@ -132,7 +127,7 @@ namespace Breeze.CMS.Controllers {
return StatusCode(HttpStatusCode.Forbidden);
Repository.Remove(entity);
await Repository.SaveAsync();
await SaveAsync();
return Ok();
}
......@@ -163,6 +158,14 @@ namespace Breeze.CMS.Controllers {
protected virtual Expression<Func<TEntity, TModel>> ConvertToModelExpression { get { return SearchSelectExpression; } }
protected virtual void Save() {
Repository.Save();
}
protected virtual Task<int> SaveAsync() {
return Repository.SaveAsync();
}
protected virtual TKey GetModelId(TModel model) {
return (TKey)typeof(TModel).GetProperty("Id").GetValue(model);
}
......@@ -223,7 +226,7 @@ namespace Breeze.CMS.Controllers {
return StatusCode(HttpStatusCode.Forbidden);
Repository.Insert(model);
await Repository.SaveAsync();
await SaveAsync();
return Created(model);
}
......@@ -241,7 +244,7 @@ namespace Breeze.CMS.Controllers {
Repository.Update(model);
try {
await Repository.SaveAsync();
await SaveAsync();
} catch (DbUpdateConcurrencyException) {
if (!EntityExists(key))
return NotFound();
......@@ -264,7 +267,7 @@ namespace Breeze.CMS.Controllers {
if (!IsAuthorizedWithLogging(AccessType.Update, entity, entity))
return StatusCode(HttpStatusCode.Forbidden);
await Repository.SaveAsync();
await SaveAsync();
return Updated(entity);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment