feat: prompts from workspace
This commit is contained in:
69
lib/core/models/prompt.dart
Normal file
69
lib/core/models/prompt.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
@immutable
|
||||
class Prompt {
|
||||
const Prompt({
|
||||
required this.command,
|
||||
required this.title,
|
||||
required this.content,
|
||||
this.accessControl,
|
||||
this.userId,
|
||||
this.timestamp,
|
||||
});
|
||||
|
||||
final String command;
|
||||
final String title;
|
||||
final String content;
|
||||
final Map<String, dynamic>? accessControl;
|
||||
final String? userId;
|
||||
final int? timestamp;
|
||||
|
||||
factory Prompt.fromJson(Map<String, dynamic> json) {
|
||||
final rawCommand = (json['command'] as String? ?? '').trim();
|
||||
final normalizedCommand = rawCommand.startsWith('/')
|
||||
? rawCommand
|
||||
: (rawCommand.isEmpty ? rawCommand : '/$rawCommand');
|
||||
|
||||
return Prompt(
|
||||
command: normalizedCommand,
|
||||
title: json['title'] as String? ?? '',
|
||||
content: json['content'] as String? ?? '',
|
||||
accessControl: json['access_control'] is Map<String, dynamic>
|
||||
? Map<String, dynamic>.from(json['access_control'] as Map)
|
||||
: null,
|
||||
userId: json['user_id'] as String?,
|
||||
timestamp: json['timestamp'] is int
|
||||
? json['timestamp'] as int
|
||||
: int.tryParse('${json['timestamp']}'),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return <String, dynamic>{
|
||||
'command': command,
|
||||
'title': title,
|
||||
'content': content,
|
||||
if (accessControl != null) 'access_control': accessControl,
|
||||
if (userId != null) 'user_id': userId,
|
||||
if (timestamp != null) 'timestamp': timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
Prompt copyWith({
|
||||
String? command,
|
||||
String? title,
|
||||
String? content,
|
||||
Map<String, dynamic>? accessControl,
|
||||
String? userId,
|
||||
int? timestamp,
|
||||
}) {
|
||||
return Prompt(
|
||||
command: command ?? this.command,
|
||||
title: title ?? this.title,
|
||||
content: content ?? this.content,
|
||||
accessControl: accessControl ?? this.accessControl,
|
||||
userId: userId ?? this.userId,
|
||||
timestamp: timestamp ?? this.timestamp,
|
||||
);
|
||||
}
|
||||
}
|
||||
32
lib/core/services/prompts_service.dart
Normal file
32
lib/core/services/prompts_service.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import 'package:conduit/core/error/api_error_handler.dart';
|
||||
import 'package:conduit/core/models/prompt.dart';
|
||||
import 'package:conduit/core/providers/app_providers.dart';
|
||||
import 'package:conduit/core/services/api_service.dart';
|
||||
|
||||
class PromptsService {
|
||||
const PromptsService(this._apiService);
|
||||
|
||||
final ApiService _apiService;
|
||||
|
||||
Future<List<Prompt>> getPrompts() async {
|
||||
try {
|
||||
final List<Map<String, dynamic>> response = await _apiService
|
||||
.getPrompts();
|
||||
return response
|
||||
.map((item) => Prompt.fromJson(item))
|
||||
.where((prompt) => prompt.command.isNotEmpty)
|
||||
.toList();
|
||||
} on DioException catch (error) {
|
||||
throw ApiErrorHandler().transformError(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final promptsServiceProvider = Provider<PromptsService?>((ref) {
|
||||
final apiService = ref.watch(apiServiceProvider);
|
||||
if (apiService == null) return null;
|
||||
return PromptsService(apiService);
|
||||
});
|
||||
Reference in New Issue
Block a user