Skip to content

Commit 45bc1f8

Browse files
author
Carlos Cañizares Estévez
committed
Merge branch 'Dev' into WebMvc
2 parents eb7298c + 725d658 commit 45bc1f8

15 files changed

Lines changed: 411 additions & 105 deletions
Lines changed: 88 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,121 @@
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+

82
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
93
{
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]")]
1113
public class CatalogController : ControllerBase
1214
{
13-
private CatalogContext _context;
15+
private readonly CatalogContext _context;
1416

1517
public CatalogController(CatalogContext context)
1618
{
1719
_context = context;
1820
}
1921

20-
// GET api/values
22+
// GET api/v1/[controller]/items/[?pageSize=3&pageIndex=10]
23+
2124
[HttpGet]
22-
public IEnumerable<CatalogItem> Get()
25+
[Route("[action]")]
26+
public async Task<IActionResult> Items(int pageSize = 10, int pageIndex = 0)
2327
{
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);
2540
}
2641

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)
3047
{
31-
var item = _context.CatalogItems.FirstOrDefault(x=> x.Id == id);
3248

33-
if(item == null)
34-
{
35-
return NotFound();
36-
}
49+
var totalItems = await _context.CatalogItems
50+
.Where(c => c.Name.StartsWith(name))
51+
.LongCountAsync();
3752

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);
3963
}
4064

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)
4470
{
45-
try
71+
var root = (IQueryable<CatalogItem>)_context.CatalogItems;
72+
73+
if (catalogTypeId.HasValue)
4674
{
47-
_context.CatalogItems.Add(item);
48-
_context.SaveChanges();
49-
return Ok();
75+
root = root.Where(ci => ci.CatalogTypeId == catalogTypeId);
5076
}
51-
catch
77+
78+
if (catalogBrandId.HasValue)
5279
{
53-
return StatusCode(500, "Unable to add new catalog item");
80+
root = root.Where(ci => ci.CatalogBrandId == catalogBrandId);
5481
}
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);
5595
}
5696

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()
60102
{
61-
_context.CatalogItems.Update(item);
62-
_context.SaveChanges();
63-
return Ok();
103+
var items = await _context.CatalogTypes
104+
.ToListAsync();
105+
106+
return Ok(items);
64107
}
65108

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()
69114
{
70-
return Ok();
115+
var items = await _context.CatalogBrands
116+
.ToListAsync();
117+
118+
return Ok(items);
71119
}
72120
}
73121
}

