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
1 change: 0 additions & 1 deletion api.tests/Notes/GetNotesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using exercise.wwwapi.DTOs.Notes;
using exercise.wwwapi.Endpoints;
using exercise.wwwapi.Models;
using exercise.wwwapi.Models.UserInfo;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
4 changes: 2 additions & 2 deletions api.tests/UserEndpointTests/UpdateUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public async Task UpdateUserMobileNumberValidationFailsTest()
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.Data.Token);
var updateUser = new UpdateUserRequestDTO
{
Phone = phone,
Mobile = phone,
};
var content = new StringContent(
JsonSerializer.Serialize(updateUser),
Expand Down Expand Up @@ -328,7 +328,7 @@ public async Task UpdateUserMobileValidationPassesTest()
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.Data.Token);
var updateUser = new UpdateUserRequestDTO
{
Phone = phone,
Mobile = phone,
Username = username,
};
var content = new StringContent(
Expand Down
18 changes: 12 additions & 6 deletions api.tests/api.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 15 additions & 1 deletion exercise.wwwapi/DTOs/Notes/NoteDTO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using exercise.wwwapi.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

Expand All @@ -20,5 +21,18 @@ public class NoteDTO

[JsonPropertyName("updatedat")]
public DateTime UpdatedAt { get; set; }
public NoteDTO()
{

}
public NoteDTO(Note model)
{
Id = model.Id;
Title = model.Title;
Content = model.Content;
CreatedAt = model.CreatedAt;
UpdatedAt = model.UpdatedAt;

}
}
}
25 changes: 24 additions & 1 deletion exercise.wwwapi/DTOs/UserDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,30 @@ public class UserDTO
public Specialism? Specialism { get; set; }

[JsonPropertyName("notes")]
public ICollection<NoteDTO> Notes { get; set; }

public ICollection<NoteDTO> Notes { get; set; } = new List<NoteDTO>();
[JsonPropertyName("role")]
public string Role { get; set; }

public UserDTO()
{

}

public UserDTO(User model)
{
Id = model.Id;
Email = model.Email;
FirstName = model.FirstName;
LastName = model.LastName;
Bio = model.Bio;
Github = model.Github;
Username = model.Username;
Mobile = model.Mobile;
Specialism = model.Specialism;
Role = model.Role.ToString();
Notes = [];


}
}
15 changes: 9 additions & 6 deletions exercise.wwwapi/Data/ModelSeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public static void Seed(ModelBuilder modelBuilder)
SeedExercises(ref modelBuilder);
SeedNotes(ref modelBuilder);

SeedCohortCourses(ref modelBuilder);
SeedCourseModules(ref modelBuilder);
SeedUserCC(ref modelBuilder);
SeedUserExercises(ref modelBuilder);
SeedCohortCourses(ref modelBuilder);
SeedUserCC(ref modelBuilder);

}

Expand All @@ -56,7 +56,8 @@ private static void SeedUsers(ref ModelBuilder modelBuilder)
Mobile = "1234567890",
Github = "",
Bio = "",
Specialism = Specialism.Frontend
Specialism = Specialism.Frontend,
PhotoUrl = ""
},
new User
{
Expand All @@ -70,7 +71,8 @@ private static void SeedUsers(ref ModelBuilder modelBuilder)
Mobile = "1234123",
Github = "",
Bio = "",
Specialism = Specialism.Backend
Specialism = Specialism.Backend,
PhotoUrl = ""
},
new User
{
Expand All @@ -84,7 +86,8 @@ private static void SeedUsers(ref ModelBuilder modelBuilder)
Mobile = "55555555",
Github = "",
Bio = "",
Specialism = Specialism.Frontend
Specialism = Specialism.Frontend,
PhotoUrl = ""
}

);
Expand Down Expand Up @@ -310,7 +313,7 @@ private static void SeedUserCC(ref ModelBuilder modelBuilder)

