-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add database support (#86)
Co-authored-by: Romain Lanz <RomainLanz@users.noreply.github.com> # What this PR is about? Add database connection for vote ## If it's a new feature - [x] `feature` Create a PHP script to add vote endpoints with ip verification - [x] `feature` Add MYSQL database support - [x] `feature` Add vote on the front side - [ ] `feature` Add env to connect the real database - [ ] `feature` Disable vote behavior if user has already vote on the front side ### Tests - [x] I have run the tests of the project. `nx affected --target=test ` ### Clean Code - [x] I made sure the code is type safe (no any) Co-authored-by: Wiliam Traoré <will@mewo.io> Co-authored-by: bdebon <benjamin.debon@gmail.com>
- Loading branch information
1 parent
c9dcdab
commit 3369952
Showing
8 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
services: | ||
db: | ||
platform: linux/x86_64 | ||
image: mysql:8 | ||
ports: | ||
- 127.0.0.1:3306:3306 | ||
volumes: | ||
- mysql-data:/var/lib/mysql | ||
environment: | ||
- MYSQL_ALLOW_EMPTY_PASSWORD=true | ||
volumes: | ||
mysql-data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './lib/interfaces/question.interface'; | ||
export * from './lib/interfaces/choice.interface'; | ||
export * from './lib/interfaces/question.interface' | ||
export * from './lib/interfaces/choice.interface' | ||
export * from './lib/interfaces/vote.interface' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface VoteInterface { | ||
count: string | ||
position: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
header("Access-Control-Allow-Origin: *"); | ||
|
||
$pdo = new PDO("mysql:dbname=" . getenv('DB_NAME') . ";host=" . getenv('DB_HOST'), getenv('DB_USER'), getenv('DB_PASSWORD')); | ||
$method = $_SERVER['REQUEST_METHOD']; | ||
$slug = $_GET['slug'] ?? null; | ||
$dispatchCountOn = 50; | ||
$ip = $_SERVER["REMOTE_ADDR"]; | ||
|
||
if ($slug === null) { | ||
die; | ||
} | ||
|
||
if ($method === 'GET') { | ||
$query = $pdo->prepare('SELECT SUM(count) as count, position FROM votes WHERE slug = ? GROUP BY position'); | ||
$query->execute([$slug]); | ||
$result = $query->fetchAll(PDO::FETCH_ASSOC); | ||
|
||
echo json_encode($result); | ||
return; | ||
} | ||
|
||
if ($method === 'POST') { | ||
$position = $_POST['position'] ?? null; | ||
|
||
if ($position === null) { | ||
die; | ||
} | ||
|
||
$hashedIp = sha1($ip); | ||
$query = $pdo->prepare('SELECT * FROM logs WHERE ip = ?'); | ||
$query->execute([$hashedIp]); | ||
|
||
$result = $query->fetch(PDO::FETCH_ASSOC); | ||
|
||
if ($result === false) { | ||
$query = $pdo->prepare('INSERT INTO logs (ip, slugs) VALUES (?, ?);'); | ||
$query->execute([$hashedIp, json_encode([$slug])]); | ||
|
||
insertVote($pdo, $slug, $position, $dispatchCountOn); | ||
return; | ||
} | ||
|
||
$alreadyVotedFor = json_decode($result['slugs'], true); | ||
$found = in_array($slug, $alreadyVotedFor); | ||
|
||
if (!$found) { | ||
array_push($alreadyVotedFor, $slug); | ||
|
||
insertVote($pdo, $slug, $position, $dispatchCountOn); | ||
$query = $pdo->prepare('UPDATE logs SET slugs = ? WHERE ip = ?;'); | ||
$query->execute([ | ||
json_encode($alreadyVotedFor), | ||
$hashedIp | ||
]); | ||
} | ||
|
||
return; | ||
} | ||
|
||
function insertVote(PDO $pdo, string $slug, int $position, int $dispatchCountOn) | ||
{ | ||
$query = $pdo->prepare('INSERT INTO votes (slug, slot, count, position) VALUES (?, RAND() * '. $dispatchCountOn .', 1, ?) ON DUPLICATE KEY UPDATE count = count + 1;'); | ||
$query->execute([$slug, $position]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE votes ( | ||
slug VARCHAR(255) NOT NULL, | ||
slot INT NOT NULL DEFAULT 0, | ||
count INT DEFAULT NULL, | ||
position INT NOT NULL, | ||
UNIQUE KEY slug_slot (slug,slot,position) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
CREATE TABLE logs ( | ||
ip VARCHAR(255) NOT NULL, | ||
slugs TEXT NOT NULL | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |