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

replace remote translate with db translation #8

Merged
merged 6 commits into from
Sep 18, 2024
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
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ build/
.packages
.vscode
test/
test_load/
test_load/
quotes.db
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ coverage/
# environment files
.env
lib/env.g.dart

# db file (too big to be in git)
quotes.db
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ FROM scratch
WORKDIR /app
COPY --from=build /runtime/ /
COPY --from=build /app/build/bin/server /app/bin/
COPY --from=build /app/build/quotes.db /app/
#COPY --from=build /app/build/quotes.db /app/
COPY --from=build /app/build/.env /app/
# Uncomment the following line if you are serving static files.
# COPY --from=build /app/build/public /public/

# Start the server.
CMD ["/app/bin/server"]
CMD ["/app/bin/server"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

Quote API for Kuwot app.

The quotes database file size is exceeding Github's 100MB limit, it can be downloaded from [here](https://drive.google.com/drive/folders/18b7Dvptf0xQ8qwXzGvCZ6qqwd7V0TPtv).

[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
[license_link]: https://opensource.org/licenses/MIT
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
Expand Down
4 changes: 3 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ services:
context: .
container_name: kuwot_api_server
ports:
- 3000:8080
- 3000:8080
volumes:
- ./quotes.db:/app/quotes.db:ro
10 changes: 9 additions & 1 deletion lib/core/data/sqlite_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ abstract class SqliteDb {
/// Initializes the database connection.
void initialize();

/// Executes a query [query] with list of [queryParams].
/// Executes a query [query] with list of [queryParams] for a single data.
/// Returns a nullable [Row] object.
Row? select(
String query, [
List<Object?> queryParams = const [],
]);

/// Executes a query [query] with list of [queryParams] for multiple data.
/// Returns a [ResultSet] object.
ResultSet selectMany(
String query, [
List<Object?> queryParams = const [],
]);

/// Closes the database connection.
void close();
}
30 changes: 30 additions & 0 deletions lib/core/error/failure.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Base class for all failures
abstract class Failure {
/// Create a new [Failure] with the provided [message].
const Failure({required this.message});

/// Failure message
final String message;
}

/// A failure for when an unexpected error occurs.
class UnexpectedFailure extends Failure {
/// Create a new [UnexpectedFailure] with the provided [message].
const UnexpectedFailure({required super.message});
}

/// A failure for when data is not found.
///
/// For example, when a quote with the provided ID is not found.
class DataNotFoundFailure extends Failure {
/// Create a new [DataNotFoundFailure] with the provided [message].
const DataNotFoundFailure({required super.message});
}

/// A failure for when an invalid request is made.
///
/// For example, when the user provides an invalid language code.
class InvalidRequestFailure extends Failure {
/// Create a new [InvalidRequestFailure] with the provided [message].
const InvalidRequestFailure({required super.message});
}
54 changes: 42 additions & 12 deletions lib/data/data_sources/local/quote_local_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ import 'dart:math' show Random;

import 'package:kuwot_api/core/data/sqlite_database.dart';
import 'package:kuwot_api/data/models/quote_model.dart';
import 'package:kuwot_api/data/models/translation_model.dart';

/// A contract for local data source of quotes.
abstract class QuoteLocalDataSource {
/// Get a random quote.
QuoteModel getRandomQuote(int maxRandomId);
QuoteModel getRandomQuote({
required int maxRandomId,
String? tableName,
});

/// Get a quote by its [id].
QuoteModel? getQuote(int id);
QuoteModel? getQuote({
required int id,
String? tableName,
});

/// Get quote count
int getQuoteCount();

/// Get translation list.
List<TranslationModel> getTranslations();
}

/// An implementation of [QuoteLocalDataSource].
Expand All @@ -30,11 +40,12 @@ class QuoteLocalDataSourceImpl implements QuoteLocalDataSource {
int _getRandomId(int min, int max) => min + _random.nextInt(max - min);

@override
QuoteModel getRandomQuote(int maxRandomId) {
final result = sqliteDb.select(
'SELECT * FROM quotes WHERE id = ?;',
[_getRandomId(1, maxRandomId)],
);
QuoteModel getRandomQuote({
required int maxRandomId,
String? tableName,
}) {
final query = 'SELECT * FROM ${tableName ?? 'quotes'} WHERE id = ?;';
final result = sqliteDb.select(query, [_getRandomId(1, maxRandomId)]);

return QuoteModel(
id: result?['id'] as int,
Expand All @@ -44,11 +55,12 @@ class QuoteLocalDataSourceImpl implements QuoteLocalDataSource {
}

@override
QuoteModel? getQuote(int id) {
final result = sqliteDb.select(
'SELECT * FROM quotes WHERE id = ?;',
[id],
);
QuoteModel? getQuote({
required int id,
String? tableName,
}) {
final query = 'SELECT * FROM ${tableName ?? 'quotes'} WHERE id = ?;';
final result = sqliteDb.select(query, [id]);

return result == null
? null
Expand All @@ -67,4 +79,22 @@ class QuoteLocalDataSourceImpl implements QuoteLocalDataSource {

return result?['COUNT(*)'] as int;
}

@override
List<TranslationModel> getTranslations() {
final result = sqliteDb.selectMany(
'SELECT * FROM translations ORDER BY id;',
);
final translations = <TranslationModel>[];
for (final row in result) {
translations.add(
TranslationModel(
id: row['id'] as String,
lang: row['lang'] as String,
tableName: row['table_name'] as String,
),
);
}
return translations;
}
}
51 changes: 0 additions & 51 deletions lib/data/data_sources/remote/translate_remote_data_source.dart

This file was deleted.

20 changes: 20 additions & 0 deletions lib/data/models/translation_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ignore_for_file: public_member_api_docs

import 'package:freezed_annotation/freezed_annotation.dart';

part 'translation_model.freezed.dart';

@freezed
class TranslationModel with _$TranslationModel {
/// A model for translation.
const factory TranslationModel({
/// The translation id, this will be a language id.
required String id,

/// Language name.
required String lang,

/// Translated quote table name.
required String tableName,
}) = _TranslationModel;
}
Loading