From 871e441eb23d9ad3aa1a1e5aec288e11ac1ae6e6 Mon Sep 17 00:00:00 2001 From: mrvoi Date: Wed, 11 May 2022 20:23:30 -0400 Subject: [PATCH] Add project files. --- SuperHeroAPI.sln | 25 +++++++ .../Controllers/SuperHeroController.cs | 75 +++++++++++++++++++ .../Controllers/WeatherForecastController.cs | 33 ++++++++ SuperHeroAPI/Data/DataContext.cs | 11 +++ .../20220511235547_CreateInitial.Designer.cs | 57 ++++++++++++++ .../20220511235547_CreateInitial.cs | 34 +++++++++ .../Migrations/DataContextModelSnapshot.cs | 55 ++++++++++++++ SuperHeroAPI/Program.cs | 32 ++++++++ SuperHeroAPI/Properties/launchSettings.json | 31 ++++++++ SuperHeroAPI/SuperHero.cs | 11 +++ SuperHeroAPI/SuperHeroAPI.csproj | 19 +++++ SuperHeroAPI/WeatherForecast.cs | 13 ++++ SuperHeroAPI/appsettings.Development.json | 8 ++ SuperHeroAPI/appsettings.json | 12 +++ 14 files changed, 416 insertions(+) create mode 100644 SuperHeroAPI.sln create mode 100644 SuperHeroAPI/Controllers/SuperHeroController.cs create mode 100644 SuperHeroAPI/Controllers/WeatherForecastController.cs create mode 100644 SuperHeroAPI/Data/DataContext.cs create mode 100644 SuperHeroAPI/Migrations/20220511235547_CreateInitial.Designer.cs create mode 100644 SuperHeroAPI/Migrations/20220511235547_CreateInitial.cs create mode 100644 SuperHeroAPI/Migrations/DataContextModelSnapshot.cs create mode 100644 SuperHeroAPI/Program.cs create mode 100644 SuperHeroAPI/Properties/launchSettings.json create mode 100644 SuperHeroAPI/SuperHero.cs create mode 100644 SuperHeroAPI/SuperHeroAPI.csproj create mode 100644 SuperHeroAPI/WeatherForecast.cs create mode 100644 SuperHeroAPI/appsettings.Development.json create mode 100644 SuperHeroAPI/appsettings.json diff --git a/SuperHeroAPI.sln b/SuperHeroAPI.sln new file mode 100644 index 0000000..f39b4fc --- /dev/null +++ b/SuperHeroAPI.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32505.173 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuperHeroAPI", "SuperHeroAPI\SuperHeroAPI.csproj", "{BC38AEA6-59BB-4CA3-ACA9-6B820A447964}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BC38AEA6-59BB-4CA3-ACA9-6B820A447964}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0B54823A-7043-4859-8EC0-998B1F4AC010} + EndGlobalSection +EndGlobal diff --git a/SuperHeroAPI/Controllers/SuperHeroController.cs b/SuperHeroAPI/Controllers/SuperHeroController.cs new file mode 100644 index 0000000..7270fb3 --- /dev/null +++ b/SuperHeroAPI/Controllers/SuperHeroController.cs @@ -0,0 +1,75 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace SuperHeroAPI.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class SuperHeroController : ControllerBase + { + private readonly DataContext _context; + + public SuperHeroController(DataContext context) + { + _context = context; + } + + [HttpGet] + public async Task>> Get() + { + return Ok(await _context.SuperHeros.ToListAsync()); + } + + [HttpGet("{id}")] + public async Task>> Get(int id) + { + var hero = await _context.SuperHeros.FindAsync(id); + if (hero == null) + { + return BadRequest("Hero not found."); + } + return Ok(hero); + } + + [HttpPost] + public async Task>> AddHero(SuperHero hero) + { + _context.SuperHeros.Add(hero); + await _context.SaveChangesAsync(); + return Ok(await _context.SuperHeros.ToListAsync()); + } + + [HttpPut] + public async Task>> UpdateHero(SuperHero request) + { + var dbhero = await _context.SuperHeros.FindAsync(request.Id); + if (dbhero == null) + { + return BadRequest("Hero not found."); + } + + dbhero.Name = request.Name; + dbhero.FirstName = request.FirstName; + dbhero.LastName = request.LastName; + dbhero.Place = request.Place; + + await _context.SaveChangesAsync(); + + return Ok(await _context.SuperHeros.ToListAsync()); + } + + [HttpDelete] + public async Task>> Delete(int id) + { + var dbhero = await _context.SuperHeros.FindAsync(id); + if (dbhero == null) + { + return BadRequest("Hero not found."); + } + + _context.SuperHeros.Remove(dbhero); + await _context.SaveChangesAsync(); + return Ok(await _context.SuperHeros.ToListAsync()); + } + } +} diff --git a/SuperHeroAPI/Controllers/WeatherForecastController.cs b/SuperHeroAPI/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..be1636a --- /dev/null +++ b/SuperHeroAPI/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace SuperHeroAPI.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 = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} \ No newline at end of file diff --git a/SuperHeroAPI/Data/DataContext.cs b/SuperHeroAPI/Data/DataContext.cs new file mode 100644 index 0000000..ba9e7cb --- /dev/null +++ b/SuperHeroAPI/Data/DataContext.cs @@ -0,0 +1,11 @@ +using Microsoft.EntityFrameworkCore; + +namespace SuperHeroAPI.Data +{ + public class DataContext :DbContext + { + public DataContext(DbContextOptions options) : base(options) { } + + public DbSet SuperHeros { get; set; } + } +} diff --git a/SuperHeroAPI/Migrations/20220511235547_CreateInitial.Designer.cs b/SuperHeroAPI/Migrations/20220511235547_CreateInitial.Designer.cs new file mode 100644 index 0000000..c022b01 --- /dev/null +++ b/SuperHeroAPI/Migrations/20220511235547_CreateInitial.Designer.cs @@ -0,0 +1,57 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SuperHeroAPI.Data; + +#nullable disable + +namespace SuperHeroAPI.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20220511235547_CreateInitial")] + partial class CreateInitial + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("SuperHeroAPI.SuperHero", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Place") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("SuperHeros"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SuperHeroAPI/Migrations/20220511235547_CreateInitial.cs b/SuperHeroAPI/Migrations/20220511235547_CreateInitial.cs new file mode 100644 index 0000000..ff7a25b --- /dev/null +++ b/SuperHeroAPI/Migrations/20220511235547_CreateInitial.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SuperHeroAPI.Migrations +{ + public partial class CreateInitial : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "SuperHeros", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + FirstName = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false), + Place = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SuperHeros", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "SuperHeros"); + } + } +} diff --git a/SuperHeroAPI/Migrations/DataContextModelSnapshot.cs b/SuperHeroAPI/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 0000000..f62d1a3 --- /dev/null +++ b/SuperHeroAPI/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,55 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SuperHeroAPI.Data; + +#nullable disable + +namespace SuperHeroAPI.Migrations +{ + [DbContext(typeof(DataContext))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("SuperHeroAPI.SuperHero", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Place") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("SuperHeros"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SuperHeroAPI/Program.cs b/SuperHeroAPI/Program.cs new file mode 100644 index 0000000..e4125fc --- /dev/null +++ b/SuperHeroAPI/Program.cs @@ -0,0 +1,32 @@ +global using SuperHeroAPI.Data; +global using Microsoft.EntityFrameworkCore; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +builder.Services.AddDbContext(options => +{ + options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); +}); +// 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/SuperHeroAPI/Properties/launchSettings.json b/SuperHeroAPI/Properties/launchSettings.json new file mode 100644 index 0000000..a23b221 --- /dev/null +++ b/SuperHeroAPI/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:12843", + "sslPort": 44318 + } + }, + "profiles": { + "SuperHeroAPI": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7054;http://localhost:5054", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/SuperHeroAPI/SuperHero.cs b/SuperHeroAPI/SuperHero.cs new file mode 100644 index 0000000..8311fa0 --- /dev/null +++ b/SuperHeroAPI/SuperHero.cs @@ -0,0 +1,11 @@ +namespace SuperHeroAPI +{ + public class SuperHero + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public string FirstName { get; set; } = string.Empty; + public string LastName { get; set; } = string.Empty; + public string Place { get; set; } = string.Empty; + } +} diff --git a/SuperHeroAPI/SuperHeroAPI.csproj b/SuperHeroAPI/SuperHeroAPI.csproj new file mode 100644 index 0000000..4a39276 --- /dev/null +++ b/SuperHeroAPI/SuperHeroAPI.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + diff --git a/SuperHeroAPI/WeatherForecast.cs b/SuperHeroAPI/WeatherForecast.cs new file mode 100644 index 0000000..330c0bb --- /dev/null +++ b/SuperHeroAPI/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace SuperHeroAPI +{ + 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; } + } +} \ No newline at end of file diff --git a/SuperHeroAPI/appsettings.Development.json b/SuperHeroAPI/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/SuperHeroAPI/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/SuperHeroAPI/appsettings.json b/SuperHeroAPI/appsettings.json new file mode 100644 index 0000000..7734da1 --- /dev/null +++ b/SuperHeroAPI/appsettings.json @@ -0,0 +1,12 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=superherodb;Trusted_Connection=True;" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}