Skip to content

Commit

Permalink
[PDA-44] feat: 비품 추가 api 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
chayoosang committed Dec 2, 2023
1 parent 519afa1 commit f5b60b3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
36 changes: 36 additions & 0 deletions lib/Model/model/equipment/equipment_add_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class EquipmentAddRequest {
String category;
String? description;
String? imgKey;
String? location;
String name;
String quantity;

EquipmentAddRequest({
required this.category,
required this.description,
required this.imgKey,
required this.location,
required this.name,
required this.quantity,
});

factory EquipmentAddRequest.fromJson(Map<String, dynamic> json) =>
EquipmentAddRequest(
category: json["category"],
description: json["description"],
imgKey: json["imgKey"],
location: json["location"],
name: json["name"],
quantity: json["quantity"],
);

Map<String, dynamic> toJson() => {
"category": category,
"description": description,
"imgKey": imgKey,
"location": location,
"name": name,
"quantity": quantity,
};
}
37 changes: 33 additions & 4 deletions lib/Presenter/equipment/equipment_service.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:frontend/Model/model/equipment/image_url_model.dart';
import 'package:frontend/Model/model/equipment/equipment_add_request.dart';
import 'package:frontend/Model/model/equipment/image_url_request.dart';
import 'package:frontend/Model/model/general_model.dart';
import 'package:frontend/Model/network/api_manager.dart';

class EquipmentService {
final equipmentURL = '/equipments?cond=';
final equipmentGetURL = '/equipments?cond=';
final equipmentPostURL = '/equipments';
final equipmentCategoryURL = "/equipments/categories";
final bukitURL =
"https://gpkzpnv8lh.execute-api.ap-northeast-2.amazonaws.com/dev/presignedurl-lambda";
Expand All @@ -21,8 +23,8 @@ class EquipmentService {
}

Future<dynamic> getEquipment() async {
final response =
APIManager().request(RequestType.get, equipmentURL, null, null, null);
final response = APIManager()
.request(RequestType.get, equipmentGetURL, null, null, null);
return response;
}

Expand Down Expand Up @@ -55,4 +57,31 @@ class EquipmentService {
return false;
}
}

Future<dynamic> addEquipment(String category, String? description,
String? imgKey, String? location, String name, String quantity) async {
try {
final body = EquipmentAddRequest(
category: category,
description: description,
imgKey: imgKey,
location: location,
name: name,
quantity: quantity)
.toJson();

var response = APIManager()
.request(RequestType.post, equipmentPostURL, null, null, body);
if (response != null) {
return true;
}
} on DioError catch (e) {
final response = e.response;
if (response != null) {
final error =
GeneralModel.fromJson(response.data as Map<String, dynamic>);
return error.message;
}
}
}
}
28 changes: 26 additions & 2 deletions lib/View/equipment/screen/equipment_add_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:frontend/Model/model/equipment/image_url_model.dart';
import 'package:frontend/Presenter/equipment/equipment_service.dart';
import 'package:frontend/View/common/component/purple_bottom_button.dart';
import 'package:frontend/View/common/component/sub_app_bar.dart';
import 'package:frontend/View/equipment/screen/equipment_screen.dart';
import 'package:image_picker/image_picker.dart';

class EquipmentAddScreen extends StatefulWidget {
Expand All @@ -21,6 +22,7 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {
var categories = [""];
File? userImage;
String? _selectedCategory;

FocusNode nameFocusNode = FocusNode();
FocusNode quantityFocusNode = FocusNode();
FocusNode loacationFocusNode = FocusNode();
Expand Down Expand Up @@ -54,7 +56,7 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {
body: futureBody(),
bottomNavigationBar: PurpleBottomButton(
title: '추가',
onPressed: checkImage,
onPressed: checkEssential,
),
);
}
Expand Down Expand Up @@ -95,6 +97,7 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {

Widget renderBody() {
return Container(
color: Colors.white,
margin: const EdgeInsets.only(top: 14, right: 20, left: 20),
child: Column(
children: [
Expand Down Expand Up @@ -474,5 +477,26 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {
}
}

void addEquipment(String? imageKey) {}
void addEquipment(String? imageKey) {
var name = nameController.text;
var quantity = quantityController.text;
var description =
descriptionController.text == "" ? null : descriptionController.text;
var location =
loacationController.text == "" ? null : loacationController.text;
var imgKey = imageKey != null ? "equipment/$imageKey" : null;

Future<dynamic> result = EquipmentService().addEquipment(
_selectedCategory!, description, imgKey, location, name, quantity);
result.then((value) => {
if (value == true) {moveToPop()} else {showAlert(value)}
});
}

void moveToPop() {
EquipmentScreenState? parent =
context.findAncestorStateOfType<EquipmentScreenState>();
Navigator.of(context).pop();
parent!.reloadData();
}
}
22 changes: 17 additions & 5 deletions lib/View/equipment/screen/equipment_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class EquipmentScreen extends StatefulWidget {
const EquipmentScreen({super.key});

@override
State<StatefulWidget> createState() => _EquipmentScreen();
State<StatefulWidget> createState() => EquipmentScreenState();
}

class _EquipmentScreen extends State<EquipmentScreen> {
class EquipmentScreenState extends State<EquipmentScreen>
with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -32,9 +33,9 @@ class _EquipmentScreen extends State<EquipmentScreen> {

Widget futureBody() {
List<EquipmentModel> notificationList;
return FutureBuilder(
future: EquipmentService().getEquipment(),
builder: (context, snapshot) {
return FutureBuilder<dynamic>(
future: fetchData(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasError) {
return noDataBody();
}
Expand Down Expand Up @@ -105,4 +106,15 @@ class _EquipmentScreen extends State<EquipmentScreen> {
Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => const EquipmentAddScreen()));
}

Future<dynamic> fetchData() async {
return EquipmentService().getEquipment();
}

void reloadData() {
print(123);
setState(() {
fetchData();
});
}
}

0 comments on commit f5b60b3

Please sign in to comment.