Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
*.exe
*.exe
mockery
57 changes: 38 additions & 19 deletions internal/handlers/bot/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ func (b *Bot) LetAhead(c telebot.Context) error {

// Выбрать предмет
func (b *Bot) ChooseSubject(c telebot.Context) error {
// Данные о пользователе
user := c.Get("user").(models.User)
entry := models.QueueEntry{
ChatID: fmt.Sprint(c.Chat().ID),
}

groups, err := b.scheduleService.GetGroups(b.ctx, user.Group)
if err != nil {
return err
Expand All @@ -144,12 +149,26 @@ func (b *Bot) ChooseSubject(c telebot.Context) error {
Subject: data,
}

// Проверяем находится ли в данной очереди человек
_, err := b.queueService.Pos(b.ctx, queue, entry)
if errors.Is(err, services.ErrNotFound) {
btnText.WriteRune('🟥')
} else if err == nil {
btnText.WriteRune('🟩')
} else {
return err
}

// Проверяем, есть ли уже очередь по этому предмету
_, err := b.queueService.Range(b.ctx, queue)
if err == nil {
btnText.WriteString("✅ ")
} else if errors.Is(err, services.ErrNotFound) {
btnText.WriteString("❌ ")
length, err := b.queueService.Len(b.ctx, queue)
if err != nil {
return err
}

if length != 0 {
fmt.Fprintf(&btnText, " (%d чел.) ", length)
} else {
btnText.WriteString(" (Пусто) ")
}
btnText.WriteString(subjects[i])

Expand Down Expand Up @@ -177,10 +196,6 @@ func (b *Bot) ChooseSubject(c telebot.Context) error {
Subject: subject,
}

entry := models.QueueEntry{
ChatID: fmt.Sprint(c.Chat().ID),
}

err = b.queueService.SaveToCache(b.ctx, c.Chat().ID, queue)
if err != nil {
return err
Expand Down Expand Up @@ -232,11 +247,11 @@ func (b *Bot) showSubject(
entry models.QueueEntry,
) error {
var sb strings.Builder
sb.WriteString("Очередь " + queue.Key())
sb.WriteString(queue.Key())

entries, err := b.queueService.Range(b.ctx, queue)
if errors.Is(err, services.ErrNotFound) {
sb.WriteString("\nОчередь не создана")
sb.WriteString("\nОчередь пуста")
} else if err == nil {
// Находим имена пользователей
for i, entry := range entries {
Expand All @@ -250,20 +265,24 @@ func (b *Bot) showSubject(
return err
}

sb.WriteString(fmt.Sprintf("\n%3d. %s", i+1, user.Name))
// Если это текущий пользователь, то выделяем жирным для видимости
if chatID == c.Chat().ID {
fmt.Fprintf(&sb, "\n*%3d. %s*", i+1, user.Name)
} else {
fmt.Fprintf(&sb, "\n%3d. %s", i+1, user.Name)
}
}

// Находим позицию текущего пользователя
pos, err := b.queueService.Pos(b.ctx, queue, entry)

msgText := fmt.Sprintf("\nВаша текущая позиция в очереди - %d", pos)
if errors.Is(err, services.ErrNotFound) {
msgText = "\nВы не записаны в очередь"
} else if err != nil {
if err == nil {
fmt.Fprintf(&sb, "\nВы %d в очереди", pos)
} else if errors.Is(err, services.ErrNotFound) {
sb.WriteString("\nВы не записаны в очередь")
} else {
return err
}

sb.WriteString(msgText)
} else {
return err
}
Expand All @@ -273,7 +292,7 @@ func (b *Bot) showSubject(
menu = b.subjectAdminMenu
}

err = c.Edit(sb.String(), menu)
err = c.Edit(sb.String(), menu, telebot.ModeMarkdown)
if err != nil && !errors.Is(err, telebot.ErrSameMessageContent) {
return err
}
Expand Down
17 changes: 12 additions & 5 deletions internal/handlers/bot/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,21 @@ func (b *Bot) getUser(c tele.Context) (models.User, error) {

// Функция отображения профиля
func (b *Bot) showProfile(c tele.Context, user models.User) error {
profile := fmt.Sprintf(
"Группа: %s\nФИО: %s\nПрава админа: %t",
user.Group, user.Name, user.QueueAccess,
var profileStr strings.Builder
fmt.Fprintf(
&profileStr,
"ФИО: %s\nГруппа: %s\nДоступ к очереди: ",
user.Name, user.Group,
)
if user.QueueAccess {
profileStr.WriteString("✔️")
} else {
profileStr.WriteString("✖️")
}
if msg, ok := c.Get("msg").(tele.Editable); ok {
_, err := c.Bot().Edit(msg, profile, b.startMenu)
_, err := c.Bot().Edit(msg, profileStr.String(), b.startMenu)
return err
} else {
return c.Send(profile, b.startMenu)
return c.Send(profileStr.String(), b.startMenu)
}
}
5 changes: 5 additions & 0 deletions internal/interfaces/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type QueueService interface {
queue models.Queue,
entry models.QueueEntry,
) error
// Получает длину очереди
Len(
ctx context.Context,
queue models.Queue,
) (int64, error)
}

// Сервис пользователей
Expand Down
3 changes: 0 additions & 3 deletions internal/repositories/redis/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ func (s *Storage) Len(

len, err := s.cl.LLen(ctx, queue.Key()).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return 0, fmt.Errorf("%s: %w", op, repositories.ErrNotFound)
}
return 0, fmt.Errorf("%s: %w", op, err)
}

Expand Down
28 changes: 28 additions & 0 deletions internal/services/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,31 @@ func (q *Queue) Remove(

return nil
}

func (q *Queue) Len(
ctx context.Context,
queue models.Queue,
) (int64, error) {
const op = "queue.Len"

log := q.log.With(
slog.String("op", op),
slog.String("queue_group", queue.Group),
slog.String("queue_subject", queue.Subject),
)

log.Info("Trying to get queue length")

length, err := q.queueLength.Len(ctx, queue)
if err != nil {
log.Error("Failed to get queue length",
slog.String("err", err.Error()),
)

return 0, fmt.Errorf("%s: %w", op, err)
}

log.Info("Successfully got")

return length, nil
}