Giter Club home page Giter Club logo

projeto_lanhouse's Introduction

LanHouse

Iniciar um projeto.

npm init adonis-ts-app@latest [nome]

Instalando o lucid para o baco de dados.

npm i @adonisjs/lucid

Configurando o lucid.

node ace configure @adonisjs/lucid

Start o servidor de desenvolvimento.

node ace serve --watch

Criar Model e Migration

node ace make:model [nome] -m

Rota

Route.resource("/cursos", "CursosController").apiOnly();

Código de uma migration

import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class extends BaseSchema {
  protected tableName = 'cursos'

  public async up () {
    this.schema.createTable(this.tableName, (table) => {
      table.increments('id')
      table.string('nome', 50).notNullable()
      table.integer('duracao')
      table.string('modalidade',1).notNullable()

      /**
       * Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
       */
      table.timestamp('created_at', { useTz: true })
      table.timestamp('updated_at', { useTz: true })
    })
  }

  public async down () {
    this.schema.dropTable(this.tableName)
  }
}

Exemplo de chave estrangeira

table
  .integer("concessionaria_id")
  .unsigned()
  .references("id")
  .inTable("concessionarias")
  .notNullable();

Codigo de um Model

import { DateTime } from "luxon";
import {
  BaseModel,
  BelongsTo,
  belongsTo,
  column,
  HasMany,
  hasMany,
} from "@ioc:Adonis/Lucid/Orm";
import Motorista from "./Motorista";
import Carga from "./Carga";

export default class Caminhao extends BaseModel {
  @column({ isPrimary: true })
  public id: number;

  @column()
  public motoristaId: number;

  @column()
  public modelo: string;

  @column()
  public cabine: string;

  @column()
  public marca: string;

  @column()
  public placa: string;

  @column()
  public cor: string;

  @column()
  public tipoCaminhao: string;

  @column()
  public potencia: number;

  @column.dateTime({ autoCreate: true })
  public createdAt: DateTime;

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  public updatedAt: DateTime;

  @belongsTo(() => Motorista)
  public motorista: BelongsTo<typeof Motorista>;

  @hasMany(() => Carga)
  public carga: HasMany<typeof Carga>;
}

Rodar as Migration

node ace migration:run

Voltar as Migration

node ace migration:rollback
ou
node ace migration:refresh

Voltar as Migration ao início

node ace migration:reset

Criar uma seeder

node ace make:seeder [Nome]

Código de uma seeder

import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
import Funcionario from 'App/Models/Funcionario'

export default class extends BaseSeeder {
  public async run () {
    await Funcionario.createMany([
      {
        concessionariaId:1,
        matricula: '12345',
        cpf:'001.002.003-04',
        salario: 2500,
        nome: 'Hugo',
        email: '[email protected]',
        idade: 20,
        telefone: 61991862235,
        endereco: 'QNO 7 Conjunto F',
      }
    ])
    // Write your database queries inside the run method
  }
}

Rodar uma seeder

node ace db:seed

Criando um Controller.

node ace make:controller [Nome]

Codigo de uma rota com Controller

// import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

import Aluno from "App/Models/Aluno";

export default class AlunosController {
  index() {
    return Aluno.all();
  }

  store({ request }) {
    const dados = request.only([
      "nome",
      "cpf",
      "matricula",
      "email",
      "telefone",
      "cep",
      "logradouro",
      "complemento",
      "numero",
      "bairro",
    ]);
    return Aluno.create(dados);
  }

  show({ request }) {
    const id = request.param("id");
    return Aluno.findOrFail(id);
  }

  async destroy({ request }) {
    const id = request.param("id");
    const aluno = await Aluno.findOrFail(id);
    return aluno.delete();
  }

  async update({ request }) {
    const id = request.param("id");
    const aluno = await Aluno.findOrFail(id);

    const dados = request.only([
      "nome",
      "cpf",
      "matricula",
      "email",
      "telefone",
      "cep",
      "logradouro",
      "complemento",
      "numero",
      "bairro",
    ]);

    aluno.merge(dados).save();

    return aluno;
  }
}

Criando Validator

node ace make:validator [Nome]

Exemplo de Validator

import { schema, rules, CustomMessages } from "@ioc:Adonis/Core/Validator";
import type { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";

export default class AlunoValidator {
  constructor(protected ctx: HttpContextContract) {}

  public schema = schema.create({
    nome: schema.string([rules.maxLength(100)]),
    cpf: schema.number.optional([
      rules.unique({ table: "alunos", column: "cpf" }),
    ]),
    matricula: schema.string([
      rules.minLength(20),
      rules.maxLength(20),
      rules.unique({ table: "alunos", column: "matricula" }),
    ]),
    email: schema.string.optional([
      rules.email(),
      rules.maxLength(100),
      rules.unique({ table: "alunos", column: "email" }),
    ]),
    telefone: schema.string.optional([
      rules.range(11, 15),
      rules.unique({ table: "alunos", column: "telefone" }),
    ]),
    cep: schema.string.optional(),
    logradouro: schema.string.optional([rules.maxLength(100)]),
    complemento: schema.string.optional([rules.maxLength(100)]),
    numero: schema.string.optional([rules.maxLength(20)]),
    bairro: schema.string.optional([rules.maxLength(100)]),
  });

  public messages: CustomMessages = {};
}

Instalando Autentificador

npm i @adonisjs/auth

Configurando Autentificador

node ace configure @adonisjs/auth
# Lucid
# API token
# User
# Yes
# Database
# Yes
npm i phc-argon2

projeto_lanhouse's People

Contributors

rikelmelopes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.