diff --git a/Unbinder/Controllers/Api/BaseApiController.cs b/Unbinder/Controllers/Api/BaseApiController.cs deleted file mode 100644 index 41b4d40..0000000 --- a/Unbinder/Controllers/Api/BaseApiController.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Unbinder.Repositories; - -namespace Unbinder.Controllers.Api -{ - public abstract class BaseApiController : ControllerBase where T : class - { - protected readonly IBaseRepository repository; - - public BaseApiController(IBaseRepository repository) - { - this.repository = repository; - } - - public IActionResult UpdateById(int id) - { - var result = repository.UpdateById(id); - if (result == null) - { - return NotFound(); - } - - return Ok(result); - } - - [HttpDelete("{id}")] - public IActionResult DeleteById(int id) - { - var result = repository.DeleteById(id); - if (result == 0) - { - return NotFound(); - } - - return Ok(result); - } - } -} diff --git a/Unbinder/Controllers/Api/RecipeApiController.cs b/Unbinder/Controllers/Api/RecipeApiController.cs index 6fde691..5fdd64f 100644 --- a/Unbinder/Controllers/Api/RecipeApiController.cs +++ b/Unbinder/Controllers/Api/RecipeApiController.cs @@ -4,12 +4,22 @@ using Unbinder.Repositories; namespace Unbinder.Controllers.Api { - [Route("api/[controller]")] [ApiController] - public class RecipeApiController : BaseApiController + public class RecipeApiController(IRecipeRepository _repository) : ControllerBase { - public RecipeApiController(IRecipeRepository recipeRepository) : base(recipeRepository) + private readonly IRecipeRepository repository = _repository; + + [HttpGet] + [Route("/api/recipe/search")] + public IActionResult Search([FromQuery] string? q) { + if (q == null) return Ok(repository.GetAll); + + var result = repository.GetAll?.Where(r => r.Name.Contains(q, StringComparison.OrdinalIgnoreCase)); + + return result == null + ? NotFound() + : Ok(result); } } } diff --git a/Unbinder/Controllers/RecipeController.cs b/Unbinder/Controllers/RecipeController.cs index 8a8b648..03d7514 100644 --- a/Unbinder/Controllers/RecipeController.cs +++ b/Unbinder/Controllers/RecipeController.cs @@ -29,7 +29,7 @@ namespace Unbinder.Controllers } [Route("[controller]/search")] - public IActionResult Search([FromQuery] string? q, string? category) + public IActionResult Search([FromQuery] string? q, [FromQuery] string? category) { if (q == null && category == null) return View(_recipeRepository.GetAll); diff --git a/Unbinder/Program.cs b/Unbinder/Program.cs index 9470d9c..f7af1ff 100644 --- a/Unbinder/Program.cs +++ b/Unbinder/Program.cs @@ -13,12 +13,12 @@ var builder = WebApplication.CreateBuilder(args); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); // Add services to the container. -builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) - .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")); +//builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) +// .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")); -builder.Services.AddAuthorization(options => - // By default, all incoming requests will be authorized according to the default policy. - options.FallbackPolicy = options.DefaultPolicy); +//builder.Services.AddAuthorization(options => +// // By default, all incoming requests will be authorized according to the default policy. +// options.FallbackPolicy = options.DefaultPolicy); builder.Services.AddDbContext(options => options.UseSqlServer(connectionString)); @@ -27,6 +27,16 @@ builder.Services.AddDbContext(options => builder.Services.AddControllersWithViews(); builder.Services.AddRazorPages(); +builder.Services.AddCors(options => +{ + options.AddPolicy("AllowAll", builder => + { + builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + }); +}); + builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -45,8 +55,8 @@ if (!app.Environment.IsDevelopment()) app.UseStaticFiles(); -app.UseAuthentication(); -app.UseAuthorization(); +//app.UseAuthentication(); +//app.UseAuthorization(); app.MapDefaultControllerRoute(); diff --git a/Unbinder/Views/Recipe/Search.cshtml b/Unbinder/Views/Recipe/Search.cshtml index 38347cb..d5fef27 100644 --- a/Unbinder/Views/Recipe/Search.cshtml +++ b/Unbinder/Views/Recipe/Search.cshtml @@ -1,20 +1,52 @@ @model IEnumerable
-

- @foreach (var recipe in @Model) - { -

@recipe.Name

-

@recipe.RecipeText

- } +
+

+ + +
+
+ @foreach (var recipe in @Model) + { +

@recipe.Name

+

@recipe.RecipeText

+ } +
\ No newline at end of file