@ -1,72 +1,45 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
using Microsoft.AspNetCore.Mvc; | |||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model; | |||||
| |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers | namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers | ||||
{ | { | ||||
[Route("/")] | |||||
using Microsoft.AspNetCore.Mvc; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model; | |||||
using System; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
[Route("api/v1/[controller]")] | |||||
public class CatalogController : ControllerBase | public class CatalogController : ControllerBase | ||||
{ | { | ||||
private CatalogContext _context; | |||||
private readonly CatalogContext _context; | |||||
public CatalogController(CatalogContext context) | public CatalogController(CatalogContext context) | ||||
{ | { | ||||
_context = context; | _context = context; | ||||
} | } | ||||
// GET api/values | |||||
[HttpGet] | |||||
public IEnumerable<CatalogItem> Get() | |||||
{ | |||||
return _context.CatalogItems.ToList(); | |||||
} | |||||
// GET api/v1/[controller]/all | |||||
// GET api/values/5 | |||||
[HttpGet("{id}")] | |||||
public IActionResult Get(Guid id) | |||||
[HttpGet] | |||||
[Route("[action]")] | |||||
public async Task<IActionResult> All() | |||||
{ | { | ||||
var item = _context.CatalogItems.FirstOrDefault(x=> x.Id == id); | |||||
if(item == null) | |||||
{ | |||||
return NotFound(); | |||||
} | |||||
var items = await _context.CatalogItems | |||||
.ToListAsync(); | |||||
return new OkObjectResult(item); | |||||
return Ok(items); | |||||
} | } | ||||
// POST api/values | |||||
[HttpPost] | |||||
public IActionResult Post([FromBody]CatalogItem item) | |||||
{ | |||||
try | |||||
{ | |||||
_context.CatalogItems.Add(item); | |||||
_context.SaveChanges(); | |||||
return Ok(); | |||||
} | |||||
catch | |||||
{ | |||||
return StatusCode(500, "Unable to add new catalog item"); | |||||
} | |||||
} | |||||
// GET api/v1/[controller]/FindByName/samplename | |||||
// PUT api/values/5 | |||||
[HttpPut("{id}")] | |||||
public IActionResult Put(int id, [FromBody]CatalogItem item) | |||||
[HttpGet] | |||||
[Route("FindByName/{name:minlength(1)}")] | |||||
public async Task<IActionResult> Find(string name) | |||||
{ | { | ||||
_context.CatalogItems.Update(item); | |||||
_context.SaveChanges(); | |||||
return Ok(); | |||||
} | |||||
var items = await _context.CatalogItems | |||||
.Where(c => c.Name.StartsWith(name, StringComparison.CurrentCultureIgnoreCase)) | |||||
.ToListAsync(); | |||||
// DELETE api/values/5 | |||||
[HttpDelete("{id}")] | |||||
public IActionResult Delete(Guid id) | |||||
{ | |||||
return Ok(); | return Ok(); | ||||
} | } | ||||
} | } | ||||
@ -1,4 +1,4 @@ | |||||
FROM microsoft/aspnetcore | |||||
FROM microsoft/aspnetcore:latest | |||||
WORKDIR /app | WORKDIR /app | ||||
EXPOSE 80 | EXPOSE 80 | ||||
ADD . /app | ADD . /app |
@ -0,0 +1,67 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model | |||||
{ | |||||
using Microsoft.AspNetCore.Builder; | |||||
using Model; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
public class CatalogContextSeed | |||||
{ | |||||
public static async Task SeedAsync(IApplicationBuilder applicationBuilder) | |||||
{ | |||||
var context = (CatalogContext)applicationBuilder | |||||
.ApplicationServices.GetService(typeof(CatalogContext)); | |||||
using (context) | |||||
{ | |||||
context.Database.EnsureCreated(); | |||||
if (!context.CatalogItems.Any()) | |||||
{ | |||||
context.CatalogItems.AddRange( | |||||
GetPreconfiguredItems()); | |||||
await context.SaveChangesAsync(); | |||||
} | |||||
} | |||||
} | |||||
static IEnumerable<CatalogItem> GetPreconfiguredItems() | |||||
{ | |||||
return new List<CatalogItem>() | |||||
{ | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" }, | |||||
new CatalogItem() { Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt" }, | |||||
new CatalogItem() { Description = "Cupt Black & White Mug", Name = "Cupt Black & White Mug", Price= 17, PictureUri = "https://fakeimg.pl/370x240/EEEEEE/000/?text=CuptBlack&WhiteMug" }, | |||||
new CatalogItem() { Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.PrismWhiteT-Shirt" }, | |||||
new CatalogItem() { Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://fakeimg.pl/370x240/EEEEEE/000/?text=.NETBotBlack" } | |||||
}; | |||||
} | |||||
} | |||||
} |
@ -1,20 +1,19 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model | |||||
{ | { | ||||
using System; | |||||
public class CatalogItem | public class CatalogItem | ||||
{ | { | ||||
public CatalogItem() | |||||
{ | |||||
} | |||||
public int Id { get; set; } | |||||
public Guid Id { get; set; } | |||||
public string Name { get; set; } | public string Name { get; set; } | ||||
public string Description { get; set; } | public string Description { get; set; } | ||||
public decimal Price { get; set; } | public decimal Price { get; set; } | ||||
public int ImageCount { get; set; } | |||||
public string PictureUri { get; set; } | |||||
public CatalogItem() { } | |||||
} | } | ||||
} | } |
@ -1,48 +1,81 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
using Microsoft.AspNetCore.Builder; | |||||
using Microsoft.AspNetCore.Builder; | |||||
using Microsoft.AspNetCore.Hosting; | using Microsoft.AspNetCore.Hosting; | ||||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | |||||
using Microsoft.AspNetCore.Mvc.Formatters; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Microsoft.EntityFrameworkCore.Infrastructure; | |||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model; | |||||
using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||
using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Newtonsoft.Json.Serialization; | |||||
using System.Threading.Tasks; | |||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API | namespace Microsoft.eShopOnContainers.Services.Catalog.API | ||||
{ | { | ||||
public class Startup | public class Startup | ||||
{ | { | ||||
public IConfigurationRoot Configuration { get; } | |||||
public Startup(IHostingEnvironment env) | public Startup(IHostingEnvironment env) | ||||
{ | { | ||||
var builder = new ConfigurationBuilder() | var builder = new ConfigurationBuilder() | ||||
.SetBasePath(env.ContentRootPath) | .SetBasePath(env.ContentRootPath) | ||||
.AddJsonFile("settings.json") | |||||
.AddJsonFile($"settings.{env.EnvironmentName}.json",optional:false) | |||||
.AddEnvironmentVariables(); | .AddEnvironmentVariables(); | ||||
Configuration = builder.Build(); | Configuration = builder.Build(); | ||||
} | } | ||||
public IConfigurationRoot Configuration { get; } | |||||
// This method gets called by the runtime. Use this method to add services to the container. | |||||
public void ConfigureServices(IServiceCollection services) | public void ConfigureServices(IServiceCollection services) | ||||
{ | { | ||||
services.AddDbContext<CatalogContext>(c => { | |||||
services.AddSingleton<IConfiguration>(Configuration); | |||||
services.AddDbContext<CatalogContext>(c => | |||||
{ | |||||
c.UseNpgsql(Configuration["ConnectionString"]); | c.UseNpgsql(Configuration["ConnectionString"]); | ||||
c.ConfigureWarnings(wb => | |||||
{ | |||||
wb.Throw(RelationalEventId.QueryClientEvaluationWarning); | |||||
}); | |||||
}); | }); | ||||
// Add framework services. | // Add framework services. | ||||
services.AddCors(); | |||||
services.AddMvcCore() | services.AddMvcCore() | ||||
.AddJsonFormatters(); | |||||
.AddJsonFormatters(settings=> | |||||
{ | |||||
settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); | |||||
}); | |||||
} | } | ||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | ||||
{ | { | ||||
//Configure logs | |||||
if(env.IsDevelopment()) | |||||
{ | |||||
app.UseDeveloperExceptionPage(); | |||||
} | |||||
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | loggerFactory.AddConsole(Configuration.GetSection("Logging")); | ||||
loggerFactory.AddDebug(); | loggerFactory.AddDebug(); | ||||
//Seed Data | |||||
CatalogContextSeed.SeedAsync(app) | |||||
.Wait(); | |||||
// Use frameworks | |||||
app.UseCors(policyBuilder=>policyBuilder.AllowAnyOrigin()); | |||||
app.UseMvc(); | app.UseMvc(); | ||||
} | } | ||||
} | } | ||||
@ -1,5 +1,5 @@ | |||||
{ | { | ||||
"ConnectionString": "Server=127.0.0.1;Port=5432;Database=postgres;username=postgres", | |||||
"ConnectionString": "Server=127.0.0.1;Port=5432;Database=Catalog;username=postgres;password=postgres", | |||||
"Logging": { | "Logging": { | ||||
"IncludeScopes": false, | "IncludeScopes": false, | ||||
"LogLevel": { | "LogLevel": { |
@ -0,0 +1,11 @@ | |||||
{ | |||||
"ConnectionString": "Server=127.0.0.1;Port=5432;Database=CatalogDB;username=postgres;password=postgres", | |||||
"Logging": { | |||||
"IncludeScopes": false, | |||||
"LogLevel": { | |||||
"Default": "Debug", | |||||
"System": "Information", | |||||
"Microsoft": "Information" | |||||
} | |||||
} | |||||
} |