Skip to content

Commit

Permalink
feat(#9): implement idea status
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryde committed Sep 25, 2018
1 parent 95d27cb commit aa44c44
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Controller;

use App\Repository\IdeaRepository;
use App\Services\IdeaStatusBadgeDefiner;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -16,11 +17,13 @@ class HomeController extends AbstractController
*
* @return Response
*/
public function index(IdeaRepository $ideaRepository)
public function index(IdeaRepository $ideaRepository, IdeaStatusBadgeDefiner $ideaStatusBadgeDefiner)
{
return $this->render(
'home/index.html.twig',
['ideas' => $ideaRepository->findBy([], ['creationDatetime' => 'DESC'])]
'home/index.html.twig', [
'ideas' => $ideaRepository->findBy([], ['creationDatetime' => 'DESC']),
'badge_definer' => $ideaStatusBadgeDefiner
]
);
}
}
18 changes: 18 additions & 0 deletions src/Entity/Idea.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class Idea implements VotableInterface
*/
private $editDatetime;

/**
* @ORM\ManyToOne(targetEntity="App\Entity\IdeaStatus", inversedBy="ideas")
*/
private $status;

public function __construct()
{
$this->comments = new ArrayCollection();
Expand Down Expand Up @@ -224,4 +229,17 @@ public function setEditDatetime(?\DateTimeInterface $editDatetime): self

return $this;
}

public function getStatus(): ?IdeaStatus
{
return $this->status;
}

public function setStatus(?IdeaStatus $status): self
{
$this->status = $status;

return $this;
}

}
120 changes: 120 additions & 0 deletions src/Entity/IdeaStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
* @ORM\Entity(repositoryClass="App\Repository\IdeaStatusRepository")
*/
class IdeaStatus
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=255)
*/
private $title;

/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $creationDatetime;

/**
* @ORM\OneToMany(targetEntity="App\Entity\Idea", mappedBy="status")
*/
private $ideas;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $slug;


public function __construct()
{
$this->ideas = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}

public function getCreationDatetime(): ?\DateTimeInterface
{
return $this->creationDatetime;
}

public function setCreationDatetime(\DateTimeInterface $creationDatetime): self
{
$this->creationDatetime = $creationDatetime;

return $this;
}

/**
* @return Collection|Idea[]
*/
public function getIdeas(): Collection
{
return $this->ideas;
}

public function addIdea(Idea $idea): self
{
if (!$this->ideas->contains($idea)) {
$this->ideas[] = $idea;
$idea->setStatus($this);
}

return $this;
}

public function removeIdea(Idea $idea): self
{
if ($this->ideas->contains($idea)) {
$this->ideas->removeElement($idea);
// set the owning side to null (unless already changed)
if ($idea->getStatus() === $this) {
$idea->setStatus(null);
}
}

return $this;
}

public function getSlug(): ?string
{
return $this->slug;
}

public function setSlug(?string $slug): self
{
$this->slug = $slug;

return $this;
}
}
34 changes: 34 additions & 0 deletions src/Migrations/2018/09/Version20180917061956.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20180917061956 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('CREATE TABLE idea_status (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, creation_datetime DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE idea ADD status_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE idea ADD CONSTRAINT FK_A8BCA456BF700BD FOREIGN KEY (status_id) REFERENCES idea_status (id)');
$this->addSql('CREATE INDEX IDX_A8BCA456BF700BD ON idea (status_id)');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE idea DROP FOREIGN KEY FK_A8BCA456BF700BD');
$this->addSql('DROP TABLE idea_status');
$this->addSql('DROP INDEX IDX_A8BCA456BF700BD ON idea');
$this->addSql('ALTER TABLE idea DROP status_id');
}
}
28 changes: 28 additions & 0 deletions src/Migrations/2018/09/Version20180925063136.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20180925063136 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE idea_status ADD slug VARCHAR(255) DEFAULT NULL');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE idea_status DROP slug');
}
}
50 changes: 50 additions & 0 deletions src/Repository/IdeaStatusRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Repository;

use App\Entity\IdeaStatus;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
* @method IdeaStatus|null find($id, $lockMode = null, $lockVersion = null)
* @method IdeaStatus|null findOneBy(array $criteria, array $orderBy = null)
* @method IdeaStatus[] findAll()
* @method IdeaStatus[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class IdeaStatusRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, IdeaStatus::class);
}

// /**
// * @return IdeaStatus[] Returns an array of IdeaStatus objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('i')
->andWhere('i.exampleField = :val')
->setParameter('val', $value)
->orderBy('i.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/

/*
public function findOneBySomeField($value): ?IdeaStatus
{
return $this->createQueryBuilder('i')
->andWhere('i.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
31 changes: 31 additions & 0 deletions src/Services/IdeaStatusBadgeDefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Services;

use App\Entity\IdeaStatus;

class IdeaStatusBadgeDefiner
{
/**
* @param IdeaStatus $ideaStatus
*
* @return string
*/
public function getBadgeLevel(IdeaStatus $ideaStatus): string
{
switch ($ideaStatus->getSlug()) {

case 'implemented' :
return 'success';
case 'closed':
return 'dark';
case 'need-example':
case 'need-author':
return 'warning';
case 'troll':
return 'info';
default:
return 'primary';
}
}
}
2 changes: 1 addition & 1 deletion templates/home/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

<h1>Latest ideas</h1>

{% include 'idea/_list.html.twig' with {ideas: ideas, show_actions: false} only %}
{% include 'idea/_list.html.twig' with {ideas: ideas, badge_definer: badge_definer, show_actions: false} only %}
{% endblock %}
6 changes: 3 additions & 3 deletions templates/idea/_list.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


<div class="row idea-list mt-5">
{% for idea in ideas %}
<div class="col-lg-12 idea-container">
Expand All @@ -11,7 +9,9 @@
</div>
</div>
<a class="idea" href="{{ path('idea_show', {'id' : idea.id }) }}">
<h2>{{ idea.title }}</h2>
<h2>{{ idea.title }} <span
class="badge badge-{{ badge_definer.getBadgeLevel(idea.status) }}">{{ idea.status.title }}</span>
</h2>
<span class="description">Posted by {{ idea.user.username }} {{ idea.creationDatetime | ago }}</span>
</a>
<span class="info-container">
Expand Down

0 comments on commit aa44c44

Please sign in to comment.