Skip to content

Commit 14bd55e

Browse files
author
Bruno Marcos Keil
committed
exercicio
0 parents  commit 14bd55e

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

CRUD.html

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="pt-BR">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="styles.css">
8+
<title>CRUD de Animais</title>
9+
</head>
10+
11+
<body>
12+
<div class="container">
13+
<h1>CRUD de Animais</h1>
14+
15+
<form id="animal_form">
16+
<label for="animal_name">Nome do Animal:</label>
17+
<input type="text" id="animal_name" required>
18+
<button type="submit">Adicionar</button>
19+
</form>
20+
21+
<table>
22+
<thead>
23+
<tr>
24+
<th>ID</th>
25+
<th>Nome do Animal</th>
26+
<th>Ações</th>
27+
</tr>
28+
</thead>
29+
<tbody id="animal_list">
30+
</tbody>
31+
</table>
32+
</div>
33+
34+
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
35+
<script src="CRUD.js"></script>
36+
</body>
37+
38+
</html>

CRUD.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
$(document).ready(function () {
2+
listAnimals();
3+
4+
$('#animal_form').submit(function (e) {
5+
e.preventDefault();
6+
const animalName = $('#animal_name').val();
7+
8+
$.post('http://cafepradev.com.br:21020/animals/insert', { animal: animalName }, function () {
9+
$('#animal_name').val('');
10+
listAnimals();
11+
});
12+
});
13+
14+
function listAnimals() {
15+
$.get('http://cafepradev.com.br:21020/animals/list', function (data) {
16+
const animalList = $('#animal_list');
17+
animalList.empty();
18+
19+
data.forEach(function (animal) {
20+
animalList.append(`
21+
<tr>
22+
<td>${animal.id}</td>
23+
<td>${animal.animal}</td>
24+
<td>
25+
<button class="edit-btn" data-id="${animal.id}">Editar</button>
26+
<button class="delete-btn" data-id="${animal.id}">Deletar</button>
27+
</td>
28+
</tr>
29+
`);
30+
});
31+
32+
$('.edit-btn').click(function () {
33+
const animalId = $(this).data('id');
34+
editAnimal(animalId);
35+
});
36+
37+
$('.delete-btn').click(function () {
38+
const animalId = $(this).data('id');
39+
deleteAnimal(animalId);
40+
});
41+
});
42+
}
43+
44+
function editAnimal(id) {
45+
const newAnimalName = prompt('Editar Nome do Animal:');
46+
if (newAnimalName !== null) {
47+
$.ajax({
48+
url: `http://cafepradev.com.br:21020/animals/update/${id}`,
49+
type: 'PUT',
50+
data: { animal: newAnimalName },
51+
success: listAnimals,
52+
});
53+
}
54+
}
55+
56+
function deleteAnimal(id) {
57+
if (confirm('Tem certeza que deseja deletar este animal?')) {
58+
$.ajax({
59+
url: `http://cafepradev.com.br:21020/animals/delete/${id}`,
60+
type: 'DELETE',
61+
success: listAnimals,
62+
});
63+
}
64+
}
65+
});

styles.css

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
}
4+
5+
.container {
6+
max-width: 600px;
7+
margin: 0 auto;
8+
padding: 20px;
9+
text-align: center;
10+
}
11+
12+
table {
13+
width: 100%;
14+
margin-top: 20px;
15+
}
16+
17+
table th {
18+
background-color: #f2f2f2;
19+
padding: 10px;
20+
}
21+
22+
table td {
23+
border-bottom: 1px solid #ccc;
24+
padding: 10px;
25+
}
26+
27+
form {
28+
margin: 20px 0;
29+
}
30+
31+
form label {
32+
font-weight: bold;
33+
}
34+
35+
form input[type="text"] {
36+
width: 100%;
37+
padding: 10px;
38+
margin-top: 5px;
39+
}
40+
41+
form button {
42+
display: block;
43+
margin-top: 10px;
44+
background-color: #007BFF;
45+
color: #fff;
46+
padding: 10px;
47+
border: none;
48+
cursor: pointer;
49+
}
50+
51+
form button:hover {
52+
background-color: #0056b3;
53+
}

0 commit comments

Comments
 (0)