Skip to content
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
10 changes: 8 additions & 2 deletions lib/core/constants/endpoint.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import 'dart:core';

class Endpoint {
//user
static const _signIn = '/login';
static const _signUp = '/sign-up';

static get signIn => _signIn;
static get signUp => _signUp;

// schedule
static const _getScheduleById = '/schedule/show/id?scheduleId=';
static const _getSchedulesByDate = '/schedule/show';
Expand Down Expand Up @@ -34,8 +41,7 @@ class Endpoint {
static const _updateDefaultPreparation =
'/preparationuser/modify'; // 사용자 준비과정 수정

static const _updatePreparationByScheduleId =
'/preparationschedule/modify'; // 스케줄별 준비과정 수정
static const _updatePreparationByScheduleId = '/preparationschedule/modify';

// delelte는 api가 없음.
// delete는 api로 요청을 보내는 게 아니라, 전부 없애고 다시 순서를 조정하는 형태로
Expand Down
90 changes: 90 additions & 0 deletions lib/data/data_sources/authentication_remote_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';
import 'package:on_time_front/core/constants/endpoint.dart';
import 'package:on_time_front/data/models/sign_in_user_response_model.dart';
import 'package:on_time_front/domain/entities/token_entity.dart';
import 'package:on_time_front/domain/entities/user_entity.dart';

abstract interface class AuthenticationRemoteDataSource {
Future<(UserEntity, TokenEntity)> signIn(String email, String password);

Future<(UserEntity, TokenEntity)> signUp(
String email, String password, String name);

// Future<(UserEntity, String)> signInWithGoogle(String idToken);
}

@Injectable(as: AuthenticationRemoteDataSource)
class AuthenticationRemoteDataSourceImpl
implements AuthenticationRemoteDataSource {
final Dio dio;
AuthenticationRemoteDataSourceImpl(this.dio);

@override
Future<(UserEntity, TokenEntity)> signIn(
String email, String password) async {
try {
final result = await dio.post(
Endpoint.signIn,
data: {
'email': email,
'password': password,
},
);
if (result.statusCode == 200) {
final user = SignInUserResponseModel.fromJson(result.data['data']);
final token = TokenEntity.fromHeaders(result.headers);
return (user.toEntity(), token);
} else {
throw Exception('Error signing in');
}
} catch (e) {
rethrow;
}
}

@override
Future<(UserEntity, TokenEntity)> signUp(
String email, String password, String name) async {
try {
final result = await dio.post(
Endpoint.signUp,
data: {
'email': email,
'password': password,
'name': name,
},
);
if (result.statusCode == 200) {
final user = SignInUserResponseModel.fromJson(result.data['data']);
final token = TokenEntity.fromHeaders(result.headers);
return (user.toEntity(), token);
} else {
throw Exception('Error signing up');
}
} catch (e) {
rethrow;
}
}

// @override
// Future<(UserEntity, String)> signInWithGoogle(String idToken) async {
// try {
// final result = await dio.post(
// Endpoint.signInWithGoogle,
// data: {
// 'idToken': idToken,
// },
// );
// if (result.statusCode == 200) {
// final user = UserEntity.fromModel(result.data);
// final token = result.headers['authorization']! as String;
// return (user, token);
// } else {
// throw Exception('Error signing in with Google');
// }
// } catch (e) {
// rethrow;
// }
// }
}
42 changes: 42 additions & 0 deletions lib/data/models/sign_in_user_response_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:on_time_front/domain/entities/user_entity.dart';

part 'sign_in_user_response_model.g.dart';

@JsonSerializable()
class SignInUserResponseModel {
final String id;
final String email;
final String name;
final int spareTime;
final String? note;
final double score;
final String? role;

const SignInUserResponseModel({
required this.id,
required this.email,
required this.name,
required this.spareTime,
required this.score,
this.role,
this.note,
});

UserEntity toEntity() {
return UserEntity(
id: id,
email: email,
name: name,
spareTime: Duration(minutes: spareTime),
score: score,
isOnboardingCompleted: role == 'GUEST' ? false : true,
note: note ?? '',
);
}

factory SignInUserResponseModel.fromJson(Map<String, dynamic> json) =>
_$SignInUserResponseModelFromJson(json);

Map<String, dynamic> toJson() => _$SignInUserResponseModelToJson(this);
}
1 change: 0 additions & 1 deletion lib/data/tables/user_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:uuid/uuid.dart';
class Users extends Table {
TextColumn get id => text().clientDefault(() => Uuid().v7())();
TextColumn get email => text().withLength(min: 1, max: 320)();
TextColumn get password => text().withLength(min: 1, max: 30)();
TextColumn get name => text().withLength(min: 1, max: 30)();
IntColumn get spareTime => integer()();
TextColumn get note => text()();
Expand Down
22 changes: 22 additions & 0 deletions lib/domain/entities/token_entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';

class TokenEntity extends Equatable {
final String accessToken;
final String refreshToken;

const TokenEntity({
required this.accessToken,
required this.refreshToken,
});

static TokenEntity fromHeaders(Headers headers) {
return TokenEntity(
accessToken: headers.value('authorization')!,
refreshToken: headers.value('refresh-token')!,
);
}

@override
List<Object?> get props => [accessToken, refreshToken];
}
16 changes: 7 additions & 9 deletions lib/domain/entities/user_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@ import '/core/database/database.dart';
class UserEntity {
final String id;
final String email;
final String password;
final String name;
final int spareTime;
final Duration spareTime;
final String note;
final double score;
final bool isOnboardingCompleted;

UserEntity(
{required this.id,
required this.email,
required this.password,
required this.name,
required this.spareTime,
required this.note,
required this.score});
required this.score,
this.isOnboardingCompleted = false});

static UserEntity fromModel(User user) {
return UserEntity(
id: user.id,
email: user.email,
password: user.password,
name: user.name,
spareTime: user.spareTime,
spareTime: Duration(minutes: user.spareTime),
note: user.note,
score: user.score,
);
Expand All @@ -34,16 +33,15 @@ class UserEntity {
return User(
id: id,
email: email,
password: password,
name: name,
spareTime: spareTime,
spareTime: spareTime.inMinutes,
note: note,
score: score,
);
}

@override
String toString() {
return 'UserEntity(id: $id, email: $email, password: $password, name: $name, spareTime: $spareTime, note: $note, score: $score)';
return 'UserEntity(id: $id, email: $email, name: $name, spareTime: $spareTime, note: $note, score: $score)';
}
}
1 change: 0 additions & 1 deletion test/data/daos/preparation_schedule_dao_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void main() {
UsersCompanion(
id: drift.Value(userId),
email: drift.Value('testuser@example.com'),
password: drift.Value('password123'),
name: drift.Value('Test User'),
spareTime: drift.Value(Duration(minutes: 30).inSeconds),
note: drift.Value('Test Note'),
Expand Down
1 change: 0 additions & 1 deletion test/data/daos/preparation_user_dao_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ void main() {
UsersCompanion(
id: drift.Value(userId),
email: drift.Value('testuser@example.com'),
password: drift.Value('password123'),
name: drift.Value('Test User'),
spareTime: drift.Value(Duration(minutes: 30).inSeconds),
note: drift.Value('Test Note'),
Expand Down