|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Threading.Tasks; |
5 | | -using Microsoft.AspNetCore.Mvc; |
6 | | -using Microsoft.eShopOnContainers.Services.Catalog.API.Model; |
7 | | - |
| 1 | + |
8 | 2 | namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
9 | 3 | { |
10 | | - [Route("/")] |
| 4 | + using Microsoft.AspNetCore.Mvc; |
| 5 | + using Microsoft.EntityFrameworkCore; |
| 6 | + using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; |
| 7 | + using System; |
| 8 | + using System.Linq; |
| 9 | + using System.Threading.Tasks; |
| 10 | + using ViewModel; |
| 11 | + |
| 12 | + [Route("api/v1/[controller]")] |
11 | 13 | public class CatalogController : ControllerBase |
12 | 14 | { |
13 | | - private CatalogContext _context; |
| 15 | + private readonly CatalogContext _context; |
14 | 16 |
|
15 | 17 | public CatalogController(CatalogContext context) |
16 | 18 | { |
17 | 19 | _context = context; |
18 | 20 | } |
19 | 21 |
|
20 | | - // GET api/values |
| 22 | + // GET api/v1/[controller]/items/[?pageSize=3&pageIndex=10] |
| 23 | + |
21 | 24 | [HttpGet] |
22 | | - public IEnumerable<CatalogItem> Get() |
| 25 | + [Route("[action]")] |
| 26 | + public async Task<IActionResult> Items(int pageSize = 10, int pageIndex = 0) |
23 | 27 | { |
24 | | - return _context.CatalogItems.ToList(); |
| 28 | + var totalItems = await _context.CatalogItems |
| 29 | + .LongCountAsync(); |
| 30 | + |
| 31 | + var itemsOnPage = await _context.CatalogItems |
| 32 | + .Skip(pageSize * pageIndex) |
| 33 | + .Take(pageSize) |
| 34 | + .ToListAsync(); |
| 35 | + |
| 36 | + var model = new PaginatedItemsViewModel<CatalogItem>( |
| 37 | + pageIndex, pageSize, totalItems, itemsOnPage); |
| 38 | + |
| 39 | + return Ok(model); |
25 | 40 | } |
26 | 41 |
|
27 | | - // GET api/values/5 |
28 | | - [HttpGet("{id}")] |
29 | | - public IActionResult Get(Guid id) |
| 42 | + // GET api/v1/[controller]/items/withname/samplename |
| 43 | + |
| 44 | + [HttpGet] |
| 45 | + [Route("[action]/withname/{name:minlength(1)}")] |
| 46 | + public async Task<IActionResult> Items(string name, int pageSize = 10, int pageIndex = 0) |
30 | 47 | { |
31 | | - var item = _context.CatalogItems.FirstOrDefault(x=> x.Id == id); |
32 | 48 |
|
33 | | - if(item == null) |
34 | | - { |
35 | | - return NotFound(); |
36 | | - } |
| 49 | + var totalItems = await _context.CatalogItems |
| 50 | + .Where(c => c.Name.StartsWith(name)) |
| 51 | + .LongCountAsync(); |
37 | 52 |
|
38 | | - return new OkObjectResult(item); |
| 53 | + var itemsOnPage = await _context.CatalogItems |
| 54 | + .Where(c => c.Name.StartsWith(name)) |
| 55 | + .Skip(pageSize * pageIndex) |
| 56 | + .Take(pageSize) |
| 57 | + .ToListAsync(); |
| 58 | + |
| 59 | + var model = new PaginatedItemsViewModel<CatalogItem>( |
| 60 | + pageIndex, pageSize, totalItems, itemsOnPage); |
| 61 | + |
| 62 | + return Ok(model); |
39 | 63 | } |
40 | 64 |
|
41 | | - // POST api/values |
42 | | - [HttpPost] |
43 | | - public IActionResult Post([FromBody]CatalogItem item) |
| 65 | + // GET api/v1/[controller]/items/type/1/brand/null |
| 66 | + |
| 67 | + [HttpGet] |
| 68 | + [Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")] |
| 69 | + public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId, int pageSize = 10, int pageIndex = 0) |
44 | 70 | { |
45 | | - try |
| 71 | + var root = (IQueryable<CatalogItem>)_context.CatalogItems; |
| 72 | + |
| 73 | + if (catalogTypeId.HasValue) |
46 | 74 | { |
47 | | - _context.CatalogItems.Add(item); |
48 | | - _context.SaveChanges(); |
49 | | - return Ok(); |
| 75 | + root = root.Where(ci => ci.CatalogTypeId == catalogTypeId); |
50 | 76 | } |
51 | | - catch |
| 77 | + |
| 78 | + if (catalogBrandId.HasValue) |
52 | 79 | { |
53 | | - return StatusCode(500, "Unable to add new catalog item"); |
| 80 | + root = root.Where(ci => ci.CatalogBrandId == catalogBrandId); |
54 | 81 | } |
| 82 | + |
| 83 | + var totalItems = await root |
| 84 | + .LongCountAsync(); |
| 85 | + |
| 86 | + var itemsOnPage = await root |
| 87 | + .Skip(pageSize * pageIndex) |
| 88 | + .Take(pageSize) |
| 89 | + .ToListAsync(); |
| 90 | + |
| 91 | + var model = new PaginatedItemsViewModel<CatalogItem>( |
| 92 | + pageIndex, pageSize, totalItems, itemsOnPage); |
| 93 | + |
| 94 | + return Ok(model); |
55 | 95 | } |
56 | 96 |
|
57 | | - // PUT api/values/5 |
58 | | - [HttpPut("{id}")] |
59 | | - public IActionResult Put(int id, [FromBody]CatalogItem item) |
| 97 | + // GET api/v1/[controller]/CatalogTypes |
| 98 | + |
| 99 | + [HttpGet] |
| 100 | + [Route("[action]")] |
| 101 | + public async Task<IActionResult> CatalogTypes() |
60 | 102 | { |
61 | | - _context.CatalogItems.Update(item); |
62 | | - _context.SaveChanges(); |
63 | | - return Ok(); |
| 103 | + var items = await _context.CatalogTypes |
| 104 | + .ToListAsync(); |
| 105 | + |
| 106 | + return Ok(items); |
64 | 107 | } |
65 | 108 |
|
66 | | - // DELETE api/values/5 |
67 | | - [HttpDelete("{id}")] |
68 | | - public IActionResult Delete(Guid id) |
| 109 | + // GET api/v1/[controller]/CatalogBrands |
| 110 | + |
| 111 | + [HttpGet] |
| 112 | + [Route("[action]")] |
| 113 | + public async Task<IActionResult> CatalogBrands() |
69 | 114 | { |
70 | | - return Ok(); |
| 115 | + var items = await _context.CatalogBrands |
| 116 | + .ToListAsync(); |
| 117 | + |
| 118 | + return Ok(items); |
71 | 119 | } |
72 | 120 | } |
73 | 121 | } |
0 commit comments