Skip to content

Commit 0a56d86

Browse files
committed
Merge branch 'temp_main' of github.com:boolean-uk/csharp-team-dev-server-team-4 into temp_main_post
2 parents 9424977 + c8ab449 commit 0a56d86

File tree

13 files changed

+266
-95
lines changed

13 files changed

+266
-95
lines changed

api.tests/UserEndpointTests/GetUserTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using exercise.wwwapi.DTOs;
22
using exercise.wwwapi.DTOs.GetUsers;
33
using exercise.wwwapi.DTOs.Login;
4+
using exercise.wwwapi.DTOs.Users;
45
using exercise.wwwapi.Endpoints;
56
using System.Text.Json;
67

exercise.wwwapi/DTOs/Cohorts/CohortDTO.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22
using System.ComponentModel.DataAnnotations;
33
using System.ComponentModel.DataAnnotations.Schema;
44
using System.Text.Json.Serialization;
5+
using exercise.wwwapi.DTOs.Courses;
6+
using exercise.wwwapi.DTOs.Exercises;
57

68
namespace exercise.wwwapi.Models;
79

810
public class CohortDTO
911
{
10-
[JsonPropertyName("course_id")]
11-
12+
public int Id { get; set; }
1213
public int CohortNumber { get; set; }
13-
1414
public string CohortName { get; set; }
15-
1615
public DateTime StartDate { get; set; }
1716
public DateTime EndDate { get; set; }
17+
public List<CourseDTO> Courses { get; set; }
18+
19+
public CohortDTO(){}
20+
public CohortDTO(Cohort model)
21+
{
22+
Id = model.Id;
23+
CohortNumber = model.CohortNumber;
24+
CohortName = model.CohortName;
25+
StartDate = model.StartDate;
26+
EndDate = model.EndDate;
27+
Courses = model.CohortCourses.Select(cc => new CourseDTO(cc)).ToList();
28+
}
1829
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using exercise.wwwapi.Models;
2+
using System.Drawing;
3+
4+
namespace exercise.wwwapi.DTOs.Courses
5+
{
6+
public class CourseDTO
7+
{
8+
public int Id { get; set; }
9+
public string Name { get; set; }
10+
public CourseDTO() { }
11+
public CourseDTO(Course model)
12+
{
13+
Id = model.Id;
14+
Name = model.Name;
15+
}
16+
public CourseDTO(CohortCourse model)
17+
{
18+
Id = model.Course.Id;
19+
Name = model.Course.Name;
20+
}
21+
}
22+
23+
}

exercise.wwwapi/DTOs/GetObjects/UsersSuccessDTO.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Serialization;
1+
using exercise.wwwapi.DTOs.Users;
2+
using System.Text.Json.Serialization;
23

34
namespace exercise.wwwapi.DTOs.GetUsers;
45

exercise.wwwapi/DTOs/Login/LoginSuccessDTO.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Serialization;
1+
using exercise.wwwapi.DTOs.Users;
2+
using System.Text.Json.Serialization;
23

34
namespace exercise.wwwapi.DTOs.Login;
45

exercise.wwwapi/DTOs/Register/RegisterSuccessDTO.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations.Schema;
1+
using exercise.wwwapi.DTOs.Users;
2+
using System.ComponentModel.DataAnnotations.Schema;
23

34
namespace exercise.wwwapi.DTOs.Register;
45

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using exercise.wwwapi.Enums;
2+
using System.Text.Json.Serialization;
3+
4+
namespace exercise.wwwapi.DTOs.Users
5+
{
6+
public class PatchUserDTO
7+
{
8+
public string? Email { get; set; }
9+
10+
public string? Password { get; set; }
11+
12+
public string? FirstName { get; set; }
13+
14+
public string? LastName { get; set; }
15+
public string? Bio { get; set; }
16+
public string? Github { get; set; }
17+
public string? Username { get; set; }
18+
public string? Mobile { get; set; }
19+
public Specialism? Specialism { get; set; }
20+
public Role? Role { get; set; }
21+
}
22+
}

exercise.wwwapi/DTOs/UserDTO.cs renamed to exercise.wwwapi/DTOs/Users/UserDTO.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.ComponentModel.DataAnnotations.Schema;
55
using System.Text.Json.Serialization;
66

7-
namespace exercise.wwwapi.DTOs;
7+
namespace exercise.wwwapi.DTOs.Users;
88

99
public class UserDTO
1010
{
@@ -59,7 +59,5 @@ public UserDTO(User model)
5959
Specialism = model.Specialism;
6060
Role = model.Role.ToString();
6161
Notes = model.Notes.Select(n => new NoteDTO(n)).ToList();
62-
63-
6462
}
6563
}

