Commit 06486e96 authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Add Vlan API method

parent 59f6a738
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
using System.Text.Json.Serialization;

namespace Cals.Visor.Models;

public class Vlan : ICreatedAt, IUpdatedAt
@@ -13,17 +15,19 @@ public class Vlan : ICreatedAt, IUpdatedAt

    public long? AreaId { get; set; }

    public Area? Area { get; set; }

    public string? Name { get; set; }

    public string? Description { get; set; }

    public string? Notes { get; set; }

    public bool? IsActive { get; set; }
    public bool? IsActive { get; set; } = true;

    [JsonIgnore]
    public virtual Area? Area { get; set; }

    public List<Subnet> Subnets { get; set; }
    [JsonIgnore]
    public virtual List<Subnet> Subnets { get; set; }

    public DateTime? CreatedAt { get; set; }

+78 −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/vlans")]
[ApiController]
public class VlansApiController : ControllerBase
{
    private readonly ApplicationDbContext _context;

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

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

        return Ok(result);
    }

    // GET: /api/vlans/1
    [ProducesResponseType<Vlan>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{vlanId:long}", Name = "GetVlanById")]
    public async Task<IActionResult> GetByIdAsync(long vlanId)
    {
        Vlan? result = await _context.Vlans
            .Where(e => e.VlanId == vlanId)
            .FirstOrDefaultAsync();

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

        return Ok(result);
    }

    // POST: /api/vlans
    [ProducesResponseType<Vlan>(StatusCodes.Status201Created)]
    [HttpPost(Name = "CreateNewVlan")]
    public async Task<IActionResult> CreateNewAsync([FromBody] Vlan data)
    {
        _context.Add(data);
        await _context.SaveChangesAsync();

        return CreatedAtAction(nameof(GetByIdAsync), new { vlanId = data.VlanId }, data);
    }

    // PUT: /api/vlans/2
    [ProducesResponseType<Vlan>(StatusCodes.Status200OK)]
    [HttpPut("{vlanId:long}", Name = "UpdateVlan")]
    public async Task<IActionResult> UpdateAsync(long vlanId, [FromBody] Vlan data)
    {
        Vlan? entity = await _context.Vlans
            .Where(e => e.VlanId == vlanId)
            .FirstOrDefaultAsync();

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

        entity.IsActive = data.IsActive;
        entity.Description = data.Description;
        entity.Notes = data.Notes;
        entity.AreaId = data.AreaId;
        entity.Tag = data.Tag;
        entity.IsActive = data.IsActive;
        entity.Name = data.Name;

        await _context.SaveChangesAsync();

        return Ok(entity);
    }
}