Skip to content

Commit

Permalink
Merge pull request #134 from 4Ailen/feat/#123_marketAPI
Browse files Browse the repository at this point in the history
Feat/#123 market api
  • Loading branch information
urlotus authored Sep 6, 2023
2 parents efbca01 + a67ed2a commit cd35d88
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 198 deletions.
10 changes: 5 additions & 5 deletions assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@

"general-board-post" : "General Board New Post",
"market-board-post": "Market Board New Post",
"Brand_New": "Brand New",
"Almost_New": "Almost New",
"Slight_Defect": "Slight Defect",
"Used": "Used",
"sale": "For sale"
"Brand_New": "BRAND_NEW",
"Almost_New": "ALMOST_NEW",
"Slight_Defect": "SLIGHT_DEFECT",
"Used": "USED",
"sale": "SELL"

}
130 changes: 63 additions & 67 deletions lib/apis/apis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,6 @@ class APIs {
}
}


/*장터 게시판 게시글 상세 조회*/
static Future<MarketBoard> getMarketArticle(int articleId) async {
final _url = 'http://3.34.2.246:8080/api/v2/market-articles/${articleId}';
Expand Down Expand Up @@ -1807,48 +1806,44 @@ class APIs {


/* 특정 판매글 찜 등록*/
static Future<String> addMarketArticleBookmark(int articleId) async {
try {
var jwtToken = await storage.read(key: 'token');
final accessToken = json.decode(jwtToken!)['data']['accessToken'];
static Future<int> marketbookmark(int articleId) async {
var url = 'http://3.34.2.246:8080/api/v2/community-articles/${articleId}/bookmarks';

final url = Uri.parse(
'http://3.34.2.246:8080/api/v2/market-articles/$articleId/bookmarks');
//토큰 읽어오기
var jwtToken = await storage.read(key: 'token');

final response = await http.post(
url,
headers: {
'Authorization': 'Bearer $accessToken',
'Content-Type': 'application/json',
},
);
//accessToken만 보내기
jwtToken = json.decode(jwtToken!)['data']['accessToken'];

if (response.statusCode == 200) {
final responseBody = json.decode(utf8.decode(response.bodyBytes));
final message = responseBody['message'];
return message;
} else {
final responseBody = json.decode(utf8.decode(response.bodyBytes));
final errorCode = responseBody['code'];
var response = await http.post(Uri.parse(url),
headers: {
'Authorization': 'Bearer $jwtToken',
'Content-Type': 'application/json'},
);

//success
if (response.statusCode == 200) {
print(json.decode(utf8.decode(response.bodyBytes)));
return json.decode(utf8.decode(response.bodyBytes))['data']['likeCount'];
//fail
} else {
print(json.decode(utf8.decode(response.bodyBytes)));
if(json.decode(utf8.decode(response.bodyBytes))['code'] == 'AT-C-002'){
print('액세스 토큰 만료');
throw 'AT-C-002';
} else if(json.decode(utf8.decode(response.bodyBytes))['code'] == 'AT-C-007'){
print('로그아웃된 토큰');
throw 'AT-C-007';
}else{

if (errorCode == 'AT-C-002') {
throw '액세스 토큰 만료';
} else if (errorCode == 'AT-C-007') {
throw '로그아웃된 토큰';
} else {
throw Exception('북마크 등록 오류');
}
}
} catch (error) {
print('Error adding market article bookmark: $error');
throw Exception('북마크 등록 오류');
return -1;
}
}


/*상품 판매글 댓글 전체 조회*/
static Future<List<MarketComment>> getMarketArticleComments(
int marketArticleId) async {
static Future<List<MarketComment>> getMarketArticleComments(int marketArticleId) async {
try {
var jwtToken = await storage.read(key: 'token');
final accessToken = json.decode(jwtToken!)['data']['accessToken'];
Expand All @@ -1871,6 +1866,8 @@ class APIs {
return body.map((dynamic item) => MarketComment.fromJson(item))
.toList();
} else {
print(json.decode(utf8.decode(response.bodyBytes)));

final responseBody = json.decode(utf8.decode(response.bodyBytes));
final errorCode = responseBody['code'];

Expand All @@ -1890,42 +1887,41 @@ class APIs {


/*상품 판매글 부모 댓글 등록*/
static Future<bool> createMarketArticleComment(int articleCommentId,
String content) async {
try {
var jwtToken = await storage.read(key: 'token');
final accessToken = json.decode(jwtToken!)['data']['accessToken'];
static Future<bool> createMarketArticleComment(String content, int articleId) async {
var url = 'http://3.34.2.246:8080/api/v2/market-articles/$articleId/market-article-comments';

final url = Uri.parse(
'http://3.34.2.246:8080/api/v2/market-articles/$articleCommentId/market-article-comments');
//토큰 읽어오기
var jwtToken = await storage.read(key: 'token');

final response = await http.post(
url,
//accessToken만 보내기
jwtToken = json.decode(jwtToken!)['data']['accessToken'];

var response = await http.post(Uri.parse(url),
headers: {
'Authorization': 'Bearer $accessToken',
'Content-Type': 'application/json',
},
body: json.encode({'content': content}),
);
'Authorization': 'Bearer $jwtToken',
'Content-Type': 'application/json'},
body: jsonEncode({
"content": content,
}));

if (response.statusCode == 201) {
print(json.decode(utf8.decode(response.bodyBytes)));
return true;
//success
if (response.statusCode == 200) {
print(json.decode(utf8.decode(response.bodyBytes)));
return true;
//fail
} else {
print(json.decode(utf8.decode(response.bodyBytes)));
if (json.decode(utf8.decode(response.bodyBytes))['code'] == 'AT-C-002') {
print('액세스 토큰 만료');
throw 'AT-C-002';
} else
if (json.decode(utf8.decode(response.bodyBytes))['code'] == 'AT-C-007') {
print('로그아웃된 토큰');
throw 'AT-C-007';
} else {
final responseBody = json.decode(utf8.decode(response.bodyBytes));
final errorCode = responseBody['code'];

if (errorCode == 'AT-C-002') {
throw '액세스 토큰 만료';
} else if (errorCode == 'AT-C-007') {
throw '로그아웃된 토큰';
} else {
throw Exception('댓글 생성 오류');
}
}
} catch (error) {
print('Error creating market article comment: $error');
throw Exception('댓글 생성 오류');
return false;
}
}

Expand All @@ -1950,7 +1946,7 @@ class APIs {
if (response.statusCode == 200) {
final responseBody = json.decode(utf8.decode(response.bodyBytes));
final message = responseBody['message'];
return message;
return true;
} else {
final responseBody = json.decode(utf8.decode(response.bodyBytes));
final errorCode = responseBody['code'];
Expand All @@ -1971,14 +1967,13 @@ class APIs {


/*특정 상품 판매글 댓글에 대댓글 등록*/
static Future<bool> addMarketArticleCommentReply(int articleCommentId,
int ArticleCommentId, String content) async {
static Future<bool> addMarketArticleCommentReply(String content, int commentId, int articleId) async {
try {
var jwtToken = await storage.read(key: 'token');
final accessToken = json.decode(jwtToken!)['data']['accessToken'];

final url = Uri.parse(
'http://3.34.2.246:8080/api/v2/market-articles/$articleCommentId/market-article-comments/$ArticleCommentId');
'http://3.34.2.246:8080/api/v2/market-articles/$articleId/market-article-comments/$commentId');

final response = await http.post(
url,
Expand All @@ -1990,9 +1985,10 @@ class APIs {
);

if (response.statusCode == 200) {
print(json.decode(utf8.decode(response.bodyBytes)));
final responseBody = json.decode(utf8.decode(response.bodyBytes));
final message = responseBody['message'];
return message;
return true;
} else {
final responseBody = json.decode(utf8.decode(response.bodyBytes));
final errorCode = responseBody['code'];
Expand Down
8 changes: 4 additions & 4 deletions lib/models/market_articles.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class MarketBoard {
int? articleId;
String? title;
String? status;
String? marketArticleStatus;
int? price;
String? productStatus;
String? content;
Expand All @@ -14,7 +14,7 @@ class MarketBoard {
MarketBoard({
this.articleId,
this.title,
this.status,
this.marketArticleStatus,
this.price,
this.productStatus,
this.content,
Expand All @@ -28,7 +28,7 @@ class MarketBoard {
MarketBoard.fromJson(Map<String, dynamic> json) {
articleId = json['articleId'];
title = json['title'];
status = json['status'];
marketArticleStatus = json['marketArticleStatus'];
price = json['price'];
productStatus = json['productStatus'];
content = json['content'];
Expand All @@ -43,7 +43,7 @@ class MarketBoard {
final Map<String, dynamic> data = <String, dynamic>{};
data['articleId'] = articleId;
data['title'] = title;
data['status'] = status;
data['marketArticleStatus'] = marketArticleStatus;
data['price'] = price;
data['productStatus'] = productStatus;
data['content'] = content;
Expand Down
25 changes: 15 additions & 10 deletions lib/providers/market_comment_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@ class MarketCommentProvider with ChangeNotifier {
bool loading = false;
MarketComment? marketcomment;

getMarketComments(int articleCommentId) async {
getMarketComments(int articleId) async {
loading = true;
try {
commentListData = await APIs.getMarketArticleComments(articleCommentId);
commentListData = await APIs.getMarketArticleComments(articleId);
} catch (e) {
if (e == "AT-C-002") {
await APIs.getAccessToken();
commentListData = await APIs.getMarketArticleComments(articleCommentId);
commentListData = await APIs.getMarketArticleComments(articleId);
} else {
// 오류 처리 로직
}
}
loading = false;
notifyListeners();

}


addMarketComment(int articleId, String content) async {
addMarketComment(String content, int articleId) async {
try {
await APIs.createMarketArticleComment(articleId, content);
await APIs.createMarketArticleComment(content,articleId);
} catch (e) {
if (e == "AT-C-002") {
await APIs.getAccessToken();
await APIs.createMarketArticleComment(articleId, content);
await APIs.createMarketArticleComment(content,articleId);
} else {
return false;
}
Expand All @@ -46,24 +47,26 @@ class MarketCommentProvider with ChangeNotifier {


notifyListeners();
getMarketComments(articleId);
return true;
}

addNestedMarketComment(int commentId, int ArticleCommentId, String content) async {
addNestedMarketComment(String content, int commentId, int articleId) async {
try {
await APIs.addMarketArticleCommentReply(commentId, ArticleCommentId, content);
await APIs.addMarketArticleCommentReply(content, commentId,articleId );
} catch (e) {
if (e == "AT-C-002") {
await APIs.getAccessToken();
await APIs.addMarketArticleCommentReply(commentId, ArticleCommentId, content);
await APIs.addMarketArticleCommentReply(content, commentId,articleId);
} else {
return false;
}
}
//TODO fcm 전송


notifyListeners();
getMarketComments(articleId);

return true;
}
deleteMarketComment(int articleId) async {
Expand All @@ -80,6 +83,8 @@ class MarketCommentProvider with ChangeNotifier {
}
loading = false;
notifyListeners();
getMarketComments(articleId);

return value;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/views/components/board_drawer_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:aliens/models/chatRoom_model.dart';
import 'package:aliens/models/memberDetails_model.dart';
import 'package:aliens/models/screenArgument.dart';
import 'package:aliens/repository/sql_message_database.dart';
import 'package:aliens/views/pages/board/article_writing_page.dart';
Expand Down Expand Up @@ -33,6 +34,7 @@ class BoardDrawerWidget extends StatefulWidget {

final ScreenArguments screenArguments;
final MarketBoard? marketBoard;
//final MemberDetails memberDetails;

final bool isTotalBoard;
final VoidCallback onpressd;
Expand Down Expand Up @@ -449,7 +451,7 @@ class _BoardDrawerWidgetState extends State<BoardDrawerWidget> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MarketBoardPage(screenArguments: widget.screenArguments, marketBoard: widget.marketBoard,)),
builder: (context) => MarketBoardPage(screenArguments: widget.screenArguments, marketBoard: widget.marketBoard)),

);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/views/components/marketcomment_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MarketCommentDialog extends StatelessWidget{
InkWell(
onTap: (){
//TODO 로딩 만들기
// marketcommentProvider.deleteComment(marketcomment.articleCommentId!);
marketcommentProvider.deleteMarketComment(marketcomment.articleCommentId!);
},
child: Container(
padding: EdgeInsets.all(13).r,
Expand Down Expand Up @@ -155,7 +155,7 @@ class MarketCommentDialog extends StatelessWidget{
InkWell(
onTap: () {
//TODO 로딩 만들기
// marketcommentProvider.deleteComment(marketcomment.articleCommentId!);
marketcommentProvider.deleteMarketComment(marketcomment.articleCommentId!);
},
child: Container(
height: 80,
Expand Down
Loading

0 comments on commit cd35d88

Please sign in to comment.