Skip to content

Commit

Permalink
updated note service
Browse files Browse the repository at this point in the history
  • Loading branch information
drriguz committed Mar 30, 2024
1 parent 6286f61 commit 4a1e0a4
Show file tree
Hide file tree
Showing 12 changed files with 726 additions and 511 deletions.
598 changes: 292 additions & 306 deletions editing/lib/database/database.g.dart

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions editing/lib/database/tables.drift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ CREATE TABLE notes (
location TEXT,
weather TEXT,
mood TEXT,
encryption_type INTEGER NOT NULL,
encryption_arguments TEXT,
encrypt_type INTEGER NOT NULL,
encrypt_arguments TEXT,
create_time TEXT NOT NULL,
last_update_time TEXT NOT NULL,
category_id INTEGER,
Expand All @@ -20,8 +20,8 @@ CREATE TABLE notes (
CREATE TABLE categories (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
encryption_type INTEGER NOT NULL,
encryption_arguments TEXT,
encrypt_type INTEGER NOT NULL,
encrypt_arguments TEXT,
create_time TEXT NOT NULL,
last_update_time TEXT NOT NULL
) As CategoryEntity;
Expand All @@ -34,8 +34,8 @@ CREATE TABLE files(
thumbnail_path TEXT,
format TEXT NOT NULL,
checksum TEXT,
encryption_type INTEGER NOT NULL,
encryption_arguments TEXT,
encrypt_type INTEGER NOT NULL,
encrypt_arguments TEXT,
remark TEXT,
category_id INTEGER,
create_time TEXT NOT NULL,
Expand Down Expand Up @@ -76,8 +76,8 @@ CREATE TABLE passwords (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
password TEXT NOT NULL,
encryption_type INTEGER NOT NULL,
encryption_arguments TEXT,
encrypt_type INTEGER NOT NULL,
encrypt_arguments TEXT,
remark TEXT,
create_time TEXT NOT NULL,
last_update_time TEXT NOT NULL
Expand Down
31 changes: 31 additions & 0 deletions editing/lib/domain/category.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:editing/domain/common.dart';

import '../database/database.dart';

class Category {
int? id;
String name;
EncryptType encryptType;
String? encryptArguments;
DateTime createTime;
DateTime lastUpdateTime;

Category(
{this.id,
required this.name,
required this.encryptType,
this.encryptArguments,
required this.createTime,
required this.lastUpdateTime});

static Category fromEntity(CategoryEntity entity) {
return Category(
id: entity.id,
name: entity.name,
encryptType: EncryptType.getByValue(entity.encryptType),
encryptArguments: entity.encryptArguments,
createTime: DateTime.parse(entity.createTime),
lastUpdateTime: DateTime.parse(entity.lastUpdateTime),
);
}
}
12 changes: 12 additions & 0 deletions editing/lib/domain/common.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
enum EncryptType {
plain(0),
aes(1);

final int value;

const EncryptType(this.value);

static EncryptType getByValue(num i) {
return EncryptType.values.firstWhere((x) => x.value == i);
}
}
111 changes: 111 additions & 0 deletions editing/lib/domain/note.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'package:drift/drift.dart';
import 'package:editing/domain/category.dart';

import '../database/database.dart';
import 'common.dart';

enum NoteType {
journal(1),
note(2),
idea(3),
todo(4);

final int value;

const NoteType(this.value);

static NoteType getByValue(num i) {
return NoteType.values.firstWhere((x) => x.value == i);
}
}

enum NoteFormat {
plain(0),
quill(1);

final int value;

const NoteFormat(this.value);

static NoteFormat getByValue(num i) {
return NoteFormat.values.firstWhere((x) => x.value == i);
}
}

class Note {
int? id;
NoteType type;
NoteFormat format;
String title;
String content;
String? abstract;
List<String>? tags;
String? location;
String? weather;
String? mood;
EncryptType encryptType;
String? encryptArguments;
DateTime createTime;
DateTime lastUpdateTime;
Category? category;

Note(
{this.id,
required this.type,
required this.format,
required this.title,
required this.content,
this.abstract,
this.tags,
this.location,
this.weather,
this.mood,
required this.encryptType,
this.encryptArguments,
required this.createTime,
required this.lastUpdateTime,
this.category});

NotesCompanion toCompanion() {
return NotesCompanion(
id: Value.absentIfNull(id),
type: Value(type.value),
format: Value(format.value),
title: Value(title),
content: Value(content),
abstract: Value.absentIfNull(abstract),
tags: Value.absentIfNull(tags?.join(",")),
location: Value.absentIfNull(location),
weather: Value.absentIfNull(weather),
mood: Value.absentIfNull(mood),
encryptType: Value(encryptType.value),
encryptArguments: Value.absentIfNull(encryptArguments),
createTime: Value(createTime.toIso8601String()),
lastUpdateTime: Value(lastUpdateTime.toIso8601String()),
categoryId: Value.absentIfNull(category?.id),
);
}

static Note fromEntity(NoteEntity entity, CategoryEntity? category) {
if (entity.categoryId != null) {
assert(category != null && category.id == entity.categoryId);
}
return Note(
id: entity.id,
type: NoteType.getByValue(entity.type),
format: NoteFormat.getByValue(entity.format),
title: entity.title,
content: entity.content,
abstract: entity.abstract,
tags: entity.tags?.split(","),
location: entity.location,
weather: entity.weather,
mood: entity.mood,
encryptType: EncryptType.getByValue(entity.encryptType),
encryptArguments: entity.encryptArguments,
createTime: DateTime.parse(entity.createTime),
lastUpdateTime: DateTime.parse(entity.lastUpdateTime),
category: category != null ? Category.fromEntity(category) : null,
);
}
}
37 changes: 22 additions & 15 deletions editing/lib/service/note_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:drift/drift.dart';
import 'package:editing/database/database.dart';

import '../domain/note.dart';
import '../store/note_item.dart';

class NoteService {
Expand Down Expand Up @@ -33,21 +34,27 @@ class NoteService {
return items.map(convertToViewModel).toList();
}

Future<NoteItem> insert(AppDb db, String title, String content) async {
final now = DateTime.now().toIso8601String();
final id = await db.into(db.notes).insert(NotesCompanion(
title: Value(title),
content: Value(content),
format: Value(1),
type: Value(1),
encryptionType: Value(1),
createTime: Value(now),
lastUpdateTime: Value(now),
));
final inserted = await (db.select(db.notes)
..where((tbl) => tbl.id.equals(id)))
.getSingle();
Future<List<Note>> fetchByType(AppDb db, NoteType type) async {
final items = await (db.notes.select()
..where((tbl) => tbl.type.isValue(type.value)))
.get();
final categories = await db.categories.select().get();
return items
.map(
(item) => Note.fromEntity(
item,
item.categoryId == null
? null
: categories.firstWhere((e) => e.id == item.categoryId),
),
)
.toList();
}

return convertToViewModel(inserted);
Future<Note> insertNote(AppDb db, Note item) async {
assert(item.id == null);
final id = await db.into(db.notes).insert(item.toCompanion());
item.id = id;
return item;
}
}
2 changes: 0 additions & 2 deletions editing/lib/store/note_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ abstract class NoteListBase with Store {
@action
Future<void> insert() async {
final db = getDb();
final inserted = await _noteService.insert(db, "hello", "world");
list.add(inserted);
}
}
Loading

0 comments on commit 4a1e0a4

Please sign in to comment.