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
106 lines
3.6 KiB
Dart
106 lines
3.6 KiB
Dart
import 'package:drift/drift.dart';
|
||
import '../database.dart';
|
||
import '../tables/discounts.dart';
|
||
import '../tables/users.dart';
|
||
|
||
part 'discount_dao.g.dart';
|
||
|
||
@DriftAccessor(tables: [DiscountCampaigns, Discounts, DiscountUserDatas])
|
||
class DiscountDao extends DatabaseAccessor<AppDatabase> with _$DiscountDaoMixin {
|
||
DiscountDao(super.db);
|
||
|
||
// ==================== DiscountCampaigns ====================
|
||
|
||
/// Получить кампанию по ID
|
||
Future<DiscountCampaign?> getCampaignById(int id) {
|
||
return (select(db.discountCampaigns)..where((c) => c.id.equals(id))).getSingleOrNull();
|
||
}
|
||
|
||
/// Получить все активные кампании
|
||
Future<List<DiscountCampaign>> getActiveCampaigns() {
|
||
final now = DateTime.now();
|
||
return (select(db.discountCampaigns)
|
||
..where((c) => c.status.equals('active'))
|
||
..where((c) => c.start.isSmallerOrEqualValue(now))
|
||
..where((c) => c.finish.isBiggerOrEqualValue(now))
|
||
..where((c) => c.isDeleted.equals(false))
|
||
).get();
|
||
}
|
||
|
||
/// Создать кампанию
|
||
Future<int> createCampaign(DiscountCampaignsCompanion campaign) {
|
||
return into(db.discountCampaigns).insert(campaign);
|
||
}
|
||
|
||
/// Обновить кампанию
|
||
Future<bool> updateCampaign(DiscountCampaign campaign) {
|
||
return update(db.discountCampaigns).replace(campaign);
|
||
}
|
||
|
||
// ==================== Discounts ====================
|
||
|
||
/// Получить скидку по ID
|
||
Future<Discount?> getDiscountById(int id) {
|
||
return (select(db.discounts)..where((d) => d.id.equals(id))).getSingleOrNull();
|
||
}
|
||
|
||
/// Получить скидки кампании
|
||
Future<List<Discount>> getDiscountsByCampaignId(int campaignId) {
|
||
return (select(db.discounts)
|
||
..where((d) => d.campaignId.equals(campaignId))
|
||
..where((d) => d.isDeleted.equals(false))
|
||
).get();
|
||
}
|
||
|
||
/// Создать скидку
|
||
Future<int> createDiscount(DiscountsCompanion discount) {
|
||
return into(db.discounts).insert(discount);
|
||
}
|
||
|
||
/// Обновить скидку
|
||
Future<bool> updateDiscount(Discount discount) {
|
||
return update(db.discounts).replace(discount);
|
||
}
|
||
|
||
// ==================== DiscountUserDatas ====================
|
||
|
||
/// Получить скидки пользователя
|
||
Future<List<Discount>> getUserDiscounts(int userId) async {
|
||
final query = select(db.discounts).join([
|
||
innerJoin(
|
||
db.discountUserDatas,
|
||
db.discountUserDatas.discountId.equalsExp(db.discounts.id) &
|
||
db.discountUserDatas.userId.equals(userId),
|
||
),
|
||
]);
|
||
|
||
return query.map((row) => row.readTable(db.discounts)).get();
|
||
}
|
||
|
||
/// Дать пользователю доступ к скидке
|
||
Future<void> grantDiscountToUser(int userId, int discountId) async {
|
||
await into(db.discountUserDatas).insert(
|
||
DiscountUserDatasCompanion.insert(
|
||
userId: userId,
|
||
discountId: discountId,
|
||
),
|
||
mode: InsertMode.insertOrIgnore,
|
||
);
|
||
}
|
||
|
||
/// Отозвать скидку у пользователя
|
||
Future<void> revokeDiscountFromUser(int userId, int discountId) async {
|
||
await (delete(db.discountUserDatas)
|
||
..where((dud) => dud.userId.equals(userId) & dud.discountId.equals(discountId))
|
||
).go();
|
||
}
|
||
|
||
/// Проверить, есть ли у пользователя доступ к скидке
|
||
Future<bool> hasDiscountAccess(int userId, int discountId) async {
|
||
final query = select(db.discountUserDatas)
|
||
..where((dud) => dud.userId.equals(userId) & dud.discountId.equals(discountId));
|
||
|
||
final result = await query.getSingleOrNull();
|
||
return result != null;
|
||
}
|
||
}
|