Commit 54fb707f authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Add area API controller

parent d297d89f
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
using Cals.Visor.Infrastructure.Contexts;
using Cals.Visor.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Cals.Visor.Web.Controllers.Api;
[Route("api/areas")]
[ApiController]
public class AreasApiController : ControllerBase
{
    private readonly ApplicationDbContext _context;

    public AreasApiController(ApplicationDbContext context) => _context = context;

    // GET: /api/areas
    [ProducesResponseType<Area>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllAreas")]
    public async Task<IActionResult> GetAllAsync()
    {
        List<Area> result = await _context.Areas
            .Where(e => e.IsActive == true)
            .ToListAsync();

        return Ok(result);
    }

    // GET: /api/areas/1
    [ProducesResponseType<Area>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{areaId:long}", Name = "GetAreaById")]
    public async Task<IActionResult> GetByIdAsync(long areaId)
    {
        Area? result = await _context.Areas
            .Where(e => e.AreaId == areaId)
            .FirstOrDefaultAsync();

        if (result == null)
            return NotFound();

        return Ok(result);
    }
}