From 4de6c945c01f1440740123e8b5856e4fbf2bc3d3 Mon Sep 17 00:00:00 2001 From: james bennett Date: Fri, 23 Aug 2024 18:39:20 +0000 Subject: [PATCH 1/3] add initial project config --- API/API.csproj | 19 +++++++++ API/Controllers/WeatherForecastController.cs | 32 +++++++++++++++ API/Program.cs | 25 ++++++++++++ API/Properties/launchSettings.json | 41 ++++++++++++++++++++ API/WeatherForecast.cs | 12 ++++++ API/appsettings.Development.json | 8 ++++ API/appsettings.json | 9 +++++ TestSolution.sln | 22 +++++++++++ 8 files changed, 168 insertions(+) create mode 100644 API/API.csproj create mode 100644 API/Controllers/WeatherForecastController.cs create mode 100644 API/Program.cs create mode 100644 API/Properties/launchSettings.json create mode 100644 API/WeatherForecast.cs create mode 100644 API/appsettings.Development.json create mode 100644 API/appsettings.json create mode 100644 TestSolution.sln diff --git a/API/API.csproj b/API/API.csproj new file mode 100644 index 0000000..37f96ad --- /dev/null +++ b/API/API.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + diff --git a/API/Controllers/WeatherForecastController.cs b/API/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..71f0254 --- /dev/null +++ b/API/Controllers/WeatherForecastController.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Mvc; + +namespace API.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 _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } +} diff --git a/API/Program.cs b/API/Program.cs new file mode 100644 index 0000000..15eacee --- /dev/null +++ b/API/Program.cs @@ -0,0 +1,25 @@ +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 app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/API/Properties/launchSettings.json b/API/Properties/launchSettings.json new file mode 100644 index 0000000..77b21af --- /dev/null +++ b/API/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:42014", + "sslPort": 44378 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5074", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7250;http://localhost:5074", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/API/WeatherForecast.cs b/API/WeatherForecast.cs new file mode 100644 index 0000000..26424a0 --- /dev/null +++ b/API/WeatherForecast.cs @@ -0,0 +1,12 @@ +namespace API; + +public class WeatherForecast +{ + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } +} diff --git a/API/appsettings.Development.json b/API/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/API/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/API/appsettings.json b/API/appsettings.json new file mode 100644 index 0000000..4d56694 --- /dev/null +++ b/API/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/TestSolution.sln b/TestSolution.sln new file mode 100644 index 0000000..62893c9 --- /dev/null +++ b/TestSolution.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{D35237DE-A277-4C26-AA84-DC9301922E23}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D35237DE-A277-4C26-AA84-DC9301922E23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D35237DE-A277-4C26-AA84-DC9301922E23}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D35237DE-A277-4C26-AA84-DC9301922E23}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D35237DE-A277-4C26-AA84-DC9301922E23}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal From 8f400e6593dfc51c2cdd20e0744a9f69a759e209 Mon Sep 17 00:00:00 2001 From: james bennett Date: Fri, 23 Aug 2024 19:09:34 +0000 Subject: [PATCH 2/3] add db context and db --- .../20240823185906_InitialCreate.Designer.cs | 46 ++++++++++++++++++ .../20240823185906_InitialCreate.cs | 36 ++++++++++++++ .../Migrations/TestContextModelSnapshot.cs | 43 ++++++++++++++++ API/Data/TestContext.cs | 12 +++++ API/Entities/User.cs | 10 ++++ API/Program.cs | 6 +++ API/appsettings.Development.json | 3 ++ API/test.db | Bin 0 -> 20480 bytes 8 files changed, 156 insertions(+) create mode 100644 API/Data/Migrations/20240823185906_InitialCreate.Designer.cs create mode 100644 API/Data/Migrations/20240823185906_InitialCreate.cs create mode 100644 API/Data/Migrations/TestContextModelSnapshot.cs create mode 100644 API/Data/TestContext.cs create mode 100644 API/Entities/User.cs create mode 100644 API/test.db diff --git a/API/Data/Migrations/20240823185906_InitialCreate.Designer.cs b/API/Data/Migrations/20240823185906_InitialCreate.Designer.cs new file mode 100644 index 0000000..2bcbdc5 --- /dev/null +++ b/API/Data/Migrations/20240823185906_InitialCreate.Designer.cs @@ -0,0 +1,46 @@ +// +using API.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace API.Data.Migrations +{ + [DbContext(typeof(TestContext))] + [Migration("20240823185906_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.7"); + + modelBuilder.Entity("API.Entities.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("User"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/API/Data/Migrations/20240823185906_InitialCreate.cs b/API/Data/Migrations/20240823185906_InitialCreate.cs new file mode 100644 index 0000000..7e9dc71 --- /dev/null +++ b/API/Data/Migrations/20240823185906_InitialCreate.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace API.Data.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "User", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + FirstName = table.Column(type: "TEXT", nullable: false), + LastName = table.Column(type: "TEXT", nullable: false), + Email = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_User", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "User"); + } + } +} diff --git a/API/Data/Migrations/TestContextModelSnapshot.cs b/API/Data/Migrations/TestContextModelSnapshot.cs new file mode 100644 index 0000000..977a899 --- /dev/null +++ b/API/Data/Migrations/TestContextModelSnapshot.cs @@ -0,0 +1,43 @@ +// +using API.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace API.Data.Migrations +{ + [DbContext(typeof(TestContext))] + partial class TestContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.7"); + + modelBuilder.Entity("API.Entities.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("User"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/API/Data/TestContext.cs b/API/Data/TestContext.cs new file mode 100644 index 0000000..62840de --- /dev/null +++ b/API/Data/TestContext.cs @@ -0,0 +1,12 @@ +using API.Entities; +using Microsoft.EntityFrameworkCore; + +namespace API.Data +{ + public class TestContext : DbContext + { + public TestContext(DbContextOptions options) : base(options) {} + + public DbSet User { get; set; } + } +} \ No newline at end of file diff --git a/API/Entities/User.cs b/API/Entities/User.cs new file mode 100644 index 0000000..6a3d96b --- /dev/null +++ b/API/Entities/User.cs @@ -0,0 +1,10 @@ +namespace API.Entities +{ + public class User + { + public required int Id { get; set; } + public required string FirstName { get; set; } + public required string LastName { get; set; } + public string? Email { get; set; } + } +} \ No newline at end of file diff --git a/API/Program.cs b/API/Program.cs index 15eacee..65d2156 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -1,3 +1,6 @@ +using API.Data; +using Microsoft.EntityFrameworkCore; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -6,6 +9,9 @@ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddDbContext(options => { + options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")); +}); var app = builder.Build(); diff --git a/API/appsettings.Development.json b/API/appsettings.Development.json index ff66ba6..bd853a4 100644 --- a/API/appsettings.Development.json +++ b/API/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "DefaultConnection": "Data source=test.db" } } diff --git a/API/test.db b/API/test.db new file mode 100644 index 0000000000000000000000000000000000000000..1fd9104b64c51ea02e4ea27f4b16df36e42e2ae9 GIT binary patch literal 20480 zcmeI)O>YuG7zgl~1^Pll*TfKFV&Vinl+X}L)vY&>EV>pJq>IYIY}RE;S7A5ZU7{S@ z@8j8%AH$0$=0q9U@7j z%org=5;Y;}yhQ!@ydxgzrSq#!B~q|yO8!IQsc&TKhx~PGQ=Fhc00Izz00bZa0SG_< z0uX?}zY4VCYngnWc0=pKfCnc7@gB|$_{oU7_Hs8-ZL1YSWk%(BU1dwxZoAfy?}(MT zQ!TchNvG-AXcDs%a6hW?h1J<;D9g5!Oq8tF%Q8(j)E8=-=}m*_oqC;BoBE;Au847# zZ5^1?1+BK$sI+jtp}rH5X3=KTb?drWb!a(* z`9iUUWbU*>*E5Pj&$LM{rm5B%PT#je$8&?1P7r$jc;%q9nC;4IF4DgU896L$dl})1 zIG0priMD*NH?qSw+z-T7m!c+fnzWXAuTbK+J^p3(N?N1PoAk|<&tmSb9FB<_Z6mtb zQle7%ClLn<1Rwwb2tWV=5P$##AOHafKmY=_PT&!ZshLu-^tiZR+S}cK@~rsO)LbWY ztU=Z1R>+k?v7jVHUR3@?#DM|<2tWV=5P$##AOHafKmY;|fWR#kco0)Julo=n`u<;% zzZ3blXiy*k0SG_<0uX=z1Rwwb2tWV=5V&~)52XaTcUP%d;W76O-a86;uWb8Pzdv?@ zf#voJwl};_6C|C^zw(aVw~qbk<7d&7qh=h Date: Fri, 23 Aug 2024 20:00:59 +0000 Subject: [PATCH 3/3] add simple db return --- API/Controllers/UserController.cs | 38 ++++++++++++++++++++ API/Controllers/WeatherForecastController.cs | 1 + API/Data/TestContext.cs | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 API/Controllers/UserController.cs diff --git a/API/Controllers/UserController.cs b/API/Controllers/UserController.cs new file mode 100644 index 0000000..a96fd5f --- /dev/null +++ b/API/Controllers/UserController.cs @@ -0,0 +1,38 @@ +using API.Data; +using API.Entities; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace API.Controllers{ + +[ApiController] +public class UserController : ControllerBase +{ + + private readonly TestContext _dbContext; + public UserController( + TestContext dbContext + ) + { + _dbContext = dbContext; + } + + + [HttpGet] + [Route("test")] + public async Task> GetUsers() + { + return await _dbContext.User.ToListAsync(); + + } + + [HttpGet] + [Route("hey")] + public string Get() + { + return "hey"; + } + +} +} + diff --git a/API/Controllers/WeatherForecastController.cs b/API/Controllers/WeatherForecastController.cs index 71f0254..68b3235 100644 --- a/API/Controllers/WeatherForecastController.cs +++ b/API/Controllers/WeatherForecastController.cs @@ -29,4 +29,5 @@ public IEnumerable Get() }) .ToArray(); } + } diff --git a/API/Data/TestContext.cs b/API/Data/TestContext.cs index 62840de..5655d63 100644 --- a/API/Data/TestContext.cs +++ b/API/Data/TestContext.cs @@ -7,6 +7,6 @@ public class TestContext : DbContext { public TestContext(DbContextOptions options) : base(options) {} - public DbSet User { get; set; } + public required DbSet User { get; set; } } } \ No newline at end of file