diff --git a/server/Application/DTO/Request/KeyCreateRequestDto.cs b/server/Application/DTO/Request/KeyCreateRequestDto.cs new file mode 100644 index 0000000..25bc8a5 --- /dev/null +++ b/server/Application/DTO/Request/KeyCreateRequestDto.cs @@ -0,0 +1,21 @@ +namespace Application.DTO.Request +{ + using Application.Interfaces; + using Domain.Models; + + public class KeyCreateRequestDto : IDtoMapper + { + public string Classroom { get; set; } + + public string QR { get; set; } + + public Key ToModel() + { + return new Key() + { + Classroom = this.Classroom, + QR = this.QR, + }; + } + } +} diff --git a/server/Application/DTO/Responce/KeyDto.cs b/server/Application/DTO/Responce/KeyDto.cs new file mode 100644 index 0000000..ffe4704 --- /dev/null +++ b/server/Application/DTO/Responce/KeyDto.cs @@ -0,0 +1,24 @@ +namespace Application.ViewModels +{ + using Domain.Models; + + public class KeyDto + { + public KeyDto(Key key) + { + this.Id = key.Id; + this.Classroom = key.Classroom; + this.QR = key.QR; + } + + public KeyDto() + { + } + + public int Id { get; set; } + + public string Classroom { get; set; } + + public string QR { get; set; } + } +} diff --git a/server/Application/Interfaces/IKeyService.cs b/server/Application/Interfaces/IKeyService.cs new file mode 100644 index 0000000..94e10ad --- /dev/null +++ b/server/Application/Interfaces/IKeyService.cs @@ -0,0 +1,13 @@ +namespace Application.Interfaces +{ + using System.Collections.Generic; + using Application.DTO.Request; + using Application.ViewModels; + + public interface IKeyService + { + List GetKeys(); + + KeyDto InsertKey(KeyCreateRequestDto key); + } +} diff --git a/server/Application/Services/KeyService.cs b/server/Application/Services/KeyService.cs new file mode 100644 index 0000000..71b0941 --- /dev/null +++ b/server/Application/Services/KeyService.cs @@ -0,0 +1,30 @@ +namespace Application.Services +{ + using System.Collections.Generic; + using System.Linq; + using Application.DTO.Request; + using Application.Interfaces; + using Application.ViewModels; + using Domain.Repository; + + public class KeyService : IKeyService + { + private IKeyRepository _keyRepository; + + public KeyService(IKeyRepository keyRepository) + { + _keyRepository = keyRepository; + } + + public List GetKeys() + { + return _keyRepository.GetKeys().Select(x => new KeyDto(x)).ToList(); + } + + public KeyDto InsertKey(KeyCreateRequestDto key) + { + var createdKey = new KeyDto(_keyRepository.InsertKey(key.ToModel())); + return createdKey; + } + } +} diff --git a/server/Domain/Models/Key.cs b/server/Domain/Models/Key.cs new file mode 100644 index 0000000..b69b097 --- /dev/null +++ b/server/Domain/Models/Key.cs @@ -0,0 +1,11 @@ +namespace Domain.Models +{ + public class Key + { + public int Id { get; set; } + + public string Classroom { get; set; } + + public string QR { get; set; } + } +} diff --git a/server/Domain/Repository/IKeyRepository.cs b/server/Domain/Repository/IKeyRepository.cs new file mode 100644 index 0000000..63f612e --- /dev/null +++ b/server/Domain/Repository/IKeyRepository.cs @@ -0,0 +1,12 @@ +namespace Domain.Repository +{ + using System.Linq; + using Domain.Models; + + public interface IKeyRepository + { + IQueryable GetKeys(); + + Key InsertKey(Key key); + } +} diff --git a/server/Domain/Repository/ITeacherRepository.cs b/server/Domain/Repository/ITeacherRepository.cs index 9b1e185..28b8ab7 100644 --- a/server/Domain/Repository/ITeacherRepository.cs +++ b/server/Domain/Repository/ITeacherRepository.cs @@ -7,6 +7,8 @@ public interface ITeacherRepository { IQueryable GetTeachers(); + Teacher GetTeacherById(int id); + Teacher InsertTeacher(Teacher teacher); } } diff --git a/server/Infrastructure/EF/DatabaseContext.cs b/server/Infrastructure/EF/DatabaseContext.cs index 82dc37d..45b052b 100644 --- a/server/Infrastructure/EF/DatabaseContext.cs +++ b/server/Infrastructure/EF/DatabaseContext.cs @@ -12,6 +12,8 @@ public DatabaseContext(DbContextOptions options) public DbSet Teachers { get; set; } + public DbSet Keys { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) { } diff --git a/server/Infrastructure/Migrations/20210625151739_AddedKeys.Designer.cs b/server/Infrastructure/Migrations/20210625151739_AddedKeys.Designer.cs new file mode 100644 index 0000000..4f0680e --- /dev/null +++ b/server/Infrastructure/Migrations/20210625151739_AddedKeys.Designer.cs @@ -0,0 +1,61 @@ +// +using Infrastructure.EF; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace Infrastructure.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20210625151739_AddedKeys")] + partial class AddedKeys + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.6") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + modelBuilder.Entity("Domain.Models.Key", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("Classroom") + .HasColumnType("text"); + + b.Property("QR") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Keys"); + }); + + modelBuilder.Entity("Domain.Models.Teacher", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("FullName") + .HasColumnType("text"); + + b.Property("PersonalKey") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Teachers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/server/Infrastructure/Migrations/20210625151739_AddedKeys.cs b/server/Infrastructure/Migrations/20210625151739_AddedKeys.cs new file mode 100644 index 0000000..7a87523 --- /dev/null +++ b/server/Infrastructure/Migrations/20210625151739_AddedKeys.cs @@ -0,0 +1,44 @@ +namespace Infrastructure.Migrations +{ + using Microsoft.EntityFrameworkCore.Migrations; + + public partial class AddedKeys : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Keys_Teachers_TeacherId", + table: "Keys"); + + migrationBuilder.DropIndex( + name: "IX_Keys_TeacherId", + table: "Keys"); + + migrationBuilder.DropColumn( + name: "TeacherId", + table: "Keys"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "TeacherId", + table: "Keys", + type: "integer", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Keys_TeacherId", + table: "Keys", + column: "TeacherId"); + + migrationBuilder.AddForeignKey( + name: "FK_Keys_Teachers_TeacherId", + table: "Keys", + column: "TeacherId", + principalTable: "Teachers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + } +} diff --git a/server/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs b/server/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs index c9aa836..229b7ea 100644 --- a/server/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs +++ b/server/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs @@ -18,7 +18,25 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasAnnotation("ProductVersion", "5.0.6") .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - modelBuilder.Entity("WebApi.Models.Teacher", b => + modelBuilder.Entity("Domain.Models.Key", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("Classroom") + .HasColumnType("text"); + + b.Property("QR") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Keys"); + }); + + modelBuilder.Entity("Domain.Models.Teacher", b => { b.Property("Id") .ValueGeneratedOnAdd() diff --git a/server/Infrastructure/Repository/KeyRepository.cs b/server/Infrastructure/Repository/KeyRepository.cs new file mode 100644 index 0000000..784703b --- /dev/null +++ b/server/Infrastructure/Repository/KeyRepository.cs @@ -0,0 +1,30 @@ +namespace CleanArchitecture.Infra.Data.Repositories +{ + using System.Linq; + using Domain.Models; + using Domain.Repository; + using Infrastructure.EF; + using Microsoft.EntityFrameworkCore; + + public class KeyRepository : IKeyRepository + { + private DatabaseContext context; + + public KeyRepository(DatabaseContext context) + { + this.context = context; + } + + public Key InsertKey(Key key) + { + var entity = context.Add(key); + context.SaveChanges(); + return entity.Entity; + } + + IQueryable IKeyRepository.GetKeys() + { + return context.Keys.AsNoTracking(); + } + } +} diff --git a/server/Infrastructure/Repository/TeacherRepository.cs b/server/Infrastructure/Repository/TeacherRepository.cs index 054381f..a685d04 100644 --- a/server/Infrastructure/Repository/TeacherRepository.cs +++ b/server/Infrastructure/Repository/TeacherRepository.cs @@ -22,6 +22,11 @@ public Teacher InsertTeacher(Teacher teacher) return entity.Entity; } + public Teacher GetTeacherById(int id) + { + return context.Teachers.AsNoTracking().FirstOrDefault(t => t.Id == id); + } + IQueryable ITeacherRepository.GetTeachers() { return context.Teachers.AsNoTracking(); diff --git a/server/WebApi/Controllers/KeysController.cs b/server/WebApi/Controllers/KeysController.cs new file mode 100644 index 0000000..1d668c0 --- /dev/null +++ b/server/WebApi/Controllers/KeysController.cs @@ -0,0 +1,35 @@ +namespace WebApi.Controllers +{ + using System.Collections.Generic; + using Application.DTO.Request; + using Application.Interfaces; + using Application.ViewModels; + using Microsoft.AspNetCore.Mvc; + using Microsoft.Extensions.Logging; + + [ApiController] + [Route("api/[controller]")] + public class KeysController : ControllerBase + { + private readonly ILogger _logger; + private IKeyService _keyService; + + public KeysController(ILogger logger, IKeyService keyService) + { + _logger = logger; + _keyService = keyService; + } + + [HttpGet] + public ActionResult> Get() + { + return this.Ok(_keyService.GetKeys()); + } + + [HttpPost] + public ActionResult Insert([FromBody] KeyCreateRequestDto key) + { + return this.Ok(_keyService.InsertKey(key)); + } + } +} diff --git a/server/WebApi/Controllers/TeachersController.cs b/server/WebApi/Controllers/TeachersController.cs index 4464162..d139dc9 100644 --- a/server/WebApi/Controllers/TeachersController.cs +++ b/server/WebApi/Controllers/TeachersController.cs @@ -11,12 +11,12 @@ [Route("api/[controller]")] public class TeachersController : ControllerBase { - private readonly ILogger logger; + private readonly ILogger _logger; private ITeacherService _teacherService; public TeachersController(ILogger logger, ITeacherService teacherService) { - this.logger = logger; + _logger = logger; _teacherService = teacherService; } diff --git a/server/WebApi/Startup.cs b/server/WebApi/Startup.cs index e1a326c..5308fb0 100644 --- a/server/WebApi/Startup.cs +++ b/server/WebApi/Startup.cs @@ -36,6 +36,8 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.