Setup ef core tools and sqlite database with some entities

This commit is contained in:
2019-04-20 00:39:07 +00:00
parent 4e5142d5ec
commit 0f9a598fae
17 changed files with 424 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
@@ -11,8 +11,9 @@
<ItemGroup>
<PackageReference Include="MediatR" Version="6.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BrightGlimmer.Cqrs.Queries;
using MediatR;
using Microsoft.AspNetCore.Mvc;

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BrightGlimmer.Data.Repositories; /* REMOVE LATER */
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace BrightGlimmer.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
// private readonly IMediator mediator;
private readonly StudentRepository studentRepository;
public StudentController(StudentRepository studentRepository)
{
this.studentRepository = studentRepository;
}
[HttpGet]
public IActionResult GetAll()
{
var customers = studentRepository.GetAll();
if (customers == null)
{
return NotFound();
}
return new ObjectResult(customers);
}
[HttpGet("createstudent")]
public IActionResult CreateStudent()
{
var created = studentRepository.Create(new Data.Domain.Student
{
Id = Guid.NewGuid(),
FirstName = "Giovani",
LastName = "Rodriguez",
Email = "giovaniluisrodriguez@gmail.com",
Phones = new List<Data.Domain.Phone>
{
new Data.Domain.Phone
{
Id = Guid.NewGuid(),
AreaCode = 305,
Number = 8888888,
Type = Data.Domain.PhoneType.HOMEPHONE
}
}
});
return new ObjectResult(created);
}
}
}

View File

@@ -12,7 +12,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@@ -20,7 +20,7 @@
"BrightGlimmer.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"launchUrl": "",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"

View File

@@ -3,11 +3,13 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BrightGlimmer.Data;
using BrightGlimmer.Data.Repositories; /* REMOVE LATER */
using MediatR;
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;
@@ -30,6 +32,9 @@ namespace BrightGlimmer.Api
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMediatR();
services.AddMediatR(typeof(Cqrs.Cqrs).Assembly); // Registers handlers in Cqrs project
services.AddScoped<StudentRepository, StudentRepository>(); /* REMOVE LATER */
services.AddDbContext<SqliteDatabaseContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<SqliteDatabaseContext>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -46,6 +51,13 @@ namespace BrightGlimmer.Api
app.UseHttpsRedirection();
app.UseMvc();
// Makes sure that the database is in fact created
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<SqliteDatabaseContext>();
context.Database.EnsureCreated();
}
}
}
}

View File

@@ -1,8 +1,20 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"AllowedHosts": "*"
}
"ConnectionStrings": {
"DefaultConnection": "Filename=../BrightGlimmer.Data/bright_glimmer.db"
},
"amqp": {
"username": "guest",
"password": "guest",
"hostname": "localhost",
"uri": "amqp://localhost:5672/",
"virtualhost": "/"
}
}

View File

@@ -9,4 +9,14 @@
<Folder Include="Repositories\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="mongocsharpdriver" Version="2.8.0" />
<PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public class Course
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Code { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public class Phone
{
public Guid Id { get; set; }
public PhoneType Type { get; set; }
public int AreaCode { get; set; }
public int Number { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public enum PhoneType
{
HOMEPHONE, CELLPHONE, WORKPHONE
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data.Domain
{
public class Student
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<Phone> Phones { get; set; }
}
}

View File

@@ -0,0 +1,68 @@
// <auto-generated />
using System;
using BrightGlimmer.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace BrightGlimmer.Data.Migrations
{
[DbContext(typeof(SqliteDatabaseContext))]
[Migration("20190420001348_CreateDatabase")]
partial class CreateDatabase
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AreaCode");
b.Property<int>("Number");
b.Property<Guid?>("StudentId");
b.Property<int>("Type");
b.HasKey("Id");
b.HasIndex("StudentId");
b.ToTable("Phone");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Student", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Email");
b.Property<string>("FirstName");
b.Property<string>("LastName");
b.Property<string>("MiddleName");
b.HasKey("Id");
b.ToTable("Students");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.HasOne("BrightGlimmer.Data.Domain.Student")
.WithMany("Phones")
.HasForeignKey("StudentId");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,61 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace BrightGlimmer.Data.Migrations
{
public partial class CreateDatabase : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Students",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
FirstName = table.Column<string>(nullable: true),
MiddleName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
Email = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Students", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Phone",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
Type = table.Column<int>(nullable: false),
AreaCode = table.Column<int>(nullable: false),
Number = table.Column<int>(nullable: false),
StudentId = table.Column<Guid>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Phone", x => x.Id);
table.ForeignKey(
name: "FK_Phone_Students_StudentId",
column: x => x.StudentId,
principalTable: "Students",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Phone_StudentId",
table: "Phone",
column: "StudentId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Phone");
migrationBuilder.DropTable(
name: "Students");
}
}
}

View File

@@ -0,0 +1,66 @@
// <auto-generated />
using System;
using BrightGlimmer.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace BrightGlimmer.Data.Migrations
{
[DbContext(typeof(SqliteDatabaseContext))]
partial class SqliteDatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AreaCode");
b.Property<int>("Number");
b.Property<Guid?>("StudentId");
b.Property<int>("Type");
b.HasKey("Id");
b.HasIndex("StudentId");
b.ToTable("Phone");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Student", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Email");
b.Property<string>("FirstName");
b.Property<string>("LastName");
b.Property<string>("MiddleName");
b.HasKey("Id");
b.ToTable("Students");
});
modelBuilder.Entity("BrightGlimmer.Data.Domain.Phone", b =>
{
b.HasOne("BrightGlimmer.Data.Domain.Student")
.WithMany("Phones")
.HasForeignKey("StudentId");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,47 @@
using BrightGlimmer.Data.Domain;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BrightGlimmer.Data.Repositories
{
public class StudentRepository
{
private readonly SqliteDatabaseContext context;
public StudentRepository(SqliteDatabaseContext context)
{
this.context = context;
}
public Student Create(Student student)
{
EntityEntry<Student> entry = context.Students.Add(student);
context.SaveChanges();
return entry.Entity;
}
public void Update(Student student)
{
context.SaveChanges();
}
public void Remove(Guid id)
{
context.Students.Remove(GetById(id));
context.SaveChanges();
}
public IQueryable<Student> GetAll()
{
return context.Students;
}
public Student GetById(Guid id)
{
return context.Students.Find(id);
}
}
}

View File

@@ -0,0 +1,24 @@
using BrightGlimmer.Data.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Data
{
public class SqliteDatabaseContext : DbContext
{
public SqliteDatabaseContext(DbContextOptions<SqliteDatabaseContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasMany(x => x.Phones);
}
public DbSet<Student> Students { get; set; }
}
}

Binary file not shown.