-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
141 lines (117 loc) · 5.19 KB
/
api.php
File metadata and controls
141 lines (117 loc) · 5.19 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
session_start();
header('Content-Type: application/json; charset=utf-8');
// اتصال به دیتابیس
require_once 'config.php';
// بررسی نوع درخواست
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'get_songs':
getSongs();
break;
case 'add_song':
addSong();
break;
case 'update_song':
updateSong();
break;
case 'delete_song':
deleteSong();
break;
default:
echo json_encode(['error' => 'عملیات نامعتبر']);
break;
}
// دریافت لیست آهنگها
function getSongs() {
global $pdo;
try {
$stmt = $pdo->prepare("SELECT * FROM Songs ORDER BY id DESC");
$stmt->execute();
$songs = $stmt->fetchAll();
echo json_encode(['success' => true, 'songs' => $songs]);
} catch(PDOException $e) {
echo json_encode(['error' => 'خطا در دریافت آهنگها: ' . $e->getMessage()]);
}
}
// افزودن آهنگ جدید
function addSong() {
global $pdo;
// دریافت دادههای ارسالی
$title = isset($_POST['title']) ? trim($_POST['title']) : '';
$artist = isset($_POST['artist']) ? trim($_POST['artist']) : '';
$image_url = isset($_POST['image_url']) ? trim($_POST['image_url']) : '';
$audio_url = isset($_POST['audio_url']) ? trim($_POST['audio_url']) : '';
$album = isset($_POST['album']) ? trim($_POST['album']) : '';
$genre = isset($_POST['genre']) ? trim($_POST['genre']) : '';
// اعتبارسنجی دادهها
if (empty($title) || empty($artist) || empty($audio_url)) {
echo json_encode(['error' => 'فیلدهای عنوان، خواننده و آدرس فایل صوتی الزامی هستند']);
return;
}
try {
$stmt = $pdo->prepare("INSERT INTO Songs (title, artist, image_url, audio_url, album, genre) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$title, $artist, $image_url, $audio_url, $album, $genre]);
$newId = $pdo->lastInsertId();
// دریافت اطلاعات آهنگ اضافه شده
$stmt = $pdo->prepare("SELECT * FROM Songs WHERE id = ?");
$stmt->execute([$newId]);
$song = $stmt->fetch();
echo json_encode(['success' => true, 'message' => 'آهنگ با موفقیت اضافه شد', 'song' => $song]);
} catch(PDOException $e) {
echo json_encode(['error' => 'خطا در افزودن آهنگ: ' . $e->getMessage()]);
}
}
// ویرایش آهنگ
function updateSong() {
global $pdo;
// دریافت دادههای ارسالی
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$title = isset($_POST['title']) ? trim($_POST['title']) : '';
$artist = isset($_POST['artist']) ? trim($_POST['artist']) : '';
$image_url = isset($_POST['image_url']) ? trim($_POST['image_url']) : '';
$audio_url = isset($_POST['audio_url']) ? trim($_POST['audio_url']) : '';
$album = isset($_POST['album']) ? trim($_POST['album']) : '';
$genre = isset($_POST['genre']) ? trim($_POST['genre']) : '';
// اعتبارسنجی دادهها
if ($id <= 0 || empty($title) || empty($artist) || empty($audio_url)) {
echo json_encode(['error' => 'اطلاعات ناقص است']);
return;
}
try {
$stmt = $pdo->prepare("UPDATE Songs SET title = ?, artist = ?, image_url = ?, audio_url = ?, album = ?, genre = ? WHERE id = ?");
$stmt->execute([$title, $artist, $image_url, $audio_url, $album, $genre, $id]);
if ($stmt->rowCount() > 0) {
// دریافت اطلاعات آهنگ بهروزرسانی شده
$stmt = $pdo->prepare("SELECT * FROM Songs WHERE id = ?");
$stmt->execute([$id]);
$song = $stmt->fetch();
echo json_encode(['success' => true, 'message' => 'آهنگ با موفقیت بهروزرسانی شد', 'song' => $song]);
} else {
echo json_encode(['error' => 'آهنگ مورد نظر یافت نشد']);
}
} catch(PDOException $e) {
echo json_encode(['error' => 'خطا در بهروزرسانی آهنگ: ' . $e->getMessage()]);
}
}
// حذف آهنگ
function deleteSong() {
global $pdo;
// دریافت شناسه آهنگ
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
if ($id <= 0) {
echo json_encode(['error' => 'شناسه آهنگ نامعتبر است']);
return;
}
try {
$stmt = $pdo->prepare("DELETE FROM Songs WHERE id = ?");
$stmt->execute([$id]);
if ($stmt->rowCount() > 0) {
echo json_encode(['success' => true, 'message' => 'آهنگ با موفقیت حذف شد', 'id' => $id]);
} else {
echo json_encode(['error' => 'آهنگ مورد نظر یافت نشد']);
}
} catch(PDOException $e) {
echo json_encode(['error' => 'خطا در حذف آهنگ: ' . $e->getMessage()]);
}
}