Reputation: 141
Can you explain to me how to add sort, filtering and paging to my ASP.NET Web API, so I could write url like this
http://localhost:8000/api/Meetings?sort_by=name&sort_type=asc&s=Joh&page=1&page_size=3
I have this MeetingsController
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication3.Models;
namespace WebApplication3.Controllers
{
[Route("api/Meetings")]
[ApiController]
public class MeetingsController : ControllerBase
{
private readonly MeetingContext _context;
public MeetingsController(MeetingContext context)
{
_context = context;
}
//GET: api/Meetings
[HttpGet]
public async Task<ActionResult<IEnumerable<Meeting>>> GetMeetings()
{
return await _context.Meetings.ToListAsync();
}
// GET: api/Meetings/5
[HttpGet("{id}")]
public async Task<ActionResult<Meeting>> GetMeeting(string id)
{
var meeting = await _context.Meetings.FindAsync(id);
if (meeting == null)
{
return NotFound();
}
return meeting;
}
// PUT: api/Meetings/5
[HttpPut("{id}")]
public async Task<IActionResult> PutMeeting(string id, Meeting meeting)
{
if (id != meeting.Id)
{
return BadRequest();
}
_context.Entry(meeting).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!MeetingExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Meetings
[HttpPost]
public async Task<ActionResult<Meeting>> PostMeeting(Meeting meeting)
{
if (ModelState.IsValid)
{
Guid obj = Guid.NewGuid();
meeting.Id = obj.ToString();
_context.Meetings.Add(meeting);
}
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (MeetingExists(meeting.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction(nameof(GetMeeting), new { id = meeting.Id }, meeting);
}
// DELETE: api/Meetings/5
[HttpDelete("{id}")]
public async Task<ActionResult<Meeting>> DeleteMeeting(string id)
{
var meeting = await _context.Meetings.FindAsync(id);
if (meeting == null)
{
return NotFound();
}
_context.Meetings.Remove(meeting);
await _context.SaveChangesAsync();
return meeting;
}
private bool MeetingExists(string id)
{
return _context.Meetings.Any(e => e.Id == id);
}
}
}
Upvotes: 0
Views: 4696
Reputation: 1978
try it:
this is only a sample:
//GET: api/Meetings
[HttpGet]
public async Task<ActionResult<IEnumerable<Meeting>>> GetMeetings(string sort_by, string sort_type, string s, int page , int page_size)
{
IQueryable<Meeting> query = _context.Meetings;
switch (sort_by)
{
case "name":
if (sort_type == "asc")
{
query = query.orderby(c => c.name);
}
else
{
query = query.OrderByDescending(c => c.name);
}
break;
case "surname":
if (sort_type == "asc")
{
query = query.orderby(c => c.surname);
}
else
{
query = query.OrderByDescending(c => c.surname);
}
break;
//do more
}
//your search
if (!string.IsNullOrEmpty(s))
{
query = query.where(c => c.name.Contains(s));
}
return await query.Skip((page - 1)* page_size).Take(page_size).ToListAsync();
}
Upvotes: 1