Skip to content

Commit 302d234

Browse files
committed
refactoring
1 parent ee31664 commit 302d234

10 files changed

+127
-34
lines changed

AspNetCore_OData.postman_collection.json

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"info": {
33
"_postman_id": "9a64ed14-4419-4fe2-a636-e341bd37e44c",
4-
"name": "AspNetCore_OData",
4+
"name": "Dotnet_Conf_OData",
55
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
66
},
77
"item": [
@@ -48,14 +48,14 @@
4848
}
4949
],
5050
"url": {
51-
"raw": "localhost:5000/odata/products(010b34c3-5d91-4f15-a926-a4edbc3a9770)",
51+
"raw": "localhost:5000/odata/products(6d42ac79-2b65-460d-9991-22b86ad66fb9)",
5252
"host": [
5353
"localhost"
5454
],
5555
"port": "5000",
5656
"path": [
5757
"odata",
58-
"products(010b34c3-5d91-4f15-a926-a4edbc3a9770)"
58+
"products(6d42ac79-2b65-460d-9991-22b86ad66fb9)"
5959
]
6060
}
6161
},
@@ -92,7 +92,7 @@
9292
"method": "GET",
9393
"header": [],
9494
"url": {
95-
"raw": "localhost:5000/odata/products?$count=true&$top=5&$skip=0&$orderby=no",
95+
"raw": "localhost:5000/odata/products?$count=true&$top=5&$skip=1",
9696
"host": [
9797
"localhost"
9898
],
@@ -112,11 +112,7 @@
112112
},
113113
{
114114
"key": "$skip",
115-
"value": "0"
116-
},
117-
{
118-
"key": "$orderby",
119-
"value": "no"
115+
"value": "1"
120116
}
121117
]
122118
}
@@ -129,7 +125,7 @@
129125
"method": "GET",
130126
"header": [],
131127
"url": {
132-
"raw": "localhost:5000/odata/products?$orderby=no",
128+
"raw": "localhost:5000/odata/products?$orderby=price",
133129
"host": [
134130
"localhost"
135131
],
@@ -141,7 +137,7 @@
141137
"query": [
142138
{
143139
"key": "$orderby",
144-
"value": "no"
140+
"value": "price"
145141
}
146142
]
147143
}
@@ -154,7 +150,7 @@
154150
"method": "GET",
155151
"header": [],
156152
"url": {
157-
"raw": "localhost:5000/odata/products?$orderby=no desc",
153+
"raw": "localhost:5000/odata/products?$orderby=price desc",
158154
"host": [
159155
"localhost"
160156
],
@@ -166,7 +162,7 @@
166162
"query": [
167163
{
168164
"key": "$orderby",
169-
"value": "no desc"
165+
"value": "price desc"
170166
}
171167
]
172168
}
@@ -179,7 +175,7 @@
179175
"method": "GET",
180176
"header": [],
181177
"url": {
182-
"raw": "localhost:5000/odata/products?$filter=translations/any(c: contains(c/name, 'dene'))",
178+
"raw": "localhost:5000/odata/products?$filter=contains(productname, 'LG')",
183179
"host": [
184180
"localhost"
185181
],
@@ -191,7 +187,7 @@
191187
"query": [
192188
{
193189
"key": "$filter",
194-
"value": "translations/any(c: contains(c/name, 'dene'))"
190+
"value": "contains(productname, 'LG')"
195191
}
196192
]
197193
}
@@ -204,7 +200,7 @@
204200
"method": "GET",
205201
"header": [],
206202
"url": {
207-
"raw": "localhost:5000/odata/products?$filter=roomTypeTranslations/any(c: c/name eq 'Executive')",
203+
"raw": "localhost:5000/odata/products?$expand=category($filter=categoryName eq 'TV')",
208204
"host": [
209205
"localhost"
210206
],
@@ -215,8 +211,8 @@
215211
],
216212
"query": [
217213
{
218-
"key": "$filter",
219-
"value": "roomTypeTranslations/any(c: c/name eq 'Executive')"
214+
"key": "$expand",
215+
"value": "category($filter=categoryName eq 'TV')"
220216
}
221217
]
222218
}
@@ -308,7 +304,7 @@
308304
"method": "GET",
309305
"header": [],
310306
"url": {
311-
"raw": "localhost:5000/odata/products?$expand=category($select=categoryName)&$select=*",
307+
"raw": "localhost:5000/odata/products?$expand=category($select=categoryName)&$select=id,productName",
312308
"host": [
313309
"localhost"
314310
],
@@ -324,7 +320,7 @@
324320
},
325321
{
326322
"key": "$select",
327-
"value": "*"
323+
"value": "id,productName"
328324
}
329325
]
330326
}
@@ -604,5 +600,6 @@
604600
]
605601
}
606602
}
607-
]
603+
],
604+
"protocolProfileBehavior": {}
608605
}

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
image: postgres:10.6-alpine
55
container_name: mypostgres
66
ports:
7-
- "5432:5432"
7+
- "5436:5432"
88
environment:
99
- POSTGRES_DB=Product
1010
- POSTGRES_USER=postgres
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNet.OData;
5+
using Microsoft.AspNetCore.Mvc;
6+
using OData.WebApi.Data.Entities;
7+
using OData.WebApi.Data.Repositories;
8+
9+
namespace Pms.Api.Controllers
10+
{
11+
[ApiController]
12+
public class BaseController<TEntity> : ControllerBase
13+
where TEntity : BaseEntity
14+
{
15+
private readonly IRepository<TEntity> _repository;
16+
public BaseController(IRepository<TEntity> repository)
17+
{
18+
_repository = repository;
19+
}
20+
public IActionResult Get()
21+
{
22+
return Ok(_repository.Query());
23+
}
24+
25+
[HttpGet("({id})")]
26+
public virtual SingleResult<TEntity> Get([FromODataUri] Guid id)
27+
{
28+
var entity = _repository.Query().Where(q => q.Id.Equals(id));
29+
return SingleResult.Create(entity);
30+
}
31+
32+
[HttpPost]
33+
public virtual async Task<IActionResult> Post([FromBody] TEntity entity)
34+
{
35+
_repository.Add(entity);
36+
await _repository.SaveChangesAsync();
37+
return Ok();
38+
}
39+
40+
[HttpPut]
41+
public virtual async Task<IActionResult> Put([FromBody] TEntity entity)
42+
{
43+
if (await _repository.IsExistAsync(entity.Id) == false)
44+
{
45+
return NotFound();
46+
}
47+
48+
_repository.Update(entity);
49+
await _repository.SaveChangesAsync();
50+
return Ok();
51+
}
52+
53+
[HttpDelete("{id}")]
54+
public virtual async Task<IActionResult> Delete(Guid id)
55+
{
56+
if (await _repository.IsExistAsync(id) == false)
57+
{
58+
return NotFound();
59+
}
60+
61+
_repository.Delete(id);
62+
await _repository.SaveChangesAsync();
63+
return Ok();
64+
}
65+
}
66+
}

