Commit c91473a0 authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Add location API methods

parent 1b7525c8
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ public class ApplicationDbContext : DbContext
    public DbSet<PaloAltoFirewall> PaloAltoFirewalls => Set<PaloAltoFirewall>();
    public DbSet<Subnet> Subnets => Set<Subnet>();
    public DbSet<Area> Areas => Set<Area>();
    public DbSet<Location> Locations => Set<Location>();
    public DbSet<LocationVlan> LocationVlans => Set<LocationVlan>();
    public DbSet<PaloAltoAccessDomain> PaloAltoAccessDomains => Set<PaloAltoAccessDomain>();
    public DbSet<Vlan> Vlans => Set<Vlan>();
    public DbSet<PaloAltoVsys> PaloAltoVsyses => Set<PaloAltoVsys>();
+4 −1
Original line number Diff line number Diff line
using System.Text.Json.Serialization;

namespace Cals.Visor.Models;

public class Location : ICreatedAt, IUpdatedAt
@@ -15,7 +17,8 @@ public class Location : ICreatedAt, IUpdatedAt

    public string? Notes { get; set; }

    public IList<Vlan> Vlans { get; set; }
    [JsonIgnore]
    public virtual IList<Vlan> Vlans { get; set; }

    public DateTime? CreatedAt { get; set; }

+7 −3
Original line number Diff line number Diff line
using System.Text.Json.Serialization;

namespace Cals.Visor.Models;

public class LocationVlan : ICreatedAt
@@ -6,11 +8,13 @@ public class LocationVlan : ICreatedAt

    public long? VlanId { get; set; }

    public Vlan? Vlan { get; set; }

    public long? LocationId { get; set; }

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

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

    public DateTime? CreatedAt { get; set; }
}
+75 −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;

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

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

    // GET: /api/locations
    [ProducesResponseType<IList<Location>>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllLocations")]
    public async Task<IActionResult> GetAllAsync()
    {
        List<Location> result = await _context.Locations
            .ToListAsync();

        return Ok(result);
    }

    // GET: /api/locations/1
    [ProducesResponseType<Location>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{locationId:long}", Name = "GetLocationById")]
    public async Task<IActionResult> GetByIdAsync(long locationId)
    {
        Location? result = await _context.Locations
            .Where(e => e.LocationId == locationId)
            .FirstOrDefaultAsync();

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

        return Ok(result);
    }

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

        return CreatedAtAction(nameof(GetByIdAsync), new { locationId = data.LocationId }, data);
    }

    // PUT: /api/locations/2
    [ProducesResponseType<Location>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPut("{locationId:long}", Name = "UpdateLocation")]
    public async Task<IActionResult> UpdateAsync(long locationId, Location data)
    {
        Location? entity = await _context.Locations
            .Where(e => e.LocationId == locationId)
            .FirstOrDefaultAsync();

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

        entity.Description = data.Description;
        entity.Notes = data.Notes;
        entity.Name = data.Name;

        await _context.SaveChangesAsync();

        return Ok(entity);
    }
}