Skip to content

Instantly share code, notes, and snippets.

@seccomiro
Created October 7, 2019 23:15
Show Gist options
  • Save seccomiro/1401b811a8d0877b801bee3d9fcd4d23 to your computer and use it in GitHub Desktop.
Save seccomiro/1401b811a8d0877b801bee3d9fcd4d23 to your computer and use it in GitHub Desktop.
Controller e views de exemplo para aula sobre framework AdonisJs
@layout('layout.master')
@section('conteudo')
<h2>Nova Tarefa</h2>
@include('tarefas.form')
@endsection
@layout('layout.master')
@section('conteudo')
<h2>Editando Tarefa</h2>
@include('tarefas.form')
@endsection
<form action="{{ tarefa.id ? route('tarefas.update', { id: tarefa.id }) + '?_method=PUT' : route('tarefas.store') }}" method="POST">
{{ csrfField() }}
<div class="form-group">
<label for="tarefa_titulo">Título</label>
<input type="text" class="form-control" id="tarefa_titulo" name="titulo" value="{{ tarefa.titulo || '' }}">
</div>
<div class="form-group">
<label for="tarefa_descricao">Descrição</label>
<textarea class="form-control" id="tarefa_descricao" name="descricao" rows="3">{{ tarefa.descricao || '' }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Salvar</button>
<a href="{{ route('tarefas.index') }}" class="btn btn-link">Cancelar</a>
</form>
@layout('layout.master')
@section('conteudo')
<div class="text-center">
<h2>Minhas Tarefas</h2>
<a href="{{ route('tarefas.create') }}" class="btn btn-primary btn-lg mb-4">Nova Tarefa</a>
<div class="row justify-content-center">
<div class="col-lg-6 card text-left">
<ul class="list-group list-group-flush">
@each(tarefa in tarefas)
<li class="list-group-item" style="font-size: 150%">
<a class="text-dark" href="#"><i class="far fa{{ tarefa.concluida ? '-check' : '' }}-square"></i></a>
<a class="text-{{ tarefa.concluida ? 'danger' : 'dark' }}" href="{{ route('tarefas.show', { id: tarefa.id }) }}">
@if(tarefa.concluida)
<del>{{ tarefa.titulo }}</del>
@else
{{ tarefa.titulo }}
@endif
</a>
</li>
@endeach
</ul>
</div>
</div>
</div>
@endsection
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css" integrity="sha256-zmfNZmXoNWBMemUOo1XUGFfc0ihGGLYdgtJS3KCr/l0=" crossorigin="anonymous">
<link rel="stylesheet" href="/temp.css">
<title>IFList</title>
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1>IFList</h1>
<p>Suas tarefas em dia</p>
</div>
</div>
<div class="container">
@!section('conteudo')
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
@layout('layout.master')
@section('conteudo')
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ tarefa.titulo }}</h5>
<p class="card-text">{{ tarefa.descricao }}</p>
<a href="{{ route('tarefas.edit', { id: tarefa.id }) }}" class="btn btn-primary">Editar</a>
<form class="d-inline" action="{{ route('tarefas.destroy', { id: tarefa.id }) + '?_method=DELETE' }}" method="POST">
{{ csrfField() }}
<button type="submit" class="btn btn-danger">Remover</button>
</form>
<a href="{{ route('tarefas.index') }}" class="btn btn-link">Voltar</a>
</div>
</div>
</div>
</div>
@endsection
'use strict';
const Tarefa = use('App/Models/Tarefa');
/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */
/**
* Resourceful controller for interacting with tarefas
*/
class TarefaController {
/**
* Show a list of all tarefas.
* GET tarefas
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async index({ request, response, view }) {
const tarefas = (await Tarefa.query()
.orderBy('concluida')
.orderBy('updated_at', 'desc')
.fetch()).rows;
return view.render('tarefas.index', { tarefas });
}
/**
* Render a form to be used for creating a new tarefa.
* GET tarefas/create
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async create({ request, response, view }) {
const tarefa = new Tarefa();
return view.render('tarefas.create', { tarefa });
}
/**
* Create/save a new tarefa.
* POST tarefas
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async store({ request, response }) {
const tarefaData = request.only(['titulo', 'descricao']);
const tarefa = await Tarefa.create(tarefaData);
response.route('tarefas.show', { id: tarefa.id });
}
/**
* Display a single tarefa.
* GET tarefas/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async show({ params, request, response, view }) {
const tarefa = await Tarefa.find(params.id);
return view.render('tarefas.show', { tarefa });
}
/**
* Render a form to update an existing tarefa.
* GET tarefas/:id/edit
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async edit({ params, request, response, view }) {
const tarefa = await Tarefa.find(params.id);
return view.render('tarefas.edit', { tarefa });
}
/**
* Update tarefa details.
* PUT or PATCH tarefas/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async update({ params, request, response }) {
let tarefa = await Tarefa.find(params.id);
const tarefaData = request.only(['titulo', 'descricao']);
tarefa.merge(tarefaData);
const success = await tarefa.save();
response.route('tarefas.show', { id: params.id });
}
/**
* Delete a tarefa with id.
* DELETE tarefas/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async destroy({ params, request, response }) {
let tarefa = await Tarefa.find(params.id);
const success = await tarefa.delete();
response.route('tarefas.index');
}
}
module.exports = TarefaController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment