~cypheon/nextcloud-chores-app

e21bd0a7254d9f1ab6db98c39c5c8ed70203d3d4 — Johann Rudloff 8 months ago 2362d45
Implement deleting chores in the backend
4 files changed, 39 insertions(+), 0 deletions(-)

M appinfo/routes.php
M lib/Controller/APIController.php
M lib/Db/Chore.php
M lib/Db/ChoreMapper.php
M appinfo/routes.php => appinfo/routes.php +1 -0
@@ 24,6 24,7 @@ return [
    ['name' => 'API#getChores', 'url' => '/api/v1.0/team/{teamId}/chores', 'verb' => 'GET'],
    ['name' => 'API#postChores', 'url' => '/api/v1.0/team/{teamId}/chores', 'verb' => 'POST'],
    ['name' => 'API#patchChore', 'url' => '/api/v1.0/team/{teamId}/chores/{choreId}', 'verb' => 'PATCH'],
    ['name' => 'API#deleteChore', 'url' => '/api/v1.0/team/{teamId}/chores/{choreId}', 'verb' => 'DELETE'],

    ['name' => 'API#getWorklog', 'url' => '/api/v1.0/team/{teamId}/work', 'verb' => 'GET'],
    ['name' => 'API#submitWork', 'url' => '/api/v1.0/team/{teamId}/work', 'verb' => 'POST'],

M lib/Controller/APIController.php => lib/Controller/APIController.php +19 -0
@@ 162,6 162,25 @@ class APIController extends Controller {
   * @NoAdminRequired
   * @NoCSRFRequired
   */
  public function deleteChore(int $teamId, int $choreId) {
    try {
      $team = $this->checkAccess($this->userId, $teamId);
      $userId = $this->userId;

      // throws DoesNotExistException if chore does not belong to team:
      $chore = $this->choreMapper->findById($team->id, $choreId);

      $this->choreMapper->markDeleted($chore->id);
      return new DataResponse([], Http::STATUS_NO_CONTENT);
    } catch (DoesNotExistException $e) {
      return new DataResponse([], Http::STATUS_NOT_FOUND);;
    }
  }

  /**
   * @NoAdminRequired
   * @NoCSRFRequired
   */
  public function postChores(int $teamId) {
    try {
      $team = $this->checkAccess($this->userId, $teamId);

M lib/Db/Chore.php => lib/Db/Chore.php +2 -0
@@ 15,12 15,14 @@ class Chore extends Entity implements JsonSerializable {
  protected $points;
  protected $due;
  protected $repeat;
  protected $deleted;

  public function __construct() {
    $this->addType('id', 'integer');
    $this->addType('teamId', 'integer');
    $this->addType('points', 'integer');
    $this->addType('due', 'datetimetz_immutable');
    $this->addType('deleted', 'bool');
  }

  public function getDue(): string {

M lib/Db/ChoreMapper.php => lib/Db/ChoreMapper.php +17 -0
@@ 1,6 1,7 @@
<?php
namespace OCA\Chores\Db;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\AppFramework\Db\QBMapper;



@@ 34,12 35,28 @@ class ChoreMapper extends QBMapper {
       ->where(
         $qb->expr()->eq('team_id', $qb->createNamedParameter($teamId))
       )
       ->andWhere(
         'deleted = false'
       )
       ->orderBy('due', 'ASC')
       ;

    return $this->findEntities($qb);
  }

  public function markDeleted(int $choreId) {
    $qb = $this->db->getQueryBuilder();

    $qb->update('chores_chores')
       ->set('deleted', $qb->expr()->literal(true, IQueryBuilder::PARAM_BOOL))
       ->where(
         $qb->expr()->eq('id', $qb->createNamedParameter($choreId))
       )
       ;

    return $this->db->executeStatement($qb->getSQL(), $qb->getParameters());
  }

  public function updateDueDate(int $choreId, \DateTimeImmutable $dueDate) {
    $due = $dueDate->format('c');
    $qb = $this->db->getQueryBuilder();