Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PC-1 Chats endpoints #2

Merged
merged 55 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
c557461
PC-1 Add chats & chatsParticipants sql script
itmaxxx Jan 9, 2022
97c9adb
PC-1 Seed chats fixtures
itmaxxx Jan 9, 2022
d823338
PC-1 Fix db drop
itmaxxx Jan 14, 2022
53bc4fc
PC-1 Improve db seeding by removing hard-coded columns arg
itmaxxx Jan 14, 2022
a86ec51
PC-1 Add chat participants fixtures
itmaxxx Jan 14, 2022
9f99e36
PC-1 Seed chat participants fixtures in db
itmaxxx Jan 14, 2022
9d23151
PC-1 Init chats controller and service
itmaxxx Jan 14, 2022
897c452
PC-1 Add create chat service
itmaxxx Jan 15, 2022
315e2c7
PC-1 Add routes for: create chat, get chats, find chat by id
itmaxxx Jan 16, 2022
36cc864
PC-1 Add services for: create chat, get chats, find chat by id
itmaxxx Jan 16, 2022
6bbb927
PC-1 Replace hard coded messages with shared messages in user controller
itmaxxx Jan 16, 2022
c617fee
PC-1 Fix isPrivate in chats fixtures
itmaxxx Jan 18, 2022
0f12e4e
PC-1 Add new chat fixture
itmaxxx Jan 18, 2022
d0becf0
PC-1 Add new user fixture
itmaxxx Jan 18, 2022
16bdbbc
PC-1 Add chatsController to appController, add get chat by id route
itmaxxx Jan 18, 2022
4dcdd44
PC-1 Add get chat by id tests for TDD
itmaxxx Jan 18, 2022
1cb725f
PC-1 Change Fk to Id in chat participants table and fixtures
itmaxxx Jan 18, 2022
cd632af
PC-1 Check if user has permission to get chat info, require auth to g…
itmaxxx Jan 18, 2022
223d510
PC-1 Add chat RO to service
itmaxxx Jan 18, 2022
b3b0a67
PC-1 Add deleted chat
itmaxxx Jan 18, 2022
5d06e9c
PC-1 Add tests for create and delete chats TDD
itmaxxx Jan 18, 2022
5e9fa1f
PC-1 Add isDeleted field to Chat model, fix deleted chat test id
itmaxxx Jan 18, 2022
13804fd
PC-1 Init regex for /users/:userId/chats
itmaxxx Jan 19, 2022
545768c
PC-1 Rename get current user chats route, init controller method for …
itmaxxx Jan 19, 2022
3890dd0
PC-1 Add test for current user chats TDD
itmaxxx Jan 19, 2022
b5e973d
PC-1 Add chat participants to Max and Matvey chat
itmaxxx Jan 19, 2022
560c4f7
PC-1 Ignore deleted chats when getting chat by id
itmaxxx Jan 19, 2022
fd1cfbf
PC-1 Add test for permission to delete chat TDD
itmaxxx Jan 19, 2022
9be131b
PC-1 Get user chats
itmaxxx Jan 21, 2022
35378b9
PC-1 Add create chat route
itmaxxx Jan 21, 2022
6717686
PC-1 Fix setting headers for request with json data and authorization
itmaxxx Jan 21, 2022
b84af29
PC-1 Generate and use random id's
itmaxxx Jan 21, 2022
ad7beea
PC-1 Require authorization to create chat
itmaxxx Jan 21, 2022
1765975
PC-1 Add user who created chat to chat participants
itmaxxx Jan 21, 2022
4b525ee
PC-1 Add delete chat route, controller and service
itmaxxx Jan 21, 2022
dd899a8
PC-1 Fix delete chat tests
itmaxxx Jan 21, 2022
a8917c5
PC-1 Improve request
itmaxxx Jan 21, 2022
65f6e29
PC-1 Add getParticipantByUserId method to chats service
itmaxxx Jan 21, 2022
88f6cae
PC-1 Check if user has enough permission to delete chat, and other ch…
itmaxxx Jan 21, 2022
2ae808b
PC-1 Fix delete chat endpoints tests
itmaxxx Jan 21, 2022
1b17f92
PC-1 Add tests for delete chat participant TDD
itmaxxx Jan 21, 2022
09cb36b
PC-1 Add tests for get chat participants TDD
itmaxxx Jan 21, 2022
641b880
PC-1 Add test for case when not authorized user trying to create chat
itmaxxx Jan 21, 2022
71ba970
PC-1 Add tests for add participant to chat endpoints TDD
itmaxxx Jan 22, 2022
3350add
PC-1 Add delete chat participant route
itmaxxx Jan 22, 2022
a1b7611
PC-1 Fix delete chat participant endpoints tests
itmaxxx Jan 22, 2022
bd5765c
PC-1 Add delete chat participant controller and service
itmaxxx Jan 22, 2022
0ed9447
PC-1 Create add user to chat route
itmaxxx Jan 22, 2022
93caafb
PC-1 Add new chat participants fixtures and fix users tests
itmaxxx Jan 22, 2022
0272cad
PC-1 Create new add user to chat endpoint tests and fix existing
itmaxxx Jan 22, 2022
4aa8389
PC-1 Create add user to chat controller
itmaxxx Jan 22, 2022
3df00c8
PC-1 Create get chat participants route
itmaxxx Jan 22, 2022
2cb4103
PC-1 Create get chat participants controller
itmaxxx Jan 22, 2022
1d08789
PC-1 Create get chat participants service
itmaxxx Jan 22, 2022
3c09850
PC-1 Improve and fix chats tests
itmaxxx Jan 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 68 additions & 10 deletions app/app.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@include_once "./utils/request.php";
@include_once "./users/users.controller.php";
@include_once "./db/db.controller.php";
@include_once "./chats/chats.controller.php";
@include_once "./auth/auth.controller.php";
@include_once "./guards/jwtAuthGuard.php";

