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 887bf4f commit 519afa1
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 12 deletions.
17 changes: 17 additions & 0 deletions lib/Model/model/equipment/image_url_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:json_annotation/json_annotation.dart';

part 'image_url_model.g.dart';

@JsonSerializable()
class ImageUrlModel {
String imageKey;
String presignedUrl;

ImageUrlModel({
required this.imageKey,
required this.presignedUrl,
});

factory ImageUrlModel.fromJson(Map<String, dynamic> json) =>
_$ImageUrlModelFromJson(json);
}
19 changes: 19 additions & 0 deletions lib/Model/model/equipment/image_url_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions lib/Model/model/equipment/image_url_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ImageUrlRequest {
String ext;
String dir;

ImageUrlRequest({
required this.ext,
required this.dir,
});

factory ImageUrlRequest.fromJson(Map<String, dynamic> json) =>
ImageUrlRequest(
ext: json["ext"],
dir: json["dir"],
);

Map<String, dynamic> toJson() => {
"ext": ext,
"dir": dir,
};
}
56 changes: 50 additions & 6 deletions lib/Model/network/api_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:dio/dio.dart';

import 'constants.dart';

enum RequestType { get, post, patch, delete }
enum RequestType { get, post, patch, delete, put }

class APIManager {
/// Singleton Pattern
Expand All @@ -20,7 +20,6 @@ class APIManager {
bool isAdmin = false;
String fcmToken = "";

/// Request methods
dynamic request(
RequestType requestType,
String path,
Expand All @@ -29,8 +28,8 @@ class APIManager {
Map<String, dynamic>? data,
) async {
setToken();
print('${await storage.read(key: accessTokenKey)}');
print('${defaultOptions.headers}');
// print('${await storage.read(key: accessTokenKey)}');
// print('${defaultOptions.headers}');

if (options != null && options.headers != null) {
defaultOptions.headers!.addAll(options.headers!);
Expand All @@ -53,14 +52,59 @@ class APIManager {
options: defaultOptions,
queryParameters: queryParameters,
data: data);
case RequestType.put:
response = await dio.put(baseUrl + path,
options: defaultOptions,
queryParameters: queryParameters,
data: data);

case RequestType.delete:
response = await dio.delete(baseUrl + path,
options: defaultOptions, queryParameters: queryParameters);
}

print('api + $response');
print('api + ${response.data}');
// print('api + $response');
// print('api + ${response.data}');

return response.data;
}

/// Request methods
dynamic imageRequest(
RequestType requestType,
String path,
Options? options,
Map<String, dynamic>? queryParameters,
dynamic data,
) async {
dynamic response;
switch (requestType) {
case RequestType.get:
response = await dio.get(path,
options: options, queryParameters: queryParameters);

case RequestType.post:
response = await dio.post(path,
options: options, queryParameters: queryParameters, data: data);

case RequestType.patch:
response = await dio.patch(path,
options: options, queryParameters: queryParameters, data: data);

case RequestType.put:
Options op = Options();
op.contentType = 'multipart/form-data';
op.maxRedirects!.isFinite;
response = await dio.put(path,
options: op, queryParameters: queryParameters, data: data);

case RequestType.delete:
response = await dio.delete(path,
options: options, queryParameters: queryParameters);
}

// print('api + $response');
// print('api + ${response.data}');

return response.data;
}
Expand Down
35 changes: 33 additions & 2 deletions lib/Presenter/equipment/equipment_service.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import 'dart:io';

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

class EquipmentService {
final equipmentURL = '/equipments?cond=';
final equipmentCategoryURL = "/equipments/categories";
final bukitURL =
"https://gpkzpnv8lh.execute-api.ap-northeast-2.amazonaws.com/dev/presignedurl-lambda";

String? url;
String? key;

static final EquipmentService _equipmentService = EquipmentService._();
EquipmentService._();
Expand All @@ -24,4 +31,28 @@ class EquipmentService {
.request(RequestType.get, equipmentCategoryURL, null, null, null);
return response;
}

Future<dynamic> getImageUrl() async {
final body = ImageUrlRequest(ext: "jpeg", dir: "equipment").toJson();
final response =
APIManager().imageRequest(RequestType.post, bukitURL, null, null, body);

return response;
}

Future<dynamic> putIamge(String url, File image) async {
Dio dio = Dio();
var len = await image.length();
var response = await dio.put(url,
data: image.openRead(),
options: Options(contentType: "multiple/form-data", headers: {
Headers.contentLengthHeader: len,
} // set content-length
));
if (response != null) {
return true;
} else {
return false;
}
}
}
78 changes: 74 additions & 4 deletions lib/View/equipment/screen/equipment_add_screen.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:frontend/Model/model/equipment/equipment_category_model.dart';
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';
Expand All @@ -17,7 +19,7 @@ class EquipmentAddScreen extends StatefulWidget {

class _EquipmentAddScreen extends State<EquipmentAddScreen> {
var categories = [""];
var userImage;
File? userImage;
String? _selectedCategory;
FocusNode nameFocusNode = FocusNode();
FocusNode quantityFocusNode = FocusNode();
Expand Down Expand Up @@ -52,7 +54,7 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {
body: futureBody(),
bottomNavigationBar: PurpleBottomButton(
title: '추가',
onPressed: equipmentAdd,
onPressed: checkImage,
),
);
}
Expand Down Expand Up @@ -382,7 +384,7 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {
icon: userImage == null
? SvgPicture.asset('asset/image/image_plus.svg')
: Image.file(
userImage,
userImage!,
fit: BoxFit.fill,
),
onPressed: imageAdd,
Expand All @@ -394,6 +396,46 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {
);
}

void showAlert(String content) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
title: const Column(
children: <Widget>[
Text("비품 추가 오류"),
],
),
//
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(content),
],
),
actions: <Widget>[
TextButton(
child: const Text(
"확인",
style: TextStyle(
color: Colors.red,
fontSize: 16,
decoration: TextDecoration.underline,
),
),
onPressed: () {
Navigator.pop(context);
},
),
],
);
});
}

void imageAdd() async {
var picker = ImagePicker();
var image = await picker.pickImage(source: ImageSource.gallery);
Expand All @@ -404,5 +446,33 @@ class _EquipmentAddScreen extends State<EquipmentAddScreen> {
}
}

void equipmentAdd() {}
void checkEssential() {
if (nameController.text == "" ||
quantityController.text == "" ||
_selectedCategory == null) {
showAlert("필수 요소를 입력해주세요.");
} else {
checkImage();
}
}

void checkImage() async {
if (userImage != null) {
dynamic response = await EquipmentService().getImageUrl();
final data = ImageUrlModel.fromJson(response);

Future<dynamic> result =
EquipmentService().putIamge(data.presignedUrl, userImage!);
result.then((value) => {
if (value == true)
{addEquipment(data.imageKey)}
else
{showAlert("알 수 없는 오류가 발생했습니다. 다시 시도해주세요.")}
});
} else {
addEquipment(null);
}
}

void addEquipment(String? imageKey) {}
}

0 comments on commit 519afa1

Please sign in to comment.