diff --git a/src/Jiaowu.Api/Controllers/ClassroomReservationsController.cs b/src/Jiaowu.Api/Controllers/ClassroomReservationsController.cs index 8c41377..d4006ab 100644 --- a/src/Jiaowu.Api/Controllers/ClassroomReservationsController.cs +++ b/src/Jiaowu.Api/Controllers/ClassroomReservationsController.cs @@ -5,10 +5,12 @@ using Jiaowu.Api.Domain.Academic; using Jiaowu.Api.Domain.Identity; using Jiaowu.Api.Infrastructure.Auth; using Jiaowu.Api.Infrastructure.Persistence; +using Jiaowu.Api.Infrastructure.Localization; using Jiaowu.Api.Infrastructure.Timetables; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Localization; namespace Jiaowu.Api.Controllers; @@ -18,8 +20,28 @@ namespace Jiaowu.Api.Controllers; public sealed class ClassroomReservationsController( AppDbContext db, ICurrentUserDataScope scope, - ClassroomReservationAvailabilityService availabilityService) : ControllerBase + ClassroomReservationAvailabilityService availabilityService, + IStringLocalizer? localizer = null) : ControllerBase { + private static readonly IReadOnlyDictionary ClassroomReservationsErrorKeys = new Dictionary + { + ["请填写借用用途。"] = "ClassroomReservations.Application.PurposeRequired", + ["请填写联系电话。"] = "ClassroomReservations.Application.PhoneRequired", + ["所选教室在该时段已有课程、考试或已批准预约。"] = "ClassroomReservations.Room.UnavailableAtTime", + ["您已提交过同一教室、重叠时段的申请。"] = "ClassroomReservations.Application.OverlappingDuplicate", + ["当前状态不能取消。"] = "ClassroomReservations.Application.CannotCancelCurrentState", + ["已过期的预约不能取消。"] = "ClassroomReservations.Application.ExpiredCannotCancel", + ["驳回时必须填写审核意见。"] = "ClassroomReservations.Review.CommentRequired", + ["该申请已处理,不能重复审核。"] = "ClassroomReservations.Review.AlreadyProcessed", + ["预约日期已过,不能批准。"] = "ClassroomReservations.Review.DatePassed", + ["预约范围包含不存在的节次。"] = "ClassroomReservations.Periods.NotFound", + ["预约范围包含不存在或未启用的节次。"] = "ClassroomReservations.Periods.Unavailable", + ["学院管理员账号未配置所属学院,无法审核。"] = "ClassroomReservations.Review.CollegeNotConfigured", + ["申请人数已超过教室当前容量,不能批准。"] = "ClassroomReservations.Review.ApplicantCountExceedsCapacity", + ["预约日期不能早于今天。"] = "ClassroomReservations.Date.CannotBeInPast", + ["预约日期不在所选学期范围内。"] = "ClassroomReservations.Date.OutsideTerm", + }; + [HttpGet("options")] public async Task GetOptions( Guid? academicTermId, @@ -623,11 +645,17 @@ public sealed class ClassroomReservationsController( private ActionResult ConflictProblem(string detail) => Conflict(new ProblemDetails { - Title = "无法完成教室预约", - Detail = detail, + Title = SharedResourceText.Get(localizer, "ClassroomReservations.Errors.Title"), + Detail = LocalizeClassroomReservationMessage(detail), Status = StatusCodes.Status409Conflict }); + private ActionResult ValidationProblem(string detail) => + base.ValidationProblem(detail: LocalizeClassroomReservationMessage(detail)); + + private string LocalizeClassroomReservationMessage(string detail) => + ClassroomReservationsErrorKeys.TryGetValue(detail, out var key) ? SharedResourceText.Get(localizer, key) : detail; + private static string? Normalize(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim(); diff --git a/src/Jiaowu.Api/Controllers/CourseAdjustmentsController.cs b/src/Jiaowu.Api/Controllers/CourseAdjustmentsController.cs index 4c05e1c..0782690 100644 --- a/src/Jiaowu.Api/Controllers/CourseAdjustmentsController.cs +++ b/src/Jiaowu.Api/Controllers/CourseAdjustmentsController.cs @@ -4,12 +4,14 @@ using Jiaowu.Api.Domain.Academic; using Jiaowu.Api.Domain.Identity; using Jiaowu.Api.Infrastructure.Auth; using Jiaowu.Api.Infrastructure.Persistence; +using Jiaowu.Api.Infrastructure.Localization; using Jiaowu.Api.Infrastructure.Timetables; using Jiaowu.Api.Infrastructure.Scheduling; using Jiaowu.Api.Infrastructure.Teaching; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Localization; namespace Jiaowu.Api.Controllers; @@ -18,8 +20,30 @@ namespace Jiaowu.Api.Controllers; [Route("api/course-adjustments")] public sealed class CourseAdjustmentsController( AppDbContext db, - ICurrentUserDataScope currentUserDataScope) : ControllerBase + ICurrentUserDataScope currentUserDataScope, + IStringLocalizer? localizer = null) : ControllerBase { + private static readonly IReadOnlyDictionary CourseAdjustmentsErrorKeys = new Dictionary + { + ["每批条数应在 1 至 100 之间。"] = "CourseAdjustments.PageSize.Range", + ["调停课申请游标无效。"] = "CourseAdjustments.Cursor.Invalid", + ["待审核调停课游标无效。"] = "CourseAdjustments.PendingCursor.Invalid", + ["只有草稿可以提交。"] = "CourseAdjustments.Application.DraftOnlyForSubmit", + ["只能审核本学院课程的调停课申请。"] = "CourseAdjustments.Review.CollegeScopeOnly", + ["只有待审核的申请可以审批。"] = "CourseAdjustments.Review.PendingOnlyApprove", + ["只有待审核的申请可以退回。"] = "CourseAdjustments.Review.PendingOnlyReturn", + ["教学班不存在或未发布。"] = "CourseAdjustments.Task.Unavailable", + ["请选择起始节次。"] = "CourseAdjustments.Schedule.StartPeriodRequired", + ["请选择代课教师。"] = "CourseAdjustments.SubstituteTeacher.Required", + ["代课教师不存在或已离职。"] = "CourseAdjustments.SubstituteTeacher.Unavailable", + ["请选择当前已发布课表中的具体原课次。"] = "CourseAdjustments.Schedule.SourceOccurrenceRequired", + ["该课次已有待审核或已通过的调停课记录。"] = "CourseAdjustments.Schedule.AdjustmentAlreadyExists", + ["目标日期必须在本学期起止日期内。"] = "CourseAdjustments.Schedule.DateOutsideTerm", + ["目标教室不存在、已停用或正在维修。"] = "CourseAdjustments.Room.Unavailable", + ["原课次已不在当前发布课表中,请退回后重新选择。"] = "CourseAdjustments.Schedule.SourceOccurrenceNoLongerPublished", + ["目标上课日期或节次不完整。"] = "CourseAdjustments.Schedule.TargetIncomplete", + }; + private const string Applicants = SystemRoles.SuperAdmin + "," + SystemRoles.AcademicAdmin + "," + @@ -184,7 +208,7 @@ public sealed class CourseAdjustmentsController( if (pageSize is < 1 or > 100) return ValidationProblem("每批条数应在 1 至 100 之间。"); if (!CursorToken.TryDecode(cursor, out var after)) - return BadRequest("调停课申请游标无效。"); + return BadRequest(SharedResourceText.Get(localizer, "CourseAdjustments.Cursor.Invalid")); var userId = currentUserDataScope.Current.UserId; var source = db.CourseAdjustments.AsNoTracking() @@ -232,7 +256,7 @@ public sealed class CourseAdjustmentsController( if (pageSize is < 1 or > 100) return ValidationProblem("每批条数应在 1 至 100 之间。"); if (!CursorToken.TryDecode(cursor, out var after)) - return BadRequest("待审核调停课游标无效。"); + return BadRequest(SharedResourceText.Get(localizer, "CourseAdjustments.PendingCursor.Invalid")); var scope = currentUserDataScope.Current; var source = db.CourseAdjustments.AsNoTracking() @@ -790,18 +814,18 @@ public sealed class CourseAdjustmentsController( ScheduleEntry source, int startWeek, int endWeek) => new() - { - SchedulePlanId = source.SchedulePlanId, - TeachingTaskId = source.TeachingTaskId, - ClassroomId = source.ClassroomId, - DayOfWeek = source.DayOfWeek, - StartPeriod = source.StartPeriod, - PeriodCount = source.PeriodCount, - StartWeek = startWeek, - EndWeek = endWeek, - WeekPattern = source.WeekPattern, - Notes = source.Notes - }; + { + SchedulePlanId = source.SchedulePlanId, + TeachingTaskId = source.TeachingTaskId, + ClassroomId = source.ClassroomId, + DayOfWeek = source.DayOfWeek, + StartPeriod = source.StartPeriod, + PeriodCount = source.PeriodCount, + StartWeek = startWeek, + EndWeek = endWeek, + WeekPattern = source.WeekPattern, + Notes = source.Notes + }; private static bool RequiresSourceOccurrence(CourseAdjustmentType type) => type is CourseAdjustmentType.Reschedule or @@ -852,46 +876,46 @@ public sealed class CourseAdjustmentsController( private System.Linq.Expressions.Expression< Func> AdjustmentProjection() => x => new - { - x.Id, - x.TeachingTaskId, - x.TeachingTask!.TaskNumber, - TaskName = x.TeachingTask.Name, - CourseCode = x.TeachingTask.Course!.Code, - CourseName = x.TeachingTask.Course.Name, - CollegeName = x.TeachingTask.Course.College!.Name, - TeacherNames = x.TeachingTask.Teachers + { + x.Id, + x.TeachingTaskId, + x.TeachingTask!.TaskNumber, + TaskName = x.TeachingTask.Name, + CourseCode = x.TeachingTask.Course!.Code, + CourseName = x.TeachingTask.Course.Name, + CollegeName = x.TeachingTask.Course.College!.Name, + TeacherNames = x.TeachingTask.Teachers .OrderByDescending(t => t.IsPrimary) .Select(t => t.Teacher!.Name), - x.Type, - x.Status, - x.Reason, - x.ReviewComment, - x.SourceScheduleEntryId, - x.SourceWeek, - x.SourceDate, - x.SourceStartPeriod, - x.SourcePeriodCount, - SourceClassroomName = x.SourceClassroomId == null + x.Type, + x.Status, + x.Reason, + x.ReviewComment, + x.SourceScheduleEntryId, + x.SourceWeek, + x.SourceDate, + x.SourceStartPeriod, + x.SourcePeriodCount, + SourceClassroomName = x.SourceClassroomId == null ? null : db.Classrooms .Where(room => room.Id == x.SourceClassroomId) .Select(room => room.Building!.Name + " " + room.Name) .FirstOrDefault(), - x.TargetDate, - x.DayOfWeek, - x.StartPeriod, - x.PeriodCount, - ClassroomName = x.Classroom != null ? x.Classroom.Name : null, - BuildingName = x.Classroom != null ? x.Classroom.Building!.Name : null, - SubstituteTeacherName = x.SubstituteTeacher != null ? x.SubstituteTeacher.Name : null, - x.CancelWeek, - x.CancelDate, - x.ApplicantUserId, - x.SubmittedAt, - x.ReviewedAt, - x.CreatedAt - }; + x.TargetDate, + x.DayOfWeek, + x.StartPeriod, + x.PeriodCount, + ClassroomName = x.Classroom != null ? x.Classroom.Name : null, + BuildingName = x.Classroom != null ? x.Classroom.Building!.Name : null, + SubstituteTeacherName = x.SubstituteTeacher != null ? x.SubstituteTeacher.Name : null, + x.CancelWeek, + x.CancelDate, + x.ApplicantUserId, + x.SubmittedAt, + x.ReviewedAt, + x.CreatedAt + }; private sealed record SourceOccurrence( ScheduleEntry Entry, @@ -902,10 +926,16 @@ public sealed class CourseAdjustmentsController( private ActionResult ConflictProblem(string detail) => Conflict(new ProblemDetails { - Title = "无法完成操作", - Detail = detail, + Title = SharedResourceText.Get(localizer, "CourseAdjustments.Errors.Title"), + Detail = LocalizeCourseAdjustmentMessage(detail), Status = StatusCodes.Status409Conflict }); + + private ActionResult ValidationProblem(string detail) => + base.ValidationProblem(detail: LocalizeCourseAdjustmentMessage(detail)); + + private string LocalizeCourseAdjustmentMessage(string detail) => + CourseAdjustmentsErrorKeys.TryGetValue(detail, out var key) ? SharedResourceText.Get(localizer, key) : detail; } // ═══════════════ Request records ═══════════════ diff --git a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx index f0c82d2..d6c8b1e 100644 --- a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx +++ b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.en-US.resx @@ -536,4 +536,39 @@ The room number is required and must be no longer than 30 characters. The floor must be between 1 and 100. The number of beds must be between 1 and 20. + Enter the purpose of the reservation. + Enter a contact phone number. + The selected room already has a class, exam or approved reservation at this time. + You already have a reservation request for this room at an overlapping time. + The reservation cannot be cancelled in its current state. + Expired reservations cannot be cancelled. + Enter a review comment when rejecting the request. + This request has already been processed and cannot be reviewed again. + A reservation date in the past cannot be approved. + The reservation includes a class period that does not exist. + The reservation includes a nonexistent or disabled class period. + The college administrator account has no college assigned and cannot review requests. + Classroom reservation could not be completed + The page size must be between 1 and 100. + The course adjustment cursor is invalid. + The pending course adjustment cursor is invalid. + Only draft requests can be submitted. + You can only review course adjustment requests for your college. + Only requests awaiting review can be approved. + Only requests awaiting review can be returned. + The teaching class does not exist or is unpublished. + Select a starting period. + Select a substitute teacher. + The substitute teacher does not exist or is no longer active. + Select the specific original class occurrence from the published timetable. + This class occurrence already has a pending or approved adjustment. + The target date must fall within the term. + The target room does not exist, is disabled or is under maintenance. + The original class is no longer on the published timetable. Return the request and select it again. + The target class date or period is incomplete. + The operation could not be completed + The number of applicants exceeds the room capacity of {0}. + The number of applicants exceeds the current room capacity, so the reservation cannot be approved. + The reservation date cannot be in the past. + The reservation date is outside the selected term. diff --git a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx index 4251f37..2ea5266 100644 --- a/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx +++ b/src/Jiaowu.Api/Resources/Infrastructure/Localization/SharedResource.resx @@ -536,4 +536,39 @@ DormitoryServices.Room.NumberLength DormitoryServices.Room.FloorRange DormitoryServices.Room.BedCountRange + ClassroomReservations.Application.PurposeRequired + ClassroomReservations.Application.PhoneRequired + ClassroomReservations.Room.UnavailableAtTime + ClassroomReservations.Application.OverlappingDuplicate + ClassroomReservations.Application.CannotCancelCurrentState + ClassroomReservations.Application.ExpiredCannotCancel + ClassroomReservations.Review.CommentRequired + ClassroomReservations.Review.AlreadyProcessed + ClassroomReservations.Review.DatePassed + ClassroomReservations.Periods.NotFound + ClassroomReservations.Periods.Unavailable + ClassroomReservations.Review.CollegeNotConfigured + ClassroomReservations.Errors.Title + CourseAdjustments.PageSize.Range + CourseAdjustments.Cursor.Invalid + CourseAdjustments.PendingCursor.Invalid + CourseAdjustments.Application.DraftOnlyForSubmit + CourseAdjustments.Review.CollegeScopeOnly + CourseAdjustments.Review.PendingOnlyApprove + CourseAdjustments.Review.PendingOnlyReturn + CourseAdjustments.Task.Unavailable + CourseAdjustments.Schedule.StartPeriodRequired + CourseAdjustments.SubstituteTeacher.Required + CourseAdjustments.SubstituteTeacher.Unavailable + CourseAdjustments.Schedule.SourceOccurrenceRequired + CourseAdjustments.Schedule.AdjustmentAlreadyExists + CourseAdjustments.Schedule.DateOutsideTerm + CourseAdjustments.Room.Unavailable + CourseAdjustments.Schedule.SourceOccurrenceNoLongerPublished + CourseAdjustments.Schedule.TargetIncomplete + CourseAdjustments.Errors.Title + 申请人数超过教室容量({0} 人)。 + 申请人数已超过教室当前容量,不能批准。 + 预约日期不能早于今天。 + 预约日期不在所选学期范围内。