Sieve/Sieve.Sample/Controllers/PostsController.cs

53 lines
1.3 KiB
C#
Raw Normal View History

2018-05-15 01:34:37 +02:00
using System.Linq;
2018-01-27 06:20:57 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Sieve.Models;
using Sieve.Sample.Entities;
2018-01-27 06:20:57 +01:00
using Sieve.Services;
namespace Sieve.Sample.Controllers
2018-01-27 06:20:57 +01:00
{
2019-11-17 00:15:07 +01:00
[Route("api/[controller]/[action]")]
2018-01-27 08:59:34 +01:00
public class PostsController : Controller
2018-01-27 06:20:57 +01:00
{
2018-05-15 01:34:37 +02:00
private readonly ISieveProcessor _sieveProcessor;
private readonly ApplicationDbContext _dbContext;
2018-01-27 06:20:57 +01:00
2018-02-07 05:43:09 +01:00
public PostsController(ISieveProcessor sieveProcessor,
2018-01-27 06:20:57 +01:00
ApplicationDbContext dbContext)
{
_sieveProcessor = sieveProcessor;
_dbContext = dbContext;
}
[HttpGet]
public JsonResult GetAllWithSieve(SieveModel sieveModel)
{
var result = _dbContext.Posts.AsNoTracking();
2018-04-20 11:27:04 +02:00
result = _sieveProcessor.Apply(sieveModel, result);
2018-01-27 06:20:57 +01:00
return Json(result.ToList());
}
[HttpGet]
public JsonResult Create(int number = 10)
{
for (int i = 0; i < number; i++)
{
_dbContext.Posts.Add(new Post());
}
_dbContext.SaveChanges();
return Json(_dbContext.Posts.ToList());
}
[HttpGet]
public JsonResult GetAll()
{
return Json(_dbContext.Posts.ToList());
}
}
}