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
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") |
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
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", | |
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
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 |
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
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); |
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
import Client from "../models/client.ts" | |
import { Request, Response } from "https://deno.land/x/attain/mod.ts"; |
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
export const getUsers = async (req: Request, res: Response) => { | |
res.status(200).send({ clients: await Client.all() }) | |
} |
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
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 }) | |
} |
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
export const getUser = async (req: Request, res: Response) => { | |
res.status(200).send({ client: await Client.where('id', req.params.id).first() }) | |
} |
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
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" }) | |
} | |
} |
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
export const deleteUser = async (req: Request, res: Response) => { | |
await Client.deleteById(req.params.id) | |
res.status(200).send({ clients: Client.all() }) | |
} |
OlderNewer