Skip to content

Instantly share code, notes, and snippets.

View kauemurakami's full-sized avatar
🎯
Concentrando

Kauê Murakami kauemurakami

🎯
Concentrando
View GitHub Profile
@kauemurakami
kauemurakami / server.ts
Created June 1, 2020 17:57
arquivo principal
import { App, Request, Response } from "https://deno.land/x/attain/mod.ts";
import "./config/db.ts"
import clientsRouter from "./routes/clients.router.ts"
const app = new App()
app.use("/clients", clientsRouter)
app.use((req : Request, res: Response) => {
res.status(404).send("not found")
@kauemurakami
kauemurakami / db.ts
Last active June 1, 2020 18:03
arquivo db
import { Database } from 'https://deno.land/x/denodb/mod.ts';
import Client from "../models/client.ts";
const db = new Database(
"mysql",{
database: "NOME-DA-BASE-DE-DADOS",
host: "localhost",
username: "root",
password: "senha",
@kauemurakami
kauemurakami / client.ts
Created June 1, 2020 18:04
Classe Client
import { DATA_TYPES, Model } from 'https://deno.land/x/denodb/mod.ts'
export default class Client extends Model {
static table = "clients"
static timestamps = true
static fields = {
id : {
primaryKey : true,
autoIncrement: true
@kauemurakami
kauemurakami / routes.router.ts
Created June 1, 2020 18:05
arquivo de rotas
import { Router } from "https://deno.land/x/attain/mod.ts";
import { getUsers, getUser, updateUser, addUser, deleteUser } from "../controllers/client_controller.ts"
const clientsRouter = new Router()
clientsRouter.get("/", getUsers )
clientsRouter.get("/:id", getUser)
clientsRouter.put("/:id", updateUser);
@kauemurakami
kauemurakami / controller.ts
Created June 1, 2020 18:07
imports controller
import Client from "../models/client.ts"
import { Request, Response } from "https://deno.land/x/attain/mod.ts";
@kauemurakami
kauemurakami / controller.ts
Created June 1, 2020 18:07
recuperando todos os registros
export const getUsers = async (req: Request, res: Response) => {
res.status(200).send({ clients: await Client.all() })
}
@kauemurakami
kauemurakami / controller.ts
Created June 1, 2020 18:09
adicinando um registro
export const addUser = async (req: Request, res: Response) => {
const body = await req.body()
const client: Client = body.value
//console.log(client)
const { name, email, password } = body.value
//await Client.create( body.value )
await Client.create({ name: name, email: email, password: password })
res.status(200).send({ client: client })
}
@kauemurakami
kauemurakami / controller.ts
Created June 1, 2020 18:09
recuperando registro por id
export const getUser = async (req: Request, res: Response) => {
res.status(200).send({ client: await Client.where('id', req.params.id).first() })
}
@kauemurakami
kauemurakami / controller.ts
Created June 1, 2020 18:10
atualizando registro
export const updateUser = async (req: Request, res: Response) => {
const body = await req.body()
if (Client.select('id', '===', req.params.id)) {
await Client.where('id', req.params.id).update({ name: body.value.name, email: body.value.email })
res.status(200).send({ clients: Client.all() })
}else{
res.status(404).send({ message: "usuário não encontrado" })
}
}
@kauemurakami
kauemurakami / controller.ts
Created June 1, 2020 18:11
deletando registro
export const deleteUser = async (req: Request, res: Response) => {
await Client.deleteById(req.params.id)
res.status(200).send({ clients: Client.all() })
}