private static void SeedUserExercises(ref ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserCC>().HasData(
modelBuilder.Entity<UserExercise>().HasData(
new UserExercise
{
Id = 1,
Expand Down
33 changes: 12 additions & 21 deletions exercise.wwwapi/Endpoints/UserEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Diagnostics;
using exercise.wwwapi.Models;
using exercise.wwwapi.Factories;
using Microsoft.EntityFrameworkCore;

namespace exercise.wwwapi.EndPoints;

Expand All @@ -30,7 +31,7 @@ public static void ConfigureAuthApi(this WebApplication app)
{
var users = app.MapGroup("users");
users.MapPost("/", Register).WithSummary("Create user");
users.MapGet("/by_cohort/{id}", GetUsersByCohort).WithSummary("Get all users from a cohort");
users.MapGet("/by_cohort/{id}", GetUsersByCohortCourse).WithSummary("Get all users from a cohort");
users.MapGet("/", GetUsers).WithSummary("Get all users or filter by first name, last name or full name");
users.MapGet("/{id}", GetUserById).WithSummary("Get user by user id");
app.MapPost("/login", Login).WithSummary("Localhost Login");
Expand Down Expand Up @@ -68,28 +69,18 @@ private static async Task<IResult> GetUsers(IRepository<User> userRepository, st
};
return TypedResults.Ok(response);
}

[ProducesResponseType(StatusCodes.Status200OK)]
private static async Task<IResult> GetUsersByCohort(IRepository<User> userRepository, int id, ClaimsPrincipal claimsPrincipal)
private static async Task<IResult> GetUsersByCohortCourse(IRepository<CohortCourse> ccRepository, int cc_id, ClaimsPrincipal claimsPrincipal)
{
var all = await userRepository.GetAllAsync(u => u.Notes);
var results = all.Where(u => u.CohortId == id).ToList();
var response = await ccRepository.GetByIdWithIncludes(a => a.Include(b => b.UserCCs).ThenInclude(a => a.User), cc_id);

var userRole = claimsPrincipal.Role();
var authorizedAsTeacher = AuthorizeTeacher(claimsPrincipal);
var results = response.UserCCs.Select(a => a.User).ToList();
var dto_results = results.Select(a => new UserDTO(a));


return TypedResults.Ok(dto_results);

var userData = new UsersSuccessDTO
{
Users = results.Select(user => authorizedAsTeacher
? UserFactory.GetUserDTO(user, PrivilegeLevel.Teacher)
: UserFactory.GetUserDTO(user, PrivilegeLevel.Student))
.ToList()
};
var response = new ResponseDTO<UsersSuccessDTO>
{
Status = "success",
Data = userData
};
return TypedResults.Ok(response);
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down Expand Up @@ -350,8 +341,8 @@ public static async Task<IResult> UpdateUser(IRepository<User> userRepository, i
Github = user.Github,
Username = user.Username,
Mobile = user.Mobile,
Specialism = user.Specialism,
Role = user.Role,
Specialism = (Specialism)user.Specialism,
Role = (Role)user.Role,
}
};

Expand Down
5 changes: 3 additions & 2 deletions exercise.wwwapi/Models/Cohort.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.ComponentModel.DataAnnotations;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.wwwapi.Models;

[Table("cohorts")]
public class Cohort
public class Cohort : IEntity
{
[Key]
[Column("id")]
Expand Down
29 changes: 0 additions & 29 deletions exercise.wwwapi/Models/Cohort_Course.cs

This file was deleted.

5 changes: 3 additions & 2 deletions exercise.wwwapi/Models/Comment.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.ComponentModel.DataAnnotations;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace exercise.wwwapi.Models;

[Table("comments")]
public class Comment
public class Comment : IEntity
{
[Key]
[Column("id")]
Expand Down
4 changes: 2 additions & 2 deletions exercise.wwwapi/Models/Course.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using exercise.wwwapi.Models.UserInfo;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.wwwapi.Models;

[Table("courses")]
public class Course
public class Course : IEntity
{
[Key]
[Column("id")]
Expand Down
3 changes: 2 additions & 1 deletion exercise.wwwapi/Models/CourseModule.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using exercise.wwwapi.Enums;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace exercise.wwwapi.Models;

[Table("Course_Module")]
public class CourseModule
public class CourseModule : IEntity
{
[Key]
[Column("id")]
Expand Down
5 changes: 3 additions & 2 deletions exercise.wwwapi/Models/Exercise.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.ComponentModel.DataAnnotations;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.wwwapi.Models;

[Table("exercise")]
public class Exercise
public class Exercise : IEntity
{
[Key]
[Column("id")]
Expand Down
4 changes: 2 additions & 2 deletions exercise.wwwapi/Models/Like.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using exercise.wwwapi.Models.UserInfo;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.wwwapi.Models
{
[Table("Likes")]
public class Like
public class Like : IEntity
{
[Key]
[Column("id")]
Expand Down
5 changes: 3 additions & 2 deletions exercise.wwwapi/Models/Module.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.ComponentModel.DataAnnotations;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.wwwapi.Models;

[Table("modules")]
public class Module
public class Module : IEntity
{
[Key]
[Column("id")]
Expand Down
5 changes: 3 additions & 2 deletions exercise.wwwapi/Models/Note.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.ComponentModel.DataAnnotations;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace exercise.wwwapi.Models
{
[Table("notes")]
public class Note
public class Note : IEntity
{
[Key]
[Column("id")]
Expand Down
5 changes: 3 additions & 2 deletions exercise.wwwapi/Models/Post.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.ComponentModel.DataAnnotations;
using exercise.wwwapi.Repository;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace exercise.wwwapi.Models;

[Table("posts")]
public class Post
public class Post : IEntity
{
[Key]
[Column("id")]
Expand Down
Loading