Skip to content

Commit

Permalink
Merge pull request #347 from hanazarraa/upstream2/rooms_management
Browse files Browse the repository at this point in the history
Upstream2/rooms management
  • Loading branch information
GhaziTriki authored Dec 27, 2022
2 parents 47a6ac4 + 3f3a04d commit 3ebd811
Show file tree
Hide file tree
Showing 16 changed files with 387 additions and 48 deletions.
2 changes: 2 additions & 0 deletions hivelvet-backend/app/config/routes.ini
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ DELETE @label_delete : /labels/@id = Actions\Labels\Delete->execut
; rooms routes
GET @rooms_index : /rooms/@user_id = Actions\Rooms\Index->show
POST @rooms_add : /rooms = Actions\Rooms\Add->save
PUT @rooms_edit : /rooms/@id = Actions\Rooms\Edit->rename
DELETE @room_delete : /rooms/@id = Actions\Rooms\Delete->execute
36 changes: 36 additions & 0 deletions hivelvet-backend/app/src/Actions/Rooms/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/*
* Hivelvet open source platform - https://riadvice.tn/
*
* Copyright (c) 2022 RIADVICE SUARL and by respective authors (see below).
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation; either version 3.0 of the License, or (at your option) any later
* version.
*
* Hivelvet is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with Hivelvet; if not, see <http://www.gnu.org/licenses/>.
*/

namespace Actions\Rooms;

use Actions\Delete as DeleteAction;
use Actions\RequirePrivilegeTrait;

/**
* Class Delete.
*/
class Delete extends DeleteAction
{
use RequirePrivilegeTrait;

protected $deleteMethodName = 'delete';
}
84 changes: 84 additions & 0 deletions hivelvet-backend/app/src/Actions/Rooms/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

/*
* Hivelvet open source platform - https://riadvice.tn/
*
* Copyright (c) 2022 RIADVICE SUARL and by respective authors (see below).
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation; either version 3.0 of the License, or (at your option) any later
* version.
*
* Hivelvet is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with Hivelvet; if not, see <http://www.gnu.org/licenses/>.
*/

namespace Actions\Rooms;

use Actions\Base as BaseAction;
use Actions\RequirePrivilegeTrait;
use Enum\ResponseCode;
use Models\Room;
use Respect\Validation\Validator;
use Validation\DataChecker;

class Edit extends BaseAction
{
use RequirePrivilegeTrait;

/**
* @param \Base $f3
* @param array $params
*/
public function rename($f3, $params): void
{
$body = $this->getDecodedBody();
$form = $body['data'];

$id = $params['id'];
$dataChecker = new DataChecker();

$dataChecker->verify($form['name'], Validator::notEmpty()->setName('name'));
$dataChecker->verify($id, Validator::notEmpty()->setName('id'));

$errorMessage = 'Room could not be updated';

$room = new Room();
$room = $room->getById($id);
if ($room->valid()) {
if ($dataChecker->allValid()) {
$checkRoom = new Room();
$room->name = $form['name'];

if ($checkRoom->nameExists($room->name, $room->user_id, $room->id)) {
$this->logger->error($errorMessage, ['error' => 'Name already exists']);
$this->renderJson(['errors' => ['name' => 'Name already exists']], ResponseCode::HTTP_PRECONDITION_FAILED);
} else {
try {
$room->save();
} catch (\Exception $e) {
$this->logger->error($errorMessage, ['error' => $e->getMessage()]);
$this->renderJson(['errors' => $e->getMessage()], ResponseCode::HTTP_INTERNAL_SERVER_ERROR);

return;
}
$this->logger->info('room successfully updated', ['room' => $room->toArray()]);
$this->renderJson(['result' => 'success', 'room' => $room->getRoomInfos($room->id)]);
}
} else {
$this->logger->error($errorMessage, ['errors' => $dataChecker->getErrors()]);
$this->renderJson(['errors' => $dataChecker->getErrors()], ResponseCode::HTTP_UNPROCESSABLE_ENTITY);
}
} else {
$this->logger->error($errorMessage);
$this->renderJson([], ResponseCode::HTTP_NOT_FOUND);
}
}
}
60 changes: 60 additions & 0 deletions hivelvet-backend/app/src/Models/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Models;

