Skip to content

Commit

Permalink
[#25] feat: add indexed object
Browse files Browse the repository at this point in the history
  • Loading branch information
Soogyo-In committed Jan 3, 2023
1 parent 744987c commit 227fd0f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 47 deletions.
55 changes: 39 additions & 16 deletions data/lib/datasource/local/recipe_local_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ part of 'local_datasource.dart';

class RecipeLocalDatasource implements RecipeDatasource {
@override
Future<Recipe> addRecipe(Recipe recipe) => _upsertRecipe(recipe);
Future<IndexedRecipe> addRecipe(Recipe recipe) => _upsertRecipe(recipe);

@override
Future<Recipe> getRecipe(Id id) async {
Future<IndexedRecipe> getRecipe(Id id) async {
final isar = await Isar.open([RecipeDataSchema]);
final recipeData = await isar.recipeDatas.get(id);

Expand All @@ -17,7 +17,7 @@ class RecipeLocalDatasource implements RecipeDatasource {
}

@override
Future<List<Recipe>> getAllRecipes() async {
Future<List<IndexedRecipe>> getAllRecipes() async {
final isar = await Isar.open([RecipeDataSchema]);
final recipeDatas = await isar.recipeDatas.where().findAll();

Expand All @@ -27,7 +27,8 @@ class RecipeLocalDatasource implements RecipeDatasource {
}

@override
Future<Recipe> updateRecipe(Recipe recipe) => _upsertRecipe(recipe);
Future<IndexedRecipe> updateRecipe(IndexedRecipe recipe) =>
_upsertRecipe(recipe);

@override
Future<void> deleteRecipe(Id id) async {
Expand All @@ -37,11 +38,11 @@ class RecipeLocalDatasource implements RecipeDatasource {
}

@override
Future<Ingredient> addIngredient(Ingredient ingredient) =>
Future<IndexedIngredient> addIngredient(Ingredient ingredient) =>
_upsertIngredient(ingredient);

@override
Future<Ingredient> getIngredient(Id id) async {
Future<IndexedIngredient> getIngredient(Id id) async {
final isar = await Isar.open([IngredientDataSchema]);
final ingredientData = await isar.ingredientDatas.get(id);

Expand All @@ -53,7 +54,7 @@ class RecipeLocalDatasource implements RecipeDatasource {
}

@override
Future<List<Ingredient>> getAllIngredients() async {
Future<List<IndexedIngredient>> getAllIngredients() async {
final isar = await Isar.open([IngredientDataSchema]);
final ingredientDatas = await isar.ingredientDatas.where().findAll();

Expand All @@ -65,7 +66,7 @@ class RecipeLocalDatasource implements RecipeDatasource {
}

@override
Future<Ingredient> updateIngredient(Ingredient ingredient) =>
Future<IndexedIngredient> updateIngredient(IndexedIngredient ingredient) =>
_upsertIngredient(ingredient);

@override
Expand All @@ -75,7 +76,7 @@ class RecipeLocalDatasource implements RecipeDatasource {
await isar.close();
}

Future<Recipe> _upsertRecipe(Recipe recipe) async {
Future<IndexedRecipe> _upsertRecipe(Recipe recipe) async {
final isar = await Isar.open([RecipeDataSchema]);
final directionData = <DirectionData>[];
for (final direction in recipe.directions) {
Expand Down Expand Up @@ -105,8 +106,12 @@ class RecipeLocalDatasource implements RecipeDatasource {
}

final id = await isar.writeTxn(() {
final id = recipe.map(
(value) => Isar.autoIncrement,
indexed: (value) => value.id,
);
return isar.recipeDatas.put(
RecipeData(id: recipe.id ?? Isar.autoIncrement)
RecipeData(id: id)
..description = recipe.description
..directions = directionData
..name = recipe.name
Expand All @@ -116,23 +121,41 @@ class RecipeLocalDatasource implements RecipeDatasource {

await isar.close();

return recipe.copyWith(id: id);
return recipe.map(
(value) => IndexedRecipe(
id: id,
name: recipe.name,
directions: recipe.directions,
description: recipe.description,
servings: recipe.servings,
),
indexed: (value) => value,
);
}

Future<Ingredient> _upsertIngredient(
Ingredient ingredient,
) async {
Future<IndexedIngredient> _upsertIngredient(Ingredient ingredient) async {
final isar = await Isar.open([IngredientDataSchema]);
final id = await isar.writeTxn(() {
final id = ingredient.map(
(value) => Isar.autoIncrement,
indexed: (value) => value.id,
);
return isar.ingredientDatas.put(
IngredientData(id: ingredient.id ?? Isar.autoIncrement)
IngredientData(id: id)
..description = ingredient.description
..name = ingredient.name,
);
});

await isar.close();

return ingredient.copyWith(id: id);
return ingredient.map(
(value) => IndexedIngredient(
id: id,
name: ingredient.name,
description: ingredient.description,
),
indexed: (value) => value,
);
}
}
4 changes: 2 additions & 2 deletions data/lib/datasource/model_mapper/ingredient_data_mapper.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
part of 'model_mapper.dart';

extension IngredientDataMapper on IngredientData {
Ingredient toIngredient() {
return Ingredient(
IndexedIngredient toIngredient() {
return IndexedIngredient(
name: name,
description: description ?? '',
id: id,
Expand Down
4 changes: 2 additions & 2 deletions data/lib/datasource/model_mapper/recipe_data_mapper.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of 'model_mapper.dart';

extension RecipDataMapper on RecipeData {
Recipe toRecipe() {
IndexedRecipe toRecipe() {
final directions = this.directions.map(
(directionData) {
final countByIngredientId = <int, Count>{};
Expand All @@ -28,7 +28,7 @@ extension RecipDataMapper on RecipeData {
},
).toList();

return Recipe(
return IndexedRecipe(
directions: directions,
name: name,
description: description ?? '',
Expand Down
16 changes: 8 additions & 8 deletions data/lib/datasource/recipe_datasource.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import 'package:domain/domain.dart';

abstract class RecipeDatasource {
Future<Recipe> addRecipe(Recipe recipe);
Future<IndexedRecipe> addRecipe(Recipe recipe);

Future<Recipe> getRecipe(int id);
Future<IndexedRecipe> getRecipe(int id);

Future<List<Recipe>> getAllRecipes();
Future<List<IndexedRecipe>> getAllRecipes();

Future<Recipe> updateRecipe(Recipe recipe);
Future<IndexedRecipe> updateRecipe(IndexedRecipe recipe);

Future<void> deleteRecipe(int id);

Future<Ingredient> addIngredient(Ingredient ingredient);
Future<IndexedIngredient> addIngredient(Ingredient ingredient);

Future<Ingredient> getIngredient(int id);
Future<IndexedIngredient> getIngredient(int id);

Future<List<Ingredient>> getAllIngredients();
Future<List<IndexedIngredient>> getAllIngredients();

Future<Ingredient> updateIngredient(Ingredient ingredient);
Future<IndexedIngredient> updateIngredient(IndexedIngredient ingredient);

Future<void> deleteIngredient(int id);
}
32 changes: 16 additions & 16 deletions data/test/datasource/local/recipe_local_datasource_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void main() async {

expect(
addedRecipe,
Recipe(
IndexedRecipe(
name: recipe.name,
directions: recipe.directions,
description: recipe.description,
Expand All @@ -76,7 +76,7 @@ void main() async {
group(
'Read',
() {
late Recipe addedRecipe;
late IndexedRecipe addedRecipe;

setUp(() async {
addedRecipe = await datasource.addRecipe(recipe);
Expand All @@ -88,7 +88,7 @@ void main() async {
test(
'Should Isar instance is closed',
() async {
await datasource.getRecipe(addedRecipe.id!);
await datasource.getRecipe(addedRecipe.id);

expect(Isar.getInstance(), isNull);
},
Expand All @@ -98,7 +98,7 @@ void main() async {
'Should return read recipe object',
() async {
expect(
await datasource.getRecipe(addedRecipe.id!),
await datasource.getRecipe(addedRecipe.id),
addedRecipe,
);
},
Expand Down Expand Up @@ -174,8 +174,8 @@ void main() async {
group(
'Update',
() {
late Recipe addedRecipe;
late Recipe updatedRecipe;
late IndexedRecipe addedRecipe;
late IndexedRecipe updatedRecipe;

setUp(() async {
addedRecipe = await datasource.addRecipe(recipe);
Expand Down Expand Up @@ -225,7 +225,7 @@ void main() async {
group(
'Delete',
() {
late Recipe addedRecipe;
late IndexedRecipe addedRecipe;

setUp(() async {
addedRecipe = await datasource.addRecipe(recipe);
Expand All @@ -237,7 +237,7 @@ void main() async {
test(
'Should Isar instance is closed',
() async {
await datasource.deleteRecipe(addedRecipe.id!);
await datasource.deleteRecipe(addedRecipe.id);

expect(Isar.getInstance(), isNull);
},
Expand Down Expand Up @@ -278,7 +278,7 @@ void main() async {

expect(
addedIngredient,
Ingredient(
IndexedIngredient(
name: ingredient.name,
description: ingredient.description,
id: addedIngredient.id,
Expand All @@ -294,7 +294,7 @@ void main() async {
group(
'Read',
() {
late Ingredient addedIngredient;
late IndexedIngredient addedIngredient;

setUp(() async {
addedIngredient = await datasource.addIngredient(ingredient);
Expand All @@ -306,7 +306,7 @@ void main() async {
test(
'Should Isar instance is closed',
() async {
await datasource.getIngredient(addedIngredient.id!);
await datasource.getIngredient(addedIngredient.id);

expect(Isar.getInstance(), isNull);
},
Expand All @@ -316,7 +316,7 @@ void main() async {
'Should return read ingredient object',
() async {
expect(
await datasource.getIngredient(addedIngredient.id!),
await datasource.getIngredient(addedIngredient.id),
addedIngredient,
);
},
Expand Down Expand Up @@ -392,8 +392,8 @@ void main() async {
group(
'Update',
() {
late Ingredient addedIngredient;
late Ingredient updatedIngredient;
late IndexedIngredient addedIngredient;
late IndexedIngredient updatedIngredient;

setUp(() async {
addedIngredient = await datasource.addIngredient(ingredient);
Expand Down Expand Up @@ -432,7 +432,7 @@ void main() async {
group(
'Delete',
() {
late Ingredient addedIngredient;
late IndexedIngredient addedIngredient;

setUp(() async {
addedIngredient = await datasource.addIngredient(ingredient);
Expand All @@ -444,7 +444,7 @@ void main() async {
test(
'Should Isar instance is closed',
() async {
await datasource.deleteIngredient(addedIngredient.id!);
await datasource.deleteIngredient(addedIngredient.id);

expect(Isar.getInstance(), isNull);
},
Expand Down
7 changes: 6 additions & 1 deletion domain/lib/model/ingredient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ part 'ingredient.freezed.dart';
@freezed
class Ingredient with _$Ingredient {
const factory Ingredient({
int? id,
required String name,
@Default('') String description,
}) = _Ingredient;

const factory Ingredient.indexed({
required int id,
required String name,
@Default('') String description,
}) = IndexedIngredient;
}
11 changes: 9 additions & 2 deletions domain/lib/model/recipe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import 'package:freezed_annotation/freezed_annotation.dart';
part 'recipe.freezed.dart';

@freezed
class Recipe with _$Recipe {
abstract class Recipe with _$Recipe {
const Recipe._();

const factory Recipe({
int? id,
required String name,
required List<Direction> directions,
@Default(1) int servings,
@Default('') String description,
}) = _Recipe;

const factory Recipe.indexed({
required int id,
required String name,
required List<Direction> directions,
@Default(1) int servings,
@Default('') String description,
}) = IndexedRecipe;

Map<int, Mass> get massByIngredientId {
final merged = <int, Mass>{};

Expand Down

0 comments on commit 227fd0f

Please sign in to comment.