instruction_app/lib/data/instruction_repository.dart

75 lines
2.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import '../models/instruction.dart';
import 'package:uuid/uuid.dart';
abstract class InstructionRepository {
Future<List<Instruction>> getInstructions();
Future<Instruction> addInstruction(Instruction instruction);
Future<void> updateInstruction(Instruction instruction);
Future<void> deleteInstruction(String id);
}
// ЛОКАЛЬНАЯ РЕАЛИЗАЦИЯ, имитирующая работу с базой данных.
class LocalInstructionRepository implements InstructionRepository {
final List<Instruction> _instructions = [
Instruction(
id: '1',
title: 'Инструктаж по пожарной безопасности',
content: '1. Не курить в помещении. 2. В случае пожара звонить 101. 3. Использовать огнетушитель, расположенный у выхода.',
tags: ['безопасность', 'офис'],
createdAt: DateTime.now().subtract(const Duration(days: 2)),
),
Instruction(
id: '2',
title: 'Работа с новым ПО "Феникс"',
content: 'Подробное описание как работать с новым программным обеспечением "Феникс".',
tags: ['it', 'софт'],
createdAt: DateTime.now().subtract(const Duration(days: 1)),
),
Instruction(
id: '3',
title: 'Правила поведения на новогоднем корпоративе',
content: 'Веселиться, но в меру. Помнить о субординации. Не злоупотреблять напитками.',
tags: ['офис', 'развлечения', 'hr'],
createdAt: DateTime.now(),
),
];
final _uuid = const Uuid();
@override
Future<List<Instruction>> getInstructions() async {
// Имитация задержки сети
await Future.delayed(const Duration(milliseconds: 400));
_instructions.sort((a, b) => b.createdAt.compareTo(a.createdAt));
return List.of(_instructions);
}
@override
Future<Instruction> addInstruction(Instruction instruction) async {
await Future.delayed(const Duration(milliseconds: 300));
final newInstruction = instruction.copyWith(
id: _uuid.v4(),
createdAt: DateTime.now(),
);
_instructions.add(newInstruction);
return newInstruction;
}
@override
Future<void> updateInstruction(Instruction instruction) async {
await Future.delayed(const Duration(milliseconds: 300));
final index = _instructions.indexWhere((i) => i.id == instruction.id);
if (index != -1) {
_instructions[index] = instruction;
} else {
throw Exception('Instruction not found');
}
}
@override
Future<void> deleteInstruction(String id) async {
await Future.delayed(const Duration(milliseconds: 200));
_instructions.removeWhere((i) => i.id == id);
}
}