Skip to content

Commit

Permalink
Merge pull request #3 from MikkyBoy357/feat/change_bio
Browse files Browse the repository at this point in the history
feat/change bio
  • Loading branch information
MikkyBoy357 authored Aug 3, 2024
2 parents 5be8c2a + 1130e41 commit 55a0b41
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 25 deletions.
31 changes: 26 additions & 5 deletions lib/data/repositories/profile/profile_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ abstract class ProfileRepositoryImpl {
required User user,
required String imageUrl,
});
// implement changeProfileBio
// see changeProfileAvatar for example

Future<User?> changeProfileBio({
required User user,
required String bio,
});
}

class ProfileRepository extends ProfileRepositoryImpl {
Expand Down Expand Up @@ -37,9 +40,27 @@ class ProfileRepository extends ProfileRepositoryImpl {

final updatedUser = user.copyWith(avatarUrl: imageUrl);

return User.fromJson(updatedUser.toJson());
return updatedUser;
}

// implement changeProfileBio
// see changeProfileAvatar for example
@override
Future<User?> changeProfileBio({
required User user,
required String bio,
}) async {
final result = await usersCollection.updateOne(
where.id(user.$_id),
modify.set('bio', bio),
);

print('📍 Write result: $result');

if (result.writeError != null) {
return null;
}

final updatedUser = user.copyWith(bio: bio);

return updatedUser;
}
}
20 changes: 10 additions & 10 deletions lib/data/request_handlers/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ class ChatRequestHandlerImpl implements ChatRequestHandler {

downloadUrl = urlOrError.url;
uploadError = urlOrError.error;

if (uploadError != null) {
return Response.json(
body: {
'message': 'Failed to upload image',
'error': uploadError,
},
statusCode: 500,
);
}
}
}

if (downloadUrl == null) {
return Response.json(
body: {
'message': 'Failed to upload image',
'error': uploadError,
},
statusCode: 500,
);
}

final chat = await _chatRepository.getChatById(chatId: chatId);

if (chat == null) {
Expand Down
48 changes: 38 additions & 10 deletions lib/data/request_handlers/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ abstract class ProfileRequestHandler {
required User user,
required FormData formData,
});

Future<Response> handleChangeBio({
required User user,
required String bio,
});
}

class ProfileRequestHandlerImpl implements ProfileRequestHandler {
Expand Down Expand Up @@ -39,26 +44,49 @@ class ProfileRequestHandlerImpl implements ProfileRequestHandler {

downloadUrl = urlOrError.url;
uploadError = urlOrError.error;
}

if (uploadError != null) {
return Response.json(
body: {
'message': 'Failed to upload avatar',
'error': uploadError,
},
statusCode: 500,
);
}
if (downloadUrl == null) {
return Response.json(
body: {
'message': 'Failed to upload avatar',
'error': uploadError,
},
statusCode: 500,
);
}

// Update user avatarUrl
final updatedUser = await _profileRepository.changeProfileAvatar(
user: user,
imageUrl: downloadUrl!,
imageUrl: downloadUrl,
);

return Response.json(
body: updatedUser,
);
}

@override
Future<Response> handleChangeBio({
required User user,
required String bio,
}) async {
if (bio.length > 69) {
return Response.json(
body: 'Error: Bio cannot be more than 69 characters.',
statusCode: 400,
);
}

final updatedUser = await _profileRepository.changeProfileBio(
user: user,
bio: bio,
);

return Response.json(
body: updatedUser,
statusCode: updatedUser != null ? 200 : 500,
);
}
}
56 changes: 56 additions & 0 deletions routes/api/profile/bio.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:convert';

import 'package:cats_backend/data/repositories/repositories.dart';
import 'package:cats_backend/data/request_handlers/profile.dart';
import 'package:cats_backend/helpers/authentication_validation.dart';
import 'package:cats_backend/services/mongo_service.dart';
import 'package:dart_frog/dart_frog.dart';

Future<Response> onRequest(RequestContext context) async {
print('======= avatar =======>');
final authValidationResponse = context.read<AuthValidationResponse>();

if (!authValidationResponse.isValid) {
return Response.json(
statusCode: 401,
body: 'Auth Error: ${authValidationResponse.errorMessage}',
);
}

final profileRepository = ProfileRepository(
database: mongoDbService.database,
);
final request = context.request;
final method = request.method;
final handler = ProfileRequestHandlerImpl(
profileRepository: profileRepository,
);
final saint = authValidationResponse.user!;

return switch (method) {
HttpMethod.post => () async {
final body = await request.body();
final bodyJson = jsonDecode(body) as Map<String, dynamic>;

final bio = bodyJson['bio'] as String?;

if (bio == null || bio.isEmpty) {
return Response.json(
body: 'Error: Bio is required and cannot be empty.',
statusCode: 400,
);
}

return handler.handleChangeBio(
user: saint,
bio: bio,
);
}(),
_ => Future.value(
Response.json(
body: 'Method not allowed',
statusCode: 405,
),
),
};
}

0 comments on commit 55a0b41

Please sign in to comment.