Skip to content

Commit

Permalink
Merge pull request #5 from auguzsto/0.1
Browse files Browse the repository at this point in the history
feat and fixes relatorios
  • Loading branch information
auguzsto authored Dec 15, 2023
2 parents ba0183c + 869d54f commit b39dffb
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 11 deletions.
27 changes: 27 additions & 0 deletions backend/app/migrations/15122023_1834.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE DATABASE IF NOT EXISTS backend_assim;

CREATE TABLE cargos(
id BIGINT AUTO_INCREMENT,
nome TEXT NOT NULL,
salario TEXT,
created_at DATETIME NOT NULL,
updated_at DATETIME,
deleted_at DATETIME,
PRIMARY KEY (id)
);

CREATE TABLE funcionarios(
id BIGINT AUTO_INCREMENT,
nome TEXT NOT NULL,
data_nascimento DATE,
endereco_completo TEXT,
cpf TEXT NOT NULL UNIQUE,
email TEXT,
telefone TEXT,
cargo_id BIGINT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME,
deleted_at DATETIME,
PRIMARY KEY (id),
FOREIGN KEY (cargo_id) REFERENCES cargos(id)
);
6 changes: 6 additions & 0 deletions backend/app/routes/routers.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
$router->patch("/{id}", "FuncionarioController@update");
$router->options("/{id}", "FuncionarioController@delete");
});

// Endpoints relatorios
$router->mount("/api/relatorios", function() use ($router) {
$router->get("/", "RelatorioController@getNomeTelefoneCargoAll");
$router->get("/(\w+)", "RelatorioController@getNomeTelefoneCargoByNomeCpf");
});

$router->run();
32 changes: 32 additions & 0 deletions backend/app/src/controllers/RelatorioController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace App\controllers;
use PDO;
use Exception;
use App\core\Database;

