Use Singleton for DataModel + Remove WeatherForecast

This commit is contained in:
Eric NGUYEN 2022-04-28 11:52:10 +02:00
parent 0abe1fb659
commit 3f3eadb3a9
5 changed files with 16 additions and 58 deletions

View file

@ -8,7 +8,6 @@ namespace TestTokenCreator.Controllers
public class TokenController : ControllerBase public class TokenController : ControllerBase
{ {
private static readonly HttpClient client = new HttpClient(); private static readonly HttpClient client = new HttpClient();
private DataModel _applicationModel;
public TokenController() public TokenController()
{ {
@ -17,13 +16,13 @@ namespace TestTokenCreator.Controllers
[HttpPost(Name = "SetApplicationModel")] [HttpPost(Name = "SetApplicationModel")]
public void SetApplicationModel(DataModel dataModel) public void SetApplicationModel(DataModel dataModel)
{ {
_applicationModel = dataModel; DataModel.Instance.SetModel(dataModel);
} }
[HttpGet(Name = "GetApplicationModel")] [HttpGet(Name = "GetApplicationModel")]
public DataModel GetApplicationModel() public DataModel GetApplicationModel()
{ {
return this._applicationModel; return DataModel.Instance;
} }
[HttpGet(Name = "GetAutorizationUrl")] [HttpGet(Name = "GetAutorizationUrl")]
@ -31,8 +30,8 @@ namespace TestTokenCreator.Controllers
{ {
string state = "User1"; string state = "User1";
string scope = Uri.EscapeUriString("vso.code_full vso.tokens vso.tokenadministration"); string scope = Uri.EscapeUriString("vso.code_full vso.tokens vso.tokenadministration");
string encodedRedirectUri = Uri.EscapeUriString(_applicationModel.RedirectUri); string encodedRedirectUri = Uri.EscapeUriString(DataModel.Instance.RedirectUri);
string urlAutorisation = $"https://app.vssps.visualstudio.com/oauth2/authorize?client_id={_applicationModel.ApplicationId}&response_type=Assertion&state={state}&scope={scope}&redirect_uri={encodedRedirectUri}"; string urlAutorisation = $"https://app.vssps.visualstudio.com/oauth2/authorize?client_id={DataModel.Instance.ApplicationId}&response_type=Assertion&state={state}&scope={scope}&redirect_uri={encodedRedirectUri}";
return urlAutorisation; return urlAutorisation;
} }
@ -46,7 +45,7 @@ namespace TestTokenCreator.Controllers
Console.WriteLine($"Code: {code}"); Console.WriteLine($"Code: {code}");
Console.WriteLine($"State: {state}"); Console.WriteLine($"State: {state}");
HttpContent content = GenerateRequestPostData(_applicationModel.Secret, code, _applicationModel.RedirectUri); HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, code, DataModel.Instance.RedirectUri);
HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content); HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content);
string resp = await response.Content.ReadAsStringAsync(); string resp = await response.Content.ReadAsStringAsync();
return resp; return resp;

View file

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace TestTokenCreator.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View file

@ -6,18 +6,22 @@
public string ApplicationId { get; set; } public string ApplicationId { get; set; }
public string RedirectUri { get; set; } public string RedirectUri { get; set; }
private static readonly Lazy<DataModel> lazy = new Lazy<DataModel>(() => new DataModel());
public static DataModel Instance => lazy.Value;
public void SetModel(string secret, string applicationId, string redirectUri) public void SetModel(string secret, string applicationId, string redirectUri)
{ {
Secret = secret; Instance.Secret = secret;
ApplicationId = applicationId; Instance.ApplicationId = applicationId;
RedirectUri = redirectUri; Instance.RedirectUri = redirectUri;
} }
public void SetModel(DataModel model) public void SetModel(DataModel model)
{ {
Secret = model.Secret; Instance.Secret = model.Secret;
ApplicationId = model.ApplicationId; Instance.ApplicationId = model.ApplicationId;
RedirectUri = model.RedirectUri; Instance.RedirectUri = model.RedirectUri;
} }
} }
} }

View file

@ -8,6 +8,7 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<DataModel>();
var app = builder.Build(); var app = builder.Build();

View file

@ -1,13 +0,0 @@
namespace TestTokenCreator
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}