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

feat(AAiT-mobile-1): Article Image Upload #415

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// import 'package:blog_app/features/Article/domain/usecases/get_article.dart';
// import 'package:blog_app/features/Article/presentation/bloc/article_bloc/article_bloc.dart';
// import 'package:blog_app/features/blog/data/datasources/remote_remote_data_source.dart';
// import 'package:blog_app/features/blog/data/repository/article_repository_implimentation.dart';
// import 'package:blog_app/features/blog/domain/entities/create_article_entity.dart';
// import 'package:blog_app/features/blog/domain/repositories/article_repository.dart';
// import 'package:blog_app/features/blog/domain/usecases/create_article.dart';
// import 'package:blog_app/features/blog/domain/usecases/update_article.dart';
// import 'package:get_it/get_it.dart';
// import 'package:internet_connection_checker/internet_connection_checker.dart';
// import 'package:shared_preferences/shared_preferences.dart';

// import '../core/network/network_info.dart';
// import '../features/Article/domain/entities/article_enitity.dart';

// final sl = GetIt.instance;

// Future<void> init() async {
// //! Features - Task Management
// // Bloc
// sl.registerFactory(() => ArticleBloc());

// // Use cases
// sl.registerLazySingleton(() => GetArticle(sl()));
// sl.registerLazySingleton(() => CreateArticle(sl()));
// sl.registerLazySingleton(() => UpdateArticle(sl()));

// // Repository
// sl.registerLazySingleton<ArticleRemoteDataSource>(
// () => ArticleRemoteDataSourceImpl());
// sl.registerLazySingleton<ArticleRepository>(
// () => ArticleRepositoryImpl(networkInfo: sl(), remoteDataSource: sl()));

// // classes
// sl.registerLazySingleton(() => <Article>[]);
// sl.registerLazySingleton(() => <CreateArticleEntity>[]);
// //! Core
// sl.registerLazySingleton<NetworkInfo>(() => NetworkInfoImpl(sl()));

// //! External
// sl.registerLazySingleton(() => InternetConnectionChecker());

// final sharedPreferences = await SharedPreferences.getInstance();
// sl.registerLazySingleton(() => sharedPreferences);
// }
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'dart:convert';
import 'dart:io';

import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import '../../../../core/utils/constants.dart';
import '../models/article_model.dart';
import '../models/create_article_model.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';

