This commit is contained in:
2026-02-27 19:17:40 -08:00
parent b6b058f05e
commit 0fb4effd26
6 changed files with 94 additions and 28 deletions
@@ -2,6 +2,7 @@ using System.Data;
using Dapper;
using MySql.Data.MySqlClient;
using WinStudentGoalTracker.Models;
using WinStudentGoalTracker.Services;
namespace WinStudentGoalTracker.DataAccess;
@@ -10,33 +11,21 @@ public class StudentRepository
private IDbConnection Connection => new MySqlConnection(DatabaseManager.ConnectionString);
public async Task<IEnumerable<dbStudent>> GetMyStudentsAsync(Guid userId, Guid programId, string role)
{
return role switch
{
UserRoles.Teacher or UserRoles.ProgramAdmin =>
await GetStudentsByProgramAsync(programId),
UserRoles.Paraeducator =>
await GetAssignedStudentsAsync(userId, programId),
_ => Enumerable.Empty<dbStudent>()
};
}
public async Task<IEnumerable<dbStudent>> GetStudentsByProgramAsync(Guid programId)
{
using var db = Connection;
return await db.QueryAsync<dbStudent>(
"sp_Student_GetByProgram",
new { p_id_program = programId.ToString() },
using var multi = await db.QueryMultipleAsync(
"sp_Student_GetWithAssignments",
new { p_id_program = programId.ToString(), p_id_user = userId.ToString() },
commandType: CommandType.StoredProcedure);
}
private async Task<IEnumerable<dbStudent>> GetAssignedStudentsAsync(Guid userId, Guid programId)
{
using var db = Connection;
return await db.QueryAsync<dbStudent>(
"sp_Student_GetByUserAndProgram",
new { p_id_user = userId.ToString(), p_id_program = programId.ToString() },
commandType: CommandType.StoredProcedure);
var students = await multi.ReadAsync<dbStudent>();
var assignments = await multi.ReadAsync<dbUserStudent>();
var myStudents = students.Where(s =>
PermissionService.IsAllowed(role, EntityType.Student, PermissionAction.Read , assignments.Any(a => a.IdStudent == s.IdStudent && a.IdUser == userId))
);
return myStudents;
}
public async Task<dbStudent?> GetByIdAsync(Guid idStudent)