forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimalController.js
More file actions
84 lines (76 loc) 路 1.82 KB
/
Copy pathanimalController.js
File metadata and controls
84 lines (76 loc) 路 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const Animal = require('../models/Animal');
// Registra un animale
exports.register = async (req, res) => {
try {
const { name, species, breed, age, owner, ownerEmail, location, imageUrl } = req.body;
if (!name || !species || !owner) {
return res.status(400).json({
success: false,
message: 'Nome, specie e proprietario sono obbligatori'
});
}
const animal = new Animal({
name,
species,
breed,
age,
owner,
ownerEmail,
location,
imageUrl,
registeredBy: req.userId
});
await animal.save();
res.status(201).json({
success: true,
message: 'Animale registrato con successo',
data: animal
});
} catch (error) {
console.error('Animal registration error:', error);
res.status(500).json({
success: false,
message: 'Errore durante la registrazione dell\'animale',
error: error.message
});
}
};
// Lista tutti gli animali
exports.getAll = async (req, res) => {
try {
const animals = await Animal.find().sort({ createdAt: -1 }).limit(100);
res.json({
success: true,
count: animals.length,
data: animals
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Errore durante il recupero degli animali',
error: error.message
});
}
};
// Dettaglio animale
exports.getOne = async (req, res) => {
try {
const animal = await Animal.findById(req.params.id);
if (!animal) {
return res.status(404).json({
success: false,
message: 'Animale non trovato'
});
}
res.json({
success: true,
data: animal
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Errore durante il recupero dell\'animale',
error: error.message
});
}
};