Backend
This commit is contained in:
70
Backend/campaigns.php
Normal file
70
Backend/campaigns.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
require "db.php";
|
||||
//Felix
|
||||
|
||||
function user_id_from_username($u){
|
||||
$st=db()->prepare("SELECT user_id FROM users WHERE username=?");
|
||||
$st->execute([$u]);
|
||||
$r=$st->fetch();
|
||||
return $r ? (int)$r['user_id'] : 0;
|
||||
}
|
||||
|
||||
$action = $_GET['action'] ?? '';
|
||||
|
||||
if ($action==='get_for_owner') {
|
||||
$uname = $_GET['owner_username'] ?? '';
|
||||
if ($uname==='') bad('owner_username required');
|
||||
|
||||
$st = db()->prepare("SELECT c.campaign_id, c.title, c.description
|
||||
FROM campaigns c
|
||||
JOIN users u ON u.user_id=c.owner_user_id
|
||||
WHERE u.username=? LIMIT 1");
|
||||
$st->execute([$uname]);
|
||||
$row = $st->fetch();
|
||||
if (!$row) bad('not_found',404);
|
||||
ok($row);
|
||||
}
|
||||
|
||||
if ($action==='ensure_for_owner') {
|
||||
$b = body(); $uname = $b['owner_username'] ?? '';
|
||||
if ($uname==='') bad('owner_username required');
|
||||
$uid = user_id_from_username($uname);
|
||||
if ($uid<=0) bad('user_not_found',404);
|
||||
|
||||
$st = db()->prepare("SELECT campaign_id, title, description FROM campaigns WHERE owner_user_id=? LIMIT 1");
|
||||
$st->execute([$uid]);
|
||||
$row = $st->fetch();
|
||||
if ($row) ok($row);
|
||||
|
||||
$ins = db()->prepare("INSERT INTO campaigns(owner_user_id,title,description) VALUES(?,?,?)");
|
||||
$ins->execute([$uid,'Meine Kampagne',null]);
|
||||
ok(['campaign_id'=>(int)db()->lastInsertId(),'title'=>'Meine Kampagne','description'=>null]);
|
||||
}
|
||||
|
||||
if ($action==='update_for_owner') {
|
||||
$b = body();
|
||||
$uname = $b['owner_username'] ?? '';
|
||||
$title = trim($b['title'] ?? '');
|
||||
$desc = $b['description'] ?? null;
|
||||
if ($uname==='') bad('owner_username required');
|
||||
if ($title==='') bad('title required');
|
||||
|
||||
$uid = user_id_from_username($uname);
|
||||
if ($uid<=0) bad('user_not_found',404);
|
||||
|
||||
$st = db()->prepare("SELECT campaign_id FROM campaigns WHERE owner_user_id=? LIMIT 1");
|
||||
$st->execute([$uid]);
|
||||
$row = $st->fetch();
|
||||
|
||||
if ($row) {
|
||||
$upd = db()->prepare("UPDATE campaigns SET title=?, description=? WHERE campaign_id=?");
|
||||
$upd->execute([$title,$desc,(int)$row['campaign_id']]);
|
||||
ok(['campaign_id'=>(int)$row['campaign_id']]);
|
||||
} else {
|
||||
$ins = db()->prepare("INSERT INTO campaigns(owner_user_id,title,description) VALUES(?,?,?)");
|
||||
$ins->execute([$uid,$title,$desc]);
|
||||
ok(['campaign_id'=>(int)db()->lastInsertId()]);
|
||||
}
|
||||
}
|
||||
|
||||
http_response_code(404); echo json_encode(['ok'=>false,'error'=>'unknown action']);
|
Reference in New Issue
Block a user