Skip to content

Commit

Permalink
Replaced POST Consultant with basic write model
Browse files Browse the repository at this point in the history
  • Loading branch information
yelodevopsi committed Sep 28, 2023
1 parent a62b890 commit e566c13
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions backend/Api/Routes/ConsultantApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<ConsultantReadModel>> GetAllConsultants(ApplicationContext context, IMemoryCache cache,
Expand Down Expand Up @@ -52,15 +52,6 @@ private static Results<Ok<ConsultantReadModel>, NotFound> GetConsultantById(Appl
return consultant is null ? TypedResults.NotFound() : TypedResults.Ok(consultant);
}

private static async Task<Created<Consultant>> 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(
Expand All @@ -74,4 +65,35 @@ private static ConsultantReadModel MapToReadModel(this Consultant consultant, in

private record ConsultantReadModel(int Id, string Name, string Email, List<string> Competences, string Department,
List<AvailabilityPerWeek> Availability);


private static async Task<Created<Consultant>> 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);
}

0 comments on commit e566c13

Please sign in to comment.