kopia lustrzana https://gitlab.com/mysocialportal/relatica
90 wiersze
2.2 KiB
Dart
90 wiersze
2.2 KiB
Dart
![]() |
import 'package:result_monad/result_monad.dart';
|
||
|
|
||
|
import '../../globals.dart';
|
||
|
import '../../models/connection.dart';
|
||
|
import '../../models/exec_error.dart';
|
||
|
import '../../objectbox.g.dart';
|
||
|
import '../interfaces/connections_repo_intf.dart';
|
||
|
import 'objectbox_cache.dart';
|
||
|
|
||
|
class ObjectBoxConnectionsRepo implements IConnectionsRepo {
|
||
|
late final Box<Connection> box;
|
||
|
|
||
|
ObjectBoxConnectionsRepo() {
|
||
|
box = getIt<ObjectBoxCache>().store.box<Connection>();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
bool addAllConnections(Iterable<Connection> newConnections) {
|
||
|
final result = box.putMany(newConnections.toList());
|
||
|
return result.length == newConnections.length;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
bool addConnection(Connection connection) {
|
||
|
box.putAsync(connection);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Result<Connection, ExecError> getById(String id) {
|
||
|
return _getConnection(
|
||
|
Connection_.id.equals(id),
|
||
|
).mapError(
|
||
|
(error) => error.copy(
|
||
|
message: "Can't find Connection for ID: $id",
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Result<Connection, ExecError> getByName(String name) {
|
||
|
return _getConnection(
|
||
|
Connection_.name.equals(name),
|
||
|
).mapError(
|
||
|
(error) => error.copy(
|
||
|
message: "Can't find Connection for ID: $name",
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
List<Connection> getKnownUsersByName(String name) {
|
||
|
return box
|
||
|
.query(
|
||
|
Connection_.name.contains(name, caseSensitive: false) |
|
||
|
Connection_.handle.contains(name, caseSensitive: false),
|
||
|
)
|
||
|
.build()
|
||
|
.find();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
List<Connection> getMyContacts() {
|
||
|
final connectedStatuses = [
|
||
|
ConnectionStatus.youFollowThem.code,
|
||
|
ConnectionStatus.theyFollowYou.code,
|
||
|
ConnectionStatus.mutual.code,
|
||
|
];
|
||
|
return box
|
||
|
.query(Connection_.dbStatus.oneOf(connectedStatuses))
|
||
|
.build()
|
||
|
.find();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
bool updateConnection(Connection connection) {
|
||
|
box.put(connection);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
Result<Connection, ExecError> _getConnection(Condition<Connection> query) {
|
||
|
final result = box.query(query).build().findFirst();
|
||
|
if (result == null) {
|
||
|
return buildErrorResult(type: ErrorType.notFound);
|
||
|
}
|
||
|
|
||
|
return Result.ok(result);
|
||
|
}
|
||
|
}
|