From 80b4ce1ad80fc0982677354233cd48b5ac8f4d63 Mon Sep 17 00:00:00 2001 From: Jeff Jumper Date: Wed, 22 Nov 2023 15:19:43 -0500 Subject: [PATCH] Initial checkin --- Controllers/TodoItemsController.cs | 107 ++++++++++++++++++ Dockerfile | 29 +++++ .../20231122161141_InitialCreate.Designer.cs | 41 +++++++ Migrations/20231122161141_InitialCreate.cs | 35 ++++++ Migrations/TodoContextModelSnapshot.cs | 38 +++++++ Models/TodoContext.cs | 13 +++ Models/TodoItem.cs | 8 ++ Program.cs | 36 ++++++ Properties/launchSettings.json | 41 +++++++ TodoApi.csproj | 32 ++++++ TodoApi.sln | 25 ++++ appsettings.Development.json | 8 ++ appsettings.json | 12 ++ data/todo.db | Bin 0 -> 20480 bytes data/todo.db-shm | Bin 0 -> 32768 bytes data/todo.db-wal | 0 16 files changed, 425 insertions(+) create mode 100644 Controllers/TodoItemsController.cs create mode 100644 Dockerfile create mode 100644 Migrations/20231122161141_InitialCreate.Designer.cs create mode 100644 Migrations/20231122161141_InitialCreate.cs create mode 100644 Migrations/TodoContextModelSnapshot.cs create mode 100644 Models/TodoContext.cs create mode 100644 Models/TodoItem.cs create mode 100644 Program.cs create mode 100644 Properties/launchSettings.json create mode 100644 TodoApi.csproj create mode 100644 TodoApi.sln create mode 100644 appsettings.Development.json create mode 100644 appsettings.json create mode 100644 data/todo.db create mode 100644 data/todo.db-shm create mode 100644 data/todo.db-wal diff --git a/Controllers/TodoItemsController.cs b/Controllers/TodoItemsController.cs new file mode 100644 index 0000000..3f2310c --- /dev/null +++ b/Controllers/TodoItemsController.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using TodoApi.Models; + +namespace TodoApi.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TodoItemsController : ControllerBase + { + private readonly TodoContext _context; + + public TodoItemsController(TodoContext context) + { + _context = context; + } + + // GET: api/TodoItems + [HttpGet] + public async Task>> GetTodoItems() + { + return await _context.TodoItems.ToListAsync(); + } + + // GET: api/TodoItems/5 + [HttpGet("{id}")] + public async Task> GetTodoItem(long id) + { + var todoItem = await _context.TodoItems.FindAsync(id); + + if (todoItem == null) + { + return NotFound(); + } + + return todoItem; + } + + // PUT: api/TodoItems/5 + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 + [HttpPut("{id}")] + public async Task PutTodoItem(long id, TodoItem todoItem) + { + if (id != todoItem.Id) + { + return BadRequest(); + } + + _context.Entry(todoItem).State = EntityState.Modified; + + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!TodoItemExists(id)) + { + return NotFound(); + } + else + { + throw; + } + } + + return NoContent(); + } + + // POST: api/TodoItems + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 + [HttpPost] + public async Task> PostTodoItem(TodoItem todoItem) + { + _context.TodoItems.Add(todoItem); + await _context.SaveChangesAsync(); + + return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem); + } + + // DELETE: api/TodoItems/5 + [HttpDelete("{id}")] + public async Task DeleteTodoItem(long id) + { + var todoItem = await _context.TodoItems.FindAsync(id); + if (todoItem == null) + { + return NotFound(); + } + + _context.TodoItems.Remove(todoItem); + await _context.SaveChangesAsync(); + + return NoContent(); + } + + private bool TodoItemExists(long id) + { + return _context.TodoItems.Any(e => e.Id == id); + } + } +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bca43cc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG TARGETARCH +WORKDIR /source + +# copy csproj and restore as distinct layers +#COPY *.csproj . +#RUN dotnet restore -a $TARGETARCH +#RUN dotnet restore -r linux-$TARGETARCH + +# copy and publish app and libraries +COPY . . +#RUN dotnet publish TodoApi.csproj -c Release -a $TARGETARCH --self-contained false --no-restore -o /app +RUN dotnet publish TodoApi.csproj -r linux-$TARGETARCH --self-contained false -o /app + +# Build database migration tool +RUN dotnet tool install --global dotnet-ef --version 8.0.0 +ENV PATH="${PATH}:/root/.dotnet/tools" +RUN dotnet ef migrations bundle --self-contained -r linux-$TARGETARCH -o /app/migratedb +#RUN dotnet ef migrations bundle --no-build -o /app/migratedb --verbose + +# final stage/image +FROM mcr.microsoft.com/dotnet/aspnet:8.0 +WORKDIR /app +COPY --from=build /app . + +EXPOSE 8080 + +ENV DOTNET_EnableDiagnostics=0 +ENTRYPOINT ["./TodoApi"] \ No newline at end of file diff --git a/Migrations/20231122161141_InitialCreate.Designer.cs b/Migrations/20231122161141_InitialCreate.Designer.cs new file mode 100644 index 0000000..12f2992 --- /dev/null +++ b/Migrations/20231122161141_InitialCreate.Designer.cs @@ -0,0 +1,41 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TodoApi.Models; + +#nullable disable + +namespace TodoApi.Migrations +{ + [DbContext(typeof(TodoContext))] + [Migration("20231122161141_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.0"); + + modelBuilder.Entity("TodoApi.Models.TodoItem", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("IsComplete") + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("TodoItems"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20231122161141_InitialCreate.cs b/Migrations/20231122161141_InitialCreate.cs new file mode 100644 index 0000000..0637fcc --- /dev/null +++ b/Migrations/20231122161141_InitialCreate.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TodoApi.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "TodoItems", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: true), + IsComplete = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TodoItems", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "TodoItems"); + } + } +} diff --git a/Migrations/TodoContextModelSnapshot.cs b/Migrations/TodoContextModelSnapshot.cs new file mode 100644 index 0000000..75394b1 --- /dev/null +++ b/Migrations/TodoContextModelSnapshot.cs @@ -0,0 +1,38 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TodoApi.Models; + +#nullable disable + +namespace TodoApi.Migrations +{ + [DbContext(typeof(TodoContext))] + partial class TodoContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.0"); + + modelBuilder.Entity("TodoApi.Models.TodoItem", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("IsComplete") + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("TodoItems"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Models/TodoContext.cs b/Models/TodoContext.cs new file mode 100644 index 0000000..1414462 --- /dev/null +++ b/Models/TodoContext.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; + +namespace TodoApi.Models; + +public class TodoContext : DbContext +{ + public TodoContext(DbContextOptions options) + : base(options) + { + } + + public DbSet TodoItems { get; set; } = null!; +} \ No newline at end of file diff --git a/Models/TodoItem.cs b/Models/TodoItem.cs new file mode 100644 index 0000000..84ef49a --- /dev/null +++ b/Models/TodoItem.cs @@ -0,0 +1,8 @@ +namespace TodoApi.Models; + +public class TodoItem +{ + public long Id { get; set; } + public string? Name { get; set; } + public bool IsComplete { get; set; } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..767b2a7 --- /dev/null +++ b/Program.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Data.Sqlite; +using TodoApi.Models; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddControllers(); + +var connectionString = new SqliteConnectionStringBuilder(builder.Configuration.GetConnectionString("TodoDatabase")) +{ + Mode = SqliteOpenMode.ReadWriteCreate +}.ToString(); +builder.Services.AddDbContext(options => + options.UseSqlite(connectionString)); + +// 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/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..623eafe --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:33447", + "sslPort": 44382 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5123", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7291;http://localhost:5123", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/TodoApi.csproj b/TodoApi.csproj new file mode 100644 index 0000000..db99554 --- /dev/null +++ b/TodoApi.csproj @@ -0,0 +1,32 @@ + + + + net8.0 + enable + enable + true + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + Always + + + + diff --git a/TodoApi.sln b/TodoApi.sln new file mode 100644 index 0000000..9704df0 --- /dev/null +++ b/TodoApi.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodoApi", "TodoApi.csproj", "{E6DC7A4C-635A-490E-9E1F-3FC7D9E03363}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E6DC7A4C-635A-490E-9E1F-3FC7D9E03363}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E6DC7A4C-635A-490E-9E1F-3FC7D9E03363}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E6DC7A4C-635A-490E-9E1F-3FC7D9E03363}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E6DC7A4C-635A-490E-9E1F-3FC7D9E03363}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3B9AE22B-9B69-44D5-8EDE-CBA3B4F26845} + EndGlobalSection +EndGlobal diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..5ae76e4 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,12 @@ +{ + "ConnectionStrings": { + "TodoDatabase": "Data Source=data/todo.db" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/data/todo.db b/data/todo.db new file mode 100644 index 0000000000000000000000000000000000000000..a54cb256a71a133d3f997ccd5eed6bc76e6e964b GIT binary patch literal 20480 zcmeI)J#X4T7zgk>Lz03@oXzUQ$?6c42w@VHjwR^YS^-1BRAs0vH(Vl1U^n(5wPW&q z`W5;ux^(K?G0M=f$4d}Frcy_w|H&4259jCZ`QgPN(R|`J1F_&?Z9p;&8rad&7?YR66?=q+DGN<2cvPRqKx!t;o zGqZE