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

Impl(AAiT-mobile-1): Create Article Page #256

Open
wants to merge 1 commit 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,41 @@
import 'dart:convert';

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';

abstract class ArticleRemoteDataSource {
Future<ArticleModel> postArticle(CreateArticleModel articleModel);
Future<ArticleModel> updateArticle(CreateArticleModel articleModel);
}

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()));

return ArticleModel.fromJson(jsonDecode(response.body));
}

@override
Future<ArticleModel> updateArticle(CreateArticleModel articleModel) async {
final String? token = await getToken();
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()));

return ArticleModel.fromJson(jsonDecode(response.body));
}

Future<String?> getToken() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString('token');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'dart:convert';

import 'package:equatable/equatable.dart';

import '../../domain/entities/article_enitity.dart';

class ArticleModel extends Article implements Equatable {
ArticleModel(
{required this.id,
required this.title,
required this.subTitle,
required this.tags,
this.user,
required this.content,
this.image,
this.estimatedtime,
this.imageCloudinaryPublicId,
this.createdAt})
: super(
id: id,
title: title,
subTitle: subTitle,
user: user,
tags: tags,
content: content,
image: image,
estimatedtime: estimatedtime,
imageCloudinaryPublicId: imageCloudinaryPublicId,
createdAt: createdAt,
);

@override
final String id;
@override
final String title;
@override
final String subTitle;
@override
final String? user;
@override
final List<String> tags;
@override
final String content;
@override
final String? image;
@override
final String? estimatedtime;
@override
final String? imageCloudinaryPublicId;
@override
final DateTime? createdAt;

factory ArticleModel.fromJson(Map<String, dynamic> json) {
return ArticleModel(
id: json['id'],
title: json["title"],
subTitle: json['subTitile'],
content: json['content'],
tags: jsonDecode(json['tags']),
user: json['user'],
image: json['image'],
estimatedtime: json['estimatedReadTime'],
imageCloudinaryPublicId: json['imageCloudinaryPublicId'],
createdAt: json['createdAt']);
}

Map<String?, dynamic> toJson(ArticleModel articleModel) {
return {
'title': articleModel.title,
'subTitle': articleModel.subTitle,
'content': articleModel.content,
'tags': jsonEncode(articleModel.tags)
};
}

@override
List<Object?> get props => [id];

@override
bool? get stringify => false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'dart:convert';

import 'package:equatable/equatable.dart';

import '../../domain/entities/create_article_entity.dart';

class CreateArticleModel extends CreateArticleEntity implements Equatable {
CreateArticleModel({
required this.title,
required this.subTitle,
required this.tags,
required this.content,
this.id,
this.image,
this.estimatedtime,
}) : super(
title: title,
subTitle: subTitle,
tags: tags,
content: content,
image: image,
estimatedtime: estimatedtime,
);

@override
final String title;
@override
final String subTitle;
@override
final String? id;
@override
final List<String> tags;
@override
final String content;
@override
final String? image;
@override
final String? estimatedtime;

factory CreateArticleModel.fromJson(Map<String, dynamic> json) {
return CreateArticleModel(
title: json["title"],
subTitle: json['subTitile'],
content: json['content'],
tags: jsonDecode(json['tags']),
image: json['image'],
estimatedtime: json['estimatedReadTime']);
}

Map<String?, dynamic> toJson() {
return {
'title': title,
'subTitle': subTitle,
'content': content,
'tags': tags
};
}

@override
List<Object?> get props => [];

@override
bool? get stringify => false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:blog_app/core/errors/failures/failure.dart';
import 'package:blog_app/core/network/network_info.dart';
import 'package:blog_app/features/blog/data/datasources/remote_remote_data_source.dart';
import 'package:blog_app/features/blog/data/models/create_article_model.dart';
import 'package:blog_app/features/blog/domain/entities/article_enitity.dart';
import 'package:blog_app/features/blog/domain/repositories/article_repository.dart';
import 'package:dartz/dartz.dart';

import '../../domain/entities/create_article_entity.dart';

class ArticleRepositoryImpl extends ArticleRepository {
final NetworkInfo networkInfo;
final ArticleRemoteDataSource remoteDataSource;

ArticleRepositoryImpl(
{required this.networkInfo, required this.remoteDataSource});

@override
Future<Either<Failure, Article>> createArticle(
CreateArticleEntity article) async {
CreateArticleModel createArticleModel = CreateArticleModel(
title: article.title,
subTitle: article.subTitle,
tags: article.tags,
content: article.content);
final isConnected = await networkInfo.isConnected;
if (isConnected) {
final article = await remoteDataSource.postArticle(createArticleModel);
return Right(article);
} else {
return Left(ConnectionFailure(message: "Failed to connect to the ethernet"));
}
}

@override
Future<Either<Failure, Article>> updateArticle(
CreateArticleEntity article) async {
CreateArticleModel createArticleModel = CreateArticleModel(
title: article.title,
subTitle: article.subTitle,
tags: article.tags,
content: article.content);
final isConnected = await networkInfo.isConnected;
if (isConnected) {
final article = await remoteDataSource.updateArticle(createArticleModel);
return Right(article);
} else {
return Left(
ConnectionFailure(message: "Failed to connect to the ethernet"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Article {
final String id;
final String title;
final String subTitle;
final String? user;
final List<String> tags;
final String content;
final String? image;
final String? estimatedtime;
final String? imageCloudinaryPublicId;
final DateTime? createdAt;

Article(
{required this.id,
required this.title,
required this.subTitle,
required this.tags,
this.user,
required this.content,
this.image,
this.estimatedtime,
this.imageCloudinaryPublicId,
this.createdAt});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CreateArticleEntity {
final String? id;
final String title;
final String subTitle;
final List<String> tags;
final String content;
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});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:dartz/dartz.dart';

import '../../../../core/errors/failures/failure.dart';
import '../entities/article_enitity.dart';
import '../entities/create_article_entity.dart';

abstract class ArticleRepository {
Future<Either<Failure, Article>> createArticle(CreateArticleEntity article);
Future<Either<Failure, Article>> updateArticle(CreateArticleEntity article);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:dartz/dartz.dart';

import '../../../../core/errors/failures/failure.dart';
import '../entities/article_enitity.dart';
import '../entities/create_article_entity.dart';
import '../repositories/article_repository.dart';

class CreateArticle {
final ArticleRepository repository;
CreateArticle(this.repository);

Future<Either<Failure, Article>> call(CreateArticleEntity article) async {
return await repository.createArticle(article);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:blog_app/core/errors/failures/failure.dart';
import 'package:blog_app/features/blog/domain/entities/article_enitity.dart';
import 'package:blog_app/features/blog/domain/repositories/article_repository.dart';
import 'package:dartz/dartz.dart';

import '../entities/create_article_entity.dart';

class UpdateArticle {
final ArticleRepository repository;
UpdateArticle(this.repository);

Future<Either<Failure, Article>> call(CreateArticleEntity article) async {
return await repository.updateArticle(article);
}
}

Loading