Skip to content

Commit

Permalink
refactor lastUpdated for Shares
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Sep 12, 2024
1 parent 12dc3a6 commit afabe5b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
12 changes: 2 additions & 10 deletions lib/Controller/ShareApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ public function newShare(int $formId, int $shareType, string $shareWith = '', ar
// Create share-notifications (activity)
$this->formsService->notifyNewShares($form, $share);

$this->formMapper->update($form);

// Append displayName for Frontend
$shareData = $share->read();
$shareData['displayName'] = $this->formsService->getShareDisplayName($shareData);
Expand Down Expand Up @@ -331,9 +329,7 @@ public function deleteShare(int $formId, int $shareId): DataResponse {
throw new OCSForbiddenException();
}

$this->shareMapper->deleteById($shareId);

$this->formMapper->update($form);
$this->shareMapper->delete($share);

return new DataResponse($shareId);
}
Expand Down Expand Up @@ -462,8 +458,6 @@ public function newShareLegacy(int $formId, int $shareType, string $shareWith =
// Create share-notifications (activity)
$this->formsService->notifyNewShares($form, $share);

$this->formMapper->update($form);

// Append displayName for Frontend
$shareData = $share->read();
$shareData['displayName'] = $this->formsService->getShareDisplayName($shareData);
Expand Down Expand Up @@ -500,9 +494,7 @@ public function deleteShareLegacy(int $id): DataResponse {
throw new OCSForbiddenException();
}

$this->shareMapper->deleteById($id);

$this->formMapper->update($form);
$this->shareMapper->delete($share);

Check warning on line 497 in lib/Controller/ShareApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ShareApiController.php#L497

Added line #L497 was not covered by tests

return new DataResponse($id);
}
Expand Down
53 changes: 38 additions & 15 deletions lib/Db/ShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCA\Forms\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand All @@ -41,10 +42,46 @@ class ShareMapper extends QBMapper {
* ShareMapper constructor.
* @param IDBConnection $db
*/
public function __construct(IDBConnection $db) {
public function __construct(

Check warning on line 45 in lib/Db/ShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ShareMapper.php#L45

Added line #L45 was not covered by tests
IDBConnection $db,
private FormMapper $formMapper
) {
parent::__construct($db, 'forms_v2_shares', Share::class);
}

/**
* @param Entity $entity
* @psalm-param Share $entity
* @return Share
* @throws \OCP\DB\Exception
*/
public function insert(Entity $entity): Share {
$this->formMapper->update($this->formMapper->findById($entity->getFormId()));
return parent::insert($entity);

Check warning on line 60 in lib/Db/ShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ShareMapper.php#L58-L60

Added lines #L58 - L60 were not covered by tests
}

/**
* @param Entity $entity
* @psalm-param Share $entity
* @return Share
* @throws \OCP\DB\Exception
*/
public function update(Entity $entity): Share {
$this->formMapper->update($this->formMapper->findById($entity->getFormId()));
return parent::update($entity);

Check warning on line 71 in lib/Db/ShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ShareMapper.php#L69-L71

Added lines #L69 - L71 were not covered by tests
}

/**
* @param Entity $entity
* @psalm-param Share $entity
* @return Share
* @throws \OCP\DB\Exception
*/
public function delete(Entity $entity): Share {
$this->formMapper->update($this->formMapper->findById($entity->getFormId()));
return parent::delete($entity);

Check warning on line 82 in lib/Db/ShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ShareMapper.php#L80-L82

Added lines #L80 - L82 were not covered by tests
}

/**
* Find a Share
* @param int $id
Expand Down Expand Up @@ -104,20 +141,6 @@ public function findPublicShareByHash(string $hash): Share {
return $this->findEntity($qb);
}

/**
* Delete a share
* @param int $id of the share.
*/
public function deleteById(int $id): void {
$qb = $this->db->getQueryBuilder();

$qb->delete($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
$qb->executeStatement();
}

/**
* Delete all Shares of a form.
* @param int $formId
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Controller/ShareApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,8 @@ public function testDeleteShare() {
->willReturn($form);

$this->shareMapper->expects($this->once())
->method('deleteById')
->with('8');
->method('delete')
->with($share);

$response = new DataResponse(8);
$this->assertEquals($response, $this->shareApiController->deleteShare(5, 8));
Expand Down

0 comments on commit afabe5b

Please sign in to comment.