src/OData.WebApi/Controllers/Base/ODataBaseController.cs renamed to src/OData.WebApi/Controllers/OData/ODataBaseController.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Linq;
33
using System.Threading.Tasks;
4-
using Mapster;
54
using Microsoft.AspNet.OData;
65
using Microsoft.AspNet.OData.Routing;
76
using Microsoft.AspNetCore.Mvc;
@@ -10,7 +9,7 @@
109

1110
namespace Pms.Api.Controllers
1211
{
13-
public class ODataBaseController<TEntity, TDto> : ODataController
12+
public class ODataBaseController<TEntity> : ODataController
1413
where TEntity : BaseEntity
1514
{
1615
private readonly IRepository<TEntity> _repository;
@@ -20,17 +19,17 @@ public ODataBaseController(IRepository<TEntity> repository)
2019
}
2120

2221
[ODataRoute]
23-
[EnableQuery(PageSize = 10)]
22+
[EnableQuery(PageSize = 2)]
2423
public IActionResult Get()
2524
{
26-
return Ok(_repository.Query().ProjectToType<TDto>());
25+
return Ok(_repository.Query());
2726
}
2827

2928
[ODataRoute("({id})")]
3029
[EnableQuery]
31-
public virtual SingleResult<TDto> Get([FromODataUri] Guid id)
30+
public virtual SingleResult<TEntity> Get([FromODataUri] Guid id)
3231
{
33-
var entity = _repository.Query().Where(q => q.Id.Equals(id)).ProjectToType<TDto>();
32+
var entity = _repository.Query().Where(q => q.Id.Equals(id));
3433
return SingleResult.Create(entity);
3534
}
3635

src/OData.WebApi/Controllers/ProductCategoryODataController.cs renamed to src/OData.WebApi/Controllers/OData/ProductCategoryODataController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Pms.Api.Controllers.OData
77
{
88
[ODataRoutePrefix("product_categories")]
9-
public class ProductCategoryODataController : ODataBaseController<ProductCategory, ProductCategoryDto>
9+
public class ProductCategoryODataController : ODataBaseController<ProductCategory>
1010
{
1111
public ProductCategoryODataController(IRepository<ProductCategory> productCategoryRepository)
1212
: base(productCategoryRepository)

src/OData.WebApi/Controllers/ProductODataController.cs renamed to src/OData.WebApi/Controllers/OData/ProductODataController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Pms.Api.Controllers.OData
77
{
88
[ODataRoutePrefix("products")]
9-
public class ProductODataController : ODataBaseController<Product, ProductDto>
9+
public class ProductODataController : ODataBaseController<Product>
1010
{
1111
public ProductODataController(IRepository<Product> productRepository)
1212
: base(productRepository)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using OData.WebApi.Data.Entities;
3+
using OData.WebApi.Data.Repositories;
4+
5+
namespace Pms.Api.Controllers.OData
6+
{
7+
[Route("api/product_categories")]
8+
public class ProductCategoryController : BaseController<ProductCategory>
9+
{
10+
public ProductCategoryController(IRepository<ProductCategory> productRepository)
11+
: base(productRepository)
12+
{
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using OData.WebApi.Data.Entities;
3+
using OData.WebApi.Data.Repositories;
4+
5+
namespace Pms.Api.Controllers.OData
6+
{
7+
[Route("api/products")]
8+
public class ProductController : BaseController<Product>
9+
{
10+
public ProductController(IRepository<Product> productRepository)
11+
: base(productRepository)
12+
{
13+
}
14+
}
15+
}

src/OData.WebApi/Startup.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Net.Http.Headers;
1212
using Microsoft.OData.Edm;
1313
using OData.WebApi.Data;
14+
using OData.WebApi.Data.Entities;
1415
using OData.WebApi.Data.Repositories;
1516
using OData.WebApi.Dto;
1617

@@ -86,10 +87,10 @@ private static IEdmModel GetEdmModel()
8687
var builder = new ODataConventionModelBuilder();
8788
builder.EnableLowerCamelCase();
8889

89-
builder.EntitySet<ProductDto>("products")
90+
builder.EntitySet<Product>("products")
9091
.EntityType.Filter().Count().Expand().OrderBy().Page().Select();
9192

92-
builder.EntitySet<ProductCategoryDto>("product_categories")
93+
builder.EntitySet<ProductCategory>("product_categories")
9394
.EntityType.Filter().Count().Expand().OrderBy().Page().Select();
9495

9596
return builder.GetEdmModel();

src/OData.WebApi/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"ConnectionStrings": {
3-
"ProductConStr": "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=Product;"
3+
"ProductConStr": "Host=localhost;Port=5436;Username=postgres;Password=postgres;Database=Product;"
44
},
55
"Logging": {
66
"LogLevel": {
7-
"Default": "Warning"
7+
"Default": "Debug"
88
}
99
},
1010
"AllowedHosts": "*"

0 commit comments

Comments
 (0)