Add models and sdk services example

This commit is contained in:
Sambo Chea 2022-10-28 10:10:46 +07:00
parent 82de95b4b7
commit d0f3debf2e
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
18 changed files with 598 additions and 85 deletions

1
lib/sdk.dart Normal file
View File

@ -0,0 +1 @@
export './sdk/client_sdk.dart';

View File

@ -1,20 +0,0 @@
import 'package:sample_dart_dsdk/sdk/services/CategoryService.dart';
import 'package:sample_dart_dsdk/sdk/services/ProductService.dart';
class ClientSDK {
static var _instance;
static ClientSDK getInstance() {
if (_instance == null) {
ClientSDK();
}
return _instance;
}
ProductService getProductService() {
return ProductService();
}
CategoryService getCategoryService() => CategoryService();
}

27
lib/sdk/client_sdk.dart Normal file
View File

@ -0,0 +1,27 @@
import 'package:sample_sdk/sdk/services/category.dart';
import 'package:sample_sdk/sdk/services/product.dart';
class ClientSDK {
static var _instance;
static ClientSDK getInstance() {
if (_instance == null) {
ClientSDK();
}
return _instance;
}
ProductService productService = ProductService();
CategoryService categoryService = CategoryService();
ClientSDK() {
_instance = this;
}
void dispose() {
_instance = null;
}
}

View File

@ -1,6 +0,0 @@
class Category {
int id;
String name;
Category(this.id, this.name);
}

View File

@ -1,20 +0,0 @@
class Product {
int id;
String name;
double price;
Product(this.id, this.name, this.price);
static Product fromJson(dynamic json) => Product(
json['id'],
json['name'],
json['price'],
);
}
class ProductRequest {
String name;
double price;
ProductRequest(this.name, this.price);
}

31
lib/sdk/models/base.dart Normal file
View File

@ -0,0 +1,31 @@
import 'dart:convert';
abstract class BaseModel<T> {
BaseModel();
Map<String, dynamic> toMap();
T fromMap(Map<String, dynamic> map);
T fromJson(String json);
@override
String toString() {
return toMap().toString();
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is BaseModel && other.toMap() == toMap();
}
@override
int get hashCode => toMap().hashCode;
String toJson() => encode(toMap());
dynamic decode(String json) => jsonDecode(json);
String encode(dynamic json) => jsonEncode(json);
}

View File

@ -0,0 +1,34 @@
import 'package:sample_sdk/sdk/models/base.dart';
class Category extends BaseModel<Category> {
int? id;
String? name;
Category? parent;
Category({this.id, this.name, this.parent});
@override
Category fromMap(Map<String, dynamic> map) {
id = map['id'];
name = map['name'];
parent = Category().fromJson(map['parent']);
return this;
}
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'parent': parent?.toMap(),
};
}
@override
Category fromJson(String json) {
return fromMap(decode(json));
}
static Category fromJsonString(dynamic json) => Category().fromJson(json);
}

View File

@ -0,0 +1,38 @@
import 'package:sample_sdk/sdk/models/base.dart';
import 'package:sample_sdk/sdk/models/category.dart';
class Product extends BaseModel<Product> {
int? id;
String? name;
double? price;
Category? category;
Product({this.id, this.name, this.price, this.category});
@override
Product fromMap(Map<String, dynamic> map) {
id = map['id'];
name = map['name'];
price = map['price'];
category = Category().fromJson(map['category']);
return this;
}
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'price': price,
'category': category?.toMap(),
};
}
@override
Product fromJson(String json) {
return fromMap(decode(json));
}
static Product fromJsonString(String json) => Product().fromJson(json);
}

View File

@ -1,7 +0,0 @@
import 'package:sample_dart_dsdk/sdk/models/CategoryModel.dart';
class CategoryService {
List<Category> getAll() {
return List.empty();
}
}

View File

@ -1,15 +0,0 @@
import 'package:sample_dart_dsdk/sdk/models/ProductModel.dart';
class ProductService {
List<Product> getAll() {
return List.empty();
}
Product getOne() {
return Product.fromJson(null);
}
Product create() {
return Product.fromJson(null);
}
}

View File

@ -0,0 +1,11 @@
abstract class BaseService<T, ID> {
Future<List<T>> findAll();
Future<T> findById(ID id);
Future<T> create(T entity);
Future<T> update(T entity);
Future<void> delete(ID id);
}

View File

