Skip to content

Commit

Permalink
feat(server): add tasks and sub-tasks feature
Browse files Browse the repository at this point in the history
cover tables, permissions and relationships for new added view and tables.

closes #26
  • Loading branch information
rafoolin committed Jul 26, 2022
1 parent 97d6049 commit 238d5b3
Show file tree
Hide file tree
Showing 28 changed files with 1,085 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../data/remote_category_repository.dart';
import '../domain/task_category.dart';
import '../domain/todo_category.dart';
import '../domain/todo_task.dart';

// TODO:: Support offline graphql
class CategoryService {
final RemoteCategoryRepository _remoteCategoryRepository;
CategoryService(this._remoteCategoryRepository);

Stream<AsyncValue<List<TaskCategory>>> watchCategories() {
Stream<AsyncValue<List<TodoCategory>>> watchCategories() {
return _remoteCategoryRepository.watchCategories();
}

Stream<AsyncValue<List<TodoTask>>> watchTasksByCategoryId(String categoryId) {
return _remoteCategoryRepository.watchTasksByCategoryId(categoryId);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../domain/task_category.dart';
import '../domain/todo_category.dart';
import '../domain/todo_task.dart';

class RemoteCategoryRepository {
final GraphQLClient _qlClient;
RemoteCategoryRepository(this._qlClient);

Stream<AsyncValue<List<TaskCategory>>> watchCategories() {
Stream<AsyncValue<List<TodoCategory>>> watchCategories() {
const query = '''query {
categories{
categories {
__typename
id
name
Expand All @@ -21,7 +22,11 @@ class RemoteCategoryRepository {
}''';

return _qlClient
.watchQuery(WatchQueryOptions(fetchResults: true, document: gql(query)))
.watchQuery(WatchQueryOptions(
fetchResults: true,
document: gql(query),
fetchPolicy: FetchPolicy.cacheAndNetwork,
))
.stream
.map((result) {
if (result.isLoading) {
Expand All @@ -35,7 +40,66 @@ class RemoteCategoryRepository {
if (result.data != null) {
final categories = result.data!['categories'] as List;
return AsyncData(
categories.map((json) => TaskCategory.fromJson(json)).toList(),
categories.map((json) => TodoCategory.fromJson(json)).toList(),
);
}
return const AsyncData([]);
});
}

Stream<AsyncValue<List<TodoTask>>> watchTasksByCategoryId(String categoryId) {
const query = '''query (\$categoryId: uuid!) {
vm_user_tasks(where: {categoryId: {_eq: \$categoryId}}) {
__typename
id
title
completed
note
parentId
userId
categoryId
createdAt
createdAt
completedAt
dateTime
sub_tasks {
__typename
id
title
completed
completedAt
dateTime
note
parentId
userId
categoryId
createdAt
updatedAt
}
}
}''';

return _qlClient
.watchQuery(WatchQueryOptions(
fetchResults: true,
document: gql(query),
variables: {'categoryId': categoryId},
fetchPolicy: FetchPolicy.cacheAndNetwork,
))
.stream
.map((result) {
if (result.isLoading) {
return const AsyncLoading();
}

if (result.hasException) {
return AsyncError(result.exception!);
}

if (result.data != null) {
final tasks = result.data!['vm_user_tasks'] as List;
return AsyncData(
tasks.map((json) => TodoTask.fromJson(json)).toList(),
);
}
return const AsyncData([]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../../common/models/converters.dart';

part 'task_category.freezed.dart';
part 'task_category.g.dart';
part 'todo_category.freezed.dart';
part 'todo_category.g.dart';

@freezed
@JsonSerializable(converters: [ColorConverter()], createFactory: false)
class TaskCategory with _$TaskCategory {
factory TaskCategory({
class TodoCategory with _$TodoCategory {
factory TodoCategory({
required String id,
required String name,
required String userId,
@ColorConverter() Color? color,
required DateTime createdAt,
DateTime? updatedAt,
}) = _TaskCategory;
}) = _TodoCategory;

TaskCategory._();
TodoCategory._();

@override
factory TaskCategory.fromJson(Map<String, dynamic> json) =>
_$TaskCategoryFromJson(json);
factory TodoCategory.fromJson(Map<String, dynamic> json) =>
_$TodoCategoryFromJson(json);

@override
Map<String, dynamic> toJson() => _$TaskCategoryToJson(this);
Map<String, dynamic> toJson() => _$TodoCategoryToJson(this);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target

part of 'task_category.dart';
part of 'todo_category.dart';

// **************************************************************************
// FreezedGenerator
Expand All @@ -14,12 +14,12 @@ T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');

TaskCategory _$TaskCategoryFromJson(Map<String, dynamic> json) {
return _TaskCategory.fromJson(json);
TodoCategory _$TodoCategoryFromJson(Map<String, dynamic> json) {
return _TodoCategory.fromJson(json);
}

/// @nodoc
mixin _$TaskCategory {
mixin _$TodoCategory {
String get id => throw _privateConstructorUsedError;
String get name => throw _privateConstructorUsedError;
String get userId => throw _privateConstructorUsedError;
Expand All @@ -30,15 +30,15 @@ mixin _$TaskCategory {

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$TaskCategoryCopyWith<TaskCategory> get copyWith =>
$TodoCategoryCopyWith<TodoCategory> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $TaskCategoryCopyWith<$Res> {
factory $TaskCategoryCopyWith(
TaskCategory value, $Res Function(TaskCategory) then) =
_$TaskCategoryCopyWithImpl<$Res>;
abstract class $TodoCategoryCopyWith<$Res> {
factory $TodoCategoryCopyWith(
TodoCategory value, $Res Function(TodoCategory) then) =
_$TodoCategoryCopyWithImpl<$Res>;
$Res call(
{String id,
String name,
Expand All @@ -49,12 +49,12 @@ abstract class $TaskCategoryCopyWith<$Res> {
}

/// @nodoc
class _$TaskCategoryCopyWithImpl<$Res> implements $TaskCategoryCopyWith<$Res> {
_$TaskCategoryCopyWithImpl(this._value, this._then);
class _$TodoCategoryCopyWithImpl<$Res> implements $TodoCategoryCopyWith<$Res> {
_$TodoCategoryCopyWithImpl(this._value, this._then);

final TaskCategory _value;
final TodoCategory _value;
// ignore: unused_field
final $Res Function(TaskCategory) _then;
final $Res Function(TodoCategory) _then;

@override
$Res call({
Expand Down Expand Up @@ -95,11 +95,11 @@ class _$TaskCategoryCopyWithImpl<$Res> implements $TaskCategoryCopyWith<$Res> {
}

/// @nodoc
abstract class _$$_TaskCategoryCopyWith<$Res>
implements $TaskCategoryCopyWith<$Res> {
factory _$$_TaskCategoryCopyWith(
_$_TaskCategory value, $Res Function(_$_TaskCategory) then) =
__$$_TaskCategoryCopyWithImpl<$Res>;
abstract class _$$_TodoCategoryCopyWith<$Res>
implements $TodoCategoryCopyWith<$Res> {
factory _$$_TodoCategoryCopyWith(
_$_TodoCategory value, $Res Function(_$_TodoCategory) then) =
__$$_TodoCategoryCopyWithImpl<$Res>;
@override
$Res call(
{String id,
Expand All @@ -111,15 +111,15 @@ abstract class _$$_TaskCategoryCopyWith<$Res>
}

/// @nodoc
class __$$_TaskCategoryCopyWithImpl<$Res>
extends _$TaskCategoryCopyWithImpl<$Res>
implements _$$_TaskCategoryCopyWith<$Res> {
__$$_TaskCategoryCopyWithImpl(
_$_TaskCategory _value, $Res Function(_$_TaskCategory) _then)
: super(_value, (v) => _then(v as _$_TaskCategory));
class __$$_TodoCategoryCopyWithImpl<$Res>
extends _$TodoCategoryCopyWithImpl<$Res>
implements _$$_TodoCategoryCopyWith<$Res> {
__$$_TodoCategoryCopyWithImpl(
_$_TodoCategory _value, $Res Function(_$_TodoCategory) _then)
: super(_value, (v) => _then(v as _$_TodoCategory));

@override
_$_TaskCategory get _value => super._value as _$_TaskCategory;
_$_TodoCategory get _value => super._value as _$_TodoCategory;

@override
$Res call({
Expand All @@ -130,7 +130,7 @@ class __$$_TaskCategoryCopyWithImpl<$Res>
Object? createdAt = freezed,
Object? updatedAt = freezed,
}) {
return _then(_$_TaskCategory(
return _then(_$_TodoCategory(
id: id == freezed
? _value.id
: id // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -161,8 +161,8 @@ class __$$_TaskCategoryCopyWithImpl<$Res>

/// @nodoc
@JsonSerializable()
class _$_TaskCategory extends _TaskCategory with DiagnosticableTreeMixin {
_$_TaskCategory(
class _$_TodoCategory extends _TodoCategory with DiagnosticableTreeMixin {
_$_TodoCategory(
{required this.id,
required this.name,
required this.userId,
Expand All @@ -171,8 +171,8 @@ class _$_TaskCategory extends _TaskCategory with DiagnosticableTreeMixin {
this.updatedAt})
: super._();

factory _$_TaskCategory.fromJson(Map<String, dynamic> json) =>
_$$_TaskCategoryFromJson(json);
factory _$_TodoCategory.fromJson(Map<String, dynamic> json) =>
_$$_TodoCategoryFromJson(json);

@override
final String id;
Expand All @@ -190,14 +190,14 @@ class _$_TaskCategory extends _TaskCategory with DiagnosticableTreeMixin {

@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return 'TaskCategory(id: $id, name: $name, userId: $userId, color: $color, createdAt: $createdAt, updatedAt: $updatedAt)';
return 'TodoCategory(id: $id, name: $name, userId: $userId, color: $color, createdAt: $createdAt, updatedAt: $updatedAt)';
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty('type', 'TaskCategory'))
..add(DiagnosticsProperty('type', 'TodoCategory'))
..add(DiagnosticsProperty('id', id))
..add(DiagnosticsProperty('name', name))
..add(DiagnosticsProperty('userId', userId))
Expand All @@ -210,7 +210,7 @@ class _$_TaskCategory extends _TaskCategory with DiagnosticableTreeMixin {
bool operator ==(dynamic other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$_TaskCategory &&
other is _$_TodoCategory &&
const DeepCollectionEquality().equals(other.id, id) &&
const DeepCollectionEquality().equals(other.name, name) &&
const DeepCollectionEquality().equals(other.userId, userId) &&
Expand All @@ -232,29 +232,29 @@ class _$_TaskCategory extends _TaskCategory with DiagnosticableTreeMixin {

@JsonKey(ignore: true)
@override
_$$_TaskCategoryCopyWith<_$_TaskCategory> get copyWith =>
__$$_TaskCategoryCopyWithImpl<_$_TaskCategory>(this, _$identity);
_$$_TodoCategoryCopyWith<_$_TodoCategory> get copyWith =>
__$$_TodoCategoryCopyWithImpl<_$_TodoCategory>(this, _$identity);

@override
Map<String, dynamic> toJson() {
return _$$_TaskCategoryToJson(
return _$$_TodoCategoryToJson(
this,
);
}
}

abstract class _TaskCategory extends TaskCategory {
factory _TaskCategory(
abstract class _TodoCategory extends TodoCategory {
factory _TodoCategory(
{required final String id,
required final String name,
required final String userId,
@ColorConverter() final Color? color,
required final DateTime createdAt,
final DateTime? updatedAt}) = _$_TaskCategory;
_TaskCategory._() : super._();
final DateTime? updatedAt}) = _$_TodoCategory;
_TodoCategory._() : super._();

factory _TaskCategory.fromJson(Map<String, dynamic> json) =
_$_TaskCategory.fromJson;
factory _TodoCategory.fromJson(Map<String, dynamic> json) =
_$_TodoCategory.fromJson;

@override
String get id;
Expand All @@ -271,6 +271,6 @@ abstract class _TaskCategory extends TaskCategory {
DateTime? get updatedAt;
@override
@JsonKey(ignore: true)
_$$_TaskCategoryCopyWith<_$_TaskCategory> get copyWith =>
_$$_TodoCategoryCopyWith<_$_TodoCategory> get copyWith =>
throw _privateConstructorUsedError;
}
Loading

0 comments on commit 238d5b3

Please sign in to comment.