-
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.
feat: add DTO to products and cart memory DAO and Reposotory pattern …
…to messages
- Loading branch information
Showing
18 changed files
with
9,442 additions
and
8,792 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,22 @@ | ||
import { Messages } from '../models/interfaces'; | ||
import { flags } from '../config/config'; | ||
import { MessagesFactoryDAO } from '../models/messages/messagesFactory'; | ||
|
||
class messagesAPI { | ||
private messages; | ||
|
||
constructor() { | ||
this.messages = MessagesFactoryDAO.get(flags.D); | ||
} | ||
|
||
async getMessages(): Promise<Messages[]> { | ||
return await this.messages.find(); | ||
} | ||
|
||
async addMessage(data: Messages): Promise<Messages> { | ||
const newMessage = await this.messages.add(data); | ||
return newMessage; | ||
} | ||
} | ||
|
||
export const messageAPI = new messagesAPI(); |
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 { IReadMem, IWriteMem } from '../models/interfaces'; | ||
|
||
export abstract class BaseMem<T> implements IReadMem<T>, IWriteMem<T> { | ||
content: T[]; | ||
|
||
constructor() { | ||
this.content = []; | ||
} | ||
|
||
randomId(): string { | ||
return Math.floor((1 + Math.random()) * 0x10000) | ||
.toString(16) | ||
.substring(1); | ||
} | ||
|
||
find(): T[] { | ||
return this.content; | ||
} | ||
|
||
add(item: T): T { | ||
const newMessage: T = { | ||
_id: this.randomId(), | ||
...item, | ||
}; | ||
this.content.push(newMessage); | ||
return newMessage; | ||
} | ||
} |
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,39 @@ | ||
import { Schema, model, connect } from 'mongoose'; | ||
import { IRead, IWrite, Messages } from '../models/interfaces'; | ||
import CONFIG from '../config/config'; | ||
|
||
const messagesCollection = 'mensajes'; | ||
|
||
const messagesSchema = new Schema<Messages>( | ||
{ | ||
email: { type: String, required: true, max: 100 }, | ||
message: { type: String, required: true, max: 100 }, | ||
}, | ||
{ versionKey: false } | ||
); | ||
|
||
export const messages = model<Messages>(messagesCollection, messagesSchema); | ||
|
||
export abstract class BaseMongo<T> implements IRead<T>, IWrite<T> { | ||
private uri: string; | ||
private messages; | ||
|
||
constructor() { | ||
this.uri = `mongodb+srv://${CONFIG.MONGO_USER}:${CONFIG.MONGO_PASSWORD}@${CONFIG.MONGO_ATLAS_CLUSTER}/${CONFIG.MONGO_DBNAME}?retryWrites=true&w=majority`; | ||
connect(this.uri); | ||
this.messages = model<Messages>(messagesCollection, messagesSchema); | ||
} | ||
|
||
async find(): Promise<Messages[]> { | ||
let outputGet: Messages[] = []; | ||
const product = await this.messages.find(); | ||
outputGet.push(...product) | ||
return outputGet; | ||
} | ||
|
||
async add(item: T): Promise<Messages> { | ||
const newProduct = new this.messages(item); | ||
await newProduct.save(); | ||
return newProduct; | ||
} | ||
} |
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,39 @@ | ||
import { Request, Response } from 'express'; | ||
import { messageAPI } from '../apis/messagesapi'; | ||
|
||
class MessageController { | ||
async getMessages(req: Request, res: Response) { | ||
try { | ||
const getMessages = await messageAPI.getMessages(); | ||
|
||
if (!getMessages.length) | ||
return res.status(404).json({ error: 'No hay mensajes cargados' }); | ||
|
||
return res.json({ messages: getMessages }); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
let errorMessage = error.message; | ||
res.status(500).json({ error: errorMessage }); | ||
} | ||
} | ||
} | ||
async addMessage(req: Request, res: Response) { | ||
try { | ||
const { email, message } = req.body; | ||
|
||
if (!email || !message) | ||
return res.status(404).json({ error: 'Body invalido' }); | ||
|
||
const newMessage = await messageAPI.addMessage({ email, message }); | ||
|
||
return res.json({ message: newMessage }); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
let errorMessage = error.message; | ||
res.status(500).json({ error: errorMessage }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const messageController = new MessageController(); |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import Server from './services/server'; | ||
import { ioServer } from './services/socket'; | ||
|
||
const port = process.env.PORT || 8080; | ||
|
||
ioServer(Server); | ||
Server.listen(port, () => console.log(`Server running in port: ${port}`)); | ||
Server.on('error', (error) => console.error(`There was an error: ${error}`)); |
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,24 @@ | ||
import { Products } from '../../interfaces'; | ||
|
||
export default class CartDTO { | ||
id: string; | ||
title: string; | ||
description: string; | ||
code: string; | ||
price: number; | ||
thumbnail: string; | ||
stock: number; | ||
FyH: string; | ||
|
||
constructor(data: Products) { | ||
this.id = data.id!; | ||
this.title = data.title!; | ||
this.description = data.description!; | ||
this.code = data.code!; | ||
this.price = data.price!; | ||
this.thumbnail = data.thumbnail!; | ||
this.stock = data.stock!; | ||
this.FyH = new Date().toLocaleString(); | ||
} | ||
|
||
} |
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,19 @@ | ||
import { Messages } from '../../interfaces'; | ||
import { BaseMem } from '../../../baseRepository/memoria'; | ||
export class MessagesDAOMEM extends BaseMem<Messages> { | ||
// Private instance of the class to use singleton pattern | ||
private static _instance: MessagesDAOMEM; | ||
|
||
// Getter to call the instance with singleton pattern. | ||
public static get instance() { | ||
if (this._instance) { | ||
console.log( | ||
'La instancia MEMORIA MESSAGES ya fue inicializada, se retorna la misma instancia que ya fue inicializada' | ||
); | ||
return this._instance; | ||
} else { | ||
console.log('Instancia MEMORIA MESSAGES inicializada por primera vez'); | ||
return (this._instance = new this()); | ||
} | ||
} | ||
} |
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,22 @@ | ||
import { BaseMongo } from '../../../baseRepository/mongodb'; | ||
import { Messages } from '../../interfaces'; | ||
|
||
export class MessageDAOMONGO extends BaseMongo<Messages> { | ||
// Private instance of the class to use singleton pattern | ||
private static _instance: MessageDAOMONGO; | ||
|
||
// Getter to call the instance with singleton pattern. | ||
public static get instance() { | ||
if (this._instance) { | ||
console.log( | ||
'La instancia MONGODB ATLAS MESSAGES ya fue inicializada, se retorna la misma instancia que ya fue inicializada' | ||
); | ||
return this._instance; | ||
} else { | ||
console.log( | ||
'Intancia MONGODB ATLAS MESSAGES inicializada por primera vez' | ||
); | ||
return (this._instance = new this()); | ||
} | ||
} | ||
} |
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,15 @@ | ||
import { MessagesDAOMEM } from './DAO/memoria'; | ||
import { MessageDAOMONGO } from './DAO/mongodb'; | ||
|
||
export class MessagesFactoryDAO { | ||
static get(tipo: string) { | ||
switch (tipo) { | ||
case 'MEMORIA': | ||
return MessagesDAOMEM.instance; | ||
case 'MONGOATLAS': | ||
return MessageDAOMONGO.instance; | ||
default: | ||
return MessagesDAOMEM.instance; | ||
} | ||
} | ||
} |
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,29 @@ | ||
import { Products } from "../../interfaces"; | ||
|
||
export default class ProductsDTO { | ||
id: string; | ||
title: string; | ||
description: string; | ||
code: string; | ||
price: number; | ||
thumbnail: string; | ||
stock: number; | ||
FyH: string; | ||
|
||
constructor(data: Products) { | ||
this.id = this.randomId(); | ||
this.title = data.title!; | ||
this.description = data.description!; | ||
this.code = data.code!; | ||
this.price = data.price!; | ||
this.thumbnail = data.thumbnail!; | ||
this.stock = data.stock!; | ||
this.FyH = new Date().toLocaleString(); | ||
} | ||
|
||
randomId(): string { | ||
return Math.floor((1 + Math.random()) * 0x10000) | ||
.toString(16) | ||
.substring(1); | ||
} | ||
} |
Oops, something went wrong.