Skip to content

Commit

Permalink
Merge pull request #27 from gohealthnow/profile
Browse files Browse the repository at this point in the history
Profile - Administrator
  • Loading branch information
Polabiel authored Oct 6, 2024
2 parents 8b6e06b + ee8be4a commit 9a4d6eb
Show file tree
Hide file tree
Showing 14 changed files with 554 additions and 19 deletions.
4 changes: 4 additions & 0 deletions lib/api/models/product_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ProductModels {
String? image;
double? weight;
double? dimensions;
bool? promotion;
double? rating;
String? createdAt;
String? updatedAt;
Expand All @@ -26,6 +27,7 @@ class ProductModels {
this.weight,
this.dimensions,
this.rating,
this.promotion,
this.createdAt,
this.updatedAt,
this.user,
Expand All @@ -44,6 +46,7 @@ class ProductModels {
dimensions: json['dimensions'],
rating: json['rating'],
createdAt: json['createdAt'],
promotion: json['promotion'],
updatedAt: json['updatedAt'],
categories: json['categories'] != null
? (json['categories'] as List).map((i) => i.toString()).toList()
Expand Down Expand Up @@ -71,6 +74,7 @@ class ProductModels {
'image': image,
'weight': weight,
'dimensions': dimensions,
'promotion': promotion,
'rating': rating,
'createdAt': createdAt,
'updatedAt': updatedAt,
Expand Down
21 changes: 21 additions & 0 deletions lib/api/repositories/pharmacy_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,25 @@ class PharmacyRepository implements IPharmacy {
throw Exception('Failed to load products');
}
}

Future<bool> createPharmacy(
{required String name,
required String image,
required String cep,
required String email,
required String phone}) async {
var response =
await repositoryHttpClient.client.post('/pharmacy/create', data: {
'name': name,
if (image.isNotEmpty) 'imageurl': image,
'cep': cep,
'phone': phone,
'email': email,
});

print(response.data);

return response.statusCode == 200 || response.statusCode == 201;
}

}
23 changes: 23 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_model.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 @@ -88,4 +89,26 @@ class ProductRepository implements IProduct {

return model;
}

Future<bool> createProduct(ProductModels product) async {
var response =
await repositoryHttpClient.client.post('/product/create', data: {
"name": product.name,
"price": product.price,
});

return response.statusCode == 200 || response.statusCode == 201;
}

Future<bool> updateStock(
{required ProductModels product,
required PharmacyModels pharmacy,
required int quantity}) async {
var response = await repositoryHttpClient.client
.put('/product/update/stock/${product.id}/${pharmacy.id}', data: {
"quantity": quantity,
});

return response.statusCode == 200 || response.statusCode == 201;
}
}
4 changes: 4 additions & 0 deletions lib/api/repositories/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class UserRepository implements IUser {
'password': password,
});

if(response.statusCode == 401) {
throw Exception('Erro ao logar');
}

logout();

UserModels model = UserModels.fromJson(response.data['user']);
Expand Down
2 changes: 1 addition & 1 deletion lib/api/services/socket_client_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Future<void> initializeService() async {
androidConfiguration: AndroidConfiguration(
autoStart: true,
onStart: onStart,
isForegroundMode: false,
isForegroundMode: true,
autoStartOnBoot: true,
),
);
Expand Down
15 changes: 14 additions & 1 deletion lib/src/app/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,20 @@ class HomepageState extends State<Homepage> {
constraints: BoxConstraints(
maxHeight: screenHeight * 0.6, // 60% da altura da tela
),
child: const BannerComponent(),
child: const BannerComponent(hasPromotion: false),
),
const Padding(
padding: EdgeInsets.all(10.0),
child: Text(
'Produtos em promoção',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: screenHeight * 0.6, // 60% da altura da tela
),
child: const BannerComponent(hasPromotion: true),
),
],
),
Expand Down
143 changes: 143 additions & 0 deletions lib/src/app/home/profile/pharmacy_form_create.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import 'package:flutter/material.dart';
import 'package:gohealth/api/models/pharmacy_model.dart';
import 'package:gohealth/api/models/product_models.dart';
import 'package:gohealth/api/repositories/pharmacy_repository.dart';
import 'package:gohealth/api/repositories/product_repository.dart';

