From a814e39026689a3b51255024c714b4bd62e69eaf Mon Sep 17 00:00:00 2001 From: wh0o7 Date: Mon, 23 Oct 2023 10:31:35 +0300 Subject: [PATCH] Fix bugs with changing teachers and subscription --- TeachersTimetable/Services/AccountService.cs | 11 +++++++---- TeachersTimetable/Services/CommandsService.cs | 4 ++-- TeachersTimetable/Services/Utils.cs | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/TeachersTimetable/Services/AccountService.cs b/TeachersTimetable/Services/AccountService.cs index 1d6e7c9..0ff70f4 100644 --- a/TeachersTimetable/Services/AccountService.cs +++ b/TeachersTimetable/Services/AccountService.cs @@ -57,8 +57,9 @@ public async Task ChangeTeacher(User telegramUser, string? teacherName) teacherNames[i] = teacherNames[i].Trim(); } - var correctTeacherNames = this._parseService.Teachers.Where(g => teacherNames.Any(teacher => - g.ToLower().Trim().Contains(teacher.ToLower().Trim()))).ToArray(); + var correctTeacherNames = teacherNames + .Select(t => this._parseService.Teachers.FirstOrDefault(teacher => + teacher.StartsWith(t, StringComparison.InvariantCultureIgnoreCase))).Where(correctTeacher => correctTeacher is not null).ToArray(); if (correctTeacherNames is null || correctTeacherNames.Length == 0) { @@ -131,8 +132,10 @@ public async Task UpdateNotificationsStatus(User telegramUser) this._botService.SendMessage(new SendMessageArgs(telegramUser.Id, user.Notifications - ? $"Вы успешно подписались на расписание преподавателя {user.Teachers}" - : $"Вы успешно отменили подписку на расписание преподавателя {user.Teachers}") + ? user.Teachers.Length == 1 + ? $"Вы успешно подписались на расписание преподавателя {user.Teachers[0]}" + : $"Вы успешно подписались на расписание преподавателей: {Utils.GetTeachersString(user.Teachers)}" + : "Вы успешно отменили подписку") { ReplyMarkup = keyboard }); diff --git a/TeachersTimetable/Services/CommandsService.cs b/TeachersTimetable/Services/CommandsService.cs index cb58644..ca9518c 100644 --- a/TeachersTimetable/Services/CommandsService.cs +++ b/TeachersTimetable/Services/CommandsService.cs @@ -110,7 +110,7 @@ private async void OnMessageReceive(Message message) case "Сменить преподавателя": { this._botService.SendMessage(new SendMessageArgs(sender.Id, - $"Для оформления подписки на преподавателей отправьте их фамилии.(Максимум - 5. Пример: Кулецкая, Шавейко, Потоцкий, Левонюк, Протасеня)\nВаши выбранные преподаватели: ```{string.Join(", ", (await this._accountService.GetUserById(sender.Id))?.Teachers ?? Array.Empty())}```") + $"Для оформления подписки на преподавателей отправьте их фамилии.(Максимум - 5. Пример: Кулецкая, Шавейко, Потоцкий, Левонюк, Протасеня)\nВаши выбранные преподаватели: ```\n{Utils.GetTeachersString((await this._accountService.GetUserById(sender.Id))?.Teachers ?? Array.Empty())}\n```") { ParseMode = ParseMode.Markdown }); @@ -165,4 +165,4 @@ private async void OnMessageReceive(Message message) } } } -} +} \ No newline at end of file diff --git a/TeachersTimetable/Services/Utils.cs b/TeachersTimetable/Services/Utils.cs index ad6d4a0..b32951d 100644 --- a/TeachersTimetable/Services/Utils.cs +++ b/TeachersTimetable/Services/Utils.cs @@ -101,5 +101,5 @@ public static bool IsDateBelongsToInterval(DateTime? date, DateTime?[]? interval return null; } - public static string GetTeachersString(string[] teachers) => string.Join(", ", teachers); + public static string GetTeachersString(string?[] teachers) => string.Join(", ", teachers); } \ No newline at end of file