exercise.wwwapi/Endpoints/CohortEndpoints.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using exercise.wwwapi.DTOs;
2+
using exercise.wwwapi.DTOs.Courses;
23
using exercise.wwwapi.Enums;
34
using exercise.wwwapi.Models;
45
using exercise.wwwapi.Repository;
@@ -14,6 +15,10 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
1415
{
1516
var cohorts = app.MapGroup("cohorts");
1617
cohorts.MapPost("/", CreateCohort).WithSummary("Create a cohort");
18+
cohorts.MapGet("/", GetAllCohorts).WithSummary("Get all cohorts");
19+
cohorts.MapGet("/{id}", GetCohortById).WithSummary("Get cohort by id");
20+
cohorts.MapPatch("/{id}", UpdateCohortById).WithSummary("Update cohort");
21+
cohorts.MapDelete("/{id}", DeleteCohortById).WithSummary("Delete cohort");
1722
}
1823
[ProducesResponseType(StatusCodes.Status201Created)]
1924
[ProducesResponseType(StatusCodes.Status400BadRequest)]
@@ -67,5 +72,99 @@ public static async Task<IResult> CreateCohort(IRepository<Cohort> cohortRepo, C
6772

6873
return TypedResults.Created($"/cohorts/{newCohortNumber}");
6974
}
75+
[ProducesResponseType(StatusCodes.Status200OK)]
76+
public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortRepo)
77+
{
78+
// Use GetWithIncludes to include CohortCourses and their Course
79+
var cohorts = await cohortRepo.GetWithIncludes(q =>
80+
q.Include(c => c.CohortCourses)
81+
.ThenInclude(cc => cc.Course)
82+
);
83+
84+
var cohortDTOs = cohorts.Select(c => new CohortDTO(c)).ToList();
85+
86+
var response = new ResponseDTO<List<CohortDTO>>()
87+
{
88+
Status = "success",
89+
Data = cohortDTOs
90+
};
91+
return TypedResults.Ok(response);
92+
}
93+
[ProducesResponseType(StatusCodes.Status200OK)]
94+
95+
public static async Task<IResult> GetCohortById(IRepository<Cohort> cohortRepo, int id)
96+
{
97+
// uses GetByIdWithIncludes for nested includes
98+
var cohort = await cohortRepo.GetByIdWithIncludes(q =>
99+
q.Include(c => c.CohortCourses)
100+
.ThenInclude(cc => cc.Course), id);
101+
102+
if (cohort == null)
103+
{
104+
return TypedResults.NotFound();
105+
}
106+
107+
var cohortDTO = new CohortDTO(cohort);
108+
109+
var response = new ResponseDTO<CohortDTO>
110+
{
111+
Status = "success",
112+
Data = cohortDTO
113+
};
114+
115+
return TypedResults.Ok(response);
116+
}
117+
[ProducesResponseType(StatusCodes.Status200OK)]
118+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
119+
[ProducesResponseType(StatusCodes.Status404NotFound)]
120+
public static async Task<IResult> UpdateCohortById(IRepository<Cohort> cohortRepo, int id, CohortPostDTO updateDto)
121+
{
122+
var cohort = await cohortRepo.GetByIdAsync(id);
123+
if (cohort == null)
124+
{
125+
return TypedResults.NotFound();
126+
}
127+
128+
if (!string.IsNullOrWhiteSpace(updateDto.CohortName))
129+
cohort.CohortName = updateDto.CohortName;
130+
if (updateDto.StartDate != DateTime.MinValue)
131+
cohort.StartDate = updateDto.StartDate;
132+
if (updateDto.EndDate != DateTime.MinValue)
133+
cohort.EndDate = updateDto.EndDate;
134+
135+
cohortRepo.Update(cohort);
136+
await cohortRepo.SaveAsync();
137+
138+
var cohortDTO = new CohortDTO
139+
{
140+
CohortNumber = cohort.CohortNumber,
141+
CohortName = cohort.CohortName,
142+
StartDate = cohort.StartDate,
143+
EndDate = cohort.EndDate
144+
};
145+
146+
var response = new ResponseDTO<CohortDTO>
147+
{
148+
Status = "success",
149+
Data = cohortDTO
150+
};
151+
152+
return TypedResults.Ok(response);
153+
}
154+
[ProducesResponseType(StatusCodes.Status200OK)]
155+
[ProducesResponseType(StatusCodes.Status404NotFound)]
156+
public static async Task<IResult> DeleteCohortById(IRepository<Cohort> cohortRepo, int id)
157+
{
158+
var cohort = await cohortRepo.GetByIdAsync(id);
159+
if (cohort == null)
160+
{
161+
return TypedResults.NotFound();
162+
}
163+
164+
cohortRepo.Delete(cohort);
165+
await cohortRepo.SaveAsync();
166+
167+
return TypedResults.Ok(new { Status = "success", Data = $"Cohort with id {id} deleted" });
168+
}
70169

71170
}

0 commit comments

Comments
 (0)