src/Services/Catalog/Catalog.API/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM microsoft/aspnetcore
1+
FROM microsoft/aspnetcore:latest
22
WORKDIR /app
33
EXPOSE 80
44
ADD . /app
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
9+
public class CatalogBrand
10+
{
11+
public int Id { get; set; }
12+
13+
public string Brand { get; set; }
14+
}
15+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using EntityFrameworkCore.Metadata.Builders;
4+
using Microsoft.EntityFrameworkCore;
5+
using Npgsql.EntityFrameworkCore.PostgreSQL;
6+
7+
public class CatalogContext : DbContext
8+
{
9+
public CatalogContext(DbContextOptions options) : base(options)
10+
{
11+
}
12+
13+
public DbSet<CatalogItem> CatalogItems { get; set; }
14+
15+
public DbSet<CatalogBrand> CatalogBrands { get; set; }
16+
17+
public DbSet<CatalogType> CatalogTypes { get; set; }
18+
19+
protected override void OnModelCreating(ModelBuilder builder)
20+
{
21+
builder.HasSequence("idseqcatalog")
22+
.StartsAt(1)
23+
.IncrementsBy(1);
24+
25+
builder.HasSequence("idseqcatalogbrand")
26+
.StartsAt(1)
27+
.IncrementsBy(1);
28+
29+
builder.HasSequence("idseqcatalogtype")
30+
.StartsAt(1)
31+
.IncrementsBy(1);
32+
33+
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
34+
builder.Entity<CatalogType>(ConfigureCatalogType);
35+
builder.Entity<CatalogItem>(ConfigureCatalogItem);
36+
37+
38+
builder.HasPostgresExtension("uuid-ossp");
39+
}
40+
41+
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
42+
{
43+
builder.ForNpgsqlToTable("catalog");
44+
45+
builder.Property(ci => ci.Id)
46+
.HasDefaultValueSql("nextval('idseqcatalog')")
47+
.IsRequired();
48+
49+
builder.Property(ci => ci.Name)
50+
.IsRequired(true)
51+
.HasMaxLength(50);
52+
53+
builder.Property(ci => ci.Price)
54+
.IsRequired(true);
55+
56+
builder.Property(ci => ci.PictureUri)
57+
.IsRequired(false);
58+
59+
builder.HasOne(ci => ci.CatalogBrand)
60+
.WithMany()
61+
.HasForeignKey(ci => ci.CatalogBrandId);
62+
63+
builder.HasOne(ci => ci.CatalogType)
64+
.WithMany()
65+
.HasForeignKey(ci => ci.CatalogTypeId);
66+
67+
}
68+
69+
void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> builder)
70+
{
71+
builder.ForNpgsqlToTable("catalogbrand");
72+
73+
builder.Property(cb => cb.Id)
74+
.HasDefaultValueSql("nextval('idseqcatalogbrand')")
75+
.IsRequired();
76+
77+
builder.Property(cb => cb.Brand)
78+
.IsRequired()
79+
.HasMaxLength(100);
80+
}
81+
82+
void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
83+
{
84+
builder.ForNpgsqlToTable("catalogtype");
85+
86+
builder.Property(cb => cb.Id)
87+
.HasDefaultValueSql("nextval('idseqcatalogtype')")
88+
.IsRequired();
89+
90+
builder.Property(cb => cb.Type)
91+
.IsRequired()
92+
.HasMaxLength(100);
93+
}
94+
}
95+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using Microsoft.AspNetCore.Builder;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
public class CatalogContextSeed
10+
{
11+
public static async Task SeedAsync(IApplicationBuilder applicationBuilder)
12+
{
13+
var context = (CatalogContext)applicationBuilder
14+
.ApplicationServices.GetService(typeof(CatalogContext));
15+
16+
using (context)
17+
{
18+
context.Database.EnsureDeleted();
19+
20+
context.Database.EnsureCreated();
21+
22+
if (!context.CatalogBrands.Any())
23+
{
24+
context.CatalogBrands.AddRange(
25+
GetPreconfiguredCatalogBrands());
26+
27+
await context.SaveChangesAsync();
28+
}
29+
30+
if (!context.CatalogTypes.Any())
31+
{
32+
context.CatalogTypes.AddRange(
33+
GetPreconfiguredCatalogTypes());
34+
35+
await context.SaveChangesAsync();
36+
}
37+
38+
if (!context.CatalogItems.Any())
39+
{
40+
context.CatalogItems.AddRange(
41+
GetPreconfiguredItems());
42+
43+
await context.SaveChangesAsync();
44+
}
45+
}
46+
}
47+
48+
static IEnumerable<CatalogBrand> GetPreconfiguredCatalogBrands()
49+
{
50+
return new List<CatalogBrand>()
51+
{
52+
new CatalogBrand() { Brand="Azure"},
53+
new CatalogBrand() { Brand = "Visual Studio" }
54+
};
55+
}
56+
57+
static IEnumerable<CatalogType> GetPreconfiguredCatalogTypes()
58+
{
59+
return new List<CatalogType>()
60+
{
61+
new CatalogType() { Type="Mug"},
62+
new CatalogType() { Type = "T-Shirt" }
63+
};
64+
}
65+
66+
static IEnumerable<CatalogItem> GetPreconfiguredItems()
67+
{
68+
return new List<CatalogItem>()
69+
{
70+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
71+
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
72+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
73+
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=1, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
74+
};
75+
}
76+
}
77+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
2+
{
3+
using System;
4+
5+
public class CatalogItem
6+
{
7+
public int Id { get; set; }
8+
9+
public string Name { get; set; }
10+
11+
public string Description { get; set; }
12+
13+
public decimal Price { get; set; }
14+
15+
public string PictureUri { get; set; }
16+
17+
public int CatalogTypeId { get; set; }
18+
19+
public CatalogType CatalogType { get; set; }
20+
21+
public int CatalogBrandId { get; set; }
22+
23+
public CatalogBrand CatalogBrand { get; set; }
24+
25+
public CatalogItem() { }
26+
}
27+
}

0 commit comments

Comments
 (0)