class PharmacyForm extends StatefulWidget {
@override
_PharmacyFormState createState() => _PharmacyFormState();
}

class _PharmacyFormState extends State<PharmacyForm> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _cepController = TextEditingController();
final _emailController = TextEditingController();
final _imageController = TextEditingController();
final _phoneController = TextEditingController();
final _repository = PharmacyRepository();

@override
void dispose() {
_nameController.dispose();
_cepController.dispose();
super.dispose();
}

Future<void> _submitForm() async {
if (_formKey.currentState!.validate()) {
final pharmacy = PharmacyModels(
name: _nameController.text,
email: _emailController.text,
image: _imageController.text,
phone: _phoneController.text,
);

await _repository
.createPharmacy(
name: pharmacy.name!,
cep: _cepController.text,
email: pharmacy.email!,
image: pharmacy.image!,
phone: pharmacy.phone!,
)
.then((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Farmacia criado com sucesso!'),
backgroundColor: Colors.green,
),
);
}).catchError((error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Erro ao criar a farmacia! $error'),
backgroundColor: Colors.red,
duration: const Duration(seconds: 15),
),
);
});
}
}

@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
Text('Criar Farmacia',
style: Theme.of(context).textTheme.headlineMedium),
TextFormField(
controller: _nameController,
decoration: InputDecoration(labelText: 'Nome da Farmacia'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira o nome da farmacia';
}
return null;
},
),
TextFormField(
controller: _cepController,
decoration: InputDecoration(labelText: 'CEP'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira o CEP';
}
if (value.length != 8 && value.length != 9) {
return 'CEP inválido. Deve ter 8 ou 9 caracteres';
}
return null;
},
),
TextFormField(
controller: _emailController,
decoration: InputDecoration(labelText: 'Email'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira o email';
}
if (!RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(value)) {
return 'Email inválido, digite corretamente';
}
return null;
},
),
TextFormField(
controller: _imageController,
decoration: InputDecoration(labelText: 'Imagem'),
validator: (value) {
if (value != null && value.isNotEmpty) {
if (!Uri.parse(value).isAbsolute) {
return 'Por favor, insira um link válido';
}
}
return null;
},
),
TextFormField(
controller: _phoneController,
decoration: InputDecoration(labelText: 'Telefone'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira o telefone';
}
if (value.length < 10 || value.length > 11) {
return 'Telefone inválido';
}
return null;
},
),
ElevatedButton(
onPressed: _submitForm,
child: const Text('Criar Farmacia'),
),
],
),
);
}
}
89 changes: 89 additions & 0 deletions lib/src/app/home/profile/product_form_create.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'package:flutter/material.dart';
import 'package:gohealth/api/models/product_models.dart';
import 'package:gohealth/api/repositories/product_repository.dart';

class ProductForm extends StatefulWidget {
@override
_ProductFormState createState() => _ProductFormState();
}

class _ProductFormState extends State<ProductForm> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _priceController = TextEditingController();
final _repository = ProductRepository();

@override
void dispose() {
_nameController.dispose();
_priceController.dispose();
super.dispose();
}

Future<void> _submitForm() async {
if (_formKey.currentState!.validate()) {
final product = ProductModels(
name: _nameController.text,
price: double.parse(_priceController.text),
);

await _repository.createProduct(product).then((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Produto criado com sucesso!'),
backgroundColor: Colors.green,
),
);
}).catchError((error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Erro ao criar o produto!'),
backgroundColor: Colors.red,
),
);
});
}
}

@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
Text('Criar Produto',
style: Theme.of(context).textTheme.headlineMedium),
TextFormField(
controller: _nameController,
decoration: InputDecoration(labelText: 'Nome do Produto'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira o nome do produto';
}
return null;
},
),
TextFormField(
controller: _priceController,
decoration: InputDecoration(labelText: 'Preço do Produto'),
keyboardType: TextInputType.number,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira o preço do produto';
}
value = value.replaceAll(',', '.');
if (double.tryParse(value) == null) {
return 'Por favor, insira um preço válido';
}
return null;
},
),
ElevatedButton(
onPressed: _submitForm,
child: Text('Criar Produto'),
),
],
),
);
}
}
Loading

0 comments on commit 9a4d6eb

Please sign in to comment.