@ -0,0 +1,39 @@
import 'package:sample_sdk/sdk/models/category.dart';
import 'package:sample_sdk/sdk/services/base.dart';
class CategoryService extends BaseService<Category, int> {
final List<Category> _categories = [
Category(id: 1, name: 'Category 1'),
Category(id: 2, name: 'Category 2'),
Category(id: 3, name: 'Category 3'),
];
@override
Future<Category> create(Category entity) {
_categories.add(entity);
return Future.value(entity);
}
@override
Future<void> delete(int id) {
_categories.removeWhere((element) => element.id == id);
return Future.value();
}
@override
Future<List<Category>> findAll() {
return Future.value(_categories);
}
@override
Future<Category> findById(int id) {
return Future.value(_categories.firstWhere((element) => element.id == id));
}
@override
Future<Category> update(Category entity) {
final index = _categories.indexWhere((element) => element.id == entity.id);
_categories[index] = entity;
return Future.value(entity);
}
}

View File

@ -0,0 +1,64 @@
import 'package:sample_sdk/sdk/models/category.dart';
import 'package:sample_sdk/sdk/models/product.dart';
import 'package:sample_sdk/sdk/services/base.dart';
class ProductService extends BaseService<Product, int> {
final List<Product> _products = [
Product(
id: 1,
name: 'Product 1',
price: 100.0,
category: Category(
id: 1,
name: 'Category 1',
),
),
Product(
id: 2,
name: 'Product 2',
price: 200.0,
category: Category(
id: 2,
name: 'Category 2',
),
),
Product(
id: 3,
name: 'Product 3',
price: 300.0,
category: Category(
id: 3,
name: 'Category 3',
),
),
];
@override
Future<Product> create(Product entity) {
_products.add(entity);
return Future.value(entity);
}
@override
Future<void> delete(int id) {
_products.removeWhere((element) => element.id == id);
return Future.value();
}
@override
Future<List<Product>> findAll() {
return Future.value(_products);
}
@override
Future<Product> findById(int id) {
return Future.value(_products.firstWhere((element) => element.id == id));
}
@override
Future<Product> update(Product entity) {
final index = _products.indexWhere((element) => element.id == entity.id);
_products[index] = entity;
return Future.value(entity);
}
}

View File

@ -1,13 +0,0 @@
import 'package:sample_dart_dsdk/sdk/ClientSDK.dart';
void main(List<String> args) {
var client = ClientSDK.getInstance();
var productService = client.getProductService();
var products = productService.getAll();
print(products);
var categoryService = client.getCategoryService();
var categories = categoryService.getAll();
print(categories);
}

View File

@ -1,6 +1,167 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_fe_analyzer_shared:
dependency: transitive
description:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "50.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "5.2.0"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.1"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.10.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.17.0"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.1"
coverage:
dependency: transitive
description:
name: coverage
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.4"
frontend_server_client:
dependency: transitive
description:
name: frontend_server_client
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
glob:
dependency: transitive
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.1"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.2"
io:
dependency: transitive
description:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.5"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.13"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
node_preamble:
dependency: transitive
description:
name: node_preamble
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
package_config:
dependency: transitive
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
pedantic:
dependency: "direct dev"
description:
@ -8,5 +169,159 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.1"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.1"
pub_semver:
dependency: transitive
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
shelf:
dependency: transitive
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
shelf_packages_handler:
dependency: transitive
description:
name: shelf_packages_handler
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
shelf_static:
dependency: transitive
description:
name: shelf_static
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
source_map_stack_trace:
dependency: transitive
description:
name: source_map_stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
source_maps:
dependency: transitive
description:
name: source_maps
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.11"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.1"
test:
dependency: "direct dev"
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.21.7"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.15"
test_core:
dependency: transitive
description:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.19"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
vm_service:
dependency: transitive
description:
name: vm_service
url: "https://pub.dartlang.org"
source: hosted
version: "9.4.0"
watcher:
dependency: transitive
description:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
webkit_inspection_protocol:
dependency: transitive
description:
name: webkit_inspection_protocol
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
yaml:
dependency: transitive
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.1"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.18.0 <3.0.0"

View File

@ -1,7 +1,7 @@
name: sample_dart_dsdk
description: A simple command-line application.
name: sample_sdk
description: A simple command-line application and sdk.
version: 1.0.0
# homepage: https://www.example.com
homepage: https://www.cubetiqs.com
environment:
sdk: '>=2.12.0 <3.0.0'
@ -11,3 +11,4 @@ environment:
dev_dependencies:
pedantic: ^1.10.0
test: ^1.16.4

33
test/sample_sdk_test.dart Normal file
View File

@ -0,0 +1,33 @@
import 'package:sample_sdk/sdk.dart';
import 'package:sample_sdk/sdk/models/category.dart';
import 'package:sample_sdk/sdk/models/product.dart';
void main() async {
final sdk = ClientSDK();
final category = Category(
id: 4,
name: 'Category 4',
parent: Category(
id: 1,
name: 'Category 1',
),
);
await sdk.categoryService.create(category);
final categories = await sdk.categoryService.findAll();
print(categories);
final product = Product(
id: 4,
name: 'Product 4',
price: 100.0,
category: category,
);
await sdk.productService.create(product);
final products = await sdk.productService.findAll();
print(products);
sdk.dispose();
}