Skip to content

Commit 34043af

Browse files
committed
Review project. Update packages. Fix Http api
1 parent 068e5da commit 34043af

10 files changed

Lines changed: 203 additions & 92 deletions

File tree

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,45 @@
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.Model;
7+
using System;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
11+
[Route("api/v1/[controller]")]
1112
public class CatalogController : ControllerBase
1213
{
13-
private CatalogContext _context;
14+
private readonly CatalogContext _context;
1415

1516
public CatalogController(CatalogContext context)
1617
{
1718
_context = context;
1819
}
1920

20-
// GET api/values
21-
[HttpGet]
22-
public IEnumerable<CatalogItem> Get()
23-
{
24-
return _context.CatalogItems.ToList();
25-
}
21+
// GET api/v1/[controller]/all
2622

27-
// GET api/values/5
28-
[HttpGet("{id}")]
29-
public IActionResult Get(Guid id)
23+
[HttpGet]
24+
[Route("[action]")]
25+
public async Task<IActionResult> All()
3026
{
31-
var item = _context.CatalogItems.FirstOrDefault(x=> x.Id == id);
32-
33-
if(item == null)
34-
{
35-
return NotFound();
36-
}
27+
var items = await _context.CatalogItems
28+
.ToListAsync();
3729

38-
return new OkObjectResult(item);
30+
return Ok(items);
3931
}
4032

41-
// POST api/values
42-
[HttpPost]
43-
public IActionResult Post([FromBody]CatalogItem item)
44-
{
45-
try
46-
{
47-
_context.CatalogItems.Add(item);
48-
_context.SaveChanges();
49-
return Ok();
50-
}
51-
catch
52-
{
53-
return StatusCode(500, "Unable to add new catalog item");
54-
}
55-
}
33+
// GET api/v1/[controller]/FindByName/samplename
5634

57-
// PUT api/values/5
58-
[HttpPut("{id}")]
59-
public IActionResult Put(int id, [FromBody]CatalogItem item)
35+
[HttpGet]
36+
[Route("FindByName/{name:minlength(1)}")]
37+
public async Task<IActionResult> Find(string name)
6038
{
61-
_context.CatalogItems.Update(item);
62-
_context.SaveChanges();
63-
return Ok();
64-
}
39+
var items = await _context.CatalogItems
40+
.Where(c => c.Name.StartsWith(name, StringComparison.CurrentCultureIgnoreCase))
41+
.ToListAsync();
6542

66-
// DELETE api/values/5
67-
[HttpDelete("{id}")]
68-
public IActionResult Delete(Guid id)
69-
{
7043
return Ok();
7144
}
7245
}

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: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
1-
using Microsoft.EntityFrameworkCore;
2-
using Npgsql.EntityFrameworkCore.PostgreSQL;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Threading.Tasks;
7-
8-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model
92
{
3+
using EntityFrameworkCore.Metadata.Builders;
4+
using Microsoft.EntityFrameworkCore;
5+
using Npgsql.EntityFrameworkCore.PostgreSQL;
6+
107
public class CatalogContext : DbContext
118
{
12-
public CatalogContext(DbContextOptions options): base(options)
9+
public CatalogContext(DbContextOptions options) : base(options)
1310
{
1411
}
1512

1613
public DbSet<CatalogItem> CatalogItems { get; set; }
1714

1815
protected override void OnModelCreating(ModelBuilder builder)
1916
{
17+
builder.HasSequence("idseq")
18+
.StartsAt(1)
19+
.IncrementsBy(1);
20+
21+
builder.Entity<CatalogItem>(ConfigureCatalogItem);
22+
2023
builder.HasPostgresExtension("uuid-ossp");
2124
}
25+
26+
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
27+
{
28+
builder.ForNpgsqlToTable("catalog");
29+
30+
builder.Property(ci => ci.Id)
31+
.HasDefaultValueSql("nextval('idseq')")
32+
.IsRequired();
33+
34+
builder.Property(ci => ci.Name)
35+
.IsRequired(true)
36+
.HasMaxLength(50);
37+
38+
builder.Property(ci => ci.Price)
39+
.IsRequired(true);
40+
41+
builder.Property(ci => ci.PictureUri)
42+
.IsRequired(false);
43+
44+
}
2245
}
2346
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model
2+
{
3+
using Microsoft.AspNetCore.Builder;
4+
using Model;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
public class CatalogContextSeed
11+
{
12+
public static async Task SeedAsync(IApplicationBuilder applicationBuilder)
13+
{
14+
var context = (CatalogContext)applicationBuilder
15+
.ApplicationServices.GetService(typeof(CatalogContext));
16+
17+
using (context)
18+
{
19+
context.Database.EnsureCreated();
20+
21+
if (!context.CatalogItems.Any())
22+
{
23+
context.CatalogItems.AddRange(
24+
GetPreconfiguredItems());
25+
26+
await context.SaveChangesAsync();
27+
}
28+
}
29+
}
30+
31+
static IEnumerable<CatalogItem> GetPreconfiguredItems()
32+
{
33+
return new List<CatalogItem>()
34+
{
35+
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
36+
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
37+
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
38+
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
39+
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
40+
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
41+
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
42+
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
43+
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
44+
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
45+
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
46+
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
47+
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
48+
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
49+
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
50+
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
51+
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
52+
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
53+
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
54+
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
55+
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
56+
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
57+
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
58+
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" },
59+
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" },
60+
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" },
61+
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" },
62+
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }
63+
64+
};
65+
}
66+
}
67+
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model
72
{
3+
using System;
4+
85
public class CatalogItem
96
{
10-
public CatalogItem()
11-
{
12-
}
7+
public int Id { get; set; }
138

14-
public Guid Id { get; set; }
159
public string Name { get; set; }
10+
1611
public string Description { get; set; }
12+
1713
public decimal Price { get; set; }
18-
public int ImageCount { get; set; }
14+
15+
public string PictureUri { get; set; }
16+
17+
public CatalogItem() { }
1918
}
2019
}

src/Services/Catalog/Catalog.API/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static void Main(string[] args)
1414
{
1515
var host = new WebHostBuilder()
1616
.UseKestrel()
17+
.UseUrls(Environment.GetEnvironmentVariable("ASPNETCORE_URLS") ?? String.Empty)
1718
.UseContentRoot(Directory.GetCurrentDirectory())
1819
.UseStartup<Startup>()
1920
.Build();

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,81 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
62
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Mvc.ApiExplorer;
4+
using Microsoft.AspNetCore.Mvc.Formatters;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Infrastructure;
7+
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
78
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Logging;
10-
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
11-
using Microsoft.EntityFrameworkCore;
11+
using Newtonsoft.Json.Serialization;
12+
using System.Threading.Tasks;
1213

1314
namespace Microsoft.eShopOnContainers.Services.Catalog.API
1415
{
1516
public class Startup
1617
{
18+
public IConfigurationRoot Configuration { get; }
19+
1720
public Startup(IHostingEnvironment env)
1821
{
1922
var builder = new ConfigurationBuilder()
2023
.SetBasePath(env.ContentRootPath)
21-
.AddJsonFile("settings.json")
24+
.AddJsonFile($"settings.{env.EnvironmentName}.json",optional:false)
2225
.AddEnvironmentVariables();
26+
27+
2328
Configuration = builder.Build();
2429
}
2530

26-
public IConfigurationRoot Configuration { get; }
31+
2732

28-
// This method gets called by the runtime. Use this method to add services to the container.
2933
public void ConfigureServices(IServiceCollection services)
3034
{
31-
services.AddDbContext<CatalogContext>(c => {
35+
services.AddSingleton<IConfiguration>(Configuration);
36+
37+
services.AddDbContext<CatalogContext>(c =>
38+
{
3239
c.UseNpgsql(Configuration["ConnectionString"]);
40+
c.ConfigureWarnings(wb =>
41+
{
42+
wb.Throw(RelationalEventId.QueryClientEvaluationWarning);
43+
});
3344
});
3445

3546
// Add framework services.
47+
48+
services.AddCors();
49+
3650
services.AddMvcCore()
37-
.AddJsonFormatters();
51+
.AddJsonFormatters(settings=>
52+
{
53+
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
54+
});
3855
}
3956

4057
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
4158
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
4259
{
60+
61+
//Configure logs
62+
63+
if(env.IsDevelopment())
64+
{
65+
app.UseDeveloperExceptionPage();
66+
}
67+
4368
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
4469
loggerFactory.AddDebug();
4570

71+
//Seed Data
72+
73+
CatalogContextSeed.SeedAsync(app)
74+
.Wait();
75+
76+
// Use frameworks
77+
app.UseCors(policyBuilder=>policyBuilder.AllowAnyOrigin());
78+
4679
app.UseMvc();
4780
}
4881
}

0 commit comments

Comments
 (0)