Skip to content

Commit 55e4938

Browse files
authored
Merge pull request #22 from bruna9165/feat-bruna
Feat bruna
2 parents 669e479 + 13102c0 commit 55e4938

File tree

6 files changed

+252
-31
lines changed

6 files changed

+252
-31
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
include_once __DIR__ . "/../db/db.php";
3+
4+
class UserController{
5+
private $conn;
6+
7+
8+
public function __construct()
9+
{
10+
$objDb = new Bd();
11+
$this->conn = $objDb->connect();
12+
13+
}
14+
15+
public function GetAllUser(){
16+
try {
17+
$sql = "SELECT * FROM usuario";
18+
$db = $this->conn->prepare($sql);
19+
$db->execute();
20+
$user = $db->fetchAll(PDO::FETCH_ASSOC);
21+
return $user;
22+
} catch (\Exception $th) {
23+
return $th->getMessage();
24+
}
25+
}
26+
27+
public function CriarUsuario($nome, $email, $cpf, $dataNascimento){
28+
try {
29+
$sql = "INSERT INTO usuario (nome, email, cpf, dataNascimento) VALUES(:nome, :email, :cpf, :dataNascimento)";
30+
$db = $this->conn->prepare($sql);
31+
$db->bindParam(":nome", $nome);
32+
$db->bindParam(":email", $email);
33+
$db->bindParam(":cpf", $cpf);
34+
$db->bindParam(":dataNascimento", $dataNascimento);
35+
// $db->bindParam(":senha", $senha);
36+
if($db->execute()){
37+
return true;
38+
}else{
39+
return false;
40+
}
41+
} catch (\Exception $th) {
42+
//throw $th;
43+
}
44+
}
45+
46+
47+
48+
}
49+
50+
?>

backend/db/db.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
11
<?php
2-
// Configurações do banco de dados
3-
$host = 'localhost';
4-
$dbname = 'cassinoDesktop';
5-
$username = 'root';
6-
$password = '';
2+
// $host = 'localhost';
3+
// $dbname = 'cassinoDesktop';
4+
// $username = 'root';
5+
// $password = '';
76

8-
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
7+
// $dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
98

