diff --git a/Migrations/20240625_AddShiftRatings.cs b/Migrations/20240625_AddShiftRatings.cs new file mode 100644 index 0000000..55427a3 --- /dev/null +++ b/Migrations/20240625_AddShiftRatings.cs @@ -0,0 +1,30 @@ +public partial class AddShiftRatings : Migration +{ + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ShiftRatings", + columns: table => new + { + ShiftId = table.Column(nullable: false), + FacilityId = table.Column(nullable: false), + WorkerId = table.Column(nullable: false), + Score = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShiftRatings", x => new { x.ShiftId, x.WorkerId }); + table.ForeignKey("FK_ShiftRatings_Shifts_ShiftId", x => x.ShiftId, "Shifts", "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey("FK_ShiftRatings_Facilities_FacilityId", x => x.FacilityId, "Facilities", "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey("FK_ShiftRatings_Workers_WorkerId", x => x.WorkerId, "Workers", "Id", onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex("IX_ShiftRatings_FacilityId", "ShiftRatings", "FacilityId"); + migrationBuilder.CreateIndex("IX_ShiftRatings_WorkerId", "ShiftRatings", "WorkerId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable("ShiftRatings"); + } +} \ No newline at end of file diff --git a/src/Controllers/FacilitiesController.cs b/src/Controllers/FacilitiesController.cs index 9701134..7213b7d 100644 --- a/src/Controllers/FacilitiesController.cs +++ b/src/Controllers/FacilitiesController.cs @@ -3,8 +3,31 @@ public class FacilitiesController : ControllerBase { private readonly AppDbContext _dbContext; - public FacilitiesController(AppDbContext dbContext) => _dbContext = dbContext; + private readonly RateWorkerHandler _rateWorkerHandler; + private readonly BlockWorkerHandler _blockWorkerHandler; - [HttpGet] - public IActionResult GetFacilities() => Ok(_dbContext.Facilities.ToList()); + public FacilitiesController(AppDbContext dbContext, RateWorkerHandler rateWorkerHandler, BlockWorkerHandler blockWorkerHandler) + { + _dbContext = dbContext; + _rateWorkerHandler = rateWorkerHandler; + _blockWorkerHandler = blockWorkerHandler; + } + + [HttpGet("facilities/list")] + public async Task GetFacilities([FromBody] RateWorkerRequest request) + { + return await _dbContext.Facilities.ToListAsync(); + } + + [HttpPost("facilities/rate-worker")] + public async Task RateWorker([FromBody] RateWorkerRequest request) + { + return await _rateWorkerHandler.HandleAsync(request); + } + + [HttpPost("facilities/block-worker")] + public async Task BlockWorker([FromBody] BlockWorkerRequest request) + { + return await _blockWorkerHandler.HandleAsync(request); + } } \ No newline at end of file diff --git a/src/Handlers/BlockWorkerHandler.cs b/src/Handlers/BlockWorkerHandler.cs new file mode 100644 index 0000000..dcb9184 --- /dev/null +++ b/src/Handlers/BlockWorkerHandler.cs @@ -0,0 +1,21 @@ +public class BlockWorkerHandler +{ + private readonly WorkerRepository _workerRepository; + + public BlockWorkerHandler(WorkerRepository workerRepository) + { + _workerRepository = workerRepository; + } + + public async Task HandleAsync(BlockWorkerRequest request) + { + try + { + _workerRepository.BlockWorker(request.WorkerId, request.FacilityId); + } + catch (Exception e) + { + return new JsonResult(new { error = e.Message }); + } + } +} \ No newline at end of file diff --git a/src/Handlers/RateWorkerHandler.cs b/src/Handlers/RateWorkerHandler.cs new file mode 100644 index 0000000..e6ec0bf --- /dev/null +++ b/src/Handlers/RateWorkerHandler.cs @@ -0,0 +1,32 @@ +public class RateWorkerHandler +{ + private readonly AppDbContext _dbContext; + + public RateWorkerHandler(AppDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task HandleAsync(RateWorkerRequest request) + { + try + { + var shiftRating = new ShiftRating + { + ShiftId = request.ShiftId, + FacilityId = request.FacilityId, + WorkerId = request.WorkerId, + Score = request.Score + }; + + _dbContext.ShiftRatings.Add(shiftRating); + await _dbContext.SaveChangesAsync(); + + return new JsonResult(shiftRating); + } + catch (Exception e) + { + return new JsonResult(new { error = e.Message }); + } + } +} \ No newline at end of file diff --git a/src/Models/ShiftRating.cs b/src/Models/ShiftRating.cs new file mode 100644 index 0000000..6a1e63b --- /dev/null +++ b/src/Models/ShiftRating.cs @@ -0,0 +1,13 @@ +public class ShiftRating +{ + public int ShiftId { get; set; } + public Shift Shift { get; set; } + + public int FacilityId { get; set; } + public Facility Facility { get; set; } + + public int WorkerId { get; set; } + public Worker Worker { get; set; } + + public int Score { get; set; } +} diff --git a/src/Models/Worker.cs b/src/Models/Worker.cs index fd21f70..10edcd1 100644 --- a/src/Models/Worker.cs +++ b/src/Models/Worker.cs @@ -2,5 +2,6 @@ public class Worker { public int Id { get; set; } public string FullName { get; set; } + public string Email { get; set; } public List Shifts { get; set; } } \ No newline at end of file