Unverified Commit bb8b805e authored by Jerome Boucher's avatar Jerome Boucher
Browse files

feat/16378 : init crud

parent b7c5f7b1
Pipeline #11194 failed with stages
in 46 seconds
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection;
/**
* Interface for collections
*
* @package Collection
* @author Jérôme Boucher <jerome.boucher@maarch.org>
*/
interface CollectionInterface
{
/**
* Update Collection
*
* @param Collection/Collection $collection Collection Object
*
* @action Collection/Collection/update
*/
public function update(object $collection = null);
/**
* Read user collection (current user if null)
*
* @param string $accountId User Account Identifier
*
* @action Collection/Collection/readByUser
*/
public function readByUser(string $accountId = null);
}
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection\Controller;
/**
* Management of Collection
*
* @author Jérôme Boucher <jerome.boucher@maarch.org>
*/
class Collection
{
public $sdoFactory;
public $userCollectionController;
/**
* Constructor of access control class
*
* @param \dependency\sdo\Factory $sdoFactory The factory
*/
public function __construct(\dependency\sdo\Factory $sdoFactory)
{
$this->sdoFactory = $sdoFactory;
$this->userCollectionController = \laabs::newController('Collection/UserCollection');
}
public function create()
{
$collection = \laabs::cast('Collection/Collection');
$uniqId = \laabs\uniqid();
$collection->collectionId = $uniqId;
$collection->name = $uniqId;
$this->sdoFactory->create($collection, 'Collection/Collection');
$this->userCollectionController->createByUser($collection->collectionId);
return $collection;
}
// TODO
// public function createOrgCollection()
// {
// }
/**
* Update Collection
*
* @param collection/collection $collection Collection Identifier
*
* @return collection/collection Updated collection object
*/
public function update(object $collection = null)
{
try {
$this->readByCollection($collection->collectionId);
} catch (Exception $e) {
throw new \core\Exception\NotFoundException("Collection not found");
}
return $this->sdoFactory->update($collection, 'Collection/Collection');
}
/**
* Retrieve default stats for screen
*
* @param string $collectionId Collection Identifier
*
* @return collection/collection Collection object
*/
public function readByCollection(string $collectionId)
{
$collection = $this->sdoFactory->read('Collection/Collection', ['collectionId' => $collectionId]);
$collection->archiveIds = json_decode($collection->archiveIds);
return $collection;
}
/**
* Retrieve Collection by User (current user if null)
*
* @param string|null $accountId UserAccount Identifer
*
* @return collection/collection $collection User Collection
*/
public function readByUser(string $accountId = null)
{
$userCollection = $this->userCollectionController->readByUser($accountId);
return $this->readByCollection($userCollection->collectionId);
}
public function delete(string $collectionId)
{
try {
$collection = $this->read($collectionId);
} catch (Exception $e) {
throw new \core\Exception\NotFoundException("Collection not found");
}
try {
$this->userCollectionController->delete($collection->collectionId);
} catch (Exception $e) {
throw new Exception("Error Processing Request");
}
return $this->sdoFactory->delete($collection, 'Collection/Collection');
}
}
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection\Controller;
/**
* Management of UserCollection
*
* @author Jérôme Boucher <jerome.boucher@maarch.org>
*/
class UserCollection
{
public $sdoFactory;
/**
* Constructor of access control class
*
* @param \dependency\sdo\Factory $sdoFactory The factory
*/
public function __construct(\dependency\sdo\Factory $sdoFactory)
{
$this->sdoFactory = $sdoFactory;
}
/**
* Create New UserCollection for a user
*
* @param string $collectionId Collection Identifier
*
* @return collection/userCollection UserCollection created
*/
public function createByUser(string $collectionId)
{
try {
$this->read($collectionId);
} catch (Exception $e) {
throw new Exception("Collection Already Exists");
}
$userCollection = \laabs::cast('Collection/UserCollection');
$userCollection->collectionId = $collectionId;
$userCollection->userId = \laabs::getToken("AUTH")->accountId;
$this->sdoFactory->create($userCollection, 'collection/userCollection');
return $userCollection;
}
/**
* Read UserCollection
*
* @param string $collectionId Collection Identifier
*
* @return collection/userCollection Collection object
*/
public function read(string $collectionId)
{
return $this->sdoFactory->read('Collection/UserCollection', ['$collectionId' => $collectionId]);
}
/**
* Get UserCollection by user provided or current user if not
*
* @param string|null $accountId User account Identifier
*
* @return collection/userCollection UserCollection Object
*/
public function readByUser(string $accountId = null)
{
if (is_null($accountId)) {
$accountId = \laabs::getToken("AUTH")->accountId;
}
return $this->sdoFactory->read('Collection/UserCollection', ['accountId' => $accountId]);
}
/**
* Delete UserCollection
*
* @param string $collectionId [description]
* @return [type] [description]
*/
public function delete(string $collectionId)
{
try {
$collection = $this->read($collectionId);
} catch (Exception $e) {
throw new Exception("Collection Already Exists");
}
return $this->sdoFactory->delete($collection, 'Collection/UserCollection');
}
}
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection\Message;
/**
* userCollection message
*
* @package Collection
* @author Jerome Boucher <jerome.boucher@maarch.org>
*/
class Collection
{
/**
* The collection identifier
*
* @var id
* @notempty
*/
public $collectionId;
/**
* Collection name
*
* @var string
*
*/
public $name;
/**
* List of archive identifiers
*
* @var json
*
*/
public $archiveIds;
}
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection\Message;
/**
* userCollection message
*
* @package Collection
* @author Jerome Boucher <jerome.boucher@maarch.org>
*/
class UserCollection
{
/**
* The user account identifier
*
* @var id
*
*/
public $accountId;
/**
* The user account identifier
*
* @var id
* @notempty
*/
public $collectionId;
/**
* The user account identifier
*
* @var id
*
*/
public $orgId;
}
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection\Model;
/**
* collection definition
*
* @package Collection
*
* @pkey [collectionId]
*
*/
class Collection
{
/**
* The collection Ientifier
*
* @var id
* @notempty
*/
public $collectionId;
/**
* The account identifier
*
* @var string
*
*/
public $name;
/**
* Array of archive Ids
*
* @var json
*
*/
public $archiveIds;
}
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection\Model;
/**
* collection userCollection definition
*
* @package Collection
*
* @key [accountId]
* @key [collectionId]
* @key [orgId]
* @fkey [orgId] organization/organization [orgId]
* @fkey [accountId] auth/account [accountId]
*/
class UserCollection
{
/**
* The account identifier
*
* @var id
*
*/
public $accountId;
/**
* The collection Ientifier
*
* @var id
* @notempty
*/
public $collectionId;
/**
* The organization identifier
*
* @var id
*
*/
public $orgId;
}
-- Schema: Collection
DROP SCHEMA IF EXISTS "Collection" CASCADE;
CREATE SCHEMA "Collection";
-- Table: "Collection"."Collection"
-- DROP TABLE "Collection"."Collection";
CREATE TABLE "Collection"."Collection"
(
"collectionId" text NOT NULL,
"name" text,
"archiveIds" jsonb
)
WITH (
OIDS=FALSE
);
-- Table: "auth"."roleMember"
-- DROP TABLE IF EXISTS "Collection"."userCollection" CASCADE;
CREATE TABLE "Collection"."userCollection"
(
"accountId" text,
"collectionId" text NOT NULL,
"orgId" text,
FOREIGN KEY ("accountId")
REFERENCES "auth"."account" ("accountId") MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
FOREIGN KEY ("orgId")
REFERENCES "organization"."organization" ("orgId") MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
UNIQUE ("accountId", "collectionId"),
UNIQUE ("orgId", "collectionId")
)
WITH (
OIDS=FALSE
);
<?php
/*
* Copyright (C) 2021 Maarch
*
* This file is part of bundle collection.
*
* Bundle collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle collection. If not, see <http://www.gnu.org/licenses/>.
*/
namespace bundle\Collection;
/**
* Interface for UserCollections
*
* @package Collection
* @author Jérôme Boucher <jerome.boucher@maarch.org>
*/
interface UserCollectionInterface
{
}
<?php
/*
* Copyright (C) 2020 Maarch
*
* This file is part of bundle Collection.
*
* Bundle Collection is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bundle Collection 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bundle Collection. If not, see <http://www.gnu.org/licenses/>.