Created
October 19, 2016 20:37
-
-
Save anonymous/fe97977150b53d542377c32edf66185d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint | |
*/ | |
namespace Endpoint.Data.Controllers | |
{ | |
public abstract class BaseDataController<T> : NavigatableByContextOdataController<T> | |
where T : BaseEntity | |
{ | |
[EnableQuery] | |
public virtual IQueryable<T> Get() | |
{ | |
return Db.Set<T>(); | |
} | |
[EnableQuery] | |
public SingleResult<T> Get([FromODataUri] int key) | |
{ | |
var result = Db.Set<T>().Where(g => g.Id == key); | |
return SingleResult.Create(result); | |
} | |
public virtual async Task<IHttpActionResult> Post(T entity) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var result = Db.Set<T>().Add(entity); | |
await Db.CommitAsync(); | |
AfterCreate(result.Id); | |
return Created(result); | |
} | |
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<T> entity) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var saved = await Db.Set<T>().FindAsync(key); | |
if (saved == null) return NotFound(); | |
entity.Patch(saved); | |
try | |
{ | |
await Db.CommitAsync(); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
if (!Exists(key)) return NotFound(); | |
throw; | |
} | |
return Updated(saved); | |
} | |
public virtual async Task<IHttpActionResult> Put([FromODataUri] int key, T update) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
if (key != update.Id) | |
{ | |
return BadRequest(); | |
} | |
Db.Entry(update).State = EntityState.Modified; | |
try | |
{ | |
await Db.CommitAsync(); | |
AfterUpdate(key); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
if (!Exists(key)) return NotFound(); | |
throw; | |
} | |
return Updated(update); | |
} | |
public async Task<IHttpActionResult> Delete([FromODataUri] int key) | |
{ | |
var entity = await Db.Set<T>().FindAsync(key); | |
if (entity == null) | |
{ | |
return NotFound(); | |
} | |
Db.Set<T>().Remove(entity); | |
await Db.CommitAsync(); | |
return StatusCode(HttpStatusCode.NoContent); | |
} | |
protected IPersister Db { get; } | |
private bool Exists(int key) => Db.Set<T>().Any(p => p.Id == key); | |
protected BaseDataController(IPersister context) : base(context) /// do not see here, it is just a persister | |
{ | |
Db = context; | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
Db.Dispose(); | |
base.Dispose(disposing); | |
} | |
protected virtual void AfterCreate(int id) { } | |
protected virtual void AfterUpdate(int id) { } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
public class SimpleController : BaseDataController<MyEntity> | |
{ | |
public SimpleController() | |
{ | |
InitializeNavigationPropertisOperations(); | |
} | |
private void InitializeNavigationPropertisOperations() | |
{ | |
AddHandler(new CustomNavigationPropertyHandler<CollectionProperty>("CollectionProperty", AddCollectionProperty, DelCollectionProperty)); | |
AddHandler(new CustomNavigationPropertyHandler<SingleProperty>("SingleProperty", AddSingleProperty, DelSingleProperty)); | |
} | |
private static void AddCollectionProperty(MyEntity myEntity, CollectionProperty element) | |
{ | |
myEntity.CollectionProperty.Add(element); | |
} | |
private static void DelCollectionProperty(MyEntity myEntity, CollectionProperty element) | |
{ | |
myEntity.CollectionProperty.Remove(element); | |
} | |
private static void AddSingleProperty(MyEntity myEntity, SingleProperty property) | |
{ | |
myEntity.SingleProperty = property; | |
} | |
private static void DelSingleProperty(MyEntity myEntity, SingleProperty property) | |
{ | |
myEntity.SingleProperty = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment