204 lines
5.8 KiB
Dart
204 lines
5.8 KiB
Dart
import 'package:social_app/data/network/i_api_client.dart';
|
|
|
|
class TodoApi {
|
|
final IApiClient _client;
|
|
static const _prefix = '/api/v1/todos';
|
|
|
|
TodoApi(this._client);
|
|
|
|
Future<List<TodoResponse>> getTodos({String? status, int? priority}) async {
|
|
final queryParts = <String>[];
|
|
if (status != null) queryParts.add('status=$status');
|
|
if (priority != null) queryParts.add('priority=$priority');
|
|
final query = queryParts.isEmpty ? '' : '?${queryParts.join('&')}';
|
|
|
|
final response = await _client.get('$_prefix$query');
|
|
final List<dynamic> data = response.data;
|
|
return data.map((json) => TodoResponse.fromJson(json)).toList();
|
|
}
|
|
|
|
Future<List<TodoResponse>> getPendingTodos() {
|
|
return getTodos(status: 'pending');
|
|
}
|
|
|
|
Future<TodoResponse> getTodo(String id) async {
|
|
final response = await _client.get('$_prefix/$id');
|
|
return TodoResponse.fromJson(response.data);
|
|
}
|
|
|
|
Future<TodoResponse> createTodo({
|
|
required String title,
|
|
String? description,
|
|
int priority = 1,
|
|
int? order,
|
|
List<String> scheduleItemIds = const [],
|
|
}) async {
|
|
final data = <String, dynamic>{'title': title, 'priority': priority};
|
|
if (description != null) data['description'] = description;
|
|
if (order != null) data['order'] = order;
|
|
if (scheduleItemIds.isNotEmpty) data['schedule_item_ids'] = scheduleItemIds;
|
|
|
|
final response = await _client.post(_prefix, data: data);
|
|
return TodoResponse.fromJson(response.data);
|
|
}
|
|
|
|
Future<TodoResponse> updateTodo(
|
|
String id, {
|
|
String? title,
|
|
String? description,
|
|
int? priority,
|
|
int? order,
|
|
String? status,
|
|
List<String>? scheduleItemIds,
|
|
}) async {
|
|
final data = <String, dynamic>{};
|
|
if (title != null) data['title'] = title;
|
|
if (description != null) data['description'] = description;
|
|
if (priority != null) data['priority'] = priority;
|
|
if (order != null) data['order'] = order;
|
|
if (status != null) data['status'] = status;
|
|
if (scheduleItemIds != null) data['schedule_item_ids'] = scheduleItemIds;
|
|
|
|
final response = await _client.patch('$_prefix/$id', data: data);
|
|
return TodoResponse.fromJson(response.data);
|
|
}
|
|
|
|
Future<void> reorderTodos(List<TodoReorderItemPayload> items) async {
|
|
final data = {
|
|
'items': items
|
|
.map(
|
|
(item) => {
|
|
'id': item.id,
|
|
'priority': item.priority,
|
|
'order': item.order,
|
|
},
|
|
)
|
|
.toList(),
|
|
};
|
|
await _client.patch('$_prefix/reorder', data: data);
|
|
}
|
|
|
|
Future<TodoResponse> completeTodo(String id) async {
|
|
final response = await _client.post('$_prefix/$id/complete', data: {});
|
|
return TodoResponse.fromJson(response.data);
|
|
}
|
|
|
|
Future<void> deleteTodo(String id) async {
|
|
await _client.delete('$_prefix/$id');
|
|
}
|
|
}
|
|
|
|
class TodoReorderItemPayload {
|
|
final String id;
|
|
final int priority;
|
|
final int order;
|
|
|
|
const TodoReorderItemPayload({
|
|
required this.id,
|
|
required this.priority,
|
|
required this.order,
|
|
});
|
|
}
|
|
|
|
class ScheduleItemBasic {
|
|
final String id;
|
|
final String title;
|
|
final DateTime startAt;
|
|
final DateTime? endAt;
|
|
|
|
ScheduleItemBasic({
|
|
required this.id,
|
|
required this.title,
|
|
required this.startAt,
|
|
this.endAt,
|
|
});
|
|
|
|
factory ScheduleItemBasic.fromJson(Map<String, dynamic> json) {
|
|
return ScheduleItemBasic(
|
|
id: json['id'] as String,
|
|
title: json['title'] as String,
|
|
startAt: DateTime.parse(json['start_at'] as String),
|
|
endAt: json['end_at'] != null
|
|
? DateTime.parse(json['end_at'] as String)
|
|
: null,
|
|
);
|
|
}
|
|
}
|
|
|
|
class TodoResponse {
|
|
final String id;
|
|
final String ownerId;
|
|
final String title;
|
|
final String? description;
|
|
final int order;
|
|
final int priority;
|
|
final String status;
|
|
final DateTime? completedAt;
|
|
final DateTime createdAt;
|
|
final DateTime updatedAt;
|
|
final List<ScheduleItemBasic> scheduleItems;
|
|
|
|
TodoResponse({
|
|
required this.id,
|
|
required this.ownerId,
|
|
required this.title,
|
|
this.description,
|
|
required this.priority,
|
|
required this.order,
|
|
required this.status,
|
|
this.completedAt,
|
|
required this.createdAt,
|
|
required this.updatedAt,
|
|
this.scheduleItems = const [],
|
|
});
|
|
|
|
factory TodoResponse.fromJson(Map<String, dynamic> json) {
|
|
final scheduleItemsList = json['schedule_items'] as List<dynamic>? ?? [];
|
|
return TodoResponse(
|
|
id: json['id'] as String,
|
|
ownerId: json['owner_id'] as String,
|
|
title: json['title'] as String,
|
|
description: json['description'] as String?,
|
|
priority: json['priority'] as int,
|
|
order: json['order'] as int,
|
|
status: json['status'] as String,
|
|
completedAt: json['completed_at'] != null
|
|
? DateTime.parse(json['completed_at'] as String)
|
|
: null,
|
|
createdAt: DateTime.parse(json['created_at'] as String),
|
|
updatedAt: DateTime.parse(json['updated_at'] as String),
|
|
scheduleItems: scheduleItemsList
|
|
.map((e) => ScheduleItemBasic.fromJson(e as Map<String, dynamic>))
|
|
.toList(),
|
|
);
|
|
}
|
|
|
|
TodoResponse copyWith({
|
|
String? id,
|
|
String? ownerId,
|
|
String? title,
|
|
String? description,
|
|
int? priority,
|
|
int? order,
|
|
String? status,
|
|
DateTime? completedAt,
|
|
DateTime? createdAt,
|
|
DateTime? updatedAt,
|
|
List<ScheduleItemBasic>? scheduleItems,
|
|
}) {
|
|
return TodoResponse(
|
|
id: id ?? this.id,
|
|
ownerId: ownerId ?? this.ownerId,
|
|
title: title ?? this.title,
|
|
description: description ?? this.description,
|
|
priority: priority ?? this.priority,
|
|
order: order ?? this.order,
|
|
status: status ?? this.status,
|
|
completedAt: completedAt ?? this.completedAt,
|
|
createdAt: createdAt ?? this.createdAt,
|
|
updatedAt: updatedAt ?? this.updatedAt,
|
|
scheduleItems: scheduleItems ?? this.scheduleItems,
|
|
);
|
|
}
|
|
}
|