LCOV - code coverage report
Current view: top level - lib/data/datasources - map_pym_remote_data_source.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 125 134 93.3 %
Date: 2020-06-26 11:36:11 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:convert';
       2             : import 'dart:io';
       3             : 
       4             : import 'package:app_pym/core/error/exceptions.dart';
       5             : import 'package:app_pym/data/models/app_pym/booking_model.dart';
       6             : import 'package:app_pym/data/models/app_pym/contact_categorie_model.dart';
       7             : import 'package:app_pym/data/models/app_pym/contact_model.dart';
       8             : import 'package:app_pym/data/models/app_pym/post_model.dart';
       9             : import 'package:app_pym/data/models/app_pym/service_categorie_model.dart';
      10             : import 'package:app_pym/data/models/app_pym/service_model.dart';
      11             : import 'package:app_pym/data/models/authentication/app_user_model.dart';
      12             : import 'package:app_pym/data/models/map_pym/batiment_model.dart';
      13             : import 'package:app_pym/data/models/map_pym/entreprise_model.dart';
      14             : import 'package:flutter/foundation.dart';
      15             : import 'package:http/http.dart' as http;
      16             : import 'package:injectable/injectable.dart';
      17             : 
      18             : abstract class MapPymRemoteDataSource {
      19             :   Future<void> createBooking(BookingModel booking, {@required String token});
      20             : 
      21             :   Future<void> deleteBooking(int booking_id, {@required String token});
      22             : 
      23             :   Future<List<BatimentModel>> fetchAllBatiment();
      24             : 
      25             :   /// Récupère toute les réservations du [service_id]
      26             :   Future<List<BookingModel>> fetchAllBookingsOf(int service_id);
      27             : 
      28             :   /// Retourne les catégories de contact_type
      29             :   Future<List<ContactCategorieModel>> fetchAllContactCategories();
      30             : 
      31             :   Future<List<EntrepriseModel>> fetchAllEntreprises();
      32             : 
      33             :   /// Récupère les posts du fil d'actualité
      34             :   Future<List<PostModel>> fetchAllPosts();
      35             : 
      36             :   /// Retourne les catégories de services
      37             :   Future<List<ServiceCategorieModel>> fetchAllServiceCategories();
      38             : 
      39             :   Future<BatimentModel> fetchBatiment(int id);
      40             : 
      41             :   Future<ContactModel> fetchContact(int id);
      42             : 
      43             :   Future<List<EntrepriseModel>> fetchEntreprisesOfBatiment(int idBatiment);
      44             : 
      45             :   /// Retourne les services d'une catégorie
      46             :   Future<List<ServiceModel>> fetchServicesOf(int categorie_id);
      47             : 
      48             :   /// Récupère un utilisateur
      49             :   Future<AppUserModel> fetchUser(String token);
      50             : 
      51             :   Future<void> updateBooking(BookingModel booking, {@required String token});
      52             : }
      53             : 
      54             : @prod
      55             : @LazySingleton(as: MapPymRemoteDataSource)
      56             : class MapPymRemoteDataSourceImpl implements MapPymRemoteDataSource {
      57             :   final http.Client client;
      58             : 
      59           1 :   const MapPymRemoteDataSourceImpl({@required this.client});
      60             : 
      61             :   @override
      62           1 :   Future<void> createBooking(BookingModel booking,
      63             :       {@required String token}) async {
      64           2 :     final bookingMap = booking.toJson()..remove('id');
      65             : 
      66           1 :     final data = json.encode(bookingMap);
      67           1 :     print(data);
      68           3 :     final response = await client.post(
      69             :       'https://admin.map-pym.com/api/bookings',
      70           1 :       headers: {
      71           1 :         HttpHeaders.authorizationHeader: "Bearer ${token}",
      72             :         HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
      73             :       },
      74             :       body: data,
      75             :     );
      76             : 
      77           2 :     if (response.statusCode == HttpStatus.ok ||
      78           2 :         response.statusCode == HttpStatus.created) {
      79             :       return;
      80             :     } else {
      81           1 :       throw ServerException(
      82           4 :           'Failed to create booking : ${response.statusCode} ${response.reasonPhrase}\n${response.body}');
      83             :     }
      84             :   }
      85             : 
      86             :   @override
      87           1 :   Future<void> deleteBooking(int booking_id, {@required String token}) async {
      88           3 :     final response = await client.delete(
      89           1 :       'https://admin.map-pym.com/api/bookings/${booking_id}',
      90           1 :       headers: {
      91           1 :         HttpHeaders.authorizationHeader: "Bearer ${token}",
      92             :         HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
      93             :       },
      94             :     );
      95             : 
      96           2 :     if (response.statusCode == HttpStatus.ok) {
      97             :       return;
      98             :     } else {
      99           1 :       throw ServerException(
     100           3 :           'Failed to delete booking : ${response.statusCode} ${response.reasonPhrase}');
     101             :     }
     102             :   }
     103             : 
     104             :   @override
     105           1 :   Future<List<BatimentModel>> fetchAllBatiment() async {
     106             :     final response =
     107           3 :         await client.get('https://admin.map-pym.com/api/batiments');
     108             : 
     109           2 :     if (response.statusCode == HttpStatus.ok) {
     110           2 :       return (json.decode(response.body) as List)
     111           2 :           ?.map((dynamic data) =>
     112           1 :               BatimentModel.fromJson(data as Map<String, dynamic>))
     113           1 :           ?.toList();
     114             :     } else {
     115           1 :       throw ServerException(
     116           3 :           'Failed to load batiments : ${response.statusCode} ${response.reasonPhrase}');
     117             :     }
     118             :   }
     119             : 
     120             :   @override
     121           1 :   Future<List<BookingModel>> fetchAllBookingsOf(int service_id) async {
     122           2 :     final response = await client
     123           2 :         .get('https://admin.map-pym.com/api/services/${service_id}/bookings');
     124             : 
     125           2 :     if (response.statusCode == HttpStatus.ok) {
     126           2 :       print(response.body);
     127           2 :       return (json.decode(response.body) as List)
     128           2 :           ?.map((dynamic data) =>
     129           1 :               BookingModel.fromJson(data as Map<String, dynamic>))
     130           4 :           ?.where((e) => e.service_id == service_id)
     131           1 :           ?.toList();
     132             :     } else {
     133           1 :       throw ServerException(
     134           3 :           'Failed to load bookings of service ${service_id} : ${response.statusCode} ${response.reasonPhrase}');
     135             :     }
     136             :   }
     137             : 
     138             :   @override
     139           1 :   Future<List<ContactCategorieModel>> fetchAllContactCategories() async {
     140             :     final response =
     141           3 :         await client.get('https://admin.map-pym.com/api/contact_categories');
     142             : 
     143           2 :     if (response.statusCode == HttpStatus.ok) {
     144           2 :       return (json.decode(response.body) as List)
     145           2 :           ?.map((dynamic data) =>
     146           1 :               ContactCategorieModel.fromJson(data as Map<String, dynamic>))
     147           1 :           ?.toList();
     148             :     } else {
     149           1 :       throw ServerException(
     150           3 :           'Failed to load categorie : ${response.statusCode} ${response.reasonPhrase}');
     151             :     }
     152             :   }
     153             : 
     154             :   @override
     155           0 :   Future<List<EntrepriseModel>> fetchAllEntreprises() async {
     156             :     final response =
     157           0 :         await client.get('https://admin.map-pym.com/api/entreprises');
     158             : 
     159           0 :     if (response.statusCode == HttpStatus.ok) {
     160           0 :       return (json.decode(response.body) as List)
     161           0 :           ?.map((dynamic data) =>
     162           0 :               EntrepriseModel.fromJson(data as Map<String, dynamic>))
     163           0 :           ?.toList();
     164             :     } else {
     165           0 :       throw ServerException(
     166           0 :           'Failed to load entreprises : ${response.statusCode} ${response.reasonPhrase}');
     167             :     }
     168             :   }
     169             : 
     170             :   @override
     171           1 :   Future<List<PostModel>> fetchAllPosts() async {
     172           3 :     final response = await client.get('https://admin.map-pym.com/api/posts');
     173             : 
     174           2 :     if (response.statusCode == HttpStatus.ok) {
     175           2 :       return (json.decode(response.body) as List)
     176           2 :           ?.map((dynamic data) =>
     177           1 :               PostModel.fromJson(data as Map<String, dynamic>))
     178           1 :           ?.toList();
     179             :     } else {
     180           1 :       throw ServerException(
     181           3 :           'Failed to load Posts : ${response.statusCode} ${response.reasonPhrase}');
     182             :     }
     183             :   }
     184             : 
     185             :   @override
     186           1 :   Future<List<ServiceCategorieModel>> fetchAllServiceCategories() async {
     187             :     final response =
     188           3 :         await client.get('https://admin.map-pym.com/api/service_categories');
     189             : 
     190           2 :     if (response.statusCode == HttpStatus.ok) {
     191           2 :       return (json.decode(response.body) as List)
     192           2 :           ?.map((dynamic data) =>
     193           1 :               ServiceCategorieModel.fromJson(data as Map<String, dynamic>))
     194           1 :           ?.toList();
     195             :     } else {
     196           1 :       throw ServerException(
     197           3 :           'Failed to load categorie : ${response.statusCode} ${response.reasonPhrase}');
     198             :     }
     199             :   }
     200             : 
     201             :   @override
     202           1 :   Future<BatimentModel> fetchBatiment(int id) async {
     203             :     final response =
     204           3 :         await client.get('https://admin.map-pym.com/api/batiments');
     205             : 
     206           2 :     if (response.statusCode == HttpStatus.ok) {
     207           2 :       return (json.decode(response.body) as List)
     208           2 :           ?.map((dynamic data) =>
     209           1 :               BatimentModel.fromJson(data as Map<String, dynamic>))
     210           4 :           ?.firstWhere((element) => element.id == id);
     211             :     } else {
     212           1 :       throw ServerException(
     213           3 :           'Failed to load batiments : ${response.statusCode} ${response.reasonPhrase}');
     214             :     }
     215             :   }
     216             : 
     217             :   @override
     218           1 :   Future<ContactModel> fetchContact(int id) async {
     219           3 :     final response = await client.get('https://admin.map-pym.com/api/contacts');
     220             : 
     221           2 :     if (response.statusCode == HttpStatus.ok) {
     222           2 :       return (json.decode(response.body) as List)
     223           2 :           ?.map((dynamic data) =>
     224           1 :               ContactModel.fromJson(data as Map<String, dynamic>))
     225           4 :           ?.firstWhere((e) => e.id == id);
     226             :     } else {
     227           1 :       throw ServerException(
     228           3 :           'Failed to fetch contact $id : ${response.statusCode} ${response.reasonPhrase}');
     229             :     }
     230             :   }
     231             : 
     232             :   @override
     233           1 :   Future<List<EntrepriseModel>> fetchEntreprisesOfBatiment(
     234             :       int idBatiment) async {
     235             :     final response =
     236           3 :         await client.get('https://admin.map-pym.com/api/entreprises');
     237             : 
     238           2 :     if (response.statusCode == HttpStatus.ok) {
     239           2 :       return (json.decode(response.body) as List)
     240           2 :           ?.map((dynamic data) =>
     241           1 :               EntrepriseModel.fromJson(data as Map<String, dynamic>))
     242           4 :           ?.where((element) => element.idBatiment == idBatiment)
     243           1 :           ?.toList();
     244             :     } else {
     245           1 :       throw ServerException(
     246           3 :           'Failed to load entreprises : ${response.statusCode} ${response.reasonPhrase}');
     247             :     }
     248             :   }
     249             : 
     250             :   @override
     251           1 :   Future<List<ServiceModel>> fetchServicesOf(int categorie_id) async {
     252           3 :     final response = await client.get('https://admin.map-pym.com/api/services');
     253             : 
     254           2 :     if (response.statusCode == HttpStatus.ok) {
     255           2 :       return (json.decode(response.body) as List)
     256           2 :           ?.map((dynamic data) =>
     257           1 :               ServiceModel.fromJson(data as Map<String, dynamic>))
     258           4 :           ?.where((e) => e.categorie_id == categorie_id)
     259           1 :           ?.toList();
     260             :     } else {
     261           1 :       throw ServerException(
     262           3 :           'Failed to load services of categorie ${categorie_id} : ${response.statusCode} ${response.reasonPhrase}');
     263             :     }
     264             :   }
     265             : 
     266             :   @override
     267           1 :   Future<AppUserModel> fetchUser(String token) async {
     268           3 :     final response = await client.get(
     269             :       'https://admin.map-pym.com/api/users/me',
     270           2 :       headers: {HttpHeaders.authorizationHeader: "Bearer $token"},
     271             :     );
     272             : 
     273           2 :     if (response.statusCode == HttpStatus.ok) {
     274           2 :       final body = json.decode(response.body) as Map<String, dynamic>;
     275           1 :       return AppUserModel.fromJson(body);
     276             :     } else {
     277           1 :       throw ServerException(
     278           3 :           'Failed to load user : ${response.statusCode} ${response.reasonPhrase}');
     279             :     }
     280             :   }
     281             : 
     282             :   @override
     283           1 :   Future<void> updateBooking(BookingModel booking,
     284             :       {@required String token}) async {
     285           2 :     final bookingMap = booking.toJson()..remove('id');
     286             : 
     287           1 :     final data = json.encode(bookingMap);
     288           1 :     print(data);
     289           3 :     final response = await client.patch(
     290           2 :       'https://admin.map-pym.com/api/bookings/${booking.id}',
     291           1 :       headers: {
     292           1 :         HttpHeaders.authorizationHeader: "Bearer ${token}",
     293             :         HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
     294             :       },
     295             :       body: data,
     296             :     );
     297             : 
     298           2 :     if (response.statusCode == HttpStatus.ok) {
     299             :       return;
     300             :     } else {
     301           1 :       throw ServerException(
     302           4 :           'Failed to edit booking : ${response.statusCode} ${response.reasonPhrase}\n${response.body}');
     303             :     }
     304             :   }
     305             : }

Generated by: LCOV version 1.13