Skip to content

Commit

Permalink
chore(examples): Format and style todo example (#27)
Browse files Browse the repository at this point in the history
Applies formatting and style rules to the `todo` example.
  • Loading branch information
dnys1 authored Feb 5, 2024
1 parent dcf24b7 commit a303e4b
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 365 deletions.
50 changes: 25 additions & 25 deletions examples/todo/celest/functions/tasks.dart
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import 'package:celest_backend/client.dart';
import 'package:celest_backend/exceptions.dart';
import 'package:celest_backend/models.dart';
import 'package:uuid/uuid.dart';

List<Task> tasks = [];
Map<String, Task> tasks = {};

List<Task> alltasks() {
Future<Map<String, Task>> listAllTasks() async {
print('fetching tasks');
return tasks;
}

Future<List<Task>> addTask(
{required String title, required String importance}) async {
Future<void> addTask({
required String title,
required Importance importance,
}) async {
print('creating new task');
if(title.trim().isEmpty){
if (title.trim().isEmpty) {
throw ServerException('Title cannot be empty');
}
var uuid = Uuid();
final newTask = Task(
id: uuid.v1(),
title: title,
importance: importance,
);
tasks.add(newTask);
return alltasks();
id: uuid.v1(),
title: title,
importance: importance,
);
tasks[newTask.id] = newTask;
}

Future<List<Task>> deleteTask({required String id}) async {
Future<void> deleteTask({required String id}) async {
print('removing task $id');
tasks.removeWhere((element) => element.id == id);
return alltasks();
tasks.remove(id);
}

Future<void> markAsCompleted({required String id}) async {
print('marking as completed');
Task task = tasks.where((element) => element.id == id).first;
int index = tasks.indexOf(task);
print('index of completed task: $index');
tasks.removeAt(index);
tasks.insert(index, task.copyWith(id: DateTime.now().microsecondsSinceEpoch.toString(), isCompleted: true));
final task = tasks[id];
if (task == null) {
throw ServerException('Task not found');
}
tasks[id] = task.copyWith(isCompleted: true);
}

Future<void> markAsIncomplete({required String id}) async {
print('marking as incomplete');
Task task = tasks.where((element) => element.id == id).first;
int index = tasks.indexOf(task);
print('index of completed task: $index');
tasks.removeAt(index);
tasks.insert(index, task.copyWith(id: DateTime.now().microsecondsSinceEpoch.toString(),isCompleted: false));
final task = tasks[id];
if (task == null) {
throw ServerException('Task not found');
}
tasks[id] = task.copyWith(isCompleted: false);
}
5 changes: 3 additions & 2 deletions examples/todo/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ class Celest {
late http.Client httpClient = http.Client();

late final Uri baseUri = kIsWeb || !Platform.isAndroid
? Uri.parse('http://localhost:7777')
: Uri.parse('http://10.0.2.2:7777');
? Uri.parse('http://localhost:7783')
: Uri.parse('http://10.0.2.2:7783');

final functions = CelestFunctions();

void init() {
Serializers.instance.put(const ImportanceSerializer());
Serializers.instance.put(const TaskSerializer());
Serializers.instance.put(const ServerExceptionSerializer());
}
Expand Down
48 changes: 13 additions & 35 deletions examples/todo/celest/lib/models.dart
Original file line number Diff line number Diff line change
@@ -1,51 +1,25 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';

// By convention, any custom types used within an API request/response must be
// defined in this file or exported from this file.

class Task {
String id;
String title;
String importance;
bool isCompleted;
enum Importance { low, medium, high }

Task({
class Task {
const Task({
required this.id,
required this.title,
required this.importance,
this.isCompleted = false ,
this.isCompleted = false,
});

Map<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'title': title,
'importance': importance,
'completed': isCompleted,
};
}

factory Task.fromMap(Map<String, dynamic> map) {
return Task(
id: map['id'] as String,
title: map['title'] as String,
importance: map['importance'] as String,
isCompleted: map['completed'] as bool,
);
}

String toJson() => json.encode(toMap());

factory Task.fromJson(String source) => Task.fromMap(json.decode(source) as Map<String, dynamic>);

@override
String toString() => 'Task(id: $id, title: $title, importance: $importance, completed: $isCompleted)';
final String id;
final String title;
final Importance importance;
final bool isCompleted;

Task copyWith({
String? id,
String? title,
String? importance,
Importance? importance,
bool? isCompleted,
}) {
return Task(
Expand All @@ -55,4 +29,8 @@ class Task {
isCompleted: isCompleted ?? this.isCompleted,
);
}

@override
String toString() =>
'Task(id: $id, title: $title, importance: $importance, completed: $isCompleted)';
}
35 changes: 20 additions & 15 deletions examples/todo/celest/lib/src/client/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ class CelestFunctions {
}

class CelestFunctionsTasks {
Future<List<Task>> alltasks() async {
Future<Map<String, Task>> listAllTasks() async {
final $response = await celest.httpClient.post(
celest.baseUri.resolve('/tasks/alltasks'),
celest.baseUri.resolve('/tasks/list-all-tasks'),
headers: const {'Content-Type': 'application/json; charset=utf-8'},
);
final $body = (jsonDecode($response.body) as Map<String, Object?>);
if ($response.statusCode == 200) {
return ($body['response'] as Iterable<Object?>)
.map((el) => Serializers.instance.deserialize<Task>(el))
.toList();
return ($body['response'] as Map<String, Object?>).map((
key,
value,
) =>
MapEntry(
key,
Serializers.instance.deserialize<Task>(value),
));
}
final $error = ($body['error'] as Map<String, Object?>);
final $code = ($error['code'] as String);
Expand All @@ -48,23 +53,21 @@ class CelestFunctionsTasks {
}
}

Future<List<Task>> addTask({
Future<void> addTask({
required String title,
required String importance,
required Importance importance,
}) async {
final $response = await celest.httpClient.post(
celest.baseUri.resolve('/tasks/add-task'),
headers: const {'Content-Type': 'application/json; charset=utf-8'},
body: jsonEncode({
r'title': title,
r'importance': importance,
r'importance': Serializers.instance.serialize<Importance>(importance),
}),
);
final $body = (jsonDecode($response.body) as Map<String, Object?>);
if ($response.statusCode == 200) {
return ($body['response'] as Iterable<Object?>)
.map((el) => Serializers.instance.deserialize<Task>(el))
.toList();
return;
}
final $error = ($body['error'] as Map<String, Object?>);
final $code = ($error['code'] as String);
Expand All @@ -87,17 +90,15 @@ class CelestFunctionsTasks {
}
}

Future<List<Task>> deleteTask({required String id}) async {
Future<void> deleteTask({required String id}) async {
final $response = await celest.httpClient.post(
celest.baseUri.resolve('/tasks/delete-task'),
headers: const {'Content-Type': 'application/json; charset=utf-8'},
body: jsonEncode({r'id': id}),
);
final $body = (jsonDecode($response.body) as Map<String, Object?>);
if ($response.statusCode == 200) {
return ($body['response'] as Iterable<Object?>)
.map((el) => Serializers.instance.deserialize<Task>(el))
.toList();
return;
}
final $error = ($body['error'] as Map<String, Object?>);
final $code = ($error['code'] as String);
Expand Down Expand Up @@ -132,6 +133,8 @@ class CelestFunctionsTasks {
final $code = ($error['code'] as String);
final $details = ($error['details'] as Map<String, Object?>?);
switch ($code) {
case r'ServerException':
throw Serializers.instance.deserialize<ServerException>($details);
case r'BadRequestException':
throw Serializers.instance.deserialize<BadRequestException>($details);
case r'InternalServerException':
Expand Down Expand Up @@ -161,6 +164,8 @@ class CelestFunctionsTasks {
final $code = ($error['code'] as String);
final $details = ($error['details'] as Map<String, Object?>?);
switch ($code) {
case r'ServerException':
throw Serializers.instance.deserialize<ServerException>($details);
case r'BadRequestException':
throw Serializers.instance.deserialize<BadRequestException>($details);
case r'InternalServerException':
Expand Down
31 changes: 28 additions & 3 deletions examples/todo/celest/lib/src/client/serializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,42 @@ import 'package:celest/celest.dart';
import 'package:celest_backend/exceptions.dart';
import 'package:celest_backend/models.dart';

final class ImportanceSerializer extends Serializer<Importance> {
const ImportanceSerializer();

@override
Importance deserialize(Object? value) {
final serialized = assertWireType<String>(value);
return Importance.values.byName(serialized);
}

@override
String serialize(Importance value) => value.name;
}

final class TaskSerializer extends Serializer<Task> {
const TaskSerializer();

@override
Task deserialize(Object? value) {
final serialized = assertWireType<String>(value);
return Task.fromJson(serialized);
final serialized = assertWireType<Map<String, Object?>>(value);
return Task(
id: (serialized[r'id'] as String),
title: (serialized[r'title'] as String),
importance: Serializers.instance
.deserialize<Importance>(serialized[r'importance']),
isCompleted: ((serialized[r'isCompleted'] as bool?)) ?? false,
);
}

@override
String serialize(Task value) => value.toJson();
Map<String, Object?> serialize(Task value) => {
r'id': value.id,
r'title': value.title,
r'importance':
Serializers.instance.serialize<Importance>(value.importance),
r'isCompleted': value.isCompleted,
};
}

final class ServerExceptionSerializer extends Serializer<ServerException> {
Expand Down
8 changes: 4 additions & 4 deletions examples/todo/celest/resources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ abstract final class functions {
functionName: r'addTask',
);

static const tasksAlltasks = CloudFunction(
static const tasksDeleteTask = CloudFunction(
api: r'tasks',
functionName: r'alltasks',
functionName: r'deleteTask',
);

static const tasksDeleteTask = CloudFunction(
static const tasksListAllTasks = CloudFunction(
api: r'tasks',
functionName: r'deleteTask',
functionName: r'listAllTasks',
);

static const tasksMarkAsCompleted = CloudFunction(
Expand Down
Loading

0 comments on commit a303e4b

Please sign in to comment.