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
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:
parent
ead08e7337
commit
42e76de869
2 changed files with 50 additions and 5 deletions
|
|
@ -462,14 +462,14 @@ class AdminCardsApiV2 {
|
|||
if (requestDto.packId != null && requestDto.packId!.isNotEmpty) {
|
||||
// Удалить все старые связи
|
||||
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!);
|
||||
} else {
|
||||
// Если packId не указан или пустой, удалить все связи
|
||||
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);
|
||||
|
||||
// Если передан packId, создать связь через CardPackCards
|
||||
// Метод addCardToPack автоматически проверяет существование связи
|
||||
if (requestDto.packId != null && requestDto.packId!.isNotEmpty) {
|
||||
await _db.packDao.addCardToPack(cardId: cardId, packId: requestDto.packId!);
|
||||
}
|
||||
|
|
@ -581,7 +582,10 @@ class AdminCardsApiV2 {
|
|||
final errorMessage = e.toString().toLowerCase();
|
||||
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.';
|
||||
} else if (errorMessage.contains('foreign key') || errorMessage.contains('pack')) {
|
||||
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);
|
||||
|
||||
// Обновить связь с паком, если 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());
|
||||
} catch (e, s) {
|
||||
print('Error in updateCard: $e\n$s');
|
||||
|
|
@ -679,7 +707,10 @@ class AdminCardsApiV2 {
|
|||
final errorMessage = e.toString().toLowerCase();
|
||||
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.';
|
||||
} else if (errorMessage.contains('foreign key') || errorMessage.contains('pack')) {
|
||||
details = 'The specified pack ID may be invalid. Please verify that the pack exists.';
|
||||
|
|
|
|||
|
|
@ -190,18 +190,32 @@ class PackDao extends DatabaseAccessor<AppDatabase> with _$PackDaoMixin {
|
|||
}
|
||||
|
||||
/// Добавить карточку в пак
|
||||
/// Если связь уже существует, метод завершится успешно без ошибки
|
||||
Future<void> addCardToPack({
|
||||
required String packId,
|
||||
required String cardId,
|
||||
int order = 0,
|
||||
}) 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(
|
||||
CardPackCardsCompanion.insert(
|
||||
packId: packId,
|
||||
cardId: cardId,
|
||||
order: Value(order),
|
||||
),
|
||||
mode: InsertMode.insertOrIgnore,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue