-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
667 additions
and
171 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex) { | ||
return knex.schema | ||
.createTable('productos', (productsTable) => { | ||
productsTable.increments(); | ||
productsTable.string('title').notNullable(); | ||
productsTable.string('description').notNullable(); | ||
productsTable.string('code').notNullable; | ||
productsTable.decimal('price', 5, 2); | ||
productsTable.string('thumbnail').notNullable(); | ||
productsTable.integer('stock'); | ||
}) | ||
.createTable('carrito', (cartTable) => { | ||
cartTable.increments(); | ||
cartTable.string('title').notNullable(); | ||
cartTable.string('description').notNullable(); | ||
cartTable.string('code').notNullable; | ||
cartTable.decimal('price', 5, 2); | ||
cartTable.string('thumbnail').notNullable(); | ||
cartTable.integer('stock'); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex) { | ||
return knex.schema.dropTable('productos').dropTable('carrito'); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { CartFactoryDAO } from '../models/cart/cartfactory'; | ||
import { Cart, Products } from '../models/interfaces'; | ||
|
||
const tipo = 2; | ||
|
||
class CartAPI { | ||
private cart; | ||
|
||
constructor() { | ||
this.cart = CartFactoryDAO.get(tipo); | ||
} | ||
|
||
async getProducts(id?: string): Promise<Cart[] | Products[]> { | ||
if (id) return await this.cart.get(id); | ||
return await this.cart.get(); | ||
} | ||
|
||
async addProduct(id: string): Promise<Products[]> { | ||
const newProduct = await this.cart.add(id); | ||
return newProduct; | ||
} | ||
|
||
async deleteProduct(id: string) { | ||
return await this.cart.delete(id); | ||
} | ||
} | ||
|
||
export const cartAPI = new CartAPI(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import firebase from 'firebase-admin'; | ||
import serviceAccount from '../../../firebasekey/fir-crud-254ad-firebase-adminsdk-mqgvd-eadcca8c0d.json'; | ||
import { newProductI, Products, Cart } from '../../interfaces'; | ||
import { productsAPI } from '../../../apis/productsapi'; | ||
|
||
export class CartDAOFirebase { | ||
private db; | ||
private query; | ||
|
||
constructor() { | ||
this.db = firebase.firestore(); | ||
this.query = this.db.collection('carrito'); | ||
} | ||
|
||
async get(id?: string): Promise<Products[]> { | ||
if (id) { | ||
const getSpecific = await this.query.doc(id).get(); | ||
const specific = getSpecific.data(); | ||
const product: Products[] = []; | ||
if (specific) { | ||
product.push({ _id: getSpecific.id, ...specific }); | ||
return product; | ||
} | ||
return product; | ||
} else { | ||
const getAll = await this.query.get(); | ||
let docs = getAll.docs; | ||
const output = docs.map((doc) => ({ | ||
_id: doc.id, | ||
...doc.data(), | ||
})); | ||
return output; | ||
} | ||
} | ||
|
||
async add(id: string): Promise<Products[]> { | ||
const getProduct = await productsAPI.getProducts(id); | ||
|
||
const doc = this.query.doc(id); | ||
await doc.create(getProduct[0]); | ||
return getProduct; | ||
} | ||
|
||
async delete(id: string): Promise<Products[]> { | ||
const getProduct = await this.get(id); | ||
const deletedProduct = []; | ||
|
||
if (getProduct.length) { | ||
await this.query.doc(id).delete(); | ||
deletedProduct.push(...getProduct); | ||
return deletedProduct; | ||
} | ||
return getProduct; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { promises as fs } from 'fs'; | ||
import path from 'path'; | ||
import { Cart, Products, ProductQuery } from '../../interfaces'; | ||
|
||
const filePath = path.resolve(__dirname, '../../files/productslog.txt'); | ||
const filePathCart = path.resolve(__dirname, '../../files/cartlog.txt'); | ||
|
||
export class CartDAOFS { | ||
content: Cart[]; | ||
|
||
constructor() { | ||
this.content = [ | ||
{ | ||
id: this.randomId(), | ||
timestamp: Date.now(), | ||
products: [], | ||
}, | ||
]; | ||
} | ||
|
||
randomId(): string { | ||
return Math.floor((1 + Math.random()) * 0x10000) | ||
.toString(16) | ||
.substring(1); | ||
} | ||
|
||
async get(id?: string): Promise<Cart[] | Products[]> { | ||
// Reads cart's file | ||
const txtFile: Cart[] = JSON.parse( | ||
await fs.readFile(filePathCart, 'utf-8') | ||
); | ||
|
||
// Replace the current array in content with the cart's file | ||
this.content = txtFile.length === 0 ? this.content : txtFile; | ||
|
||
// If id exist filter product by id, else return the whole cart's array | ||
const result = id | ||
? this.content[0].products.filter((product) => product._id === id) | ||
: this.content; | ||
return result; | ||
} | ||
|
||
async add(id: string): Promise<Products[]> { | ||
// Reads cart's file | ||
const txtFileC: Cart[] = JSON.parse( | ||
await fs.readFile(filePathCart, 'utf-8') | ||
); | ||
|
||
// Reads cart's file | ||
this.content = txtFileC.length === 0 ? this.content : txtFileC; | ||
|
||
// Reads the products file | ||
const txtFile: Products[] = JSON.parse( | ||
await fs.readFile(filePath, 'utf-8') | ||
); | ||
|
||
// Filtering a single product from product file by passed id | ||
const newProduct = txtFile.filter((product) => product._id === id); | ||
|
||
// Pushing the filtered product into the cart's products array | ||
this.content[0].products.push(...newProduct); | ||
|
||
// Re-write the cart's file | ||
await fs.writeFile(filePathCart, JSON.stringify(this.content, null, 2)); | ||
|
||
// Return an empty array if the product does not exist, else return the array with the matched product | ||
return newProduct.length === 0 ? [] : newProduct; | ||
} | ||
|
||
async delete(id: string): Promise<Products[]> { | ||
// Reads cart's file | ||
this.content = JSON.parse(await fs.readFile(filePathCart, 'utf-8')); | ||
|
||
// Mapping a new array by id if exists returns an the id else returns -1 | ||
const arrayPosition: number = this.content[0].products | ||
.map((product) => product._id) | ||
.indexOf(id); | ||
|
||
// Filtering the deleted product into a new Array | ||
const deletedProduct: Products[] = this.content[0].products.filter( | ||
(product) => product._id == id | ||
); | ||
|
||
// If the product exists, remove the product from the cart's products array | ||
arrayPosition !== -1 && this.content[0].products.splice(arrayPosition, 1); | ||
|
||
// Re-write the cart's file | ||
await fs.writeFile(filePathCart, JSON.stringify(this.content, null, 2)); | ||
|
||
// Return the deleted product if the product exist else return [] | ||
return arrayPosition === -1 ? [] : deletedProduct; | ||
} | ||
|
||
} |
Oops, something went wrong.