10-
try {
11-
$pdo = new PDO($dsn, $username, $password);
9+
// try {
10+
// $pdo = new PDO($dsn, $username, $password);
1211

13-
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
12+
// $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1413

15-
echo "Conexão bem-sucedida!";
16-
} catch (PDOException $e) {
17-
echo "Erro na conexão: " . $e->getMessage();
14+
// echo "Conexão bem-sucedida!";
15+
// } catch (PDOException $e) {
16+
// echo "Erro na conexão: " . $e->getMessage();
17+
// }
18+
19+
class Bd
20+
{
21+
private $host = 'localhost';
22+
private $dbname = 'cassinoDesktop';
23+
private $username = 'root';
24+
private $password = '';
25+
26+
public function connect()
27+
{
28+
$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbname . ";charset=utf8mb4";
29+
30+
try {
31+
$pdo = new PDO($dsn, $this->username, $this->password);
32+
33+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
34+
35+
return $pdo;
36+
} catch (PDOException $e) {
37+
echo "Erro na conexão: " . $e->getMessage();
38+
return null;
39+
}
40+
}
1841
}
42+
43+
1944
?>

backend/router/userRouter.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
header("Access-Control-Allow-Origin: *");
4+
5+
// Permite os métodos que seu frontend vai usar
6+
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
7+
8+
// Permite headers personalizados (como Content-Type, Authorization, etc.)
9+
header("Access-Control-Allow-Headers: Content-Type, Authorization");
10+
11+
require_once __DIR__ . "/../controller/userController.php";
12+
$userController = new UserController();
13+
14+
if($_SERVER["REQUEST_METHOD"] == "POST"){
15+
switch ($_GET["acao"]){
16+
case 'cadastrar':
17+
$valores = json_decode(file_get_contents("php://input"), true);
18+
19+
$nome = $valores["nome"];
20+
$email = $valores["email"];
21+
$cpf = $valores["cpf"];
22+
$dataNascimento = $valores["dataNascimento"];
23+
// $senha = $_POST["senha"];
24+
25+
if(!(empty($nome) || empty($email) || empty($cpf) || empty($dataNascimento))){
26+
$resposta = $userController->CriarUsuario($nome,$email, $cpf, $dataNascimento) ;
27+
echo $resposta;
28+
// if($resposta){
29+
// header("Location: ../../src/pages/login/index.tsx");
30+
// }
31+
}
32+
// break;
33+
}
34+
}
35+
?>

src/api/api.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export async function Cadastro(nome: string, email: string, cpf: string, dataNascimento: string,){
2+
3+
const resposta = await fetch("http://localhost/cassino/backend/router/userRouter.php?acao=cadastrar", {
4+
method: "POST",
5+
headers: {
6+
"Content-Type": "application/json"
7+
},
8+
body: JSON.stringify({nome: nome, email: email, cpf: cpf, dataNascimento: dataNascimento})
9+
})
10+
const dados = await resposta.json()
11+
return dados
12+
13+
}

src/components/teste/teste.tsx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// src/components/UserList.tsx
2+
import React, { useState, useEffect } from 'react';
3+
4+
interface User {
5+
id: number;
6+
name: string;
7+
}
8+
9+
const UserList: React.FC = () => {
10+
const [users, setUsers] = useState<User[]>([]);
11+
const [loading, setLoading] = useState<boolean>(true);
12+
const [error, setError] = useState<string | null>(null);
13+
14+
const apiUrl = 'http://localhost/index.php'; // Ajuste para a URL do seu back-end
15+
16+
// Função para buscar usuários
17+
const fetchUsers = async () => {
18+
try {
19+
const response = await fetch(apiUrl);
20+
if (!response.ok) throw new Error('Erro ao buscar usuários');
21+
const data: User[] = await response.json();
22+
setUsers(data);
23+
} catch (err) {
24+
setError('Erro ao carregar usuários');
25+
} finally {
26+
setLoading(false);
27+
}
28+
};
29+
30+
// Função para adicionar usuário
31+
const addUser = async () => {
32+
const name = prompt('Digite o nome do usuário:');
33+
if (name) {
34+
try {
35+
const response = await fetch(apiUrl, {
36+
method: 'POST',
37+
headers: { 'Content-Type': 'application/json' },
38+
body: JSON.stringify({ name }),
39+
});
40+
if (!response.ok) throw new Error('Erro ao adicionar usuário');
41+
fetchUsers(); // Atualiza a lista após adicionar
42+
} catch (err) {
43+
setError('Erro ao adicionar usuário');
44+
}
45+
}
46+
};
47+
48+
useEffect(() => {
49+
fetchUsers();
50+
}, []);
51+
52+
if (loading) return <div>Carregando...</div>;
53+
if (error) return <div>{error}</div>;
54+
55+
return (
56+
<div>
57+
<h1>Usuários</h1>
58+
<ul>
59+
{users.map(user => (
60+
<li key={user.id}>{user.id}: {user.name}</li>
61+
))}
62+
</ul>
63+
<button onClick={addUser}>Adicionar Usuário</button>
64+
</div>
65+
);
66+
};
67+
68+
export default UserList;

src/pages/register/index.tsx

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
import { Button } from "@/components/ui/button";
2-
import { Link } from "react-router";
2+
import { Link, data } from "react-router";
33
import { Input } from "@/components/ui/input";
44
import { Heading } from "@/components/ui/heading";
55
import { Lock, Mail, UserRound } from "lucide-react";
6-
6+
import { useState } from "react";
7+
import { Cadastro } from "@/api/api";
8+
79
export function Register() {
10+
const [nome, setNome] = useState("");
11+
const [email, setEmail] = useState("");
12+
const [cpf, setCpf] = useState("");
13+
const [dataNascimento, setDataNascimento] = useState("");
14+
const [senha, setSenha] = useState("");
15+
16+
17+
18+
19+
async function submit(event: any) {
20+
event.preventDefault();
21+
console.log(nome)
22+
const resultado = await Cadastro(nome,email,cpf,dataNascimento)
23+
console.log(resultado)
24+
}
25+
// const cadastro = Cadastro(nome, email, cpf, dataNascimento)
826
return (
927
<>
1028
<style>
@@ -35,7 +53,7 @@ export function Register() {
3553
}
3654
`}
3755
</style>
38-
56+
3957
<div className="flex items-center bg-gradient-to-b from-[#07080D] to-[#1D1F2C] w-screen h-[100vh] animate-fadeUp p-0 m-0">
4058
<div className="w-1/2 flex justify-center items-center">
4159
<img src="../../../banner.png" alt="banner" />
@@ -45,40 +63,52 @@ export function Register() {
4563
<Heading className="flex w-full font-bold text-white animate-fadeUp">
4664
Criar Conta
4765
</Heading>
48-
<Input
49-
type="text"
50-
label="Nome Completo"
51-
placeholder="Digite seu nome completo"
52-
iconRight={<Mail />}
53-
className="w-96"
54-
/>
55-
<Input
56-
type="email"
57-
label="Endereço de E-mail"
58-
placeholder="Digite seu e-mail"
59-
iconRight={<Mail />}
60-
className="w-96"
61-
/>
66+
<form onSubmit={submit}>
67+
<Input
68+
name="nome"
69+
onChange={(e) => setNome(e.target.value)}
70+
type="text"
71+
label="Nome Completo"
72+
placeholder="Digite seu nome completo"
73+
iconRight={<Mail />}
74+
className="w-96"
75+
/>
76+
<Input
77+
name="email"
78+
onChange={(e) => setEmail(e.target.value)}
79+
type="email"
80+
label="Endereço de E-mail"
81+
placeholder="Digite seu e-mail"
82+
iconRight={<Mail />}
83+
className="w-96"
84+
/>
6285
<Input
86+
name="cpf"
87+
onChange={(e) => setCpf(e.target.value)}
6388
type="text"
6489
label="CPF"
6590
placeholder="Digite seu CPF"
6691
iconRight={<UserRound />}
6792
className="w-96"
6893
/>
6994
<Input
95+
name="dataNascimento"
96+
onChange={(e) => setDataNascimento(e.target.value)}
7097
type="date"
7198
label="Data de Nascimento"
7299
placeholder="Digite sua data de nascimento"
73100
// iconRight={<UserRound />}
74101
className="text-slate-300 w-96 placeholder:text-slate-500"
75102
/>
76103
<div className="flex flex-col gap-2 items-center">
77-
<Link to={'/confirmar-senha'} className="w-full">
104+
{/* <Link to={'/confirmar-senha'} className="w-full">
78105
<Button type="submit" className="text-lg h-12 w-96 text-black">Cadastrar</Button>
79-
</Link>
106+
</Link> */}
107+
<Button type="submit" className="text-lg h-12 w-96 text-black">Cadastrar</Button>
80108
<p className="text-gray-400">Já tem uma conta? <Link className="text-yellow-500 hover:underline hover:text-yellow-600 no-underline" to={'/'} >Entrar</Link></p>
81109
</div>
110+
</form>
111+
82112
</div>
83113
</div>
84114
</div>

0 commit comments

Comments
 (0)