Add models and sdk services example
This commit is contained in:
1
lib/sdk.dart
Normal file
1
lib/sdk.dart
Normal file
@@ -0,0 +1 @@
|
||||
export './sdk/client_sdk.dart';
|
||||
@@ -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
27
lib/sdk/client_sdk.dart
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
class Category {
|
||||
int id;
|
||||
String name;
|
||||
|
||||
Category(this.id, this.name);
|
||||
}
|
||||
@@ -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
31
lib/sdk/models/base.dart
Normal 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);
|
||||
}
|
||||
34
lib/sdk/models/category.dart
Normal file
34
lib/sdk/models/category.dart
Normal 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);
|
||||
}
|
||||
38
lib/sdk/models/product.dart
Normal file
38
lib/sdk/models/product.dart
Normal 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);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import 'package:sample_dart_dsdk/sdk/models/CategoryModel.dart';
|
||||
|
||||
class CategoryService {
|
||||
List<Category> getAll() {
|
||||
return List.empty();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
11
lib/sdk/services/base.dart
Normal file
11
lib/sdk/services/base.dart
Normal 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);
|
||||
}
|
||||
39
lib/sdk/services/category.dart
Normal file
39
lib/sdk/services/category.dart
Normal 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);
|
||||
}
|
||||
}
|
||||
64
lib/sdk/services/product.dart
Normal file
64
lib/sdk/services/product.dart
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user