admin
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions

This commit is contained in:
Dmitry 2025-12-20 18:38:55 +03:00
parent ead08e7337
commit 42e76de869
2 changed files with 50 additions and 5 deletions

View file

@ -462,14 +462,14 @@ class AdminCardsApiV2 {
if (requestDto.packId != null && requestDto.packId!.isNotEmpty) { if (requestDto.packId != null && requestDto.packId!.isNotEmpty) {
// Удалить все старые связи // Удалить все старые связи
for (final pack in currentPacks) { for (final pack in currentPacks) {
await _db.packDao.removeCardFromPack(updated.id, pack.id); await _db.packDao.removeCardFromPack(pack.id, updated.id);
} }
// Затем добавить новую связь // Затем добавить новую связь
await _db.packDao.addCardToPack(cardId: updated.id, packId: requestDto.packId!); await _db.packDao.addCardToPack(cardId: updated.id, packId: requestDto.packId!);
} else { } else {
// Если packId не указан или пустой, удалить все связи // Если packId не указан или пустой, удалить все связи
for (final pack in currentPacks) { for (final pack in currentPacks) {
await _db.packDao.removeCardFromPack(updated.id, pack.id); await _db.packDao.removeCardFromPack(pack.id, updated.id);
} }
} }
@ -518,6 +518,7 @@ class AdminCardsApiV2 {
final cardId = await _db.packDao.createCard(companion); final cardId = await _db.packDao.createCard(companion);
// Если передан packId, создать связь через CardPackCards // Если передан packId, создать связь через CardPackCards
// Метод addCardToPack автоматически проверяет существование связи
if (requestDto.packId != null && requestDto.packId!.isNotEmpty) { if (requestDto.packId != null && requestDto.packId!.isNotEmpty) {
await _db.packDao.addCardToPack(cardId: cardId, packId: requestDto.packId!); await _db.packDao.addCardToPack(cardId: cardId, packId: requestDto.packId!);
} }
@ -581,7 +582,10 @@ class AdminCardsApiV2 {
final errorMessage = e.toString().toLowerCase(); final errorMessage = e.toString().toLowerCase();
String details = 'An unexpected error occurred while saving the card.'; String details = 'An unexpected error occurred while saving the card.';
if (errorMessage.contains('constraint') || errorMessage.contains('unique')) { if (errorMessage.contains('card_pack_cards_pkey') ||
(errorMessage.contains('duplicate key') && errorMessage.contains('card_pack_cards'))) {
details = 'This card is already associated with the specified pack. The card was created successfully, but the pack association already exists.';
} else if (errorMessage.contains('constraint') || errorMessage.contains('unique')) {
details = 'A card with similar data may already exist. Please check for duplicates.'; details = 'A card with similar data may already exist. Please check for duplicates.';
} else if (errorMessage.contains('foreign key') || errorMessage.contains('pack')) { } else if (errorMessage.contains('foreign key') || errorMessage.contains('pack')) {
details = 'The specified pack ID may be invalid. Please verify that the pack exists.'; details = 'The specified pack ID may be invalid. Please verify that the pack exists.';
@ -671,6 +675,30 @@ class AdminCardsApiV2 {
await _db.packDao.updateCard(updated); await _db.packDao.updateCard(updated);
// Обновить связь с паком, если packId указан
final currentPacks = await _db.packDao.getPacksForCard(cardId);
if (requestDto.packId != null && requestDto.packId!.isNotEmpty) {
// Проверить, нужно ли обновлять связь
final needsUpdate = currentPacks.isEmpty ||
currentPacks.length != 1 ||
currentPacks.first.id != requestDto.packId;
if (needsUpdate) {
// Удалить все старые связи
for (final pack in currentPacks) {
await _db.packDao.removeCardFromPack(pack.id, cardId);
}
// Затем добавить новую связь
await _db.packDao.addCardToPack(cardId: cardId, packId: requestDto.packId!);
}
} else {
// Если packId не указан или пустой, удалить все связи
for (final pack in currentPacks) {
await _db.packDao.removeCardFromPack(pack.id, cardId);
}
}
return _json(SuccessResponse(success: true).toJson()); return _json(SuccessResponse(success: true).toJson());
} catch (e, s) { } catch (e, s) {
print('Error in updateCard: $e\n$s'); print('Error in updateCard: $e\n$s');
@ -679,7 +707,10 @@ class AdminCardsApiV2 {
final errorMessage = e.toString().toLowerCase(); final errorMessage = e.toString().toLowerCase();
String details = 'An unexpected error occurred while updating the card.'; String details = 'An unexpected error occurred while updating the card.';
if (errorMessage.contains('constraint') || errorMessage.contains('unique')) { if (errorMessage.contains('card_pack_cards_pkey') ||
(errorMessage.contains('duplicate key') && errorMessage.contains('card_pack_cards'))) {
details = 'This card is already associated with the specified pack. The card was updated successfully, but the pack association already exists.';
} else if (errorMessage.contains('constraint') || errorMessage.contains('unique')) {
details = 'A card with similar data may already exist. Please check for duplicates.'; details = 'A card with similar data may already exist. Please check for duplicates.';
} else if (errorMessage.contains('foreign key') || errorMessage.contains('pack')) { } else if (errorMessage.contains('foreign key') || errorMessage.contains('pack')) {
details = 'The specified pack ID may be invalid. Please verify that the pack exists.'; details = 'The specified pack ID may be invalid. Please verify that the pack exists.';

View file

@ -190,18 +190,32 @@ class PackDao extends DatabaseAccessor<AppDatabase> with _$PackDaoMixin {
} }
/// Добавить карточку в пак /// Добавить карточку в пак
/// Если связь уже существует, метод завершится успешно без ошибки
Future<void> addCardToPack({ Future<void> addCardToPack({
required String packId, required String packId,
required String cardId, required String cardId,
int order = 0, int order = 0,
}) async { }) async {
// Проверить, существует ли уже связь
final existing = await (select(cardPackCards)
..where((cpc) =>
cpc.packId.equals(packId) &
cpc.cardId.equals(cardId)
)
).getSingleOrNull();
// Если связь уже существует, ничего не делаем
if (existing != null) {
return;
}
// Иначе создаем новую связь
await into(cardPackCards).insert( await into(cardPackCards).insert(
CardPackCardsCompanion.insert( CardPackCardsCompanion.insert(
packId: packId, packId: packId,
cardId: cardId, cardId: cardId,
order: Value(order), order: Value(order),
), ),
mode: InsertMode.insertOrIgnore,
); );
} }