Skip to content
Draft
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,5 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
*.db
*.db-shm
*.db-wal
14 changes: 11 additions & 3 deletions webapi/Controllers/RecipeController.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
using Microsoft.AspNetCore.Mvc;
using webapi.Dtos;
using webapi.Entities;
using webapi.Stuff;
using webapi.Services;
using webapi.Structure;

namespace webapi.Controllers
{
public class RecipeController : Controller
{
private readonly RecipeService _recipeService;

public RecipeController(RecipeService recipeService)
{
_recipeService = recipeService ?? throw new ArgumentNullException(nameof(recipeService));
}

[HttpGet]
[Route("")]
public IReadOnlyCollection<RecipeDto>? GetAllRecipes()
{
return null;
return _recipeService.GetAllRecipes();
}

[HttpGet]
Expand All @@ -30,7 +38,7 @@ public class RecipeController : Controller

[HttpGet]
[Route("ingredients")]
public IReadOnlyCollection<RecipeDto>? GetRecipesByIngredients([FromBody] IReadOnlyCollection<Ingredient> availableIngredients)
public IReadOnlyCollection<RecipeDto>? GetRecipesByIngredients([FromBody] IReadOnlyCollection<IngredientDto> availableIngredients)
{
return null;
}
Expand Down
20 changes: 20 additions & 0 deletions webapi/Dtos/IngredientDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace webapi;

public class IngredientDto
{
public IngredientDto(int id, string name, int count, string unit)
{
this.Id = id;
this.Name = name ?? throw new ArgumentNullException(nameof(name));
this.Count = count;
this.Unit = unit ?? throw new ArgumentNullException(nameof(unit));
}

public int Id { get; }

public string Name { get; }

public int Count { get; }

public string Unit { get; }
}
2 changes: 1 addition & 1 deletion webapi/Dtos/RecipeDto.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using webapi.Entities;
using webapi.Stuff;
using webapi.Structure;

namespace webapi.Dtos
{
Expand Down
20 changes: 20 additions & 0 deletions webapi/Dtos/StepDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace webapi;

public class StepDto
{
public StepDto(int id, int recipeId, int number, string descripton)
{
this.Id = id;
this.RecipeId = recipeId;
this.Number = number;
this.Descripton = descripton ?? throw new ArgumentNullException(nameof(descripton));
}

public int Id { get; }

public int RecipeId { get; }

public int Number { get; }

public string Descripton { get; }
}
2 changes: 1 addition & 1 deletion webapi/Entities/Recipe.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using webapi.Stuff;
using webapi.Structure;

namespace webapi.Entities
{
Expand Down
11 changes: 8 additions & 3 deletions webapi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using Microsoft.EntityFrameworkCore;
using webapi;
using webapi.Data;

var builder = WebApplication.CreateBuilder(args);
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add services to the container.
ServiceRegistrations registrations = new();
registrations.RegisterServices(builder);


builder.Services.AddControllers();
builder.Services.AddDbContext<DataContext>(opt =>
{
opt.UseSqlite(builder.Configuration.GetConnectionString("Default"));
});

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
WebApplication app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
Expand All @@ -29,4 +34,4 @@

app.MapControllers();

app.Run();
app.Run();
18 changes: 18 additions & 0 deletions webapi/ServiceRegistrations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using webapi.Services;

namespace webapi
{
public class ServiceRegistrations
{
public ServiceRegistrations()
{
}

public void RegisterServices(WebApplicationBuilder builder)
{
builder.Services.AddSingleton(typeof(RecipeService));
//builder.Services.AddSingleton(typeof(IngredientProvider));
//builder.Services.AddSingleton(typeof(StepProvider));
}
}
}
58 changes: 58 additions & 0 deletions webapi/Services/RecipeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.Data.Sqlite;
using webapi.Dtos;
using webapi.Entities;

namespace webapi.Services
{
public class RecipeService
{
private readonly List<Recipe>? recipes = new(); // store the db cache here

public RecipeService() // todo inherit project db path
{
string connectionString = "Data source = isCooking.db"; // todo how to get this dynamically?

var connection = new SqliteConnection(connectionString);
connection.Open();

var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM Recipe";

using ( var reader = command.ExecuteReader() )
{
while ( reader.Read() ) {
// todo cache
}

}

connection.Close();
}

public void SaveRecipe(RecipeDto recipe)
{
// todo save always goes to db and updates internal cache, avoid desync states
}

public Recipe? GetRecipeById(int id)
{
return null; // todo
}

public IReadOnlyCollection<RecipeDto>? GetAllRecipes()
{
if (this.recipes != null)
{
IReadOnlyCollection<RecipeDto>? result = this.recipes.Select(x =>
{
return new RecipeDto(x.Id, x.Name, x.Description, x.Difficulty, x.MinutesToMake, x.Ingredients, x.Steps, x.ImageRoute);
}).ToList(); // todo maybe dont pass internal types in dto, make dto versions of step and ingredient too?

return result;
}

//the pattern will be return empty list if not found for instance in filtered gets like getbyingredient
return new List<RecipeDto>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace webapi.Stuff
namespace webapi.Structure
{
public enum Difficulty
{
Expand Down
1 change: 1 addition & 0 deletions webapi/webapi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
Expand Down