From e3e7c4550f08ea73fb7952c76783c2598ae36a53 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 19 Feb 2024 10:06:44 +0300 Subject: [PATCH] feat: add a query to get schedule data --- .../GetScheduleList/GetScheduleListQuery.cs | 12 +++ .../GetScheduleListQueryHandler.cs | 85 +++++++++++++++++++ .../Queries/GetScheduleList/ScheduleListVm.cs | 14 +++ .../GetScheduleList/ScheduleLookupDto.cs | 80 +++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQuery.cs create mode 100644 Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQueryHandler.cs create mode 100644 Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleListVm.cs create mode 100644 Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleLookupDto.cs diff --git a/Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQuery.cs b/Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQuery.cs new file mode 100644 index 0000000..939e1c6 --- /dev/null +++ b/Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQuery.cs @@ -0,0 +1,12 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Schedule.Queries.GetScheduleList; + +public class GetScheduleListQuery : IRequest +{ + public int[]? GroupIds { get; set; } + public int[]? DisciplineIds { get; set; } + public int[]? LectureHallIds { get; set; } + public int[]? ProfessorIds { get; set; } + public bool? IsEven { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQueryHandler.cs b/Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQueryHandler.cs new file mode 100644 index 0000000..76993c1 --- /dev/null +++ b/Application/Cqrs/Schedule/Queries/GetScheduleList/GetScheduleListQueryHandler.cs @@ -0,0 +1,85 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Schedule.Queries.GetScheduleList; + +public class GetScheduleListQueryHandler(ILessonDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetScheduleListQuery request, CancellationToken cancellationToken) + { + var query = dbContext.Lessons.Include(l => l.LessonAssociations) + .ThenInclude(la => la.LectureHall) + .ThenInclude(lh => lh!.Campus) + .Include(l => l.LessonAssociations) + .ThenInclude(la => la.Professor) + .Include(l => l.Group) + .Include(l => l.TypeOfOccupation) + .Include(l => l.Discipline) + .AsQueryable(); + + if (request.IsEven != null) + query = query.Where(l => l.IsEven == request.IsEven); + + if (request.GroupIds != null && request.GroupIds.Length != 0) + query = query.Where(l => request.GroupIds.Contains(l.GroupId)); + + if (request.DisciplineIds != null && request.DisciplineIds.Length != 0) + query = query.Where(l => request.DisciplineIds.Contains(l.DisciplineId)); + + if (request.LectureHallIds != null && request.LectureHallIds.Length != 0) + query = query.Where(l => l.LessonAssociations!.Any(la => + la.LectureHallId != null && request.LectureHallIds.Contains(la.LectureHallId.Value))); + + if (request.ProfessorIds != null && request.ProfessorIds.Length != 0) + query = query.Where(l => + l.LessonAssociations!.Any(la => + la.ProfessorId != null && request.ProfessorIds.Contains(la.ProfessorId.Value))); + + var data = await query.ToArrayAsync(cancellationToken); + + var result = data.Select(l => new ScheduleLookupDto() + { + DayOfWeek = l.DayOfWeek, + PairNumber = l.PairNumber, + IsEven = l.IsEven, + Discipline = l.Discipline!.Name, + TypeOfOccupation = l.TypeOfOccupation!.ShortName, + + Group = l.Group!.Name, + GroupId = l.GroupId, + + LectureHalls = l.LessonAssociations! + .Where(la => !string.IsNullOrEmpty(la.LectureHall?.Name)) + .Select(la => la.LectureHall?.Name), + LectureHallsId = l.LessonAssociations! + .Where(la => la.LectureHallId != null) + .Select(la => la.LectureHallId), + + Campus = l.LessonAssociations! + .Where(la => !string.IsNullOrEmpty(la.LectureHall?.Campus?.CodeName)) + .Select(la => la.LectureHall?.Campus?.CodeName), + CampusId = l.LessonAssociations! + .Where(la => la.LectureHall?.Campus != null) + .Select(la => la.LectureHall?.CampusId), + + + Professors = l.LessonAssociations! + .Where(la => !string.IsNullOrEmpty(la.Professor?.Name)) + .Select(la => la.Professor?.Name), + ProfessorsId = l.LessonAssociations! + .Where(la => la.ProfessorId != null) + .Select(la => la.ProfessorId), + + LinkToMeet = l.LessonAssociations!.Select(la => la.LinkToMeet) + }).ToList(); + + return new ScheduleListVm + { + Schedules = result + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleListVm.cs b/Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleListVm.cs new file mode 100644 index 0000000..565e5b9 --- /dev/null +++ b/Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleListVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Schedule.Queries.GetScheduleList; + +/// +/// Represents a view model for a list of schedules. +/// +public class ScheduleListVm +{ + /// + /// Gets or sets the list of schedules. + /// + public IList Schedules { get; set; } = new List(); +} \ No newline at end of file diff --git a/Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleLookupDto.cs b/Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleLookupDto.cs new file mode 100644 index 0000000..028a190 --- /dev/null +++ b/Application/Cqrs/Schedule/Queries/GetScheduleList/ScheduleLookupDto.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Schedule.Queries.GetScheduleList; + +/// +/// Represents a data transfer object for schedule lookup. +/// +public class ScheduleLookupDto +{ + /// + /// Gets or sets the day of the week. + /// + public DayOfWeek DayOfWeek { get; set; } + + /// + /// Gets or sets the pair number. + /// + public int PairNumber { get; set; } + + /// + /// Gets or sets a value indicating whether the pair is on an even week. + /// + public bool IsEven { get; set; } + + /// + /// Gets or sets the name of the discipline. + /// + public required string Discipline { get; set; } + + /// + /// Gets or sets the type of occupation. + /// + public required string TypeOfOccupation { get; set; } + + /// + /// Gets or sets the name of the group. + /// + public required string Group { get; set; } + + /// + /// Gets or sets the ID of the group. + /// + public required int GroupId { get; set; } + + /// + /// Gets or sets the names of the lecture halls. + /// + public required IEnumerable LectureHalls { get; set; } + + /// + /// Gets or sets the IDs of the lecture halls. + /// + public required IEnumerable LectureHallsId { get; set; } + + /// + /// Gets or sets the names of the professors. + /// + public required IEnumerable Professors { get; set; } + + /// + /// Gets or sets the IDs of the professors. + /// + public required IEnumerable ProfessorsId { get; set; } + + /// + /// Gets or sets the names of the campuses. + /// + public required IEnumerable Campus { get; set; } + + /// + /// Gets or sets the IDs of the campuses. + /// + public required IEnumerable CampusId { get; set; } + + /// + /// Gets or sets the links to online meetings. + /// + public required IEnumerable LinkToMeet { get; set; } +} \ No newline at end of file