Skip to content

Commit

Permalink
Merge pull request #30 from ISUCT/KeyListBackend
Browse files Browse the repository at this point in the history
Key list backend ready
  • Loading branch information
jskonst authored Jun 30, 2021
2 parents 932eeae + 0783a96 commit f8890bd
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 3 deletions.
21 changes: 21 additions & 0 deletions server/Application/DTO/Request/KeyCreateRequestDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Application.DTO.Request
{
using Application.Interfaces;
using Domain.Models;

public class KeyCreateRequestDto : IDtoMapper<Key>
{
public string Classroom { get; set; }

public string QR { get; set; }

public Key ToModel()
{
return new Key()
{
Classroom = this.Classroom,
QR = this.QR,
};
}
}
}
24 changes: 24 additions & 0 deletions server/Application/DTO/Responce/KeyDto.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
13 changes: 13 additions & 0 deletions server/Application/Interfaces/IKeyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Application.Interfaces
{
using System.Collections.Generic;
using Application.DTO.Request;
using Application.ViewModels;

public interface IKeyService
{
List<KeyDto> GetKeys();

KeyDto InsertKey(KeyCreateRequestDto key);
}
}
30 changes: 30 additions & 0 deletions server/Application/Services/KeyService.cs
Original file line number Diff line number Diff line change
@@ -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<KeyDto> 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;
}
}
}
11 changes: 11 additions & 0 deletions server/Domain/Models/Key.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
12 changes: 12 additions & 0 deletions server/Domain/Repository/IKeyRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Domain.Repository
{
using System.Linq;
using Domain.Models;

public interface IKeyRepository
{
IQueryable<Key> GetKeys();

Key InsertKey(Key key);
}
}
2 changes: 2 additions & 0 deletions server/Domain/Repository/ITeacherRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public interface ITeacherRepository
{
IQueryable<Teacher> GetTeachers();

Teacher GetTeacherById(int id);

Teacher InsertTeacher(Teacher teacher);
}
}
2 changes: 2 additions & 0 deletions server/Infrastructure/EF/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public DatabaseContext(DbContextOptions<DatabaseContext> options)

public DbSet<Teacher> Teachers { get; set; }

public DbSet<Key> Keys { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions server/Infrastructure/Migrations/20210625151739_AddedKeys.cs
Original file line number Diff line number Diff line change
@@ -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<int>(
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);
}
}
}
20 changes: 19 additions & 1 deletion server/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Classroom")
.HasColumnType("text");
b.Property<string>("QR")
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Keys");
});

modelBuilder.Entity("Domain.Models.Teacher", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand Down
30 changes: 30 additions & 0 deletions server/Infrastructure/Repository/KeyRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Key> IKeyRepository.GetKeys()
{
return context.Keys.AsNoTracking();
}
}
}
5 changes: 5 additions & 0 deletions server/Infrastructure/Repository/TeacherRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Teacher> ITeacherRepository.GetTeachers()
{
return context.Teachers.AsNoTracking();
Expand Down
35 changes: 35 additions & 0 deletions server/WebApi/Controllers/KeysController.cs
Original file line number Diff line number Diff line change
@@ -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<KeysController> _logger;
private IKeyService _keyService;

public KeysController(ILogger<KeysController> logger, IKeyService keyService)
{
_logger = logger;
_keyService = keyService;
}

[HttpGet]
public ActionResult<List<KeyDto>> Get()
{
return this.Ok(_keyService.GetKeys());
}

[HttpPost]
public ActionResult<KeyDto> Insert([FromBody] KeyCreateRequestDto key)
{
return this.Ok(_keyService.InsertKey(key));
}
}
}
4 changes: 2 additions & 2 deletions server/WebApi/Controllers/TeachersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
[Route("api/[controller]")]
public class TeachersController : ControllerBase
{
private readonly ILogger<TeachersController> logger;
private readonly ILogger<TeachersController> _logger;
private ITeacherService _teacherService;

public TeachersController(ILogger<TeachersController> logger, ITeacherService teacherService)
{
this.logger = logger;
_logger = logger;
_teacherService = teacherService;
}

Expand Down
2 changes: 2 additions & 0 deletions server/WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void ConfigureServices(IServiceCollection services)

services.AddScoped<ITeacherService, TeacherService>();
services.AddScoped<ITeacherRepository, TeacherRepository>();
services.AddScoped<IKeyService, KeyService>();
services.AddScoped<IKeyRepository, KeyRepository>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down

0 comments on commit f8890bd

Please sign in to comment.