From 53e75629b738ba7a86e726a2d7afca15c957d105 Mon Sep 17 00:00:00 2001 From: Thomas Clark Date: Thu, 20 May 2021 16:15:07 +0100 Subject: [PATCH] feat: add warnings --- lib/apetito_product_api_client.dart | 3 +- lib/src/apetito_product_api_client_base.dart | 5 ++ lib/src/models/product_warning.dart | 25 ++++++++ lib/src/models/product_warning.g.dart | 26 ++++++++ lib/src/models/warning.dart | 24 +++++++ lib/src/models/warning.g.dart | 19 ++++++ lib/src/services/product_service.dart | 39 ++++++++++++ lib/src/services/warning_service.dart | 66 ++++++++++++++++++++ 8 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 lib/src/models/product_warning.dart create mode 100644 lib/src/models/product_warning.g.dart create mode 100644 lib/src/models/warning.dart create mode 100644 lib/src/models/warning.g.dart create mode 100644 lib/src/services/warning_service.dart diff --git a/lib/apetito_product_api_client.dart b/lib/apetito_product_api_client.dart index 9cac4e8..75fd1ef 100644 --- a/lib/apetito_product_api_client.dart +++ b/lib/apetito_product_api_client.dart @@ -1,5 +1,6 @@ library apetito_product_api_client; +export 'package:apetito_product_api_client/src/apetito_product_api_client_base.dart'; export 'package:apetito_product_api_client/src/constants/uri_constants.dart'; export 'package:apetito_product_api_client/src/exceptions/client_exception.dart'; export 'package:apetito_product_api_client/src/extensions/response_extensions.dart'; @@ -39,4 +40,4 @@ export 'package:apetito_product_api_client/src/services/nutrition_service.dart'; export 'package:apetito_product_api_client/src/services/product_group_service.dart'; export 'package:apetito_product_api_client/src/services/product_service.dart'; export 'package:apetito_product_api_client/src/services/ranking_service.dart'; -export 'package:apetito_product_api_client/src/apetito_product_api_client_base.dart'; +export 'package:apetito_product_api_client/src/services/warning_service.dart'; diff --git a/lib/src/apetito_product_api_client_base.dart b/lib/src/apetito_product_api_client_base.dart index b96168d..d80dbe5 100644 --- a/lib/src/apetito_product_api_client_base.dart +++ b/lib/src/apetito_product_api_client_base.dart @@ -11,6 +11,7 @@ import 'package:apetito_product_api_client/src/services/nutrition_service.dart'; import 'package:apetito_product_api_client/src/services/product_group_service.dart'; import 'package:apetito_product_api_client/src/services/product_service.dart'; import 'package:apetito_product_api_client/src/services/ranking_service.dart'; +import 'package:apetito_product_api_client/src/services/warning_service.dart'; import 'package:http/http.dart'; class ApetitoProductApiClient { @@ -74,6 +75,10 @@ class ApetitoProductApiClient { RankingService get rankings => RankingService( context: _context, ); + + WarningService get warnings => WarningService( + context: _context, + ); } class ApetitoProductApiClientContext { diff --git a/lib/src/models/product_warning.dart b/lib/src/models/product_warning.dart new file mode 100644 index 0000000..18a3ed6 --- /dev/null +++ b/lib/src/models/product_warning.dart @@ -0,0 +1,25 @@ +import 'package:apetito_product_api_client/src/models/model.dart'; +import 'package:apetito_product_api_client/src/models/product.dart'; +import 'package:apetito_product_api_client/src/models/warning.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'product_warning.g.dart'; + +@JsonSerializable() +class ProductWarning extends Model { + Product? product; + Warning? warning; + + ProductWarning({ + required String id, + this.product, + this.warning, + }) : super( + id: id, + ); + + factory ProductWarning.fromJson(Map json) => + _$ProductWarningFromJson(json); + + Map toJson() => _$ProductWarningToJson(this); +} diff --git a/lib/src/models/product_warning.g.dart b/lib/src/models/product_warning.g.dart new file mode 100644 index 0000000..50e1f49 --- /dev/null +++ b/lib/src/models/product_warning.g.dart @@ -0,0 +1,26 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'product_warning.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +ProductWarning _$ProductWarningFromJson(Map json) { + return ProductWarning( + id: json['id'] as String, + product: json['product'] == null + ? null + : Product.fromJson(json['product'] as Map), + warning: json['warning'] == null + ? null + : Warning.fromJson(json['warning'] as Map), + ); +} + +Map _$ProductWarningToJson(ProductWarning instance) => + { + 'id': instance.id, + 'product': instance.product, + 'warning': instance.warning, + }; diff --git a/lib/src/models/warning.dart b/lib/src/models/warning.dart new file mode 100644 index 0000000..34435f6 --- /dev/null +++ b/lib/src/models/warning.dart @@ -0,0 +1,24 @@ +import 'package:apetito_product_api_client/src/models/model.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'warning.g.dart'; + +@JsonSerializable() +class Warning extends Model { + String? name; + + Warning({ + required String id, + this.name, + }) : super( + id: id, + ); + + factory Warning.fromJson(Map json) => + _$WarningFromJson(json); + + @override + String toString() => name ?? super.toString(); + + Map toJson() => _$WarningToJson(this); +} diff --git a/lib/src/models/warning.g.dart b/lib/src/models/warning.g.dart new file mode 100644 index 0000000..5e2ac62 --- /dev/null +++ b/lib/src/models/warning.g.dart @@ -0,0 +1,19 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'warning.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +Warning _$WarningFromJson(Map json) { + return Warning( + id: json['id'] as String, + name: json['name'] as String?, + ); +} + +Map _$WarningToJson(Warning instance) => { + 'id': instance.id, + 'name': instance.name, + }; diff --git a/lib/src/services/product_service.dart b/lib/src/services/product_service.dart index f410de5..4bf26e2 100644 --- a/lib/src/services/product_service.dart +++ b/lib/src/services/product_service.dart @@ -19,6 +19,8 @@ import 'package:apetito_product_api_client/src/models/product_made_without.dart' import 'package:apetito_product_api_client/src/models/product_meal_type.dart'; import 'package:apetito_product_api_client/src/models/product_microwave_stage.dart'; import 'package:apetito_product_api_client/src/models/product_nutrition.dart'; +import 'package:apetito_product_api_client/src/models/product_warning.dart'; +import 'package:apetito_product_api_client/src/models/warning.dart'; class ProductService { final ApetitoProductApiClientContext _context; @@ -51,6 +53,17 @@ class ProductService { return Product.fromJson(json.decode(response.body)); } + /// Gets the product that matches a code. + Future getByCode(String code) async { + final response = await _context.client.get( + Uri.https(authority, '$path/products/code/$code'), + ); + + ClientException.checkIsSuccessStatusCode(response); + + return Product.fromJson(json.decode(response.body)); + } + /// Gets all the channels of the product that matches an id. Future> getByIdChannels(String id) async { final response = await _context.client.get( @@ -246,4 +259,30 @@ class ProductService { .map((e) => ProductNutrition.fromJson(e)) .toList(); } + + /// Gets all the product warning information of the product that matches an id. + Future> getByIdProductWarnings(String id) async { + final response = await _context.client.get( + Uri.https(authority, '$path/products/$id/productwarnings'), + ); + + ClientException.checkIsSuccessStatusCode(response); + + return (json.decode(response.body) as List) + .map((e) => ProductWarning.fromJson(e)) + .toList(); + } + + /// Gets all the warnings of the product that matches an id. + Future> getByIdWarnings(String id) async { + final response = await _context.client.get( + Uri.https(authority, '$path/products/$id/warnings'), + ); + + ClientException.checkIsSuccessStatusCode(response); + + return (json.decode(response.body) as List) + .map((e) => Warning.fromJson(e)) + .toList(); + } } diff --git a/lib/src/services/warning_service.dart b/lib/src/services/warning_service.dart new file mode 100644 index 0000000..f54fb2d --- /dev/null +++ b/lib/src/services/warning_service.dart @@ -0,0 +1,66 @@ +import 'dart:convert'; + +import 'package:apetito_product_api_client/src/apetito_product_api_client_base.dart'; +import 'package:apetito_product_api_client/src/constants/uri_constants.dart'; +import 'package:apetito_product_api_client/src/exceptions/client_exception.dart'; +import 'package:apetito_product_api_client/src/models/product.dart'; +import 'package:apetito_product_api_client/src/models/product_warning.dart'; +import 'package:apetito_product_api_client/src/models/warning.dart'; + +class WarningService { + final ApetitoProductApiClientContext _context; + + WarningService({ + required ApetitoProductApiClientContext context, + }) : _context = context; + + /// Gets all the warnings. + Future> get() async { + final response = await _context.client.get( + Uri.https(authority, '$path/warnings'), + ); + + ClientException.checkIsSuccessStatusCode(response); + + return (json.decode(response.body) as List) + .map((e) => Warning.fromJson(e)) + .toList(); + } + + /// Gets the warning that matches an id. + Future getById(String id) async { + final response = await _context.client.get( + Uri.https(authority, '$path/warnings/$id'), + ); + + ClientException.checkIsSuccessStatusCode(response); + + return Warning.fromJson(json.decode(response.body)); + } + + /// Gets all the product warning information of the warning that matches an id. + Future> getByIdProductWarnings(String id) async { + final response = await _context.client.get( + Uri.https(authority, '$path/warnings/$id/productwarnings'), + ); + + ClientException.checkIsSuccessStatusCode(response); + + return (json.decode(response.body) as List) + .map((e) => ProductWarning.fromJson(e)) + .toList(); + } + + /// Gets all the products of the warning that matches an id. + Future> getByIdProducts(String id) async { + final response = await _context.client.get( + Uri.https(authority, '$path/warnings/$id/products'), + ); + + ClientException.checkIsSuccessStatusCode(response); + + return (json.decode(response.body) as List) + .map((e) => Product.fromJson(e)) + .toList(); + } +}