Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

持ち物チェックAPIクライアント周り実装 #118

Merged
merged 4 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/core/http/api_client/abstract_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ abstract class AbstractApiClient {
ProgressCallback onSendProgress,
ProgressCallback onReceiveProgress,
});

Future<ApiResponse> put(
String path, {
Map<String, dynamic> data,
Map<String, dynamic> queryParameters,
Map<String, dynamic> header,
Options options,
CancelToken cancelToken,
ProgressCallback onSendProgress,
ProgressCallback onReceiveProgress,
});
}
75 changes: 47 additions & 28 deletions lib/core/http/api_client/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,7 @@ class ApiClient implements AbstractApiClient {
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
);
final responseData = response.data;
final statusCode = response.statusCode;
if (responseData == null || statusCode == null) {
throw const ApiException(statusCode: 500);
}
if (statusCode >= 400 && statusCode < 600) {
final errorResponse = ErrorResponse.fromJson(responseData);
throw ApiException(
statusCode: statusCode,
errorCode: errorResponse.errorCode,
description: errorResponse.description,
);
}
return ApiResponse.fromJson(responseData);
return _toApiResponse(response);
} on DioError catch (e) {
final exception = _handleDioError(e);
throw exception;
Expand Down Expand Up @@ -99,26 +86,58 @@ class ApiClient implements AbstractApiClient {
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
final responseData = response.data;
final statusCode = response.statusCode;
if (responseData == null || statusCode == null) {
throw const ApiException(statusCode: 500);
}
if (statusCode >= 400 && statusCode < 600) {
final errorResponse = ErrorResponse.fromJson(responseData);
throw ApiException(
statusCode: statusCode,
errorCode: errorResponse.errorCode,
description: errorResponse.description,
);
}
return ApiResponse.fromJson(responseData);
return _toApiResponse(response);
} on DioError catch (e) {
final exception = _handleDioError(e);
throw exception;
}
}

@override
Future<ApiResponse> put(
String path, {
Map<String, dynamic>? data,
Map<String, dynamic>? queryParameters,
Map<String, dynamic>? header,
Options? options,
CancelToken? cancelToken,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
try {
final response = await _dio.put<Json>(
path,
data: data,
queryParameters: queryParameters,
options: options ?? Options(headers: header),
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
return _toApiResponse(response);
} on DioError catch (e) {
final exception = _handleDioError(e);
throw exception;
}
}

ApiResponse _toApiResponse(Response<Map<String, dynamic>> res) {
final responseData = res.data;
final statusCode = res.statusCode;
if (responseData == null || statusCode == null) {
throw const ApiException(statusCode: 500);
}
if (statusCode >= 400 && statusCode < 600) {
final errorResponse = ErrorResponse.fromJson(responseData);
throw ApiException(
statusCode: statusCode,
errorCode: errorResponse.errorCode,
description: errorResponse.description,
);
}
return ApiResponse.fromJson(responseData);
}

/// DioError を受けて [ApiException] もしくはそのサブクラスを返す。
/// デバッグモードの場合は [DioError] の原因が [SocketException] によるものかを判別して、
/// そうである場合は、 [SocketException] を返す。
Expand Down

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

14 changes: 14 additions & 0 deletions lib/features/trips/data/models/change_check_status_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'change_check_status_response.freezed.dart';
part 'change_check_status_response.g.dart';

@freezed
class ChangeCheckStatusResponse with _$ChangeCheckStatusResponse {
const factory ChangeCheckStatusResponse({
required bool isChecked,
}) = _ChangeCheckStatusResponse;

factory ChangeCheckStatusResponse.fromJson(Map<String, dynamic> json) =>
_$ChangeCheckStatusResponseFromJson(json);
}

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

29 changes: 29 additions & 0 deletions lib/features/trips/data/models/change_check_status_response.g.dart

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

18 changes: 18 additions & 0 deletions lib/features/trips/data/repositories/trip_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:trip_app_nativeapp/core/extensions/datetime.dart';
import 'package:trip_app_nativeapp/core/http/api_client/abstract_api_client.dart';
import 'package:trip_app_nativeapp/core/http/api_client/api_client.dart';
import 'package:trip_app_nativeapp/features/trips/data/models/change_check_status_response.dart';
import 'package:trip_app_nativeapp/features/trips/data/models/create_trip_response.dart';
import 'package:trip_app_nativeapp/features/trips/data/models/fetch_trip_belongings_response.dart';
import 'package:trip_app_nativeapp/features/trips/data/models/fetch_trips_response.dart';
Expand Down Expand Up @@ -38,6 +39,7 @@ class TripRepository implements TripRepositoryInterface {
final AbstractApiClient privateV1Client;
static const _basePath = '/trips';
static const _invitationBasePath = '/trip_invitations';
static const _belongingBasePath = '/trip_belongings';

@override
Future<List<ExistingTrip>> fetchTripsByUserId(int userId) async {
Expand Down Expand Up @@ -189,4 +191,20 @@ class TripRepository implements TripRepositoryInterface {
) as AddedTripBelonging;
}).toList();
}

@override
Future<bool> changeBelongingCheckStatus({
required int belongingId,
required bool isChecked,
}) async {
final res = await privateV1Client.put(
'$_belongingBasePath/$belongingId/check_status',
data: {
'is_checked': isChecked,
},
);

final changeStatusRes = ChangeCheckStatusResponse.fromJson(res.data);
return changeStatusRes.isChecked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ abstract class TripRepositoryInterface {
Future<List<AddedTripBelonging>> fetchTripBelongings(
int tripId,
);

/// 持ち物チェックステータスを変更
Future<bool> changeBelongingCheckStatus({
required int belongingId,
required bool isChecked,
});
}
Loading
Loading