fisr commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text;
|
||||
using YandexGameServer.Models;
|
||||
using YandexGameServer.Services;
|
||||
|
||||
namespace YandexGameServer.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly DatabaseService _databaseService;
|
||||
private readonly CodeGenerationService _codeGenerationService;
|
||||
private readonly ILogger<AuthController> _logger;
|
||||
public AuthController(DatabaseService databaseService, ILogger<AuthController> logger, CodeGenerationService codeGenerationService)
|
||||
{
|
||||
_codeGenerationService = codeGenerationService;
|
||||
_logger = logger;
|
||||
_databaseService = databaseService;
|
||||
}
|
||||
|
||||
[HttpGet("{code:int}")]
|
||||
public async Task<IActionResult> Authorization(int code)
|
||||
{
|
||||
var cities = await _databaseService.GetCitiesCodesAsync();
|
||||
CityUniqueCode? cityCode = cities.Find(x => x.key == code);
|
||||
|
||||
if (cityCode == null) return NotFound("Такого кода нет в базе");
|
||||
else
|
||||
{
|
||||
var client = new Client() { city = cityCode.cityEnum, games = new List<Games>() };
|
||||
await _databaseService.CreateAsync(client);
|
||||
|
||||
return Ok(client);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("admin/{code:int}")]
|
||||
public async Task<IActionResult> AdminAuthorization(int code)
|
||||
{
|
||||
var cities = await _databaseService.GetCitiesCodesAsync();
|
||||
CityUniqueCode? cityCode = cities.Find(x => x.adminKey == code);
|
||||
|
||||
if (cityCode == null) return NotFound();
|
||||
else return Ok(cityCode);
|
||||
}
|
||||
|
||||
[HttpGet("secretcode/{cityId:int}")]
|
||||
public async Task<IActionResult> GetAuthorizationCodeAsync(int cityId)
|
||||
{
|
||||
Cities cityCode = (Cities)cityId;
|
||||
|
||||
var cities = await _databaseService.GetCitiesCodesAsync();
|
||||
if (cities.Any(x => x.cityEnum == cityCode))
|
||||
{
|
||||
var cityData = cities.Find(x => x.cityEnum == cityCode);
|
||||
return Ok(cityData.key);
|
||||
}
|
||||
else return NotFound("Нет соответствующего секретного кода");
|
||||
}
|
||||
|
||||
[HttpGet("all")]
|
||||
public async Task<IActionResult> GetAllCodes()
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
var codes = await _databaseService.GetCitiesCodesAsync();
|
||||
foreach(var code in _codeGenerationService.citiesTranslation.Keys)
|
||||
{
|
||||
var data = codes.Find(x => x.cityEnum.Equals(code));
|
||||
stringBuilder.AppendLine($"{_codeGenerationService.citiesTranslation[code]} - {data.key}");
|
||||
}
|
||||
|
||||
return Ok(stringBuilder.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user