mirror of
https://github.com/cyberbread-ru/notepad.git
synced 2026-07-14 12:51:01 +03:00
54 lines
1.3 KiB
Dart
54 lines
1.3 KiB
Dart
import 'package:uuid/uuid.dart';
|
|
|
|
import '../models/note.dart';
|
|
import '../repositories/local_note_repository.dart';
|
|
import 'sync_service.dart';
|
|
|
|
class NoteService {
|
|
final LocalNoteRepository _local;
|
|
final SyncService? _sync;
|
|
final _uuid = const Uuid();
|
|
|
|
NoteService({
|
|
required LocalNoteRepository local,
|
|
SyncService? sync,
|
|
}) : _local = local,
|
|
_sync = sync;
|
|
|
|
Future<List<Note>> getAllNotes() => _local.getAll();
|
|
|
|
Future<Note> createNote({String title = '', String content = ''}) async {
|
|
final note = Note(
|
|
id: _uuid.v4(),
|
|
title: title,
|
|
content: content,
|
|
createdAt: DateTime.now(),
|
|
updatedAt: DateTime.now(),
|
|
);
|
|
await _local.save(note);
|
|
return note;
|
|
}
|
|
|
|
Future<Note> updateNote(Note note) async {
|
|
final updated = note.copyWith(updatedAt: DateTime.now());
|
|
await _local.save(updated);
|
|
return updated;
|
|
}
|
|
|
|
Future<void> deleteNote(String id) async {
|
|
await _local.delete(id);
|
|
}
|
|
|
|
/// Manual backup — push everything to the server.
|
|
Future<void> triggerBackup() async {
|
|
if (_sync == null) throw Exception('Backup not configured');
|
|
await _sync!.pushAll();
|
|
}
|
|
|
|
/// Restore — pull everything from the server.
|
|
Future<void> triggerRestore() async {
|
|
if (_sync == null) throw Exception('Backup not configured');
|
|
await _sync!.pullAll();
|
|
}
|
|
}
|