Skip to content

Commit

Permalink
Merge pull request #25 from gohealthnow/23-implementao-de-busca-na-ba…
Browse files Browse the repository at this point in the history
…rra-de-pesquisa

New features
  • Loading branch information
Polabiel authored Oct 5, 2024
2 parents 02dd1d7 + 0e3582b commit 8b6e06b
Show file tree
Hide file tree
Showing 21 changed files with 778 additions and 138 deletions.
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
analyzer:
errors:
invalid_return_type_for_catch_error: ignore
library_private_types_in_public_api: ignore
unused_field: ignore
unused_local_variable: ignore
use_build_context_synchronously: ignore
# include: package:flutter_lints/flutter.yaml
1 change: 1 addition & 0 deletions lib/api/interfaces/pharmacy_to_product.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abstract class IPharmacyStockItem {}
25 changes: 25 additions & 0 deletions lib/api/models/pharmacy_to_product_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class PharmacyStockItem {
int? id;
int? pharmacyId;
int? productId;
int? quantity;

PharmacyStockItem({this.id, this.pharmacyId, this.productId, this.quantity});

PharmacyStockItem.fromJson(Map<String, dynamic> json) {
id = json['id'];
pharmacyId = json['pharmacyId'];
productId = json['productId'];
quantity = json['quantity'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['pharmacyId'] = this.pharmacyId;
data['productId'] = this.productId;
data['quantity'] = this.quantity;
return data;
}

}
169 changes: 140 additions & 29 deletions lib/api/models/product_models.dart
Original file line number Diff line number Diff line change
@@ -1,56 +1,167 @@
import 'package:gohealth/api/models/pharmacy_to_product_model.dart';
import 'package:gohealth/api/models/user_models.dart';

class ProductModels {
int? id;
String? name;
String? description;
double? price;
int? stock;
String? image;
double? weight;
double? dimensions;
double? rating;
String? createdAt;
String? updatedAt;
List<UserModels>? user;
List<PharmacyStockItem>? pharmacyProduct;
List<Review>? reviews;
List<String>? categories;

ProductModels(
{this.id,
this.name,
this.description,
this.price,
this.stock,
this.image,
this.weight,
this.dimensions,
this.rating,
this.createdAt,
this.updatedAt});

ProductModels.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
description = json['description'];
price = json['price'];
stock = json['stock'];
image = json['image'];
weight = json['weight'];
dimensions = json['dimensions'];
rating = json['rating'];
createdAt = json['createdAt'];
updatedAt = json['updatedAt'];
this.updatedAt,
this.user,
this.pharmacyProduct,
this.reviews,
this.categories});

factory ProductModels.fromJson(Map<String, dynamic> json) {
return ProductModels(
id: json['id'],
name: json['name'],
description: json['description'],
price: json['price'],
image: json['image'],
weight: json['weight'],
dimensions: json['dimensions'],
rating: json['rating'],
createdAt: json['createdAt'],
updatedAt: json['updatedAt'],
categories: json['categories'] != null
? (json['categories'] as List).map((i) => i.toString()).toList()
: null,
user: json['user'] != null
? (json['user'] as List).map((i) => UserModels.fromJson(i)).toList()
: null,
pharmacyProduct: json['PharmacyProduct'] != null
? (json['PharmacyProduct'] as List)
.map((i) => PharmacyStockItem.fromJson(i))
.toList()
: null,
reviews: json['review'] != null
? (json['review'] as List).map((i) => Review.fromJson(i)).toList()
: null,
);
}

Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'description': description,
'price': price,
'image': image,
'weight': weight,
'dimensions': dimensions,
'rating': rating,
'createdAt': createdAt,
'updatedAt': updatedAt,
'user': user?.map((v) => v.toJson()).toList(),
'PharmacyProduct': pharmacyProduct?.map((v) => v.toJson()).toList(),
'review': reviews?.map((v) => v.toJson()).toList(),
};
}
}

class Order {
String id;
int userId;
int productId;
int quantity;
DateTime createdAt;
DateTime updatedAt;
ProductModels product;
UserModels user;

Order({
required this.id,
required this.userId,
required this.productId,
required this.quantity,
required this.createdAt,
required this.updatedAt,
required this.product,
required this.user,
});

factory Order.fromJson(Map<String, dynamic> json) {
return Order(
id: json['id'],
userId: json['userId'],
productId: json['productId'],
quantity: json['quantity'],
createdAt: DateTime.parse(json['createdAt']),
updatedAt: DateTime.parse(json['updatedAt']),
product: ProductModels.fromJson(json['product']),
user: UserModels.fromJson(json['user']),
);
}

Map<String, dynamic> toJson() {
return {
'id': id,
'userId': userId,
'productId': productId,
'quantity': quantity,
'createdAt': createdAt.toIso8601String(),
'updatedAt': updatedAt.toIso8601String(),
'product': product.toJson(),
'user': user.toJson(),
};
}
}