use Enum\ResponseCode;
use Models\Base as BaseModel;

/**
Expand Down Expand Up @@ -51,6 +52,20 @@ public function nameExists($name, $userId, $id = null)
return $this->load(['lower(name) = ? and user_id = ? and id != ?', mb_strtolower($name), $userId, $id]);
}

/**
* Get room record by id value.
*
* @param mixed $id
*
* @return $this
*/
public function getById($id): self
{
$this->load(['id = ?', $id]);

return $this;
}

public function collectAllByUserId($userId): array
{
return $this->db->exec('SELECT id, name, short_link, preset_id FROM rooms where user_id =?', $userId);
Expand Down Expand Up @@ -116,4 +131,49 @@ public function getLabels($room): array

return $lbs;
}

/**
* Delete a room if it's allowed and removing its associated roomlabels.
*
* @return Array[2](Array[], ResponsCode)
*/
public function delete(): array
{
// delete associated roomslabels
$result = $this->deleteRoomsLabels();

if ($result) {
try {
$this->erase();
$this->logger->info('Room successfully deleted', ['room' => $this->toArray()]);
} catch (\Exception $e) {
$this->logger->error('room could not be deleted', ['room' => $this->toArray(), 'error' => $e->getMessage()]);

throw $e;
}

return [['result' => 'success'], ResponseCode::HTTP_OK];
}

return [[], ResponseCode::HTTP_FORBIDDEN];
}

public function deleteRoomsLabels(): bool
{
$this->logger->info('Starting delete rooms labels transaction.');
$this->db->begin();
$roomId = $this->id;

$roomlabel = new RoomLabel();
$deleteResult = $roomlabel->erase(['room_id = ?', $roomId]);
if ($deleteResult) {
$this->logger->info('All Rooms Labels successfully deleted');
$this->db->commit();
$this->logger->info('Delete rooms and its associations transaction successfully commit.');

return true;
}

return false;
}
}
3 changes: 1 addition & 2 deletions hivelvet-backend/tests/src/Actions/Settings/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ final class EditTest extends Scenario
final protected const EDIT_SETTINGS_ROUTE = 'PUT /settings';
protected $group = 'Action Setting Edit';

/**
/**
* @param mixed $f3
*
* @return array
*
* @throws \ReflectionException
Expand Down
2 changes: 1 addition & 1 deletion hivelvet-frontend/src/components/AddLabelForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { DataContext } from 'lib/RoomsContext';
import labelsService from 'services/labels.service';
import Notifications from './Notifications';
import { FormInstance } from 'antd/lib/form';
import { PaginationType } from '../types/PaginationType';

type Props = {
isLogin?: boolean;
errors?: string[];
Expand Down
2 changes: 1 addition & 1 deletion hivelvet-frontend/src/components/Install.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const Install = () => {
const stepsData: formType = stepForm.getFieldsValue(true);
if (activeStep == 0) {
UsersService.collect_users(stepsData)
.then((result) => {
.then(() => {
next();
})
.catch((error) => {
Expand Down
3 changes: 2 additions & 1 deletion hivelvet-frontend/src/components/Presets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@ const Presets = () => {
//delete
const deletePreset = (id) => {
PresetsService.delete_preset(id)
.then(() => {
.then((response) => {
console.log(response);
setMyPresets(myPresets.filter((p) => p.id != id));
const indexPreset = dataContext.dataPresets.findIndex((item) => id === item.id);
if (indexPreset !== -1) {
Expand Down
1 change: 1 addition & 0 deletions hivelvet-frontend/src/components/Roles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const Roles = () => {
const getPrivileges = () => {
RolesService.list_permissions()
.then((response) => {
console.log(response);
const privileges = response.data;
if (privileges instanceof Object) {
setAllPrivileges(privileges);
Expand Down
Loading

0 comments on commit 3ebd811

Please sign in to comment.