first commit
This commit is contained in:
58
backend/Controllers/AdminController.cs
Normal file
58
backend/Controllers/AdminController.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using backend.Data;
|
||||
using backend.DTOs;
|
||||
using backend.Models;
|
||||
|
||||
namespace backend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AdminController : ControllerBase
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly AppConfig _appConfig;
|
||||
|
||||
public AdminController(AppDbContext db, IOptions<AppConfig> appConfig)
|
||||
{
|
||||
_db = db;
|
||||
_appConfig = appConfig.Value;
|
||||
}
|
||||
|
||||
[HttpGet("report")]
|
||||
public async Task<IActionResult> GetReport([FromQuery] string password)
|
||||
{
|
||||
if (password != _appConfig.AdminPassword)
|
||||
{
|
||||
return StatusCode(403, new { message = "رمز عبور اشتباه است." });
|
||||
}
|
||||
|
||||
var users = await _db.Users
|
||||
.Include(u => u.QuizAttempt)
|
||||
.Include(u => u.KhatmSelections)
|
||||
.ThenInclude(ks => ks.KhatmItem)
|
||||
.Include(u => u.TotalScore)
|
||||
.OrderBy(u => u.Id)
|
||||
.ToListAsync();
|
||||
|
||||
var report = users.Select(u => new AdminReportItem
|
||||
{
|
||||
UserId = u.Id,
|
||||
FullName = u.FullName,
|
||||
Contact = u.Contact,
|
||||
TrackingCode = u.TrackingCode,
|
||||
HasTakenQuiz = u.QuizAttempt != null,
|
||||
ParticipationPoints = u.QuizAttempt?.ParticipationPoints ?? 0,
|
||||
CorrectCount = u.QuizAttempt?.CorrectAnswersCount ?? 0,
|
||||
TotalQuizPoints = u.QuizAttempt?.TotalQuizPoints ?? 0,
|
||||
KhatmTitles = string.Join("، ",
|
||||
u.KhatmSelections
|
||||
.Where(ks => ks.IsSelected)
|
||||
.Select(ks => ks.KhatmItem.Title)),
|
||||
TotalScore = u.TotalScore?.TotalPoints ?? 0
|
||||
}).ToList();
|
||||
|
||||
return Ok(report);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user