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

Click views #31

Merged
merged 7 commits into from
Aug 18, 2019
99 changes: 73 additions & 26 deletions app/scripts/services/DataRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
var DataRepository = function (dataContainer, syncObject, recordSets) {
this.syncObject = syncObject;
console.log('this.syncObject:', this.syncObject);
this.recordSets = recordSets;
this.brands = [];
this.categories = [];
Expand Down Expand Up @@ -178,9 +179,11 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
return null;
};

this.save = function save(successMsg, callback) {
this.syncObject.$save().then(
this.save = function save(collectionName, recordIndex, successMsg, callback) {
console.log('save (new style)...');
this.recordSets[collectionName].$save(recordIndex).then(
function () {
console.log('Success...');
if (successMsg) {
window.alert(successMsg);
}
Expand All @@ -189,6 +192,7 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
}
},
function (error) {
console.log('Error...', error);
window.alert('Error: ' + error.toString());
}
);
Expand All @@ -203,18 +207,25 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
*/
this.persistBrand = function persistBrand(brand, successMsg, callback) {
if (!brand.hasId()) {
console.log('Brand does not have an id');
// This is an insert.
brand.setId(this.generateNextBrandId());
// @TODO: This will need to change.
dataContainer.data.brands[brand.getId()] = {id: brand.getId()};
}
var brandSource = dataContainer.data.brands[brand.getId()];
// Determine the index of the record in the array from the model's id.
var brandIndex = this.determineIndexFromBrandModel(brand);
// Create a reference to the object in the array, overwrite its values, and save it.
var brandSource = this.recordSets.brands[brandIndex];
brandSource.name = brand.getName();
brandSource.description = brand.getDescription();
brandSource.categories = brand.getCategoryIdAsCsl();
brandSource.image = brand.getImage();
brandSource.ranking = brand.getRanking();
// brandSource.brandUrl = brand.getBrandURL(); // @TODO: There does not seem to be a URL property in the data.
this.save(successMsg, callback);

// Save this brand record.
this.save('brands', brandIndex, successMsg, callback);
};

/**
Expand All @@ -226,16 +237,23 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
*/
this.persistCategory = function persistCategory(category, successMsg, callback) {
if (!category.hasId()) {
console.log('Category does not have an id');
// This is an insert.
category.setId(this.generateNextCategoryId());
// @TODO: This will need to change.
dataContainer.data.categories[category.getId()] = {id: category.getId()};
}
var categorySource = dataContainer.data.categories[category.getId()];
// Determine the index of the record in the array from the model's id.
var categoryIndex = this.determineIndexFromCategoryModel(category);
// Create a reference to the object in the array, overwrite its values, and save it.
var categorySource = this.recordSets.categories[categoryIndex];
categorySource.description = category.getDescription();
categorySource.image = category.getImage();
categorySource.name = category.getName();
categorySource.parentCategoryId = category.getParentCategoryId();
this.save(successMsg, callback);

// Save this category record.
this.save('categories', categoryIndex, successMsg, callback);
};

/**
Expand All @@ -247,11 +265,17 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
*/
this.persistProduct = function persistProduct(product, successMsg, callback) {
if (!product.hasId()) {
console.log('Product does not have an id');
// This is an insert.
product.setId(this.generateNextProductId());
// @TODO: This will need to change.
dataContainer.data.products[product.getId()] = {id: product.getId()};
// @TODO: Do we need to use .$add() for this type of operation?
}
var productSource = dataContainer.data.products[product.getId()];
// Determine the index of the record in the array from the model's id.
var productIndex = this.determineIndexFromProductModel(product);
// Create a reference to the object in the array, overwrite its values, and save it.
var productSource = this.recordSets.products[productIndex];
productSource.brandId = product.getBrandId();
productSource.categoryId = product.getCategoryId();
productSource.description = product.getDescription();
Expand All @@ -262,7 +286,9 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
productSource.purchaseURlClicks = product.getPurchaseUrlClicks();
productSource.purchaseUrl = product.getPurchaseUrl();
// productSource.$$hashKey = product.$$hashKey(); @TODO: Do we save this? When does it change?
this.save(successMsg, callback);

// Save this product record.
this.save('products', productIndex, successMsg, callback);
};

this.generateNextBrandId = function generateNextBrandId() {
Expand All @@ -289,6 +315,42 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
return id;
};

this.determineIndexFromBrandModel = function determineIndexFromBrandModel(brandModel) {
// Determine the index of the firebase array, using the brand model's id.
var indexToDelete = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other places we may want to use this function? If there are we may want to rename this variable so it isn't related to deletion. Maybe something like targetIndex or indexMatch or something else completely :) . (same in the other 2 functions below)

Copy link
Contributor Author

@pedanticantic pedanticantic Aug 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, you're right. Well spotted! I re-used the code that was explicitly for deleting a record, but didn't notice the variable name.
I'll change it (and the other methods) now - this method is also used when updating a record.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I went with a different format for the name :-)
I went with brandIndex, categoryIndex, etc.
The commit is here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect - love it!

this.recordSets.brands.forEach(function(brand, index) {
if (brand.id === brandModel.getId()) {
indexToDelete = index;
}
});

return indexToDelete;
};

this.determineIndexFromCategoryModel = function determineIndexFromCategoryModel(categoryModel) {
// Determine the index of the firebase array, using the category model's id.
var indexToDelete = null;
this.recordSets.categories.forEach(function(category, index) {
if (category.id === categoryModel.getId()) {
indexToDelete = index;
}
});

return indexToDelete;
};

this.determineIndexFromProductModel = function determineIndexFromProductModel(productModel) {
// Determine the index of the firebase array, using the product model's id.
var indexToDelete = null;
this.recordSets.products.forEach(function(product, index) {
if (product.id === productModel.getId()) {
indexToDelete = index;
}
});

return indexToDelete;
};

/**
* @TODO: Presumably we should update any references to this brand (or prevent delete if anything references it)?
* @TODO: And what about uploaded images? Should they be deleted? What if something else is referencing it?
Expand All @@ -298,12 +360,7 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
*/
this.deleteBrand = function deleteBrand(brandModel, callback) {
// Determine the index of the firebase array, using the brand model's id.
var indexToDelete = null;
this.recordSets.brands.forEach(function(brand, index) {
if (brand.id === brandModel.getId()) {
indexToDelete = index;
}
});
var indexToDelete = this.determineIndexFromBrandModel(brandModel);
if (indexToDelete !== null) {
// We found it, so delete it. If the delete was successful, run the callback function.
this.recordSets.brands.$remove(indexToDelete).then(function () {
Expand All @@ -324,12 +381,7 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
*/
this.deleteCategory = function deleteCategory(categoryModel, callback) {
// Determine the index of the firebase array, using the category model's id.
var indexToDelete = null;
this.recordSets.categories.forEach(function(category, index) {
if (category.id === categoryModel.getId()) {
indexToDelete = index;
}
});
var indexToDelete = this.determineIndexFromCategoryModel(categoryModel);
if (indexToDelete !== null) {
// We found it, so delete it. If the delete was successful, run the callback function.
this.recordSets.categories.$remove(indexToDelete).then(function () {
Expand All @@ -350,12 +402,7 @@ var DataRepository = function (dataContainer, syncObject, recordSets) {
*/
this.deleteProduct = function deleteProduct(productModel, callback) {
// Determine the index of the firebase array, using the product model's id.
var indexToDelete = null;
this.recordSets.products.forEach(function(product, index) {
if (product.id === productModel.getId()) {
indexToDelete = index;
}
});
var indexToDelete = this.determineIndexFromProductModel(productModel);
if (indexToDelete !== null) {
// We found it, so delete it. If the delete was successful, run the callback function.
this.recordSets.products.$remove(indexToDelete).then(function () {
Expand Down