Expand All @@ -20,6 +21,7 @@ class AppController
# Controllers
private $dbController;
private $usersController;
private $chatsController;
private $authController;
# Guards
private $jwtAuthGuard;
Expand All @@ -36,6 +38,7 @@ function __construct($dbConfig)
# Initialize controllers
$this->usersController = new UsersController($this->conn);
$this->authController = new AuthController($this->conn);
$this->chatsController = new ChatsController($this->conn);

# Initialize guards
$this->jwtAuthGuard = new JwtAuthGuard(new UsersService($this->conn));
Expand All @@ -55,22 +58,49 @@ private function setHeaders()

private function router()
{
switch ($this->req['method']) {
switch ($this->req['method'])
{
case 'GET':
if ($this->req['resource'] === '/api/users/me') {
if ($this->req['resource'] === '/api/users/me')
{
// When we require authorization and want to get user in controller
$this->_req->useGuard($this->jwtAuthGuard);
// We need to call getRequest() method in original request method
$this->usersController->getMe($this->_req->getRequest());
return;
}
// /users/:userId
if (strpos($this->req['resource'], '/api/users/') === 0) {
# /users/me/chats
if ($this->req['resource'] === '/api/users/me/chats')
{
$this->_req->useGuard($this->jwtAuthGuard);
$this->usersController->getUserChats($this->_req->getRequest());
return;
}
# /users/:userId
if (strpos($this->req['resource'], '/api/users/') === 0)
{
$this->usersController->getUserById($this->req);
return;
}
if ($this->req['resource'] === '/api/users') {
if ($this->req['resource'] === '/api/users')
{
$this->usersController->getUsers();
return;
}
# /chats/:chatId/users
if (preg_match("/\/api\/chats\/(?'chatId'[a-z0-9]+)\/users/", $this->req['resource']))
{
$this->_req->useGuard($this->jwtAuthGuard);
$this->chatsController->getChatParticipants($this->_req->getRequest());
return;
}
# /chats/:chatId
if (strpos($this->req['resource'], '/api/chats/') === 0)
{
$this->_req->useGuard($this->jwtAuthGuard);
$this->chatsController->getChatById($this->_req->getRequest());
return;
}

httpException("Route not found " . $this->req['resource'], 404)['end']();
logMessage("Route not found $this->req");
Expand All @@ -79,23 +109,51 @@ private function router()

case 'POST':
$reqBody = $this->_req->parseBody();

if ($this->req['resource'] === '/api/users') {
$this->usersController->createUser($reqBody["data"]);

if (preg_match("/\/api\/chats\/(?'chatId'[a-z0-9]+)\/users/", $this->req['resource']))
{
$this->_req->useGuard($this->jwtAuthGuard);
$this->chatsController->addUserToChat($this->_req->getRequest(), $reqBody["data"]);
return;
}
if ($this->req['resource'] === '/api/chats')
{
$this->_req->useGuard($this->jwtAuthGuard);
$this->chatsController->createChat($this->_req->getRequest(), $reqBody["data"]);
return;
}
if ($this->req['resource'] === '/api/auth/sign-up') {
if ($this->req['resource'] === '/api/auth/sign-up')
{
$this->authController->signUp($reqBody["data"]);
return;
}
if ($this->req['resource'] === '/api/auth/sign-in') {
if ($this->req['resource'] === '/api/auth/sign-in')
{
$this->authController->signIn($reqBody["data"]);
return;
}

httpException("Route not found", 404)['end']();

break;

case 'DELETE':
$reqBody = $this->_req->parseBody();

if (preg_match("/\/api\/chats\/(?'chatId'[a-z0-9]+)\/users\/(?'userId'[a-z0-9]+)/", $this->req['resource']))
{
$this->_req->useGuard($this->jwtAuthGuard);
$this->chatsController->deleteChatParticipant($this->_req->getRequest());
return;
}
if (strpos($this->req['resource'], '/api/chats/') === 0)
{
$this->_req->useGuard($this->jwtAuthGuard);
$this->chatsController->deleteChat($this->_req->getRequest());
return;
}

break;

default:
httpException("Method not supported", 404)['end']();
Expand Down
6 changes: 2 additions & 4 deletions auth/auth.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

@include_once __DIR__ . "/../utils/httpException.php";
@include_once __DIR__ . "/../utils/jsonResponse.php";
@include_once __DIR__ . "/auth.service.php";
@include_once __DIR__ . "/../utils/jwt.php";
@include_once __DIR__ . "/../locale/en/messages.php";
@include_once __DIR__ . "/../utils/randomId.php";

class AuthController
{
private $authService;
private $usersService;

function __construct($conn)
{
$this->authService = new AuthService($conn);
$this->usersService = new UsersService($conn);
}

Expand All @@ -30,7 +28,7 @@ function signUp($registerUserDto): array
httpException($messages["username_taken"])['end']();
}

$id = random_int(0, 9999999);
$id = randomId();

try {
$this->usersService->createUser($id, $registerUserDto["username"], $registerUserDto["password"]);
Expand Down
11 changes: 0 additions & 11 deletions auth/auth.service.php

This file was deleted.

Loading