Skip to content
Open
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: 3 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
.env
1,137 changes: 1,137 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/package-lock.json

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "case-cubo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev ./src/index.ts",
"migrations": "tsnd ./src/data/Migrations.ts",
"start": "tsc && node ./build/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^14.2.0",
"express": "^4.17.2",
"knex": "^1.0.1",
"mysql": "^2.18.1",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/knex": "^0.16.1",
"@types/node": "^17.0.10",
"@types/uuid": "^8.3.4",
"ts-node-dev": "^1.1.8",
"typescript": "^4.5.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MissingFieldsToComplet } from "../Error/MissingFieldsToComplet"
import { UserInput, UserInsert } from "../Model/User"
import { UserDataBase } from "../Data/UserDataBase"
import IdGenerator from "../services/IdGenerator"

export class UserBusiness {

async enteringUser(input: UserInput) {

if (input.participation === 0) {
throw new Error("participation can't be zeor")
}

if (!input.firstName || !input.lastName || !input.participation) {
throw new MissingFieldsToComplet()
}

const user: UserInsert = {
id: IdGenerator.generate(),
...input
}

const userDataBase = new UserDataBase()

const result = await userDataBase.insertUser(user)

return result
}

async allUser() {

const userDataBase = new UserDataBase()

const result = await userDataBase.getAllUsers()

return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Request, Response } from "express"
import { UserBusiness } from "../Business/UserBusiness"
import { UserInput } from "../Model/User"

export class UserController {

async createUser(req: Request, res: Response) {
try {
const { firstName, lastName, participation } = req.body

const input: UserInput = {
firstName,
lastName,
participation
}

const userBusiness = new UserBusiness()

const message = await userBusiness.enteringUser(input)

res.status(200).json({ message })

} catch (error) {
if (error instanceof Error) {
res.status(400).json({ message: error.message })
} else {
res.status(400).json({ message: "Unexpected Error" })
}
}
}

async getAllUser(req: Request, res: Response) {
try {
const userBusiness = new UserBusiness()

const allUsers = await userBusiness.allUser()

res.status(200).send(allUsers)

} catch (error) {
if (error instanceof Error) {
res.status(400).json({ message: error.message })
} else {
res.status(400).send({ message: "Unexpected Error" })
}
}
}
}
17 changes: 17 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/src/controller/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import express from "express"
import cors from "cors"
import { AddressInfo } from "net"

export const app = express()

app.use(express.json())
app.use(cors())

const server = app.listen(process.env.PORT || 3003, () => {
if (server) {
const address = server.address() as AddressInfo
console.log(`Server is running in http://localhost:${address.port}`)
} else {
console.error(`Failure upon starting server.`)
}
})
25 changes: 25 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/src/data/BaseDataBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import knex, { Knex } from 'knex'
import { config } from 'dotenv'

config()

export class BaseDataBase {

protected static connection: Knex | null = null

protected getConnection(): Knex {
if (!BaseDataBase.connection) {
BaseDataBase.connection = knex({
client: "mysql",
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SCHEMA,
port: 3306,
}
})
}
return BaseDataBase.connection
}
}
19 changes: 19 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/src/data/Migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseDataBase } from "./BaseDataBase"

class Migrations extends BaseDataBase {

async createTable() {
await this.getConnection().raw(`
create table Cubo_User(
id VARCHAR(255) PRIMARY KEY,
firstName VARCHAR(255) NOT NULL,
lastName VARCHAR(255) NOT NULL,
participation FLOAT
);
`)
console.log("Table created successfully")
}
}

const createTableMigration = new Migrations()
createTableMigration.createTable()
43 changes: 43 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/src/data/UserDataBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { BaseDataBase } from "./BaseDataBase"
import { User, UserInsert } from "../Model/User"

export class UserDataBase extends BaseDataBase {

private static TABLE_NAME = "Cubo_User"

async insertUser(user: UserInsert): Promise<string> {
try {
await this.getConnection().insert(user).into(UserDataBase.TABLE_NAME)

return "User created successfully"

} catch (error) {
if (error instanceof Error) {
throw new Error(error.message)
} else {
throw new Error("Unexpected database error")
}
}
}

async getAllUsers() {
try {
const result: User[] = await this.getConnection()
.select("*")
.from(UserDataBase.TABLE_NAME)

const users = result.map((user) => {
return User.userModel(user)
})

return users

} catch (error) {
if (error instanceof Error) {
throw new Error(error.message)
} else {
throw new Error("Unexpected database error")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class BaseError extends Error {
constructor(message: string) {
super(message)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseError } from "./BaseError"

export class MissingFieldsToComplet extends BaseError {
constructor() {
super("Missing fields to complet")
}
}
4 changes: 4 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { app } from "./Controller/app"
import { userRouter } from "./Router/Router"

app.use("/user", userRouter)
22 changes: 22 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/src/model/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export class User {
constructor(
private id: string,
private firstName: string,
private lastName: string,
private participation: number
) {}

static userModel(user: User) {
return new User(user.id, user.firstName, user.lastName, user.participation)
}
}

export interface UserInput {
firstName: string,
lastName: string,
participation: number
}

export interface UserInsert extends UserInput {
id: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from "express"
import { UserController } from "../Controller/UserController"

export const userRouter = Router()

const userController = new UserController()

userRouter.post("/create", userController.createUser)
userRouter.get("/all", userController.getAllUser)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { v4 } from "uuid"

export class IdGenerator {
generate(): string {
return v4()
}
}

export default new IdGenerator()
12 changes: 12 additions & 0 deletions semana22/case-cubo-fullstack/cubo-backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"outDir": "./build" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"resolveJsonModule": true
}
}
23 changes: 23 additions & 0 deletions semana22/case-cubo-fullstack/cubo-frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Loading