createBlogPost method

Future<BlogPost> createBlogPost({
  1. required String authToken,
  2. required int appId,
  3. required String title,
  4. required String content,
  5. String? excerpt,
  6. String? slug,
  7. String? featuredImage,
  8. String? author,
  9. bool isPublished = false,
  10. String? publishedAt,
})

Create a blog post (authenticated)

Implementation

Future<BlogPost> createBlogPost({
  required String authToken,
  required int appId,
  required String title,
  required String content,
  String? excerpt,
  String? slug,
  String? featuredImage,
  String? author,
  bool isPublished = false,
  String? publishedAt,
}) async {
  try {
    final body = {
      'app_id': appId,
      'title': title,
      'content': content,
      if (excerpt != null) 'excerpt': excerpt,
      if (slug != null) 'slug': slug,
      if (featuredImage != null) 'featured_image': featuredImage,
      if (author != null) 'author': author,
      'is_published': isPublished,
      if (publishedAt != null) 'published_at': publishedAt,
    };

    final response = await _dio.post(
      '$baseUrl/blog-posts',
      data: body,
      options: Options(headers: {'Authorization': 'Bearer $authToken'}),
    );

    if (response.statusCode == 201) {
      return BlogPost.fromJson(response.data['data'] as Map<String, dynamic>);
    } else if (response.statusCode == 403) {
      throw Exception('Unauthorized - you do not own this app');
    } else if (response.statusCode == 422) {
      throw Exception('Validation failed - check your input');
    } else {
      throw Exception('Failed to create blog post');
    }
  } on DioException catch (e) {
    throw Exception('Error creating blog post: ${e.message}');
  } catch (e) {
    throw Exception('Error: $e');
  }
}