class Review {
int id;
DateTime createdAt;
DateTime updatedAt;
String title;
String body;
double rating;

Review({
required this.id,
required this.createdAt,
required this.updatedAt,
required this.title,
required this.body,
required this.rating,
});

Review.fromJson(Map<String, dynamic> json)
: id = json['id'],
createdAt = DateTime.parse(json['createdAt']),
updatedAt = DateTime.parse(json['updatedAt']),
title = json['title'],
body = json['body'],
rating = json['rating'];

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['name'] = name;
data['description'] = description;
data['price'] = price;
data['stock'] = stock;
data['image'] = image;
data['weight'] = weight;
data['dimensions'] = dimensions;
data['rating'] = rating;
data['createdAt'] = createdAt;
data['updatedAt'] = updatedAt;
return data;
return {
'id': id,
'createdAt': createdAt.toIso8601String(),
'updatedAt': updatedAt.toIso8601String(),
'title': title,
'body': body,
'rating': rating,
};
}
}
82 changes: 55 additions & 27 deletions lib/api/models/user_models.dart
Original file line number Diff line number Diff line change
@@ -1,48 +1,76 @@
import 'package:gohealth/api/models/product_models.dart';

class UserModels {
int? id;
String? createdAt;
String? updatedAt;
DateTime? createdAt;
DateTime? updatedAt;
String? email;
String? password;
String? name;
String? avatar;
Map? product;
String? bio;
String? role;
Role? role;
List<ProductModels>? products;
List<Review>? reviews;
List<Order>? orders;

UserModels({
this.id,
this.createdAt,
this.updatedAt,
this.email,
this.password,
this.name,
this.role,
this.avatar,
this.product,
this.bio,
this.role,
this.products,
this.reviews,
this.orders,
});

UserModels.fromJson(Map<String, dynamic> json)
: id = json['id'],
createdAt = json['createdAt'],
updatedAt = json['updatedAt'],
email = json['email'],
name = json['name'],
avatar = json['avatar'],
product = json['product'],
bio = json['bio'],
role = json['role'];
factory UserModels.fromJson(Map<String, dynamic> json) {
return UserModels(
id: json['id'],
createdAt: DateTime.parse(json['createdAt']),
updatedAt: DateTime.parse(json['updatedAt']),
email: json['email'],
password: json['password'],
name: json['name'],
avatar: json['avatar'],
bio: json['bio'],
role:
Role.values.firstWhere((e) => e.toString() == 'Role.' + json['role']),
products: json['products'] != null
? (json['products'] as List)
.map((i) => ProductModels.fromJson(i))
.toList()
: null,
reviews: json['reviews'] != null
? (json['reviews'] as List).map((i) => Review.fromJson(i)).toList()
: null,
orders: json['orders'] != null
? (json['orders'] as List).map((i) => Order.fromJson(i)).toList()
: null,
);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['createdAt'] = createdAt;
data['updatedAt'] = updatedAt;
data['email'] = email;
data['name'] = name;
data['avatar'] = avatar;
data['product'] = product;
data['bio'] = bio;
data['role'] = role;
return data;
return {
'id': id,
'createdAt': createdAt?.toIso8601String(),
'updatedAt': updatedAt?.toIso8601String(),
'email': email,
'password': password,
'name': name,
'avatar': avatar,
'bio': bio,
'role': role.toString().split('.').last,
'products': products?.map((i) => i.toJson()).toList(),
'reviews': reviews?.map((i) => i.toJson()).toList(),
'orders': orders?.map((i) => i.toJson()).toList(),
};
}
}

enum Role { USER, ADMIN }
22 changes: 22 additions & 0 deletions lib/api/repositories/pharmacy_to_product.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:gohealth/api/interfaces/pharmacy_to_product.dart';
import 'package:gohealth/api/services/http_client.dart';

class PharmacyUserRepository implements IPharmacyStockItem {
late HttpClient stockItemHttpClient;

PharmacyUserRepository() {
stockItemHttpClient = HttpClient();
}

getAvailableQuantity(int pharmacy, int product) async {
var response =
await stockItemHttpClient.client.get('/stock/$pharmacy/$product');

if (response.statusCode == 200) {
var data = response.data;
return data['availableQuantity']['quantity'];
} else {
return 0;
}
}
}
28 changes: 28 additions & 0 deletions lib/api/repositories/product_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:gohealth/api/interfaces/product_interface.dart';
import 'package:gohealth/api/models/pharmacy_to_product_model.dart';
import 'package:gohealth/api/models/product_models.dart';
import 'package:gohealth/api/services/http_client.dart';

Expand Down Expand Up @@ -60,4 +61,31 @@ class ProductRepository implements IProduct {

return model;
}

Future<List<PharmacyStockItem>> getQuantity(
int productId, int pharmacyId) async {
var response = await repositoryHttpClient.client
.get('/product/stock/$productId/$pharmacyId');

List<PharmacyStockItem> model = [];

for (var item in response.data['products']) {
model.add(PharmacyStockItem.fromJson(item));
}

return model;
}

Future<List<ProductModels>> getProducts(String searchText) async {
var response =
await repositoryHttpClient.client.post('/product/name/$searchText');

List<ProductModels> model = [];

for (var item in response.data['products']) {
model.add(ProductModels.fromJson(item));
}

return model;
}
}
Loading

0 comments on commit 8b6e06b

Please sign in to comment.