getAllPageFromBlog method

Future<PageModel> getAllPageFromBlog({
  1. required String blogId,
  2. required String apiKey,
})

getAllPageFromBlog This function will return all Pages in the blog

We need to pass BlogId and ApiKey.

if the statusCode is 200 then the API call is Successful elase the will be updated in BlogsModel.error field.

if PageModel.error is null then your all is Successful.

As of the API Not return Page Comments so Replies is null for all the pages

Implementation

Future<PageModel> getAllPageFromBlog({
  required String blogId,
  required String apiKey,
}) async {
  PageModel pageModel = PageModel(error: 'API Call Failed');
  List<PostItemModel>? items = [];
  try {
    final res = await http
        .get(Uri.parse('$blogApiUrlByid/$blogId/pages?key=$apiKey'));

    switch (res.statusCode) {

      /// if the status code is 2000 then you API call is Successful and we got the datat from server
      case 200:
        if (jsonDecode(res.body)['items'] != null) {
          for (int i = 0; i < jsonDecode(res.body)['items'].length; i++) {
            PostItemModel postItemModel = PostItemModel.fromJson(
                    jsonEncode(jsonDecode(res.body)['items'][i]))
                .copyWith(
                    replies: Replies(
                        postComments: [], selfLink: '', totalItems: 0));

            items.add(postItemModel);
          }

          pageModel = PageModel(
            error: null,
            etag: jsonDecode(res.body)['etag'],
            kind: jsonDecode(res.body)['kind'],
            items: items,
          );
        } else {
          /// it means no items in the Blog
          pageModel = PageModel(
            error: null,
            etag: jsonDecode(res.body)['etag'],
            kind: jsonDecode(res.body)['kind'],
            items: items,
          );
        }

        break;
      default:

        /// we got some error so it;s update in [Error] column
        pageModel = PageModel(error: res.body, items: null);
    }
  } catch (e) {
    /// we got some error so it;s update in [Error] column
    pageModel = PageModel(error: e.toString());
  }
  return pageModel;
}