abstract class ArticleRemoteDataSource {
Future<ArticleModel> postArticle(CreateArticleModel articleModel);
Expand All @@ -15,23 +18,75 @@ abstract class ArticleRemoteDataSource {
class ArticleRemoteDataSourceImpl extends ArticleRemoteDataSource {
@override
Future<ArticleModel> postArticle(CreateArticleModel articleModel) async {
final String? token = await getToken();
final response = await http.post(Uri.parse('$baseApi/article'),
headers: {'Content-Type': 'application/json', "token": token!},
body: json.encode(articleModel.toJson()));
String? token = await getToken();
final uri = Uri.parse('$baseApi/article');
var request = http.MultipartRequest('POST', uri);

return ArticleModel.fromJson(jsonDecode(response.body));
Map<String, String> headers = {
HttpHeaders.authorizationHeader:
'Bearer $token', // Add your authentication token here
};

request.headers.addAll(headers);
File imageFile = File(articleModel.image);
final String fileType = MimeTypeResolver().lookup(imageFile.path) ?? "";

request.files.add(http.MultipartFile(
'photo', imageFile.readAsBytes().asStream(), imageFile.lengthSync(),
filename: imageFile.path.split("/").last,
contentType: MediaType.parse(fileType)));

request.fields['title'] = articleModel.title;
request.fields['subTitle'] = articleModel.subTitle;
request.fields['tags'] = jsonEncode(articleModel.tags);
request.fields['content'] = articleModel.content;
request.fields['estimatedReadTime'] = articleModel.estimatedtime;

var response = await request.send();

if (response.statusCode == 200) {
return articleModel as ArticleModel;
}
throw ("Couldn't Post Article ${response.statusCode}");
}

@override
Future<ArticleModel> updateArticle(CreateArticleModel articleModel) async {
final String? token = await getToken();
String? token = await getToken();
print("something wrong");
if (articleModel.id == null) {
throw ("Couldn't Post Article");
}
final id = articleModel.id;
final response = await http.post(Uri.parse('$baseApi/article/$id'),
headers: {'Content-Type': 'application/json', "token": token!},
body: json.encode(articleModel.toJson()));
final uri = Uri.parse('$baseApi/article/$id');
var request = http.MultipartRequest('PUT', uri);

return ArticleModel.fromJson(jsonDecode(response.body));
Map<String, String> headers = {
HttpHeaders.authorizationHeader:
'Bearer $token', // Add your authentication token here
};

request.headers.addAll(headers);
File imageFile = File(articleModel.image);
final String fileType = MimeTypeResolver().lookup(imageFile.path) ?? "";

request.files.add(http.MultipartFile(
'photo', imageFile.readAsBytes().asStream(), imageFile.lengthSync(),
filename: imageFile.path.split("/").last,
contentType: MediaType.parse(fileType)));

request.fields['title'] = articleModel.title;
request.fields['subTitle'] = articleModel.subTitle;
request.fields['tags'] = jsonEncode(articleModel.tags);
request.fields['content'] = articleModel.content;
request.fields['estimatedReadTime'] = articleModel.estimatedtime;

var response = await request.send();

if (response.statusCode == 200) {
return articleModel as ArticleModel;
}
throw ("Couldn't Post Article ${response.statusCode}");
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class ArticleModel extends Article implements Equatable {
required this.tags,
this.user,
required this.content,
this.image,
this.estimatedtime,
required this.image,
required this.estimatedtime,
this.imageCloudinaryPublicId,
this.createdAt})
: super(
Expand Down Expand Up @@ -42,9 +42,9 @@ class ArticleModel extends Article implements Equatable {
@override
final String content;
@override
final String? image;
final String image;
@override
final String? estimatedtime;
final String estimatedtime;
@override
final String? imageCloudinaryPublicId;
@override
Expand All @@ -69,7 +69,9 @@ class ArticleModel extends Article implements Equatable {
'title': articleModel.title,
'subTitle': articleModel.subTitle,
'content': articleModel.content,
'tags': jsonEncode(articleModel.tags)
'tags': jsonEncode(articleModel.tags),
'image': articleModel.image,
'estimatedReadTime': articleModel.estimatedtime
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class CreateArticleModel extends CreateArticleEntity implements Equatable {
required this.tags,
required this.content,
this.id,
this.image,
this.estimatedtime,
required this.image,
required this.estimatedtime,
}) : super(
title: title,
subTitle: subTitle,
Expand All @@ -33,9 +33,9 @@ class CreateArticleModel extends CreateArticleEntity implements Equatable {
@override
final String content;
@override
final String? image;
final String image;
@override
final String? estimatedtime;
final String estimatedtime;

factory CreateArticleModel.fromJson(Map<String, dynamic> json) {
return CreateArticleModel(
Expand All @@ -52,7 +52,9 @@ class CreateArticleModel extends CreateArticleEntity implements Equatable {
'title': title,
'subTitle': subTitle,
'content': content,
'tags': tags
'tags': tags,
'image': image,
'estimatedReadTime': estimatedtime
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class ArticleRepositoryImpl extends ArticleRepository {
title: article.title,
subTitle: article.subTitle,
tags: article.tags,
content: article.content);
content: article.content,
image: article.image,
estimatedtime: article.estimatedtime);
final isConnected = await networkInfo.isConnected;
if (isConnected) {
final article = await remoteDataSource.postArticle(createArticleModel);
Expand All @@ -39,7 +41,9 @@ class ArticleRepositoryImpl extends ArticleRepository {
title: article.title,
subTitle: article.subTitle,
tags: article.tags,
content: article.content);
content: article.content,
image: article.image,
estimatedtime: article.estimatedtime);
final isConnected = await networkInfo.isConnected;
if (isConnected) {
final article = await remoteDataSource.updateArticle(createArticleModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Article {
final String? user;
final List<String> tags;
final String content;
final String? image;
final String? estimatedtime;
final String image;
final String estimatedtime;
final String? imageCloudinaryPublicId;
final DateTime? createdAt;

Expand All @@ -17,8 +17,8 @@ class Article {
required this.tags,
this.user,
required this.content,
this.image,
this.estimatedtime,
required this.image,
required this.estimatedtime,
this.imageCloudinaryPublicId,
this.createdAt});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class CreateArticleEntity {
final String subTitle;
final List<String> tags;
final String content;
final String? image;
final String? estimatedtime;
final String image;
final String estimatedtime;

CreateArticleEntity(
{this.id,
required this.title,
required this.subTitle,
required this.tags,
required this.content,
this.image,
this.estimatedtime});
required this.image,
required this.estimatedtime});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:blog_app/features/Article/domain/usecases/create_article.dart';
import 'package:blog_app/features/Article/domain/usecases/get_article.dart';
import 'package:blog_app/features/Article/domain/usecases/update_article.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../../Injection/article_injection.dart';
import '../../../../../Injection/injection_container.dart';
import '../../../../../core/network/network_info.dart';
import '../../../data/datasources/remote_remote_data_source.dart';
Expand All @@ -22,12 +22,16 @@ class ArticleBloc extends Bloc<ArticleEvent, ArticleState> {

on<CreateArticleEvent>((CreateArticleEvent event, Emitter emit) async {
CreateArticle usecase = CreateArticle(repository);
emit(Loading());
await usecase.repository.createArticle(event.article);
emit(Idle());
});

on<UpdateArticleEvent>((UpdateArticleEvent event, Emitter emit) async {
UpdateArticle usecase = UpdateArticle(repository);
emit(Loading());
await usecase.repository.updateArticle(event.article);
emit(Idle());
});

on<GetArticleEvent>((GetArticleEvent event, Emitter emit) async {
Expand Down
Loading