Commit 62dc4fe8 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Merge branch '20-make-group-titles-unique' into 'master'

Resolve "make group titles unique"

Closes #20

See merge request kretschmar/doorcode!25
parents 15fa30d8 1c860078
Pipeline #2900 passed with stages
in 2 minutes and 3 seconds
......@@ -4,13 +4,10 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Group extends Model
{
use SoftDeletes;
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
......
......@@ -16,10 +16,9 @@ class CreateGroupsTable extends Migration
// This table stores the groups objects stored in the system
Schema::create('groups', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('title')->unique();
$table->string('description');
$table->timestamps();
$table->softDeletes();
});
}
......
......@@ -32,33 +32,25 @@ class Group
*/
protected ?Carbon $updatedAt;
/**
* @var \Carbon\Carbon|null
*/
protected ?Carbon $deletedAt;
/**
* @param int $id
* @param string $title
* @param string $description
* @param \Carbon\Carbon|null $createdAt
* @param \Carbon\Carbon|null $updatedAt
* @param \Carbon\Carbon|null $deletedAt
*/
public function __construct(
int $id,
string $title,
string $description,
?Carbon $createdAt = null,
?Carbon $updatedAt = null,
?Carbon $deletedAt = null
?Carbon $updatedAt = null
) {
$this->id = $id;
$this->title = $title;
$this->description = $description;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
$this->deletedAt = $deletedAt;
}
/**
......@@ -93,14 +85,6 @@ class Group
return $this->updatedAt;
}
/**
* @return \Carbon\Carbon|null
*/
public function getDeletedAt(): ?Carbon
{
return $this->deletedAt;
}
public function hasIdOf(?string $id): bool
{
if ($id === null) {
......@@ -110,6 +94,15 @@ class Group
return $this->getId() === (int)$id;
}
public function hasTitleOf(?string $title): bool
{
if ($title === null) {
return false;
}
return $this->getTitle() === $title;
}
/**
* @return int
*/
......@@ -125,4 +118,13 @@ class Group
{
$this->id = $id;
}
/**
* @param \Source\Entities\Group $group
* @return bool
*/
public function is(Group $group): bool
{
return $this->hasIdOf($group->getId()) || $this->hasTitleOf($group->getTitle());
}
}
......@@ -61,7 +61,7 @@ class InMemoryGroupUserRepository implements GroupUserRepository
*/
public function getUsersForGroup(string $groupId): array
{
if (!$this->groups->exists($groupId)) {
if (!$this->groups->get($groupId)) {
throw new EntityNotFoundException();
}
......
......@@ -33,7 +33,6 @@ class DatabaseGroupsRepository implements GroupsRepository
$group->description,
$group->created_at,
$group->updated_at,
$group->deleted_at
);
}
......@@ -52,7 +51,7 @@ class DatabaseGroupsRepository implements GroupsRepository
/**
* @inheritDoc
*/
public function create(Group $group): ?Group
public function create(Group $group): Group
{
$newGroup = self::fillBasicAttrs($group, new \App\Group());
......@@ -98,8 +97,8 @@ class DatabaseGroupsRepository implements GroupsRepository
/**
* @inheritDoc
*/
public function exists(string $groupId): bool
public function exists(Group $group): bool
{
return $this->get($groupId) !== null;
return \App\Group::where('title', $group->getTitle())->orWhere('id', $group->getId())->first() !== null;
}
}
......@@ -19,10 +19,12 @@ interface GroupsRepository
public function all(): array;
/**
* It is the callers responsibility to check the exists function first to make sure it does not exist.
*
* @param Group $group
* @return Group|null
* @return Group
*/
public function create(Group $group): ?Group;
public function create(Group $group): Group;
/**
* @param string $groupId
......@@ -38,8 +40,8 @@ interface GroupsRepository
public function delete(string $groupId): bool;
/**
* @param string $groupId
* @param \Source\Entities\Group $group
* @return bool
*/
public function exists(string $groupId): bool;
public function exists(Group $group): bool;
}
......@@ -28,16 +28,12 @@ class InMemoryGroupsRepository implements GroupsRepository
/**
* @inheritDoc
*/
public function create(Group $group): ?Group
public function create(Group $group): Group
{
if ($group->hasIdOf(0)) {
$group->setId(static::$idCounter++);
}
if ($this->exists($group->getId())) {
return null;
}
$this->groups[] = $group;
return $group;
......@@ -46,9 +42,15 @@ class InMemoryGroupsRepository implements GroupsRepository
/**
* @inheritDoc
*/
public function exists(string $groupId): bool
public function exists(Group $group): bool
{
return $this->get($groupId) !== null;
foreach ($this->groups as $g) {
if ($g->is($group)) {
return true;
}
}
return false;
}
/**
......
......@@ -29,10 +29,12 @@ class CreateGroup implements CreateGroupUseCase
$attributes['description']
);
if (!($group = $this->groups->create($group))) {
if ($this->groups->exists($group)) {
throw new EntityExistsException();
}
$group = $this->groups->create($group);
$response = new ResponseModel($group);
$presenter->present($response);
......
......@@ -30,8 +30,8 @@ class GroupDatabaseTest extends DatabaseTestCase
$this->assertEquals([], $all);
$this->groups->create(new Group(0, '', ''));
$this->groups->create(new Group(0, '', ''));
$this->groups->create(new Group(0, 't1', ''));
$this->groups->create(new Group(0, 't2', ''));
$all = $this->groups->all();
......@@ -79,7 +79,7 @@ class GroupDatabaseTest extends DatabaseTestCase
$this->assertTrue($this->groups->delete(1));
$this->assertFalse($this->groups->exists(1));
$this->assertFalse($this->groups->exists(new Group(2, '', '')));
$this->assertFalse($this->groups->delete(2));
......@@ -93,9 +93,9 @@ class GroupDatabaseTest extends DatabaseTestCase
{
$this->groups->create(new Group(0, '', ''));
$this->assertTrue($this->groups->exists(1));
$this->assertFalse($this->groups->exists(2));
$this->assertFalse($this->groups->exists('ree'));
$this->assertTrue($this->groups->exists(new Group(1, 'asdf', 'asdf')));
$this->assertFalse($this->groups->exists(new Group(2, 'asdf', 'asdf')));
$this->assertTrue($this->groups->exists(new Group(3, '', '')));
}
/**
......
......@@ -40,7 +40,7 @@ class InMemoryGroupsRepositoryStub implements GroupsRepository
* @param Group $group
* @return Group|null
*/
public function create(Group $group): ?Group
public function create(Group $group): Group
{
return null;
}
......@@ -65,10 +65,10 @@ class InMemoryGroupsRepositoryStub implements GroupsRepository
}
/**
* @param string $groupId
* @param \Source\Entities\Group $group
* @return bool
*/
public function exists(string $groupId): bool
public function exists(Group $group): bool
{
return true;
}
......
......@@ -95,4 +95,19 @@ class UseCaseTest extends TestCase
$this->handleTest($this->createGroupAttributes($group));
}
/**
* @test
* @throws \Source\Exceptions\EntityExistsException
*/
public function it_cannot_create_two_groups_with_the_same_title(): void
{
$this->groupsRepository->create(new Group(1, 'title', 'desc'));
$group = new Group(2, 'title', 'new desc');
$this->expectException(EntityExistsException::class);
$this->handleTest($this->createGroupAttributes($group));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment