Commit 1c7deec5 authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Add Subnet API method

parent 06486e96
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
using System.Text.Json.Serialization;

namespace Cals.Visor.Models;

public class Subnet : ICreatedAt, IUpdatedAt
@@ -14,9 +16,10 @@ public class Subnet : ICreatedAt, IUpdatedAt

    public long? VlanId { get; set; }

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

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

    public DateTime? CreatedAt { get; set; }

+80 −0
Original line number Diff line number Diff line
using Cals.Visor.Infrastructure.Contexts;
using Cals.Visor.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using static System.Runtime.InteropServices.JavaScript.JSType;

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

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

    // GET: /api/subnets
    [ProducesResponseType<IList<Subnet>>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllSubnets")]
    public async Task<IActionResult> GetAllAsync()
    {
        List<Subnet> result = await _context.Subnets
            .Where(e => e.IsActive == true)
            .ToListAsync();

        return Ok(result);
    }

    // GET: /api/subnets/1
    [ProducesResponseType<Subnet>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{subnetId:long}", Name = "GetSubnetById")]
    public async Task<IActionResult> GetByIdAsync(long subnetId)
    {
        Subnet? result = await _context.Subnets
            .Where(e => e.SubnetId == subnetId)
            .FirstOrDefaultAsync();

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

        return Ok(result);
    }

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

        return CreatedAtAction(nameof(GetByIdAsync), new { subnetId = data.SubnetId }, data);
    }

    // PUT: /api/subnets/2
    [ProducesResponseType<Subnet>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPut("{subnetId:long}", Name = "UpdateSubnet")]
    public async Task<IActionResult> UpdateAsync(long subnetId, Subnet data)
    {
        Subnet? entity = await _context.Subnets
            .Where(e => e.SubnetId == subnetId)
            .FirstOrDefaultAsync();

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

        entity.IsActive = data.IsActive;
        entity.Netname = data.Netname;
        entity.Cidr = data.Cidr;
        entity.Description = data.Description;
        entity.Gateway = data.Gateway;
        entity.VlanId = data.VlanId;

        await _context.SaveChangesAsync();

        return Ok(entity);
    }
}