Skip to content

Commit b9effa7

Browse files
added database and simplified launch
1 parent ff202f8 commit b9effa7

17 files changed

+335
-56
lines changed

.vscode/launch.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,7 @@
1616
"serverReadyAction": {
1717
"action": "openExternally",
1818
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
19-
},
20-
"launchBrowser": {
21-
"enabled": true,
22-
"args": "${auto-detect-url}/swagger",
23-
"windows": {
24-
"command": "cmd.exe",
25-
"args": "/C start ${auto-detect-url}/swagger"
26-
},
27-
"osx": {
28-
"command": "open",
29-
"args": "${auto-detect-url}/swagger"
30-
}
31-
},
19+
},
3220
"env": {
3321
"ASPNETCORE_ENVIRONMENT": "Development"
3422
},

CarvedRock.Api/Controllers/ProductController.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
using CarvedRock.Data.Entities;
12
using CarvedRock.Domain;
2-
using CarvedRock.Domain.Models;
33
using Microsoft.AspNetCore.Mvc;
44

55
namespace CarvedRock.Api.Controllers;
@@ -18,9 +18,24 @@ public ProductController(ILogger<ProductController> logger, IProductLogic produc
1818
}
1919

2020
[HttpGet]
21-
public async Task<IEnumerable<ProductModel>> Get(string category = "all")
21+
public async Task<IEnumerable<Product>> Get(string category = "all")
2222
{
2323
_logger.LogInformation("Getting products in API for {category}", category);
24-
return await _productLogic.GetProductsForCategory(category);
24+
return await _productLogic.GetProductsForCategoryAsync(category);
25+
//return _productLogic.GetProductsForCategory(category);
26+
}
27+
28+
[HttpGet("{id:int}")]
29+
[ProducesResponseType(typeof(Product), StatusCodes.Status200OK)]
30+
[ProducesResponseType(StatusCodes.Status404NotFound)]
31+
public async Task<IActionResult> Get(int id)
32+
{
33+
//var product = await _productLogic.GetProductByIdAsync(id);
34+
var product = _productLogic.GetProductById(id);
35+
if (product != null)
36+
{
37+
return Ok(product);
38+
}
39+
return NotFound();
2540
}
2641
}

CarvedRock.Api/Program.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1+
using CarvedRock.Data;
12
using CarvedRock.Domain;
23

34
var builder = WebApplication.CreateBuilder(args);
45

5-
// Add services to the container.
6-
6+
// Services
77
builder.Services.AddControllers();
88
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
99
builder.Services.AddEndpointsApiExplorer();
1010
builder.Services.AddSwaggerGen();
1111

1212
builder.Services.AddScoped<IProductLogic, ProductLogic>();
1313

14+
builder.Services.AddDbContext<LocalContext>();
15+
builder.Services.AddScoped<ICarvedRockRepository, CarvedRockRepository>();
16+
1417
var app = builder.Build();
1518

16-
// Configure the HTTP request pipeline.
19+
using (var scope = app.Services.CreateScope())
20+
{
21+
var services = scope.ServiceProvider;
22+
var context = services.GetRequiredService<LocalContext>();
23+
context.MigrateAndCreateData();
24+
}
25+
26+
// HTTP request pipeline
1727
if (app.Environment.IsDevelopment())
1828
{
1929
app.UseSwagger();
2030
app.UseSwaggerUI();
2131
}
22-
32+
app.MapFallback(() => Results.Redirect("/swagger"));
2333
app.UseHttpsRedirection();
24-
2534
app.UseAuthorization();
26-
2735
app.MapControllers();
2836

2937
app.Run();

CarvedRock.Api/Properties/launchSettings.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"commandName": "Project",
1414
"dotnetRunMessages": true,
1515
"launchBrowser": true,
16-
"launchUrl": "swagger",
1716
"applicationUrl": "https://localhost:7213;http://localhost:5022",
1817
"environmentVariables": {
1918
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -22,7 +21,6 @@
2221
"IIS Express": {
2322
"commandName": "IISExpress",
2423
"launchBrowser": true,
25-
"launchUrl": "swagger",
2624
"environmentVariables": {
2725
"ASPNETCORE_ENVIRONMENT": "Development"
2826
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using CarvedRock.Data.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace CarvedRock.Data
5+
{
6+
public class CarvedRockRepository :ICarvedRockRepository
7+
{
8+
private readonly LocalContext _ctx;
9+
10+
public CarvedRockRepository(LocalContext ctx)
11+
{
12+
_ctx = ctx;
13+
}
14+
public async Task<List<Product>> GetProductsAsync(string category)
15+
{
16+
return await _ctx.Products.Where(p => p.Category == category || category == "all").ToListAsync();
17+
}
18+
19+
public async Task<Product?> GetProductByIdAsync(int id)
20+
{
21+
return await _ctx.Products.FindAsync(id);
22+
}
23+
24+
public List<Product> GetProducts(string category)
25+
{
26+
return _ctx.Products.Where(p => p.Category == category || category == "all").ToList();
27+
}
28+
29+
public Product? GetProductById(int id)
30+
{
31+
return _ctx.Products.Find(id);
32+
}
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace CarvedRock.Data.Entities
2+
{
3+
public class Product
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; } = null!;
7+
public string Description { get; set; } = null!;
8+
public double Price { get; set; }
9+
public string Category { get; set; } = null!;
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using CarvedRock.Data.Entities;
2+
3+
namespace CarvedRock.Data
4+
{
5+
public interface ICarvedRockRepository
6+
{
7+
Task<List<Product>> GetProductsAsync(string category);
8+
Task<Product?> GetProductByIdAsync(int id);
9+
10+
List<Product> GetProducts(string category);
11+
Product? GetProductById(int id);
12+
}
13+
}

CarvedRock.Data/LocalContext.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using CarvedRock.Data.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace CarvedRock.Data
5+
{
6+
public class LocalContext : DbContext
7+
{
8+
public DbSet<Product> Products { get; set; } = null!;
9+
10+
public string DbPath { get; set; }
11+
12+
public LocalContext()
13+
{
14+
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
15+
DbPath = Path.Join(path, "carvedrock-logging.db");
16+
}
17+
18+
protected override void OnConfiguring(DbContextOptionsBuilder options)
19+
=> options.UseSqlite($"Data Source={DbPath}");
20+
21+
public void MigrateAndCreateData()
22+
{
23+
Database.Migrate();
24+
25+
if (Products.Any()) return;
26+
27+
Products.Add(new Product
28+
{
29+
Name = "Trailblazer",
30+
Category = "boots",
31+
Price = 69.99,
32+
Description = "Great support in this high-top to take you to great heights and trails."
33+
});
34+
Products.Add(new Product
35+
{
36+
Name = "Coastliner",
37+
Category = "boots",
38+
Price = 49.99,
39+
Description =
40+
"Easy in and out with this lightweight but rugged shoe with great ventilation to get your around shores, beaches, and boats."
41+
});
42+
Products.Add(new Product
43+
{
44+
Name = "Woodsman",
45+
Category = "boots",
46+
Price = 64.99,
47+
Description =
48+
"All the insulation and support you need when wandering the rugged trails of the woods and backcountry."
49+
});
50+
Products.Add(new Product
51+
{
52+
Name = "Billy",
53+
Category = "boots",
54+
Price = 79.99,
55+
Description =
56+
"Get up and down rocky terrain like a billy-goat with these awesome high-top boots with outstanding support."
57+
});
58+
59+
SaveChanges();
60+
}
61+
}
62+
}

CarvedRock.Data/Migrations/20211218131519_Initial.Designer.cs

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)