diff --git a/.gitignore b/.gitignore index 492ee63..3c312df 100644 --- a/.gitignore +++ b/.gitignore @@ -397,3 +397,5 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml *.db +*.db-shm +*.db-wal diff --git a/webapi/Controllers/RecipeController.cs b/webapi/Controllers/RecipeController.cs index 413d619..28fc8d9 100644 --- a/webapi/Controllers/RecipeController.cs +++ b/webapi/Controllers/RecipeController.cs @@ -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? GetAllRecipes() { - return null; + return _recipeService.GetAllRecipes(); } [HttpGet] @@ -30,7 +38,7 @@ public class RecipeController : Controller [HttpGet] [Route("ingredients")] - public IReadOnlyCollection? GetRecipesByIngredients([FromBody] IReadOnlyCollection availableIngredients) + public IReadOnlyCollection? GetRecipesByIngredients([FromBody] IReadOnlyCollection availableIngredients) { return null; } diff --git a/webapi/Dtos/IngredientDto.cs b/webapi/Dtos/IngredientDto.cs new file mode 100644 index 0000000..7fa7aba --- /dev/null +++ b/webapi/Dtos/IngredientDto.cs @@ -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; } +} diff --git a/webapi/Dtos/RecipeDto.cs b/webapi/Dtos/RecipeDto.cs index a83f763..8a9630f 100644 --- a/webapi/Dtos/RecipeDto.cs +++ b/webapi/Dtos/RecipeDto.cs @@ -1,5 +1,5 @@ using webapi.Entities; -using webapi.Stuff; +using webapi.Structure; namespace webapi.Dtos { diff --git a/webapi/Dtos/StepDto.cs b/webapi/Dtos/StepDto.cs new file mode 100644 index 0000000..8a08df1 --- /dev/null +++ b/webapi/Dtos/StepDto.cs @@ -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; } +} diff --git a/webapi/Entities/Recipe.cs b/webapi/Entities/Recipe.cs index f9feeb1..286439b 100644 --- a/webapi/Entities/Recipe.cs +++ b/webapi/Entities/Recipe.cs @@ -1,4 +1,4 @@ -using webapi.Stuff; +using webapi.Structure; namespace webapi.Entities { diff --git a/webapi/Program.cs b/webapi/Program.cs index 7229f94..3f67daa 100644 --- a/webapi/Program.cs +++ b/webapi/Program.cs @@ -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(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()) @@ -29,4 +34,4 @@ app.MapControllers(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/webapi/ServiceRegistrations.cs b/webapi/ServiceRegistrations.cs new file mode 100644 index 0000000..3e37a60 --- /dev/null +++ b/webapi/ServiceRegistrations.cs @@ -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)); + } + } +} diff --git a/webapi/Services/RecipeService.cs b/webapi/Services/RecipeService.cs new file mode 100644 index 0000000..e4ae50c --- /dev/null +++ b/webapi/Services/RecipeService.cs @@ -0,0 +1,58 @@ +using Microsoft.Data.Sqlite; +using webapi.Dtos; +using webapi.Entities; + +namespace webapi.Services +{ + public class RecipeService + { + private readonly List? 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? GetAllRecipes() + { + if (this.recipes != null) + { + IReadOnlyCollection? 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(); + } + } +} diff --git a/webapi/Stuff/Difficulty.cs b/webapi/Structure/Difficulty.cs similarity index 79% rename from webapi/Stuff/Difficulty.cs rename to webapi/Structure/Difficulty.cs index d528b48..d27a55b 100644 --- a/webapi/Stuff/Difficulty.cs +++ b/webapi/Structure/Difficulty.cs @@ -1,4 +1,4 @@ -namespace webapi.Stuff +namespace webapi.Structure { public enum Difficulty { diff --git a/webapi/webapi.csproj b/webapi/webapi.csproj index d697175..74d6ad3 100644 --- a/webapi/webapi.csproj +++ b/webapi/webapi.csproj @@ -21,6 +21,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive +