Skip to content

Commit

Permalink
Merge pull request IT-Cotato#65 from dayoung20/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
siiion authored Jan 31, 2025
2 parents 53d9c6c + 93f26cc commit 92f99b9
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 271 deletions.
3 changes: 3 additions & 0 deletions economic_fe/lib/data/models/article_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ArticleModel with ChangeNotifier {
String? url;
String? category;
String? createdDate;
bool? isScraped;

ArticleModel({
this.id,
Expand All @@ -24,6 +25,7 @@ class ArticleModel with ChangeNotifier {
this.url,
this.category,
this.createdDate,
this.isScraped,
});

factory ArticleModel.fromJson(Map<String, dynamic> json) {
Expand All @@ -36,6 +38,7 @@ class ArticleModel with ChangeNotifier {
url: json['url'],
category: json['category'],
createdDate: json['createdDate'],
isScraped: json['isScraped'],
);
}
}
3 changes: 3 additions & 0 deletions economic_fe/lib/data/models/dictionary_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ class DictionaryModel with ChangeNotifier {
int? termId;
String? termName;
String? termDescription;
bool? isScraped;

DictionaryModel({
this.termId,
this.termName,
this.termDescription,
this.isScraped,
});

factory DictionaryModel.fromJson(Map<String, dynamic> json) {
return DictionaryModel(
termId: json['termId'],
termName: json['termName'],
termDescription: json['termDescription'],
isScraped: json['isScraped'],
);
}
}
133 changes: 130 additions & 3 deletions economic_fe/lib/data/services/remote_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class RemoteDataSource {
/// API POST
///
/// 데이터 생성시 사용
/// authToken을 포함하도록 수정
static Future<dynamic> postApi(
/// jsonData 포함X
static Future<dynamic> postApiWithJson(
String endPoint,
Map<String, dynamic> jsonData,
) async {
Expand Down Expand Up @@ -46,6 +46,42 @@ class RemoteDataSource {
}
}

/// API POST
///
/// 데이터 생성시 사용
/// jsonData 포함X
static Future<dynamic> _postApi(
String endPoint,
// Map<String, dynamic> jsonData,
) async {
String apiUrl = '$baseUrl/$endPoint';
// String authToken = dotenv.env['AUTHORIZATION_KEY']!; // 환경 변수에서 가져오기
Map<String, String> headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
};

try {
final response = await http.post(
Uri.parse(apiUrl),
headers: headers,
// body: jsonEncode(jsonData),
);

if (response.statusCode == 200) {
debugPrint('POST 요청 성공');
return response.statusCode;
} else {
debugPrint('POST 요청 실패: (${response.statusCode}) ${response.body}');
}

return response.statusCode;
} catch (e) {
debugPrint('POST 요청 중 예외 발생: $e');
return null;
}
}

/// API PATCH
///
/// 데이터 일부 수정시 사용
Expand Down Expand Up @@ -137,7 +173,14 @@ class RemoteDataSource {
debugPrint('DELETE 요청: $endPoint');

try {
final response = await http.delete(Uri.parse(apiUrl));
final headers = {
'Authorization': 'Bearer $accessToken',
'accept': '*/*',
};
final response = await http.delete(
Uri.parse(apiUrl),
headers: headers,
);

if (response.statusCode == 200) {
debugPrint('DELETE 요청 성공');
Expand Down Expand Up @@ -193,6 +236,48 @@ class RemoteDataSource {
return response;
}

/// api/news/{id}/scrap
/// 뉴스 스크랩
static Future<dynamic> postNewsScrap(int id) async {
String endPoint = "api/news/$id/scrap";

try {
final response = await _postApi(endPoint);

if (response != null) {
debugPrint("스크랩 post 성공");
return true;
} else {
debugPrint("스크랩 실패");
return false;
}
} catch (e) {
debugPrint("scrap Error : $e");
return false;
}
}

/// 뉴스 스크랩 취소
/// api/news/{id}/scrap
static Future<dynamic> deleteNewsScrap(int id) async {
String endPoint = "api/news/$id/scrap";

try {
final response = await _deleteApi(endPoint);

if (response != null) {
debugPrint("스크랩 delete 성공");
return true;
} else {
debugPrint("스크랩 delete 실패");
return false;
}
} catch (e) {
debugPrint("scrap delete Error : $e");
return false;
}
}

/// 자음 별 용어 조회
/// api/v1/terms/search/consonant
static Future<dynamic> getDictionary(int page, String consonant) async {
Expand Down Expand Up @@ -408,4 +493,46 @@ class RemoteDataSource {
return null;
}
}

/// api/v1/terms/{id}/scrap
/// 용어 스크랩
static Future<dynamic> postTermsScrap(int id) async {
String endPoint = "api/v1/terms/$id/scrap";

try {
final response = await _postApi(endPoint);

if (response != null) {
debugPrint("스크랩 post 성공");
return true;
} else {
debugPrint("스크랩 실패");
return false;
}
} catch (e) {
debugPrint("scrap Error : $e");
return false;
}
}

/// api/v1/terms/{id}/scrap
/// 용어 스크랩 취소
static Future<dynamic> deleteScrap(int id) async {
String endPoint = "api/v1/terms/$id/scrap";

try {
final response = await _deleteApi(endPoint);

if (response != null) {
debugPrint("스크랩 delete 성공");
return true;
} else {
debugPrint("스크랩 delete 실패");
return false;
}
} catch (e) {
debugPrint("scrap delete Error : $e");
return false;
}
}
}
2 changes: 1 addition & 1 deletion economic_fe/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RippleApp extends StatelessWidget {
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Ripple',
initialRoute: '/mypage',
initialRoute: '/article',
getPages: UserRouter.getPages(), // 라우트 설정
);
}
Expand Down
Loading

0 comments on commit 92f99b9

Please sign in to comment.