Skip to content

Commit

Permalink
feat: add warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tnc1997 committed May 20, 2021
1 parent 7ce45c4 commit 53e7562
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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';
Expand Down Expand Up @@ -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';
5 changes: 5 additions & 0 deletions lib/src/apetito_product_api_client_base.dart
Expand Up @@ -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 {
Expand Down Expand Up @@ -74,6 +75,10 @@ class ApetitoProductApiClient {
RankingService get rankings => RankingService(
context: _context,
);

WarningService get warnings => WarningService(
context: _context,
);
}

class ApetitoProductApiClientContext {
Expand Down
25 changes: 25 additions & 0 deletions 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<String> {
Product? product;
Warning? warning;

ProductWarning({
required String id,
this.product,
this.warning,
}) : super(
id: id,
);

factory ProductWarning.fromJson(Map<String, dynamic> json) =>
_$ProductWarningFromJson(json);

Map<String, dynamic> toJson() => _$ProductWarningToJson(this);
}
26 changes: 26 additions & 0 deletions lib/src/models/product_warning.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions 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> {
String? name;

Warning({
required String id,
this.name,
}) : super(
id: id,
);

factory Warning.fromJson(Map<String, dynamic> json) =>
_$WarningFromJson(json);

@override
String toString() => name ?? super.toString();

Map<String, dynamic> toJson() => _$WarningToJson(this);
}
19 changes: 19 additions & 0 deletions lib/src/models/warning.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions lib/src/services/product_service.dart
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +53,17 @@ class ProductService {
return Product.fromJson(json.decode(response.body));
}

/// Gets the product that matches a code.
Future<Product> 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<List<Channel>> getByIdChannels(String id) async {
final response = await _context.client.get(
Expand Down Expand Up @@ -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<List<ProductWarning>> 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<List<Warning>> 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();
}
}
66 changes: 66 additions & 0 deletions 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<List<Warning>> 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<Warning> 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<List<ProductWarning>> 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<List<Product>> 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();
}
}

0 comments on commit 53e7562

Please sign in to comment.