feat: добавил каталог достижений и автоначисление
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 10s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 2m53s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 28s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 7s
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 10s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 2m53s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 28s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 7s
Реализовал автосоздание и обновление каталога достижений на бэке и синхронизацию на фронте.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using UniVerse.Domain.Entities;
|
||||
|
||||
namespace UniVerse.Infrastructure.Data;
|
||||
|
||||
public static class AchievementCatalogSeeder
|
||||
{
|
||||
private static readonly IReadOnlyList<AchievementSeed> Catalog =
|
||||
[
|
||||
new(1001, "Добро пожаловать в UniVerse", "Совершить первое действие: записаться на лекцию, оставить отзыв или посетить занятие.", "sparkles", 10, "first_activity:1"),
|
||||
new(1002, "Первый шаг", "Посетить первую открытую лекцию.", "book-2", 10, "lectures_attended:1"),
|
||||
new(1003, "Вошел во вкус", "Посетить 3 открытые лекции.", "books", 20, "lectures_attended:3"),
|
||||
new(1004, "Постоянный слушатель", "Посетить 5 открытых лекций.", "calendar-event", 35, "lectures_attended:5"),
|
||||
new(1005, "Академический марафон", "Посетить 10 открытых лекций.", "stopwatch", 60, "lectures_attended:10"),
|
||||
new(1006, "Грандмастер лекций", "Посетить 25 открытых лекций.", "trophy", 120, "lectures_attended:25"),
|
||||
new(1007, "Первый отзыв", "Оставить первый отзыв о посещенной лекции.", "message-circle", 10, "reviews_written:1"),
|
||||
new(1008, "Голос аудитории", "Оставить 3 отзыва о лекциях.", "thumb-up", 25, "reviews_written:3"),
|
||||
new(1009, "Рецензент", "Оставить 10 отзывов о лекциях.", "clipboard-list", 70, "reviews_written:10"),
|
||||
new(1010, "Голос перемен", "Оставить 25 отзывов о лекциях.", "chart-line", 150, "reviews_written:25"),
|
||||
new(1011, "Смелый выбор", "Записаться на первую открытую лекцию.", "calendar", 5, "lectures_registered:1"),
|
||||
new(1012, "План на неделю", "Иметь 3 активные записи на будущие лекции.", "calendar-event", 15, "active_registrations:3"),
|
||||
new(1013, "Полный календарь", "Иметь 5 активных записей на будущие лекции.", "alarm", 30, "active_registrations:5"),
|
||||
new(1014, "Серия интереса", "Посещать открытые лекции 3 недели подряд.", "star", 50, "attendance_streak_weeks:3"),
|
||||
new(1015, "Учебный месяц", "Посещать открытые лекции 4 недели подряд.", "sparkles", 80, "attendance_streak_weeks:4"),
|
||||
new(1016, "Без пропусков", "Посетить 5 лекций, на которые была оформлена запись.", "circle-check", 40, "attended_registered:5"),
|
||||
new(1017, "Надежный участник", "Посетить 10 лекций, на которые была оформлена запись.", "shield", 75, "attended_registered:10"),
|
||||
new(1018, "Капитал знаний", "Получить 500 монет за активность на платформе.", "coin", 80, "coins_earned:500"),
|
||||
new(1019, "Новый уровень", "Достигнуть 2 уровня.", "star", 25, "level_reached:2"),
|
||||
new(1020, "Уверенный рост", "Достигнуть 5 уровня.", "chart-bar", 100, "level_reached:5"),
|
||||
new(1021, "Профиль заполнен", "Заполнить имя и аватар в профиле.", "user", 10, "profile_completed:1")
|
||||
];
|
||||
|
||||
private static readonly IReadOnlyDictionary<string, string> LegacyConditions = new Dictionary<string, string>
|
||||
{
|
||||
["reviews_1"] = "reviews_written:1",
|
||||
["reviews_5"] = "reviews_written:5",
|
||||
["reviews_10"] = "reviews_written:10",
|
||||
["attended_5"] = "lectures_attended:5",
|
||||
["attended_10"] = "lectures_attended:10"
|
||||
};
|
||||
|
||||
public static async Task SeedAsync(IServiceProvider services, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var scope = services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
var legacyConditionKeys = LegacyConditions.Keys.ToArray();
|
||||
|
||||
var legacyAchievements = await db.Achievements
|
||||
.Where(a => a.Condition != null && legacyConditionKeys.Contains(a.Condition))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var achievement in legacyAchievements)
|
||||
achievement.Condition = LegacyConditions[achievement.Condition!];
|
||||
|
||||
foreach (var seed in Catalog)
|
||||
{
|
||||
var achievement = await db.Achievements.FindAsync([seed.Id], cancellationToken);
|
||||
if (achievement == null)
|
||||
{
|
||||
db.Achievements.Add(new Achievement
|
||||
{
|
||||
Id = seed.Id,
|
||||
Name = seed.Name,
|
||||
Description = seed.Description,
|
||||
IconUrl = seed.IconUrl,
|
||||
XpReward = 0,
|
||||
CoinReward = seed.CoinReward,
|
||||
Condition = seed.Condition
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
achievement.Name = seed.Name;
|
||||
achievement.Description = seed.Description;
|
||||
achievement.IconUrl = seed.IconUrl;
|
||||
achievement.XpReward = 0;
|
||||
achievement.CoinReward = seed.CoinReward;
|
||||
achievement.Condition = seed.Condition;
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private sealed record AchievementSeed(
|
||||
int Id,
|
||||
string Name,
|
||||
string Description,
|
||||
string IconUrl,
|
||||
int CoinReward,
|
||||
string Condition);
|
||||
}
|
||||
Reference in New Issue
Block a user