Commit 91a322da authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Add location vlan, swagger description from Xml

parent eab7a75b
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -39,6 +39,11 @@
            Permission to simply access the app (minimum permission.
            </summary>
        </member>
        <member name="M:Cals.Visor.Api.Controllers.VlansController.GetLocationsByVlan(System.Int64)">
            <summary>
            Get all locations for supplied vlan
            </summary>
        </member>
        <member name="M:Cals.Visor.Api.Identity.ApplicationBuildingExtensions.UseAppIdentity(Microsoft.AspNetCore.Builder.IApplicationBuilder)">
            <summary>
            Enables use of the Visor user identity.
+41 −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.Api.Controllers;

[Route("location_vlans")]
[ApiController]
public class LocationVlansController : ControllerBase
{
    private readonly ApplicationDbContext _context;

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

    // GET: /locations_vlans
    [ProducesResponseType<IEnumerable<LocationVlan>>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllLocationVlans")]
    public async Task<IActionResult> GetAllLocationVlansAsync()
    {
        List<LocationVlan> locationVlans = await _context.LocationVlans.ToListAsync();

        return Ok(locationVlans);
    }

    // GET: /locations_vlans/1
    [ProducesResponseType<LocationVlan>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{locationVlanId:long}", Name= "GetLocationVlanById" )]
    public async Task<IActionResult> GetLocationVlanByIdAsync(long locationVlanId)
    {
        LocationVlan? result = await _context.LocationVlans
            .Where(e => e.LocationVlanId == locationVlanId)
            .FirstOrDefaultAsync();

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

        return Ok(result);
    }
}
+52 −4
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ using Cals.Visor.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.Web.CodeGeneration;

namespace Cals.Visor.Api.Controllers;
[Route("[controller]")]
@@ -13,7 +14,7 @@ public class LocationsController : ControllerBase

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

    // GET: /api/locations
    // GET: /locations
    [ProducesResponseType<IList<Location>>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllLocations")]
    public async Task<IActionResult> GetAllAsync()
@@ -24,7 +25,7 @@ public class LocationsController : ControllerBase
        return Ok(result);
    }

    // GET: /api/locations/1
    // GET: /locations/1
    [ProducesResponseType<Location>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{locationId:long}", Name = "GetLocationById")]
@@ -40,7 +41,7 @@ public class LocationsController : ControllerBase
        return Ok(result);
    }

    // POST: /api/locations
    // POST: /locations
    [ProducesResponseType<Location>(StatusCodes.Status201Created)]
    [HttpPost(Name = "CreateNewLocation")]
    public async Task<IActionResult> CreateNewAsync(Location data)
@@ -51,7 +52,7 @@ public class LocationsController : ControllerBase
        return CreatedAtAction(nameof(GetByIdAsync), new { locationId = data.LocationId }, data);
    }

    // PUT: /api/locations/2
    // PUT: /locations/2
    [ProducesResponseType<Location>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPut("{locationId:long}", Name = "UpdateLocation")]
@@ -72,4 +73,51 @@ public class LocationsController : ControllerBase

        return Ok(entity);
    }

    // GET: /locations/2/vlans
    [ProducesResponseType<IEnumerable<Vlan>>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{locationId:long}/vlans", Name = "GetVlansByLocation")]
    public async Task<IActionResult> GetVlansByLocationAsync(long locationId)
    {
        Location? entity = await _context.Locations
            .Where(e => e.LocationId == locationId)
            .FirstOrDefaultAsync();

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


        List<Vlan?> vlans = await _context.LocationVlans
            .Where(lv => lv.LocationId == locationId)
            .Select(lv => lv.Vlan)
            .ToListAsync();

        return Ok(vlans);
    }

    // POST: /locations/2/vlans/4

    [ProducesResponseType<LocationVlan>(StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPost("{locationId:long}/vlans/{vlanId:long}", Name = "AssignVlanToLocation")]
    public async Task<IActionResult> AssignVlanToLocationAsync(long locationId, long vlanId)
    {
        if (!await _context.Locations.AnyAsync(l => l.LocationId == locationId))
            return NotFound();

        if (!await _context.Vlans.AnyAsync(v => v.VlanId == vlanId))
            return NotFound();

        var mapping = new LocationVlan()
        {
            LocationId = locationId,
            VlanId = vlanId
        };

        _context.LocationVlans.Add(mapping);
        await _context.SaveChangesAsync();

        return CreatedAtRoute("GetLocationVlanById", new { locationVlanId = mapping.LocationVlanId }, mapping);
    }
}
+26 −0
Original line number Diff line number Diff line
@@ -70,11 +70,37 @@ public class VlansController : ControllerBase
        entity.Tag = data.Tag;
        entity.IsActive = data.IsActive;
        entity.Name = data.Name;
        entity.VlanUseTypeId = data.VlanUseTypeId;

        await _context.SaveChangesAsync();

        return Ok(entity);
    }

    // GET: /vlans/2/locations
    /// <summary>
    /// Get all locations for supplied vlan
    /// </summary>
    [ProducesResponseType<IList<Location>>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{vlanId:long}/locations", Name = "GetLocationsByVlan")]
    public async Task<IActionResult> GetLocationsByVlan(long vlanId)
    {
        Vlan? entity = await _context.Vlans
            .Where(e => e.VlanId == vlanId)
            .FirstOrDefaultAsync();

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

        List<Location?> locations = await _context.LocationVlans
            .Where(lv => lv.VlanId == vlanId)
            .Select(lv => lv.Location)
            .ToListAsync();

        return Ok(locations);
    }

    // ######################################################
    // ### VLAN USE TYPES
    //
+12 −0
Original line number Diff line number Diff line
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using Azure.Core;
using Azure.Identity;
@@ -90,6 +91,10 @@ builder.Services.AddSwaggerGen(options =>
            Array.Empty<string>()
        }
    });

    // integrate xml comments
    options.IncludeXmlComments(XmlCommentsFilePath());

});

//// ############# AUTHORIZATION ################
@@ -164,5 +169,12 @@ static void ConfigureAzureKeyVault(WebApplicationBuilder builder)
    }
}

static string XmlCommentsFilePath()
{
        string fileName = typeof(Program).GetTypeInfo().Assembly.GetName().Name + ".xml";
        return Path.Combine(AppContext.BaseDirectory, fileName);
}


#pragma warning restore SA1005
#pragma warning restore SA1512 // Single-line comments should not be followed by blank line