added Microser

This commit is contained in:
M. Akif Tokatlioglu
2024-03-13 23:25:54 +03:00
parent d11ec09c4e
commit 7ae76afee4
208 changed files with 68884 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
using Microser.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Microser.API.Weather.Controllers;
[Authorize(Policy = "ClientIdPolicy")]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecasts")]
public ActionResult<IEnumerable<WeatherForecast>> Get()
{
return Ok(Data.WeatherForecasts);
}
[HttpGet("{id}", Name = "GetWeatherForecast")]
public ActionResult<WeatherForecast> Get(int id)
{
var item = Data.WeatherForecasts.FirstOrDefault(x => x.Id == id);
if (item == null)
return NotFound();
return Ok(item);
}
[HttpPost(Name = "PostWeatherForecast")]
public ActionResult<WeatherForecast> Post(WeatherForecast weatherForecast)
{
var last = Data.WeatherForecasts.LastOrDefault();
if (last == null)
weatherForecast.Id = 1;
else
weatherForecast.Id = last.Id + 1;
Data.WeatherForecasts.Add(weatherForecast);
return new CreatedAtRouteResult("GetWeatherForecast", new { id = weatherForecast.Id }, weatherForecast);
}
[HttpPut("{id}", Name = "PutWeatherForecast")]
public ActionResult<WeatherForecast> Put(int id, WeatherForecast weatherForecast)
{
var item = Data.WeatherForecasts.FirstOrDefault(x => x.Id == id);
if (item == null)
return NotFound();
item.Date = weatherForecast.Date;
item.TemperatureC = weatherForecast.TemperatureC;
item.Summary = weatherForecast.Summary;
return NoContent();
}
[HttpDelete("{id}", Name = "DeleteWeatherForecast")]
public ActionResult<WeatherForecast> Delete(int id)
{
var item = Data.WeatherForecasts.FirstOrDefault(x => x.Id == id);
if (item == null)
return NotFound();
Data.WeatherForecasts.Remove(item);
return Ok();
}
}

View File

@@ -0,0 +1,35 @@
using Microser.Core.Models;
namespace Microser.API.Weather;
public class Data
{
public static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private static List<WeatherForecast>? weatherForecasts;
public static List<WeatherForecast> WeatherForecasts
{
get
{
if (weatherForecasts == null)
weatherForecasts = GetWeatherForecasts();
return weatherForecasts;
}
}
private static List<WeatherForecast> GetWeatherForecasts()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Id = index,
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToList();
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microser.Core\Microser.Core.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,36 @@
@Microser.API.Weather_HostAddress = http://localhost:7001
GET {{Microser.API.Weather_HostAddress}}/weatherforecast
###
GET {{Microser.API.Weather_HostAddress}}/weatherforecast/1
###
POST {{Microser.API.Weather_HostAddress}}/weatherforecast
Content-Type: application/json
{
"date": "2024-01-01",
"temperatureC": -99,
"summary": "Cool"
}
###
PUT {{Microser.API.Weather_HostAddress}}/weatherforecast/1
Content-Type: application/json
{
"id": 1,
"date": "2024-01-01",
"temperatureC": 111,
"summary": "AAAA"
}
###
DELETE {{Microser.API.Weather_HostAddress}}/weatherforecast/1
###

View File

@@ -0,0 +1,50 @@
using Microsoft.IdentityModel.Tokens;
internal class Program
{
private static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "https://localhost:5001";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false
};
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("ClientIdPolicy", policy => policy.RequireClaim("client_id", "microser_api_weather", "dotnet_blazor_serverapp"));
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}

View File

@@ -0,0 +1,15 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:6001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}