Files
tiku-backend.net/Tiku.Api/Controllers/ScorelineController.cs
xiong 4bea745b79 feat: Add tags and endpoint summaries to various controllers for better API documentation
- Added tags to BrowserAuthController for browser authentication endpoints.
- Added tags to CatalogController for public catalog access.
- Added tags to CommerceController for student transaction operations.
- Added tags to CommissionController for tenant commission management.
- Added tags to CrmController for tenant CRM functionalities.
- Added tags to HealthController for system health checks.
- Added tags to LearningController for student learning resources.
- Added tags to MeController for current user information.
- Added tags to PlatformAdminController for platform management.
- Introduced PlatformBackofficeController for backend permissions management.
- Added tags to PlatformBillingCallbackController for billing callbacks.
- Added tags to PlatformPaymentSettingsController for payment settings management.
- Added tags to PlatformSaasController for SaaS package management.
- Added tags to PlatformTenantCapabilitiesController for tenant capabilities.
- Added tags to PointsController for student points management.
- Added tags to ProfileController for student profile management.
- Added tags to QuestionVideosController for question video resources.
- Added tags to ReferralController for referral growth management.
- Added tags to RuntimeController for runtime configurations.
- Added tags to ScorelineController for scoreline management.
- Added tags to TaxonomyController for category management.
- Added tags to TenantAdminDirectController for tenant operations management.
- Introduced TenantBackofficeController for tenant backend permissions.
- Added tags to TenantBillingController for tenant billing operations.
- Added tags to TenantCommerceController for tenant commerce operations.
- Added tags to TenantContentController for tenant content management.
- Added tags to TenantContentDirectController for direct content management.
- Added tags to TenantFrontendConfigController for frontend configurations.
- Added tags to TenantOnboardingController for onboarding guidance.
- Added tags to TenantPublicController for public tenant configurations.
- Added tags to TenantsController for current tenant information.
- Added tags to VideosController for student video resources.
2026-07-29 16:16:27 +08:00

129 lines
4.6 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Tiku.Api.Contracts;
using Tiku.Application.Catalog;
using Tiku.Application.Scoreline;
using Tiku.Application.Security;
using Tiku.Domain.Tenancy;
using Tiku.Infrastructure.Persistence;
namespace Tiku.Api.Controllers;
[ApiController]
[Tags("学生端-分数线")]
[AllowAnonymous]
[Tiku.Api.Security.RequireSaasFeature(SaasFeatureCatalog.Scoreline)]
[Produces("application/json")]
[Route("api/scoreline")]
public sealed class ScorelineController(
IScorelineQueryService scorelineQueryService,
ITenantContext currentTenant,
TikuDbContext dbContext) : ControllerBase
{
private static readonly string[] DynamicPrefixes = ["field.", "min.", "max."];
[HttpGet("fields")]
[EndpointSummary("查询分数线字段配置")]
[ProducesResponseType<CatalogList<ScorelineFieldItem>>(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public async Task<ActionResult<CatalogList<ScorelineFieldItem>>> GetFields(
[FromQuery] ScorelineQueryDto query,
CancellationToken cancellationToken)
{
return Ok(await scorelineQueryService.GetFieldsAsync(
query.ToFilter(await ResolveTenantIdAsync(query, cancellationToken)),
cancellationToken));
}
[HttpGet("records")]
[EndpointSummary("分页查询分数线记录")]
[ProducesResponseType<ScorelineRecordPage>(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public async Task<ActionResult<ScorelineRecordPage>> GetRecords(
[FromQuery] ScorelineQueryDto query,
CancellationToken cancellationToken)
{
return Ok(await scorelineQueryService.GetRecordsAsync(
query.ToFilter(await ResolveTenantIdAsync(query, cancellationToken), ResolveDynamicFilters()),
cancellationToken));
}
[HttpGet("trend")]
[EndpointSummary("查询历年分数线趋势")]
[ProducesResponseType<CatalogList<ScorelineRecordItem>>(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public async Task<ActionResult<CatalogList<ScorelineRecordItem>>> GetTrend(
[FromQuery] ScorelineQueryDto query,
CancellationToken cancellationToken)
{
return Ok(await scorelineQueryService.GetTrendAsync(
query.ToFilter(await ResolveTenantIdAsync(query, cancellationToken), ResolveDynamicFilters()),
cancellationToken));
}
[HttpGet("years")]
[EndpointSummary("查询分数线可用年份")]
[ProducesResponseType<CatalogList<int>>(StatusCodes.Status200OK)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public async Task<ActionResult<CatalogList<int>>> GetYears(
[FromQuery] ScorelineQueryDto query,
CancellationToken cancellationToken)
{
return Ok(await scorelineQueryService.GetYearsAsync(
query.ToFilter(await ResolveTenantIdAsync(query, cancellationToken)),
cancellationToken));
}
private IReadOnlyCollection<ScorelineDynamicFilter> ResolveDynamicFilters()
{
var filters = new List<ScorelineDynamicFilter>();
foreach (var (key, value) in Request.Query)
{
var prefix = DynamicPrefixes.FirstOrDefault(key.StartsWith);
if (prefix is null)
{
continue;
}
var raw = value.FirstOrDefault();
if (string.IsNullOrWhiteSpace(raw))
{
continue;
}
filters.Add(new ScorelineDynamicFilter(
prefix.TrimEnd('.'),
key[prefix.Length..],
raw.Trim()));
}
return filters;
}
private async Task<Guid> ResolveTenantIdAsync(
ScorelineQueryDto query,
CancellationToken cancellationToken)
{
if (currentTenant.TenantId.HasValue)
{
return currentTenant.TenantId.Value;
}
var tenantCode = query.TenantCode ?? Request.Headers["x-tenant-code"].FirstOrDefault();
if (string.IsNullOrWhiteSpace(tenantCode))
{
throw new TenantNotFoundException();
}
var tenantId = await dbContext.Tenants
.Where(tenant =>
tenant.Slug == tenantCode.Trim() &&
tenant.Status == TenantStatus.Active)
.Select(tenant => (Guid?)tenant.Id)
.SingleOrDefaultAsync(cancellationToken);
return tenantId ?? throw new TenantNotFoundException();
}
}