Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions exercise.wwwapi/DTOs/Cohorts/CohortDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using exercise.wwwapi.DTOs.Courses;
using exercise.wwwapi.DTOs.Exercises;

namespace exercise.wwwapi.Models;

public class CohortDTO
{
[JsonPropertyName("course_id")]

public int Id { get; set; }
public int CohortNumber { get; set; }

public string CohortName { get; set; }

public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public List<CourseDTO> Courses { get; set; }

public CohortDTO(){}
public CohortDTO(Cohort model)
{
Id = model.Id;
CohortNumber = model.CohortNumber;
CohortName = model.CohortName;
StartDate = model.StartDate;
EndDate = model.EndDate;
Courses = model.CohortCourses.Select(cc => new CourseDTO(cc)).ToList();
}
}
23 changes: 23 additions & 0 deletions exercise.wwwapi/DTOs/Courses/CourseDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using exercise.wwwapi.Models;
using System.Drawing;

namespace exercise.wwwapi.DTOs.Courses
{
public class CourseDTO
{
public int Id { get; set; }
public string Name { get; set; }
public CourseDTO() { }
public CourseDTO(Course model)
{
Id = model.Id;
Name = model.Name;
}
public CourseDTO(CohortCourse model)
{
Id = model.Course.Id;
Name = model.Course.Name;
}
}

}
99 changes: 99 additions & 0 deletions exercise.wwwapi/Endpoints/CohortEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using exercise.wwwapi.DTOs;
using exercise.wwwapi.DTOs.Courses;
using exercise.wwwapi.Enums;
using exercise.wwwapi.Models;
using exercise.wwwapi.Repository;
Expand All @@ -14,6 +15,10 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
{
var cohorts = app.MapGroup("cohorts");
cohorts.MapPost("/", CreateCohort).WithSummary("Create a cohort");
cohorts.MapGet("/", GetAllCohorts).WithSummary("Get all cohorts");
cohorts.MapGet("/{id}", GetCohortById).WithSummary("Get cohort by id");
cohorts.MapPatch("/{id}", UpdateCohortById).WithSummary("Update cohort");
cohorts.MapDelete("/{id}", DeleteCohortById).WithSummary("Delete cohort");
}
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
Expand Down Expand Up @@ -67,5 +72,99 @@ public static async Task<IResult> CreateCohort(IRepository<Cohort> cohortRepo, C

return TypedResults.Created($"/cohorts/{newCohortNumber}");
}
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortRepo)
{
// Use GetWithIncludes to include CohortCourses and their Course
var cohorts = await cohortRepo.GetWithIncludes(q =>
q.Include(c => c.CohortCourses)
.ThenInclude(cc => cc.Course)
);

var cohortDTOs = cohorts.Select(c => new CohortDTO(c)).ToList();

var response = new ResponseDTO<List<CohortDTO>>()
{
Status = "success",
Data = cohortDTOs
};
return TypedResults.Ok(response);
}
[ProducesResponseType(StatusCodes.Status200OK)]

public static async Task<IResult> GetCohortById(IRepository<Cohort> cohortRepo, int id)
{
// uses GetByIdWithIncludes for nested includes
var cohort = await cohortRepo.GetByIdWithIncludes(q =>
q.Include(c => c.CohortCourses)
.ThenInclude(cc => cc.Course), id);

if (cohort == null)
{
return TypedResults.NotFound();
}

var cohortDTO = new CohortDTO(cohort);

var response = new ResponseDTO<CohortDTO>
{
Status = "success",
Data = cohortDTO
};

return TypedResults.Ok(response);
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> UpdateCohortById(IRepository<Cohort> cohortRepo, int id, CohortPostDTO updateDto)
{
var cohort = await cohortRepo.GetByIdAsync(id);
if (cohort == null)
{
return TypedResults.NotFound();
}

if (!string.IsNullOrWhiteSpace(updateDto.CohortName))
cohort.CohortName = updateDto.CohortName;
if (updateDto.StartDate != DateTime.MinValue)
cohort.StartDate = updateDto.StartDate;
if (updateDto.EndDate != DateTime.MinValue)
cohort.EndDate = updateDto.EndDate;

cohortRepo.Update(cohort);
await cohortRepo.SaveAsync();

var cohortDTO = new CohortDTO
{
CohortNumber = cohort.CohortNumber,
CohortName = cohort.CohortName,
StartDate = cohort.StartDate,
EndDate = cohort.EndDate
};

var response = new ResponseDTO<CohortDTO>
{
Status = "success",
Data = cohortDTO
};

return TypedResults.Ok(response);
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> DeleteCohortById(IRepository<Cohort> cohortRepo, int id)
{
var cohort = await cohortRepo.GetByIdAsync(id);
if (cohort == null)
{
return TypedResults.NotFound();
}

cohortRepo.Delete(cohort);
await cohortRepo.SaveAsync();

return TypedResults.Ok(new { Status = "success", Data = $"Cohort with id {id} deleted" });
}

}