From d08716cb962c7563186603aff2072aff316fbfbe Mon Sep 17 00:00:00 2001 From: Jacob Kirkbride Date: Sat, 28 Sep 2019 16:46:35 -0400 Subject: [PATCH] Gets an Ok! --- .../Conferences.Api/Conferences.Api.csproj | 3 +- .../Controllers/ConferencesController.cs | 20 +++++ .../Controllers/ValuesController.cs | 45 ----------- .../Conferences.Api/Domain/Conference.cs | 35 +++++++++ .../Domain/ConferenceDataContext.cs | 17 +++++ ...0190928183702_InitialMigration.Designer.cs | 75 +++++++++++++++++++ .../20190928183702_InitialMigration.cs | 65 ++++++++++++++++ .../ConferenceDataContextModelSnapshot.cs | 73 ++++++++++++++++++ Conferences.Api/Conferences.Api/Startup.cs | 17 ++++- .../Conferences.Api/appsettings.json | 3 + .../ConferencesResource/GetConferences.cs | 19 +++++ .../HttpBase.cs | 32 ++++++++ .../UnitTest1.cs | 14 ---- .../XConferences.Api.IntegrationTests.csproj | 1 + 14 files changed, 358 insertions(+), 61 deletions(-) create mode 100644 Conferences.Api/Conferences.Api/Controllers/ConferencesController.cs delete mode 100644 Conferences.Api/Conferences.Api/Controllers/ValuesController.cs create mode 100644 Conferences.Api/Conferences.Api/Domain/Conference.cs create mode 100644 Conferences.Api/Conferences.Api/Domain/ConferenceDataContext.cs create mode 100644 Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.Designer.cs create mode 100644 Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.cs create mode 100644 Conferences.Api/Conferences.Api/Migrations/ConferenceDataContextModelSnapshot.cs create mode 100644 Conferences.Api/XConferences.Api.IntegrationTests/ConferencesResource/GetConferences.cs create mode 100644 Conferences.Api/XConferences.Api.IntegrationTests/HttpBase.cs delete mode 100644 Conferences.Api/XConferences.Api.IntegrationTests/UnitTest1.cs diff --git a/Conferences.Api/Conferences.Api/Conferences.Api.csproj b/Conferences.Api/Conferences.Api/Conferences.Api.csproj index f7140df..a523653 100644 --- a/Conferences.Api/Conferences.Api/Conferences.Api.csproj +++ b/Conferences.Api/Conferences.Api/Conferences.Api.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.2 @@ -11,6 +11,7 @@ + diff --git a/Conferences.Api/Conferences.Api/Controllers/ConferencesController.cs b/Conferences.Api/Conferences.Api/Controllers/ConferencesController.cs new file mode 100644 index 0000000..e452f94 --- /dev/null +++ b/Conferences.Api/Conferences.Api/Controllers/ConferencesController.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Conferences.Api.Controllers +{ + [Route("api/v1/[controller]")] + [ApiController] + public class ConferencesController : ControllerBase + { + [HttpGet("")] + public async Task GetConferences() + { + return Ok(); + } + } +} \ No newline at end of file diff --git a/Conferences.Api/Conferences.Api/Controllers/ValuesController.cs b/Conferences.Api/Conferences.Api/Controllers/ValuesController.cs deleted file mode 100644 index 8956748..0000000 --- a/Conferences.Api/Conferences.Api/Controllers/ValuesController.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace Conferences.Api.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class ValuesController : ControllerBase - { - // GET api/values - [HttpGet] - public ActionResult> Get() - { - return new string[] { "value1", "value2" }; - } - - // GET api/values/5 - [HttpGet("{id}")] - public ActionResult Get(int id) - { - return "value"; - } - - // POST api/values - [HttpPost] - public void Post([FromBody] string value) - { - } - - // PUT api/values/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } - - // DELETE api/values/5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } -} diff --git a/Conferences.Api/Conferences.Api/Domain/Conference.cs b/Conferences.Api/Conferences.Api/Domain/Conference.cs new file mode 100644 index 0000000..e2e6bb0 --- /dev/null +++ b/Conferences.Api/Conferences.Api/Domain/Conference.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Conferences.Api.Domain +{ + public class Conference + { + public int Id { get; set; } + + public string Name { get; set; } + + public string State { get; set; } + + public string City { get; set; } + + public DateTime StartDate { get; set; } + + public DateTime EndDate { get; set; } + + public bool Attending { get; set; } + + public bool Speaking { get; set; } + + public Topic FocusTopic { get; set; } + } + + public class Topic + { + public int Id { get; set; } + + public string Name { get; set; } + } +} diff --git a/Conferences.Api/Conferences.Api/Domain/ConferenceDataContext.cs b/Conferences.Api/Conferences.Api/Domain/ConferenceDataContext.cs new file mode 100644 index 0000000..df22f9e --- /dev/null +++ b/Conferences.Api/Conferences.Api/Domain/ConferenceDataContext.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Conferences.Api.Domain +{ + public class ConferenceDataContext : DbContext + { + public ConferenceDataContext(DbContextOptions ctx) : base(ctx) { } + + public virtual DbSet Conferences { get; set; } + + public virtual DbSet Topics { get; set; } + } +} diff --git a/Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.Designer.cs b/Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.Designer.cs new file mode 100644 index 0000000..df05741 --- /dev/null +++ b/Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.Designer.cs @@ -0,0 +1,75 @@ +// +using System; +using Conferences.Api.Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace Conferences.Api.Migrations +{ + [DbContext(typeof(ConferenceDataContext))] + [Migration("20190928183702_InitialMigration")] + partial class InitialMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "2.2.6-servicing-10079") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("Conferences.Api.Domain.Conference", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Attending"); + + b.Property("City"); + + b.Property("EndDate"); + + b.Property("FocusTopicId"); + + b.Property("Name"); + + b.Property("Speaking"); + + b.Property("StartDate"); + + b.Property("State"); + + b.HasKey("Id"); + + b.HasIndex("FocusTopicId"); + + b.ToTable("Conferences"); + }); + + modelBuilder.Entity("Conferences.Api.Domain.Topic", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Name"); + + b.HasKey("Id"); + + b.ToTable("Topics"); + }); + + modelBuilder.Entity("Conferences.Api.Domain.Conference", b => + { + b.HasOne("Conferences.Api.Domain.Topic", "FocusTopic") + .WithMany() + .HasForeignKey("FocusTopicId"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.cs b/Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.cs new file mode 100644 index 0000000..3937522 --- /dev/null +++ b/Conferences.Api/Conferences.Api/Migrations/20190928183702_InitialMigration.cs @@ -0,0 +1,65 @@ +using System; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Conferences.Api.Migrations +{ + public partial class InitialMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Topics", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), + Name = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Topics", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Conferences", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), + Name = table.Column(nullable: true), + State = table.Column(nullable: true), + City = table.Column(nullable: true), + StartDate = table.Column(nullable: false), + EndDate = table.Column(nullable: false), + Attending = table.Column(nullable: false), + Speaking = table.Column(nullable: false), + FocusTopicId = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Conferences", x => x.Id); + table.ForeignKey( + name: "FK_Conferences_Topics_FocusTopicId", + column: x => x.FocusTopicId, + principalTable: "Topics", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_Conferences_FocusTopicId", + table: "Conferences", + column: "FocusTopicId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Conferences"); + + migrationBuilder.DropTable( + name: "Topics"); + } + } +} diff --git a/Conferences.Api/Conferences.Api/Migrations/ConferenceDataContextModelSnapshot.cs b/Conferences.Api/Conferences.Api/Migrations/ConferenceDataContextModelSnapshot.cs new file mode 100644 index 0000000..199a244 --- /dev/null +++ b/Conferences.Api/Conferences.Api/Migrations/ConferenceDataContextModelSnapshot.cs @@ -0,0 +1,73 @@ +// +using System; +using Conferences.Api.Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace Conferences.Api.Migrations +{ + [DbContext(typeof(ConferenceDataContext))] + partial class ConferenceDataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "2.2.6-servicing-10079") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("Conferences.Api.Domain.Conference", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Attending"); + + b.Property("City"); + + b.Property("EndDate"); + + b.Property("FocusTopicId"); + + b.Property("Name"); + + b.Property("Speaking"); + + b.Property("StartDate"); + + b.Property("State"); + + b.HasKey("Id"); + + b.HasIndex("FocusTopicId"); + + b.ToTable("Conferences"); + }); + + modelBuilder.Entity("Conferences.Api.Domain.Topic", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Name"); + + b.HasKey("Id"); + + b.ToTable("Topics"); + }); + + modelBuilder.Entity("Conferences.Api.Domain.Conference", b => + { + b.HasOne("Conferences.Api.Domain.Topic", "FocusTopic") + .WithMany() + .HasForeignKey("FocusTopicId"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Conferences.Api/Conferences.Api/Startup.cs b/Conferences.Api/Conferences.Api/Startup.cs index a2de3db..9b8891a 100644 --- a/Conferences.Api/Conferences.Api/Startup.cs +++ b/Conferences.Api/Conferences.Api/Startup.cs @@ -2,10 +2,12 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Conferences.Api.Domain; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -15,17 +17,30 @@ namespace Conferences.Api { public class Startup { - public Startup(IConfiguration configuration) + public Startup(IConfiguration configuration, IHostingEnvironment environment) { Configuration = configuration; + Environment = environment; } public IConfiguration Configuration { get; } + public IHostingEnvironment Environment { get; set; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + + if (Environment.IsEnvironment("testing")) + { + services.AddDbContext(options => options.UseInMemoryDatabase("test")); + } + else + { + services.AddDbContext(options => + options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + } + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/Conferences.Api/Conferences.Api/appsettings.json b/Conferences.Api/Conferences.Api/appsettings.json index def9159..ed43967 100644 --- a/Conferences.Api/Conferences.Api/appsettings.json +++ b/Conferences.Api/Conferences.Api/appsettings.json @@ -1,4 +1,7 @@ { + "ConnectionStrings": { + "DefaultConnection": "Server=DESKTOP-VMLTKJ8\\SQLEXPRESS;Database=Conference;Trusted_Connection=True;MultipleActiveResultSets=true;" + }, "Logging": { "LogLevel": { "Default": "Warning" diff --git a/Conferences.Api/XConferences.Api.IntegrationTests/ConferencesResource/GetConferences.cs b/Conferences.Api/XConferences.Api.IntegrationTests/ConferencesResource/GetConferences.cs new file mode 100644 index 0000000..7b2a1af --- /dev/null +++ b/Conferences.Api/XConferences.Api.IntegrationTests/ConferencesResource/GetConferences.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace XConferences.Api.IntegrationTests.ConferencesResource +{ + public class GetConferences : HttpBase + { + [Fact] + public async Task GetsAnOk() + { + var response = await Client.GetAsync("api/v1/conferences"); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + } +} diff --git a/Conferences.Api/XConferences.Api.IntegrationTests/HttpBase.cs b/Conferences.Api/XConferences.Api.IntegrationTests/HttpBase.cs new file mode 100644 index 0000000..3f5937d --- /dev/null +++ b/Conferences.Api/XConferences.Api.IntegrationTests/HttpBase.cs @@ -0,0 +1,32 @@ +using Conferences.Api; +using Conferences.Api.Domain; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; + +namespace XConferences.Api.IntegrationTests +{ + public class HttpBase + { + protected readonly HttpClient Client; + protected readonly ConferenceDataContext Context; + + public HttpBase() + { + var builder = new WebHostBuilder() + .ConfigureAppConfiguration((hosting, config) => + { + + }) + .UseEnvironment("testing") + .UseStartup(); + + var server = new TestServer(builder); + Client = server.CreateClient(); + Context = server.Host.Services.GetService(typeof(ConferenceDataContext)) as ConferenceDataContext; + } + } +} diff --git a/Conferences.Api/XConferences.Api.IntegrationTests/UnitTest1.cs b/Conferences.Api/XConferences.Api.IntegrationTests/UnitTest1.cs deleted file mode 100644 index 4ebf9ab..0000000 --- a/Conferences.Api/XConferences.Api.IntegrationTests/UnitTest1.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using Xunit; - -namespace XConferences.Api.IntegrationTests -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - - } - } -} diff --git a/Conferences.Api/XConferences.Api.IntegrationTests/XConferences.Api.IntegrationTests.csproj b/Conferences.Api/XConferences.Api.IntegrationTests/XConferences.Api.IntegrationTests.csproj index 4a83a4a..8a9b666 100644 --- a/Conferences.Api/XConferences.Api.IntegrationTests/XConferences.Api.IntegrationTests.csproj +++ b/Conferences.Api/XConferences.Api.IntegrationTests/XConferences.Api.IntegrationTests.csproj @@ -9,6 +9,7 @@ +