configure queryable rest endpoints
This commit is contained in:
42
FakePieShop/Controllers/Api/SearchController.cs
Normal file
42
FakePieShop/Controllers/Api/SearchController.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using FakePieShop.Repositories;
|
||||||
|
using FakePieShop.Models;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace FakePieShop.Controllers.Api
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class SearchController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IPieRepository _pieRepository;
|
||||||
|
|
||||||
|
public SearchController(IPieRepository pieRepository)
|
||||||
|
{
|
||||||
|
_pieRepository = pieRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult GetAll() => Ok(_pieRepository.AllPies);
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public IActionResult GetById(int id)
|
||||||
|
{
|
||||||
|
Pie? pie = _pieRepository.GetPieById(id);
|
||||||
|
if (pie == null) return NotFound();
|
||||||
|
return Ok(pie);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult SearchPies([FromBody] string searchQuery)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(searchQuery))
|
||||||
|
return BadRequest();
|
||||||
|
|
||||||
|
IEnumerable<Pie> pies = new List<Pie>();
|
||||||
|
|
||||||
|
pies = _pieRepository.SearchPies(searchQuery);
|
||||||
|
return new JsonResult(pies);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using FakePieShop.Models;
|
using FakePieShop.Models;
|
||||||
using FakePieShop.Repositories;
|
using FakePieShop.Repositories;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@@ -14,9 +15,14 @@ builder.Services.AddScoped<IShoppingCart, ShoppingCart>(services => ShoppingCart
|
|||||||
builder.Services.AddSession();
|
builder.Services.AddSession();
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
|
// use this configuration if using .net for backend only
|
||||||
|
// builder.Services.AddControllers();
|
||||||
|
|
||||||
// configuration for MVC and entity framework core
|
// configuration for MVC and entity framework core
|
||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews() // this does the same as the above, but also includes support for .net views
|
||||||
|
.AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
|
||||||
|
|
||||||
builder.Services.AddDbContext<FakePieShopDbContext>(options =>
|
builder.Services.AddDbContext<FakePieShopDbContext>(options =>
|
||||||
{
|
{
|
||||||
options.UseSqlServer(
|
options.UseSqlServer(
|
||||||
@@ -36,7 +42,9 @@ if (app.Environment.IsDevelopment())
|
|||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.MapDefaultControllerRoute();
|
// if writing a backend only project:
|
||||||
|
// app.MapControllers();
|
||||||
|
app.MapDefaultControllerRoute(); // otherwise, this does the same configuration, but full stack
|
||||||
app.MapRazorPages();
|
app.MapRazorPages();
|
||||||
|
|
||||||
// use our own seed data
|
// use our own seed data
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ namespace FakePieShop.Repositories
|
|||||||
IEnumerable<Pie> AllPies { get; }
|
IEnumerable<Pie> AllPies { get; }
|
||||||
IEnumerable<Pie> PiesOfTheWeek { get; }
|
IEnumerable<Pie> PiesOfTheWeek { get; }
|
||||||
Pie? GetPieById(int pieId);
|
Pie? GetPieById(int pieId);
|
||||||
|
IEnumerable<Pie> SearchPies(string searchQuery);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,5 +32,8 @@ namespace FakePieShop.Repositories
|
|||||||
{
|
{
|
||||||
return _fakePieShopDbContext.Pies.FirstOrDefault(p => p.PieId == pieId);
|
return _fakePieShopDbContext.Pies.FirstOrDefault(p => p.PieId == pieId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Pie> SearchPies(string searchQuery) =>
|
||||||
|
_fakePieShopDbContext.Pies.Where(p => p.Name.Contains(searchQuery));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user