web api base

main
syneffort 2 years ago
parent 38399bd863
commit 3c97c27c94
  1. 12
      BlazorApp/BlazorApp.sln
  2. 4
      BlazorApp/RankingApp/RankingApp.csproj
  3. 17
      BlazorApp/SharedData/Models/GameResult.cs
  4. 9
      BlazorApp/SharedData/SharedData.csproj
  5. 33
      BlazorApp/WebAPI/Controllers/WeatherForecastController.cs
  6. 16
      BlazorApp/WebAPI/Data/ApplicationDbContext.cs
  7. 56
      BlazorApp/WebAPI/Migrations/20230825060048_WebAPI.Designer.cs
  8. 38
      BlazorApp/WebAPI/Migrations/20230825060048_WebAPI.cs
  9. 53
      BlazorApp/WebAPI/Migrations/ApplicationDbContextModelSnapshot.cs
  10. 42
      BlazorApp/WebAPI/Program.cs
  11. 31
      BlazorApp/WebAPI/Properties/launchSettings.json
  12. 13
      BlazorApp/WebAPI/WeatherForecast.cs
  13. 27
      BlazorApp/WebAPI/WebAPI.csproj
  14. 8
      BlazorApp/WebAPI/appsettings.Development.json
  15. 12
      BlazorApp/WebAPI/appsettings.json

@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorStateApp", "BlazorSta
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RankingApp", "RankingApp\RankingApp.csproj", "{6DCA770F-A98B-4A84-9139-A24F77C592D3}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RankingApp", "RankingApp\RankingApp.csproj", "{6DCA770F-A98B-4A84-9139-A24F77C592D3}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAPI", "WebAPI\WebAPI.csproj", "{0AC89B1F-7635-4513-B030-CABF1D582DB1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedData", "SharedData\SharedData.csproj", "{0985C05F-BC2C-4BBE-A413-5E668EC9CFA0}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -33,6 +37,14 @@ Global
{6DCA770F-A98B-4A84-9139-A24F77C592D3}.Debug|Any CPU.Build.0 = Debug|Any CPU {6DCA770F-A98B-4A84-9139-A24F77C592D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DCA770F-A98B-4A84-9139-A24F77C592D3}.Release|Any CPU.ActiveCfg = Release|Any CPU {6DCA770F-A98B-4A84-9139-A24F77C592D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DCA770F-A98B-4A84-9139-A24F77C592D3}.Release|Any CPU.Build.0 = Release|Any CPU {6DCA770F-A98B-4A84-9139-A24F77C592D3}.Release|Any CPU.Build.0 = Release|Any CPU
{0AC89B1F-7635-4513-B030-CABF1D582DB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AC89B1F-7635-4513-B030-CABF1D582DB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AC89B1F-7635-4513-B030-CABF1D582DB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AC89B1F-7635-4513-B030-CABF1D582DB1}.Release|Any CPU.Build.0 = Release|Any CPU
{0985C05F-BC2C-4BBE-A413-5E668EC9CFA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0985C05F-BC2C-4BBE-A413-5E668EC9CFA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0985C05F-BC2C-4BBE-A413-5E668EC9CFA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0985C05F-BC2C-4BBE-A413-5E668EC9CFA0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -15,4 +15,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.20" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.20" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharedData\SharedData.csproj" />
</ItemGroup>
</Project> </Project>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharedData.Models
{
public class GameResult
{
public int Id { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
public int Score { get; set; }
public DateTime Date { get; set; }
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
namespace WebAPI.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();
}
}
}

@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using SharedData.Models;
namespace WebAPI.Data
{
public class ApplicationDbContext : DbContext
{
public DbSet<GameResult> GameResults { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}

@ -0,0 +1,56 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebAPI.Data;
#nullable disable
namespace WebAPI.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20230825060048_WebAPI")]
partial class WebAPI
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("SharedData.Models.GameResult", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("Date")
.HasColumnType("datetime2");
b.Property<int>("Score")
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("GameResults");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace WebAPI.Migrations
{
/// <inheritdoc />
public partial class WebAPI : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "GameResults",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
UserId = table.Column<int>(type: "int", nullable: false),
UserName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Score = table.Column<int>(type: "int", nullable: false),
Date = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_GameResults", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "GameResults");
}
}
}

@ -0,0 +1,53 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebAPI.Data;
#nullable disable
namespace WebAPI.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("SharedData.Models.GameResult", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("Date")
.HasColumnType("datetime2");
b.Property<int>("Score")
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("GameResults");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore;
using WebAPI.Data;
namespace WebAPI
{
public class Program
{
public 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();
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56899",
"sslPort": 44377
}
},
"profiles": {
"WebAPI": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7021;http://localhost:5024",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

@ -0,0 +1,13 @@
namespace WebAPI
{
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; }
}
}

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharedData\SharedData.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project>

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

@ -0,0 +1,12 @@
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=peacecloud.synology.me,21433;Initial Catalog=RankingAPI;User ID=study;Password=Study1234;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Loading…
Cancel
Save