class RelatorioController {

public function getNomeTelefoneCargoAll() {
try {
$db = Database::getInstace();
$data = $db->query("select f.id as funcionario_id, c.id as cargo_id, f.nome AS nome_funcionario, f.telefone , c.nome AS nome_cargo, c.salario FROM cargos c, funcionarios f where f.nome like '%Matheus%' or c.nome like '%a'GROUP BY f.nome;")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
} catch (Exception $e) {
header('HTTP/1.1 400 Bad Request');
echo json_encode(["error"=> $e->getMessage()]);
throw $e;
}
}

public function getNomeTelefoneCargoByNomeCpf(string $params) {
try {
$db = Database::getInstace();
$data = $db->query("select f.id as funcionario_id, c.id as cargo_id, f.nome AS nome_funcionario, f.telefone , c.nome AS nome_cargo, c.salario FROM cargos c, funcionarios f where f.nome like '%$params%' or c.nome like '%$params%' GROUP BY f.nome;")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
} catch (Exception $e) {
header('HTTP/1.1 400 Bad Request');
echo json_encode(["error"=> $e->getMessage()]);
throw $e;
}
}
}
3 changes: 1 addition & 2 deletions backend/app/src/core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ private function con(): PDO {
global $config;
$host = $config['host'];
$port = $config['port'];
$dbname = $config['database'];
$dbuser = $config['user'];
$dbpassword = $config['password'];

$pdo = new PDO("mysql:host=$host:$port;dbname=$dbname", "$dbuser", "$dbpassword");
$pdo = new PDO("mysql:host=$host:$port;dbname=backend_assim", "$dbuser", "$dbpassword");
return $pdo;

} catch (Exception $e) {
Expand Down
7 changes: 3 additions & 4 deletions backend/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

global $config;

$config['host'] = "127.0.0.1"; // Change the host to 'mariadb' if you are using docker compose.
$config['port'] = "3306"; // Make sure there is no service on port 3306 if you use docker compose.
$config['host'] = "127.0.0.1";
$config['port'] = "3306";
$config['user'] = "root";
$config['password'] = "";
$config['database'] = "backend_assim";
$config['password'] = "";
2 changes: 1 addition & 1 deletion backend/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

error_reporting(0); // SET 0 FOR PROD, SET -1 FOR DEVELOPEMENT
date_default_timezone_set("America/Sao_Paulo"); // DEFAULT CONFIG HOURS
Migration::auto("14122023_1834.sql");
Migration::auto("15122023_1834.sql");
19 changes: 19 additions & 0 deletions frontend/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ICargo from "@/types/Cargo"
import IFuncionario from "@/types/Funcionario";
import IRelatorio from "@/types/Relatorio";

const baseUrl = "http://localhost:8000/api"

Expand Down Expand Up @@ -119,3 +120,21 @@ export const deleteFuncionario = async (id: number): Promise<void> => {

res;
}

export const getAllRelatorios = async (): Promise<IRelatorio[]> => {
const request = await fetch(`${baseUrl}/relatorios`, {
cache: "no-cache",
mode: "no-cors",
});
const relatorios = request.json();
return relatorios;
}

export const getRelatorioByNomeCargo = async (params: string | number): Promise<IRelatorio[]> => {
const res = await fetch(`${baseUrl}/relatorios/${params}`, {
cache: "no-cache",
mode: "no-cors",
});

return await res.json() as IRelatorio[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Modal from "@/components/Modal";
import ICargo from "@/types/Cargo";
import React, { FormEventHandler, useState } from "react";
import { deleteCargo } from "../../../../api";
import { deleteCargo, deleteFuncionario } from "../../../../api";
import { useRouter } from "next/navigation";
import IFuncionario from "@/types/Funcionario";

Expand All @@ -23,7 +23,7 @@ const ButtonDeleteFuncionario = (props: IButtonDeleteFuncionario) => {
let error = document.getElementById('error');
return error!.innerHTML = "Nome inválido."
}
await deleteCargo(props.funcionario.id!);
await deleteFuncionario(props.funcionario.id!);
setModal(false)
router.push("/funcionarios");
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function RootLayout({
}) {
return (
<html lang="pt-BR">
<body className={inter.className}>{children}</body>
<body className={`${inter.className} h-screen`}>{children}</body>
</html>
)
}
2 changes: 1 addition & 1 deletion frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Header from "@/components/Header";
function Main() {
return (
<div>
<Header/>
<Header title="Início"/>
</div>
);
}
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/app/relatorios/buscar/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import Header from "@/components/Header";

import Link from "next/link";
import ListRelatoriosByNomeCargo from "../../components/ListRelatoriosByNomeCargo";
import ButtonSearchRelatorio from "../../components/ButtonSearchRelatorio";
const BuscarFuncionarios = async ({ params }: { params: { slug: string } }) => {
return (
<>
<Header title={`Busca de cargo ${params.slug}`}/>
<div className="p-1 mt-1 text-2xl flex items-start justify-between w-full">
<b>Relatórios</b>
<div>
<ButtonSearchRelatorio />
</div>
</div>
<ListRelatoriosByNomeCargo params={params} />
</>
);
}

export default BuscarFuncionarios;
54 changes: 54 additions & 0 deletions frontend/src/app/relatorios/components/ButtonSearchRelatorio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use client'

import Modal from "@/components/Modal";
import ICargo from "@/types/Cargo";
import React, { FormEventHandler, useState } from "react";
import { useRouter } from "next/navigation";

interface IButtonSearchRelatorio {

}

const ButtonSearchRelatorio = (props: IButtonSearchRelatorio) => {
const [modal, setModal] = useState(false);
const [nomeCargoFuncionario, setNomeCargoFuncionario] = useState('');

const router = useRouter();

const handlerSearchFuncionario: FormEventHandler<HTMLFormElement> = async (e) => {
e.preventDefault();
router.push(`/relatorios/buscar/${nomeCargoFuncionario}`);
}

const handlerModalOpen = () => {
setModal(true)
}
return (
<>
<button
className="btn btn-neutral"
onClick={handlerModalOpen}
>
Pesquisar
</button>

<Modal open={modal} close={() => setModal(false)} title="Pesquisar">
<div id="error" className="modal-open"></div>
<form onSubmit={handlerSearchFuncionario}>
<div className="modal-action">
<input
className="input input-bordered w-full"
name="nome"
placeholder="Digite cargo ou nome do funcionário"
value={nomeCargoFuncionario}
onChange={(e) => setNomeCargoFuncionario(e.target.value)}
/>
<button type="submit" className="btn">Pesquisar</button>
</div>
</form>
</Modal>
</>
);
}

export default ButtonSearchRelatorio;
35 changes: 35 additions & 0 deletions frontend/src/app/relatorios/components/ListRelatorios.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getAllRelatorios} from "../../../../api";


const ListRelatorios = async () => {
const relatorios = await getAllRelatorios();

return (
<div>
<table className="table-auto w-full shadow-md mt-5 rounded border-separate border-spacing-y-3">
<thead className="text-left tracking-wider">
<tr>
<th className="p-4">Nome do funcionário</th>
<th className="p-4">Telefone</th>
<th className="p-4">Cargo</th>
<th className="p-4">Salario</th>
</tr>
</thead>
<tbody className="">
{relatorios.map(relatorios => (
<tr key={relatorios.funcionario_id} className="bg-card rounded">
<td className="p-4"><a href={`/funcionarios/${relatorios.funcionario_id}`}>{relatorios.nome_funcionario}</a></td>
<td className="p-4">{relatorios.telefone}</td>
<td className="p-4"><a href={`/cargos/${relatorios.cargo_id}`}>{relatorios.nome_cargo}</a></td>
<td className="p-4">{relatorios.salario}</td>

</tr>
))}

</tbody>
</table>
</div>
);
}

export default ListRelatorios;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getCargoById, getFuncionarioByNome, getRelatorioByNomeCargo } from "../../../../api";

const ListRelatoriosByNomeCargo = async ({ params }: { params: { slug: string } }) => {
const relatorios = await getRelatorioByNomeCargo(params.slug);

return (
<div>
<table className="table-auto w-full shadow-md mt-5 rounded border-separate border-spacing-y-3">
<thead className="text-left tracking-wider">
<tr>
<th className="p-4">Nome do funcionário</th>
<th className="p-4">Telefone</th>
<th className="p-4">Cargo</th>
<th className="p-4">Salario</th>
</tr>
</thead>
<tbody className="">
{relatorios.map(relatorios => (
<tr key={relatorios.funcionario_id} className="bg-card rounded">
<td className="p-4"><a href={`/funcionarios/${relatorios.funcionario_id}`}>{relatorios.nome_funcionario}</a></td>
<td className="p-4">{relatorios.telefone}</td>
<td className="p-4"><a href={`/cargos/${relatorios.cargo_id}`}>{relatorios.nome_cargo}</a></td>
<td className="p-4">{relatorios.salario}</td>

</tr>
))}

</tbody>
</table>
</div>
);
}

export default ListRelatoriosByNomeCargo;
23 changes: 23 additions & 0 deletions frontend/src/app/relatorios/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Header from "@/components/Header";
import React from "react";
import Link from "next/link";
import ButtonSearchRelatorio from "./components/ButtonSearchRelatorio";
import ListRelatorios from "./components/ListRelatorios";

function RelatoriosPage() {
return (
<div>
<Header title="Relatório"/>
<div className="p-1 mt-1 text-2xl flex items-start justify-between w-full">
<b>Relatório</b>
<div>
<ButtonSearchRelatorio/>
</div>
</div>
<ListRelatorios/>
</div>
);
}


export default RelatoriosPage;
1 change: 1 addition & 0 deletions frontend/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Header = (props: IHeader) => {
<a href="/" className="mr-5">Início</a>
<a href="/cargos" className="mr-5">Cargos</a>
<a href="/funcionarios" className="mr-5">Funcionários</a>
<a href="/relatorios" className="mr-5">Relatórios</a>
</div>
</header>
);
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/types/Relatorio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interface IRelatorio {
funcionario_id: number,
cargo_id: number,
nome_funcionario: string,
telefone: string,
nome_cargo: string,
salario: string,
}

export default IRelatorio;

0 comments on commit b39dffb

Please sign in to comment.