Skip to content

Commit 481bbdc

Browse files
committed
Merge pull request #1 from 4nonym0us/patch-1
Retrieving the SingleResult in a correct way.
2 parents e658cb9 + a88454b commit 481bbdc

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

src/AbpODataDemo.WebApi/Controllers/AbpODataEntityController.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected AbpODataEntityController(IRepository<TEntity> repository) : base(repos
2121
}
2222

2323
public abstract class AbpODataEntityController<TEntity, TPrimaryKey> : ODataController
24+
where TPrimaryKey: IEquatable<TPrimaryKey>
2425
where TEntity : class, IEntity<TPrimaryKey>
2526
{
2627
public IUnitOfWorkManager UnitOfWorkManager { get; set; }
@@ -50,12 +51,60 @@ public IQueryable<TEntity> Get()
5051
[EnableQuery]
5152
public SingleResult<TEntity> Get([FromODataUri] TPrimaryKey key)
5253
{
53-
var entity = Repository.FirstOrDefault(key);
54+
var entity = Repository.GetAll().Where(e => e.Id.Equals(key));
55+
return SingleResult.Create(entity);
56+
}
57+
58+
public async Task<IHttpActionResult> Post(TEntity entity)
59+
{
60+
if (!ModelState.IsValid)
61+
{
62+
return BadRequest(ModelState);
63+
}
64+
var created = await Repository.InsertAsync(entity);
65+
return Created(created);
66+
}
5467

55-
//TODO: How to return a single value, instead of Queryable???
56-
return SingleResult.Create(new[] { entity }.AsQueryable());
68+
public async Task<IHttpActionResult> Patch([FromODataUri] TPrimaryKey key, Delta<TEntity> entity)
69+
{
70+
if (!ModelState.IsValid)
71+
{
72+
return BadRequest(ModelState);
73+
}
74+
var dbLookup = await Repository.GetAsync(key);
75+
if (entity == null)
76+
{
77+
return NotFound();
78+
}
79+
entity.Patch(dbLookup);
80+
return Updated(entity);
5781
}
5882

83+
public async Task<IHttpActionResult> Put([FromODataUri] TPrimaryKey key, TEntity update)
84+
{
85+
if (!ModelState.IsValid)
86+
{
87+
return BadRequest(ModelState);
88+
}
89+
if (!Equals(key, update.Id))
90+
{
91+
return BadRequest();
92+
}
93+
var updated = await Repository.UpdateAsync(update);
94+
return Updated(updated);
95+
}
96+
97+
public async Task<IHttpActionResult> Delete([FromODataUri] TPrimaryKey key)
98+
{
99+
var product = await Repository.GetAsync(key);
100+
if (product == null)
101+
{
102+
return NotFound();
103+
}
104+
await Repository.DeleteAsync(key);
105+
return StatusCode(HttpStatusCode.NoContent);
106+
}
107+
59108
protected override void Dispose(bool disposing)
60109
{
61110
if (!_disposed)
@@ -69,4 +118,4 @@ protected override void Dispose(bool disposing)
69118
base.Dispose(disposing);
70119
}
71120
}
72-
}
121+
}

0 commit comments

Comments
 (0)