Skip to content

Commit 8bb8ee4

Browse files
author
Roman Eriksen
committed
Fixed comments, deleted old files
1 parent bbcdabb commit 8bb8ee4

File tree

7 files changed

+40
-357
lines changed

7 files changed

+40
-357
lines changed

exercise.wwwapi/DTOs/Cohort/UserCohortDTO.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

exercise.wwwapi/Data/DataContext.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5252
.HasForeignKey(ccu => ccu.UserId);
5353
#endregion CohortCourse
5454

55-
modelBuilder.Entity<UserCohort>()
56-
.HasKey(uc => new { uc.UserId, uc.CohortId });
57-
58-
5955
modelBuilder.Entity<User>()
6056
.Property(u => u.Role)
6157
.HasConversion<string>();
@@ -89,6 +85,5 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
8985
public DbSet<Course> Courses { get; set; }
9086
public DbSet<CohortCourse> CohortCourses { get; set; }
9187
public DbSet<CohortCourseUser> CohortCourseUsers { get; set; }
92-
public DbSet<UserCohort> UserCohorts { get; set; }
9388
}
9489
}

exercise.wwwapi/Endpoints/CohortEndpoints.cs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using exercise.wwwapi.Repository;
77
using Microsoft.AspNetCore.Mvc;
88
using Microsoft.EntityFrameworkCore;
9+
using System.Linq;
910

1011
namespace exercise.wwwapi.Endpoints
1112
{
@@ -17,6 +18,7 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
1718
cohorts.MapPost("/", CreateCohort).WithSummary("Create a cohort");
1819
cohorts.MapGet("/", GetAllCohorts).WithSummary("Get all cohorts");
1920
cohorts.MapGet("/{id}", GetCohort).WithSummary("Get a cohort by ID");
21+
//cohorts.MapGet("/{userId}", GetCohortByUserId).WithSummary("");
2022
cohorts.MapPost("/{cohortId}/{userId}/{courseId}", AddUserToCohort).WithSummary("Add a user to a cohort");
2123
cohorts.MapDelete("/{cohortId}/{userId}/{courseId}", DeleteUserFromCohort).WithSummary("Delete a user from a cohort");
2224
}
@@ -73,10 +75,13 @@ public static async Task<IResult> CreateCohort(
7375
{
7476

7577
var results = cohortService.GetAllFiltered(c => c.Title == request.Title);
76-
if (results != null) return TypedResults.BadRequest(new ResponseDTO<object>
77-
{
78-
Message = $"Cohort with name {request.Title} already exists"
79-
});
78+
Console.WriteLine(results);
79+
if (results.Any())
80+
return TypedResults.BadRequest(new ResponseDTO<object>
81+
{
82+
Message = $"Cohort with name '{request.Title}' already exists",
83+
Data = results
84+
});
8085

8186
Cohort cohort = new Cohort() { Title = request.Title };
8287

@@ -128,10 +133,11 @@ public static async Task<IResult> AddUserToCohort(
128133
{
129134
// 1. Get the user
130135
var user = userService.GetById(userId);
131-
if (user == null) return TypedResults.BadRequest(new ResponseDTO<object>
132-
{
133-
Message = $"User with Id {userId} not found."
134-
});
136+
if (user == null)
137+
return TypedResults.BadRequest(new ResponseDTO<object>
138+
{
139+
Message = $"User with Id {userId} not found."
140+
});
135141

136142
// 2. Get the cohort including its users and courses for verification steps
137143
var cohort = cohortService.GetById(cohortId, q =>
@@ -141,10 +147,11 @@ public static async Task<IResult> AddUserToCohort(
141147
.ThenInclude(cc => cc.CohortCourseUsers)
142148
.ThenInclude(ccu => ccu.User));
143149

144-
if (cohort == null) return TypedResults.BadRequest(new ResponseDTO<object>
145-
{
146-
Message = $"Cohort with Id {cohortId} not found."
147-
});
150+
if (cohort == null)
151+
return TypedResults.BadRequest(new ResponseDTO<object>
152+
{
153+
Message = $"Cohort with Id {cohortId} not found."
154+
});
148155

149156
// 3. Verify that the course exists in this cohort
150157
var cohortCourse = cohort.CohortCourses.FirstOrDefault(cc => cc.CourseId == courseId);
@@ -155,11 +162,11 @@ public static async Task<IResult> AddUserToCohort(
155162
});
156163

157164
// 4. Check if the user is already in this cohort
158-
if (cohortCourse.CohortCourseUsers.Any(cu => cu.UserId == userId))
159-
return TypedResults.BadRequest(new ResponseDTO<object>
160-
{
161-
Message = "User is already a member of this cohort."
162-
});
165+
//if (cohortCourse.CohortCourseUsers.Any(cu => cu.UserId == userId))
166+
// return TypedResults.BadRequest(new ResponseDTO<object>
167+
// {
168+
// Message = "User is already a member of this cohort."
169+
// });
163170

164171
// 7. Add user to CohortCourseUser
165172
var existingCcu = cohortCourseUserService
@@ -196,7 +203,7 @@ public static async Task<IResult> AddUserToCohort(
196203
}
197204
return TypedResults.BadRequest(new ResponseDTO<object>
198205
{
199-
Message = "Failed to add user."
206+
Message = "User is already in the specified course in the cohort."
200207
});
201208
}
202209

@@ -232,6 +239,19 @@ public static async Task<IResult> DeleteUserFromCohort(
232239
Message = $"Cohort with Id {cohortId} not found."
233240
});
234241

242+
var isTrue = false;
243+
foreach (var cc in cohort.CohortCourses)
244+
{
245+
if (cc.CohortCourseUsers.Any(ccu => ccu.UserId == userId)) isTrue = true;
246+
}
247+
if (!isTrue)
248+
{
249+
return TypedResults.BadRequest(new ResponseDTO<object>
250+
{
251+
Message = "The specified user is not part of this cohort."
252+
});
253+
}
254+
235255
// 3. Verify that the course exists in this cohort
236256
var cohortCourse = cohort.CohortCourses.FirstOrDefault(cc => cc.CourseId == courseId);
237257
if (cohortCourse == null)
@@ -241,10 +261,10 @@ public static async Task<IResult> DeleteUserFromCohort(
241261
});
242262

243263
// 4. Check if the user is already in this cohort
244-
if (!cohortCourse.CohortCourseUsers.Any(cu => cu.UserId == userId))
264+
if (!cohortCourse.CohortCourseUsers.Any(cu => cu.UserId == userId && cu.CourseId == courseId))
245265
return TypedResults.BadRequest(new ResponseDTO<object>
246266
{
247-
Message = "User is not a member of this cohort."
267+
Message = "User is in cohort, but is not taking the specified course."
248268
});
249269

250270
// 7. Add user to CohortCourseUser

0 commit comments

Comments
 (0)