updateBlogPost method

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

Update a blog post (authenticated)

Implementation

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

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

    if (response.statusCode == 200) {
      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 == 404) {
      throw Exception('Blog post not found');
    } else {
      throw Exception('Failed to update blog post');
    }
  } on DioException catch (e) {
    throw Exception('Error updating blog post: ${e.message}');
  } catch (e) {
    throw Exception('Error: $e');
  }
}