Skip to content

Commit

Permalink
Merge pull request #3 from Memphis-ARTCC/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
fixterjake authored May 7, 2024
2 parents be384f6 + 6ac90b4 commit 1d89a9e
Show file tree
Hide file tree
Showing 89 changed files with 5,182 additions and 1,634 deletions.
55 changes: 30 additions & 25 deletions Memphis.API/Controllers/AirportsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Sentry;
using Constants = Memphis.Shared.Utils.Constants;

namespace Memphis.API.Controllers;
Expand All @@ -24,11 +23,11 @@ public class AirportsController : ControllerBase
private readonly RedisService _redisService;
private readonly LoggingService _loggingService;
private readonly IValidator<AirportDto> _validator;
private readonly IHub _sentryHub;
private readonly ISentryClient _sentryHub;
private readonly ILogger<AirportsController> _logger;

public AirportsController(DatabaseContext context, RedisService redisService, LoggingService loggingService,
IValidator<AirportDto> validator, IHub sentryHub, ILogger<AirportsController> logger)
IValidator<AirportDto> validator, ISentryClient sentryHub, ILogger<AirportsController> logger)
{
_context = context;
_redisService = redisService;
Expand All @@ -41,19 +40,21 @@ public AirportsController(DatabaseContext context, RedisService redisService, Lo

[HttpPost]
[Authorize(Roles = Constants.CanAirports)]
[ProducesResponseType(typeof(Response<Airport>), 200)]
[ProducesResponseType(typeof(Response<Airport>), 201)]
[ProducesResponseType(typeof(Response<IList<ValidationFailure>>), 400)]
[ProducesResponseType(401)]
[ProducesResponseType(403)]
[ProducesResponseType(typeof(Response<string?>), 500)]
public async Task<ActionResult<Response<Airport>>> CreateAirport(AirportDto data)
public async Task<ActionResult<Response<Airport>>> CreateAirport(AirportDto payload)
{
try
{
if (!await _redisService.ValidateRoles(Request.HttpContext.User, Constants.CanAirportsList))
{
return StatusCode(401);
}

var validation = await _validator.ValidateAsync(data);
ValidationResult validation = await _validator.ValidateAsync(payload);
if (!validation.IsValid)
{
return BadRequest(new Response<IList<ValidationFailure>>
Expand All @@ -64,18 +65,18 @@ public async Task<ActionResult<Response<Airport>>> CreateAirport(AirportDto data
});
}

var result = await _context.Airports.AddAsync(new Airport
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<Airport> result = await _context.Airports.AddAsync(new Airport
{
Name = data.Name,
Icao = data.Icao,
Name = payload.Name,
Icao = payload.Icao,
});
await _context.SaveChangesAsync();
var newData = JsonConvert.SerializeObject(result.Entity);
string newData = JsonConvert.SerializeObject(result.Entity);
await _loggingService.AddWebsiteLog(Request, $"Created airport {result.Entity.Id}", string.Empty, newData);

return Ok(new Response<Airport>
return StatusCode(201, new Response<Airport>
{
StatusCode = 200,
StatusCode = 201,
Message = $"Created airport '{result.Entity.Id}'",
Data = result.Entity
});
Expand All @@ -94,7 +95,7 @@ public async Task<ActionResult<Response<IList<Airport>>>> GetAirports()
{
try
{
var result = await _context.Airports.ToListAsync();
List<Airport> result = await _context.Airports.ToListAsync();
return Ok(new Response<IList<Airport>>
{
StatusCode = 200,
Expand All @@ -117,7 +118,7 @@ public async Task<ActionResult<Response<IList<Airport>>>> GetAirport(int airport
{
try
{
var result = await _context.Airports.FindAsync(airportId);
Airport? result = await _context.Airports.FindAsync(airportId);
if (result == null)
{
return NotFound(new Response<int>
Expand All @@ -142,22 +143,24 @@ public async Task<ActionResult<Response<IList<Airport>>>> GetAirport(int airport
}
}

[HttpPut("{airportId:int}")]
[HttpPut]
[Authorize(Roles = Constants.CanAirports)]
[ProducesResponseType(typeof(Response<Airport>), 200)]
[ProducesResponseType(typeof(Response<IList<ValidationFailure>>), 400)]
[ProducesResponseType(401)]
[ProducesResponseType(403)]
[ProducesResponseType(typeof(Response<string?>), 404)]
[ProducesResponseType(typeof(Response<string?>), 500)]
public async Task<ActionResult<Response<Airport>>> UpdateAirport(int airportId, AirportDto data)
public async Task<ActionResult<Response<Airport>>> UpdateAirport(AirportDto payload)
{
try
{
if (!await _redisService.ValidateRoles(Request.HttpContext.User, Constants.CanAirportsList))
{
return StatusCode(401);
}

var validation = await _validator.ValidateAsync(data);
ValidationResult validation = await _validator.ValidateAsync(payload);
if (!validation.IsValid)
{
return BadRequest(new Response<IList<ValidationFailure>>
Expand All @@ -168,22 +171,22 @@ public async Task<ActionResult<Response<Airport>>> UpdateAirport(int airportId,
});
}

var airport = await _context.Airports.FindAsync(airportId);
Airport? airport = await _context.Airports.FindAsync();
if (airport == null)
{
return NotFound(new Response<string?>
{
StatusCode = 404,
Message = $"Airport '{airportId}' not found",
Message = $"Airport '{payload.Id}' not found",
});
}

var oldData = JsonConvert.SerializeObject(airport);
airport.Name = data.Name;
airport.Icao = data.Icao;
string oldData = JsonConvert.SerializeObject(airport);
airport.Name = payload.Name;
airport.Icao = payload.Icao;
airport.Updated = DateTimeOffset.UtcNow;
await _context.SaveChangesAsync();
var newData = JsonConvert.SerializeObject(airport);
string newData = JsonConvert.SerializeObject(airport);

await _loggingService.AddWebsiteLog(Request, $"Updated airport '{airport.Id}'", oldData, newData);

Expand Down Expand Up @@ -213,9 +216,11 @@ public async Task<ActionResult<Response<string>>> DeleteAirport(int airportId)
try
{
if (!await _redisService.ValidateRoles(Request.HttpContext.User, Constants.CanAirportsList))
{
return StatusCode(401);
}

var airport = await _context.Airports.FindAsync(airportId);
Airport? airport = await _context.Airports.FindAsync(airportId);
if (airport == null)
{
return NotFound(new Response<int>
Expand All @@ -226,7 +231,7 @@ public async Task<ActionResult<Response<string>>> DeleteAirport(int airportId)
});
}

var oldData = JsonConvert.SerializeObject(airport);
string oldData = JsonConvert.SerializeObject(airport);
_context.Airports.Remove(airport);
await _context.SaveChangesAsync();

Expand Down
32 changes: 20 additions & 12 deletions Memphis.API/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using Memphis.API.Extensions;
using Memphis.API.Services;
using Memphis.Shared.Dtos.auth;
using Memphis.Shared.Dtos.Auth;
using Memphis.Shared.Enums;
using Memphis.Shared.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Sentry;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
Expand All @@ -22,18 +22,18 @@ public class AuthController : ControllerBase
{
private readonly DatabaseContext _context;
private readonly RedisService _redisService;
private readonly IHub _sentryHub;
private readonly ISentryClient _sentryHub;
private readonly ILogger<AuthController> _logger;

public AuthController(DatabaseContext context, RedisService redisService, IHub sentryHub,
ILogger<AuthController> logger)
public AuthController(DatabaseContext context, RedisService redisService, ISentryClient sentryHub, ILogger<AuthController> logger)
{
_context = context;
_redisService = redisService;
_sentryHub = sentryHub;
_logger = logger;
}


[HttpGet("redirect")]
[ProducesResponseType(301)]
[ProducesResponseType(typeof(Response<Guid>), 500)]
Expand All @@ -58,10 +58,10 @@ public async Task<IActionResult> RedirectToVatsim()
}
}

[HttpGet("callback")]
[ProducesResponseType(301)]
[HttpPost("callback")]
[ProducesResponseType(typeof(Response<string>), 200)]
[ProducesResponseType(typeof(Response<Guid>), 500)]
public async Task<IActionResult> ProcessCallback(string code)
public async Task<ActionResult<Response<string>>> ProcessCallback(CodeDto payload)
{
try
{
Expand All @@ -73,8 +73,6 @@ public async Task<IActionResult> ProcessCallback(string code)
throw new ArgumentNullException("CONNECT_CLIENT_SECRET env variable not found");
var redirectUrl = Environment.GetEnvironmentVariable("CONNECT_REDIRECT_URL") ??
throw new ArgumentNullException("CONNECT_REDIRECT_URL env variable not found");
var uiRedirect = Environment.GetEnvironmentVariable("CONNEXT_REDIRECT_URL_UI") ??
throw new ArgumentNullException("CONNEXT_REDIRECT_URL_UI env variable not found");
var issuer = Environment.GetEnvironmentVariable("JWT_ISSUER") ??
throw new ArgumentNullException("JWT_ISSUER env variable not found");
var audience = Environment.GetEnvironmentVariable("JWT_AUDIENCE") ??
Expand All @@ -89,7 +87,7 @@ public async Task<IActionResult> ProcessCallback(string code)
{
["client_id"] = clientId,
["client_secret"] = clientSecret,
["code"] = code,
["code"] = payload.Code,
["grant_type"] = "authorization_code",
["redirect_uri"] = redirectUrl
});
Expand Down Expand Up @@ -148,7 +146,12 @@ public async Task<IActionResult> ProcessCallback(string code)
)
);
var accessTokenNone = new JwtSecurityTokenHandler().WriteToken(jwtNone);
return RedirectPreserveMethod($"{uiRedirect}?accessToken={accessTokenNone}");
return Ok(new Response<string>
{
StatusCode = 200,
Message = "Logged in",
Data = accessTokenNone
});
}

claims.Add(new Claim("isMember", $"{true}"));
Expand All @@ -175,7 +178,12 @@ public async Task<IActionResult> ProcessCallback(string code)
)
);
var accessToken = new JwtSecurityTokenHandler().WriteToken(jwt);
return RedirectPreserveMethod($"{uiRedirect}?accessToken={accessToken}");
return Ok(new Response<string>
{
StatusCode = 200,
Message = "Logged in",
Data = accessToken
});
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit 1d89a9e

Please sign in to comment.