From e566c1381cb6fe92493a4f7cc8d6bacb8f8cf332 Mon Sep 17 00:00:00 2001 From: Magnus Gule Date: Thu, 28 Sep 2023 13:48:38 +0200 Subject: [PATCH] Replaced POST Consultant with basic write model --- backend/Api/Routes/ConsultantApi.cs | 42 ++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/backend/Api/Routes/ConsultantApi.cs b/backend/Api/Routes/ConsultantApi.cs index cefbaed2..cfc179a4 100644 --- a/backend/Api/Routes/ConsultantApi.cs +++ b/backend/Api/Routes/ConsultantApi.cs @@ -14,7 +14,7 @@ public static void MapConsultantApi(this RouteGroupBuilder group) { group.MapGet("/", GetAllConsultants); group.MapGet("/{id}", GetConsultantById); - group.MapPost("/", AddConsultant).WithOpenApi(); + group.MapPost("/", AddBasicConsultant).WithOpenApi(); } private static Ok> GetAllConsultants(ApplicationContext context, IMemoryCache cache, @@ -52,15 +52,6 @@ private static Results, NotFound> GetConsultantById(Appl return consultant is null ? TypedResults.NotFound() : TypedResults.Ok(consultant); } - private static async Task> AddConsultant(ApplicationContext db, IMemoryCache cache, - Consultant variant) - { - await db.Consultant.AddAsync(variant); - await db.SaveChangesAsync(); - cache.Remove(CacheKeys.ConsultantAvailability8Weeks); - return TypedResults.Created($"/variant/{variant.Id}", variant); - } - private static ConsultantReadModel MapToReadModel(this Consultant consultant, int weeks) { return new ConsultantReadModel( @@ -74,4 +65,35 @@ private static ConsultantReadModel MapToReadModel(this Consultant consultant, in private record ConsultantReadModel(int Id, string Name, string Email, List Competences, string Department, List Availability); + + + private static async Task> AddBasicConsultant(ApplicationContext db, + IMemoryCache cache, + [FromBody] ConsultantWriteModel basicVariant) + { + var selectedDepartment = db.Department.Single(d => d.Id == basicVariant.DepartmentId); + + // TODO + // Sjekk epost + // Sjekk at den er unik + // Sjekk @-epost-format + // sjekk at Department blir funnet + // Sjekk unikt navn + + + var fullVariant = new Consultant + { + Name = basicVariant.Name, + Email = basicVariant.Email, + Department = selectedDepartment + }; + + await db.Consultant.AddAsync(fullVariant); + await db.SaveChangesAsync(); + cache.Remove(CacheKeys.ConsultantAvailability8Weeks); + + return TypedResults.Created($"/variant/{fullVariant.Id}", fullVariant); + } + + private record ConsultantWriteModel(string Name, string Email, string DepartmentId); } \ No newline at end of file