Skip to content

Commit 2e90ae7

Browse files
committed
Added REST API
1 parent 2cea655 commit 2e90ae7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.MD

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ Simply pastebin coded for education purposes. It looks like it works.
1010

1111
## TODO
1212
- Avatars
13+
- REST API endpoint for creating pastes
1314

1415
![Screenshot](/screenshot.png?raw=true)

src/Controller/RestController.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use App\Repository\PasteRepository;
6+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\Routing\Annotation\Route;
10+
11+
/**
12+
* @Route("/api")
13+
*/
14+
class RestController extends AbstractController {
15+
16+
/**
17+
* @Route("/read")
18+
* @Method("GET")
19+
*/
20+
public function read(Request $request, PasteRepository $pasteRepository) {
21+
$name = $request->get('name');
22+
if ($name == null) {
23+
return $this->json([
24+
'error' => 1,
25+
'message' => 'Missing name parameter'
26+
], 400);
27+
}
28+
29+
$paste = $pasteRepository->findOneByName($name);
30+
if ($paste == null) {
31+
return $this->json([
32+
'error' => 2,
33+
'message' => 'Paste does not exists'
34+
]);
35+
}
36+
37+
return $this->json($paste);
38+
}
39+
40+
/**
41+
* @Route("/last")
42+
* @Method("GET")
43+
*/
44+
public function last(PasteRepository $pasteRepository) {
45+
return $this->json($pasteRepository->getSidebar());
46+
}
47+
}

0 commit comments

Comments
 (0)