diff --git a/BrightGlimmer.Api/BrightGlimmer.Api.csproj b/BrightGlimmer.Api/BrightGlimmer.Api.csproj index cde3763..1707ad7 100644 --- a/BrightGlimmer.Api/BrightGlimmer.Api.csproj +++ b/BrightGlimmer.Api/BrightGlimmer.Api.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.1 + netcoreapp2.2 @@ -11,8 +11,9 @@ + - + diff --git a/BrightGlimmer.Api/Controllers/CourseController.cs b/BrightGlimmer.Api/Controllers/CourseController.cs index 831aff2..c010e33 100644 --- a/BrightGlimmer.Api/Controllers/CourseController.cs +++ b/BrightGlimmer.Api/Controllers/CourseController.cs @@ -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; diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs new file mode 100644 index 0000000..0c0e89c --- /dev/null +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -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 + { + new Data.Domain.Phone + { + Id = Guid.NewGuid(), + AreaCode = 305, + Number = 8888888, + Type = Data.Domain.PhoneType.HOMEPHONE + } + } + }); + + return new ObjectResult(created); + } + } +} \ No newline at end of file diff --git a/BrightGlimmer.Api/Properties/launchSettings.json b/BrightGlimmer.Api/Properties/launchSettings.json index 35c5320..e06e184 100644 --- a/BrightGlimmer.Api/Properties/launchSettings.json +++ b/BrightGlimmer.Api/Properties/launchSettings.json @@ -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" diff --git a/BrightGlimmer.Api/Startup.cs b/BrightGlimmer.Api/Startup.cs index 2e982a5..c846e07 100644 --- a/BrightGlimmer.Api/Startup.cs +++ b/BrightGlimmer.Api/Startup.cs @@ -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(); /* REMOVE LATER */ + services.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); + services.AddTransient(); } // 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().CreateScope()) + { + var context = serviceScope.ServiceProvider.GetRequiredService(); + context.Database.EnsureCreated(); + } } } } \ No newline at end of file diff --git a/BrightGlimmer.Api/appsettings.json b/BrightGlimmer.Api/appsettings.json index def9159..28234fd 100644 --- a/BrightGlimmer.Api/appsettings.json +++ b/BrightGlimmer.Api/appsettings.json @@ -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": "/" + } +} \ No newline at end of file diff --git a/BrightGlimmer.Data/BrightGlimmer.Data.csproj b/BrightGlimmer.Data/BrightGlimmer.Data.csproj index 43edd53..5892ee8 100644 --- a/BrightGlimmer.Data/BrightGlimmer.Data.csproj +++ b/BrightGlimmer.Data/BrightGlimmer.Data.csproj @@ -9,4 +9,14 @@ + + + + + + + + + + diff --git a/BrightGlimmer.Data/Domain/Course.cs b/BrightGlimmer.Data/Domain/Course.cs new file mode 100644 index 0000000..01f670b --- /dev/null +++ b/BrightGlimmer.Data/Domain/Course.cs @@ -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; } + + } +} diff --git a/BrightGlimmer.Data/Domain/Phone.cs b/BrightGlimmer.Data/Domain/Phone.cs new file mode 100644 index 0000000..e3858a7 --- /dev/null +++ b/BrightGlimmer.Data/Domain/Phone.cs @@ -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; } + } +} diff --git a/BrightGlimmer.Data/Domain/PhoneType.cs b/BrightGlimmer.Data/Domain/PhoneType.cs new file mode 100644 index 0000000..7b14a40 --- /dev/null +++ b/BrightGlimmer.Data/Domain/PhoneType.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BrightGlimmer.Data.Domain +{ + public enum PhoneType + { + HOMEPHONE, CELLPHONE, WORKPHONE + } +} diff --git a/BrightGlimmer.Data/Domain/Student.cs b/BrightGlimmer.Data/Domain/Student.cs new file mode 100644 index 0000000..7e3a2b1 --- /dev/null +++ b/BrightGlimmer.Data/Domain/Student.cs @@ -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 Phones { get; set; } + } +} diff --git a/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.Designer.cs b/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.Designer.cs new file mode 100644 index 0000000..6e33439 --- /dev/null +++ b/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.Designer.cs @@ -0,0 +1,68 @@ +// +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("Id") + .ValueGeneratedOnAdd(); + + b.Property("AreaCode"); + + b.Property("Number"); + + b.Property("StudentId"); + + b.Property("Type"); + + b.HasKey("Id"); + + b.HasIndex("StudentId"); + + b.ToTable("Phone"); + }); + + modelBuilder.Entity("BrightGlimmer.Data.Domain.Student", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Email"); + + b.Property("FirstName"); + + b.Property("LastName"); + + b.Property("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 + } + } +} diff --git a/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.cs b/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.cs new file mode 100644 index 0000000..0a215d0 --- /dev/null +++ b/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.cs @@ -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(nullable: false), + FirstName = table.Column(nullable: true), + MiddleName = table.Column(nullable: true), + LastName = table.Column(nullable: true), + Email = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Students", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Phone", + columns: table => new + { + Id = table.Column(nullable: false), + Type = table.Column(nullable: false), + AreaCode = table.Column(nullable: false), + Number = table.Column(nullable: false), + StudentId = table.Column(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"); + } + } +} diff --git a/BrightGlimmer.Data/Migrations/SqliteDatabaseContextModelSnapshot.cs b/BrightGlimmer.Data/Migrations/SqliteDatabaseContextModelSnapshot.cs new file mode 100644 index 0000000..a586045 --- /dev/null +++ b/BrightGlimmer.Data/Migrations/SqliteDatabaseContextModelSnapshot.cs @@ -0,0 +1,66 @@ +// +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("Id") + .ValueGeneratedOnAdd(); + + b.Property("AreaCode"); + + b.Property("Number"); + + b.Property("StudentId"); + + b.Property("Type"); + + b.HasKey("Id"); + + b.HasIndex("StudentId"); + + b.ToTable("Phone"); + }); + + modelBuilder.Entity("BrightGlimmer.Data.Domain.Student", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Email"); + + b.Property("FirstName"); + + b.Property("LastName"); + + b.Property("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 + } + } +} diff --git a/BrightGlimmer.Data/Repositories/StudentRepository.cs b/BrightGlimmer.Data/Repositories/StudentRepository.cs new file mode 100644 index 0000000..738595a --- /dev/null +++ b/BrightGlimmer.Data/Repositories/StudentRepository.cs @@ -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 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 GetAll() + { + return context.Students; + } + + public Student GetById(Guid id) + { + return context.Students.Find(id); + } + } +} diff --git a/BrightGlimmer.Data/SqliteDatabaseContext.cs b/BrightGlimmer.Data/SqliteDatabaseContext.cs new file mode 100644 index 0000000..8c203d9 --- /dev/null +++ b/BrightGlimmer.Data/SqliteDatabaseContext.cs @@ -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 options) + : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasMany(x => x.Phones); + } + + public DbSet Students { get; set; } + } +} diff --git a/BrightGlimmer.Data/bright_glimmer.db b/BrightGlimmer.Data/bright_glimmer.db new file mode 100644 index 0000000..4616386 Binary files /dev/null and b/BrightGlimmer.Data/bright_glimmer.db differ