Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add Campaign model and repository #7520

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
facf4dd
initial commit
alaca Aug 26, 2024
f9999ee
fix: tests
alaca Aug 26, 2024
f17cb1a
fix: tests
alaca Aug 26, 2024
df176b9
refactor: remove wpdb global prop
alaca Aug 26, 2024
9f71085
refactor: remove url
alaca Aug 26, 2024
68df00c
refactor: use columns from exiting give_campaigns table
alaca Aug 26, 2024
6da3878
refactor: remove core reference from table name
alaca Aug 26, 2024
bec28b8
feature: add campaign type
alaca Aug 26, 2024
0ade3af
Initial commit
alaca Aug 26, 2024
c235cbf
refactor: table name
alaca Aug 26, 2024
a72f80c
refactor: campaign type
alaca Aug 27, 2024
ef1cbd0
feature: delete campaign page action
alaca Aug 27, 2024
10be69b
feature: register delete campaign page action
alaca Aug 27, 2024
0516133
feature: register table names
alaca Aug 27, 2024
d52f769
feature: register migration for setting up the campaign type for exis…
alaca Aug 27, 2024
4d102c3
Merge branch 'refs/heads/feature/campaign-table-GIVE-1137' into featu…
alaca Aug 27, 2024
986cfe1
feature: register migration for setting up the campaign type for exis…
alaca Aug 27, 2024
ac6a5bf
Merge branch 'refs/heads/feature/campaign-table-GIVE-1137' into featu…
alaca Aug 27, 2024
6519153
fix: migrations timestamp
alaca Aug 27, 2024
b0bbb0f
refactor: use wpdb property for table name
alaca Aug 27, 2024
e09aa97
chore: remove comments
alaca Aug 27, 2024
c33ac0d
refactor: format dates
alaca Aug 27, 2024
5e2d9fe
refactor: format dates
alaca Aug 27, 2024
949a4b4
feature: add campaign model test
alaca Aug 27, 2024
0ac80af
fix: use correct column name
alaca Aug 27, 2024
f883eb3
feature: add default values
alaca Aug 27, 2024
7526842
feature: add campaign page id
alaca Aug 27, 2024
ad71af6
refactor: update query
alaca Aug 27, 2024
eb697ee
fix: prop name
alaca Aug 27, 2024
1e869a9
Merge branch 'epic/campaigns' into feature/campaign-model-and-reposit…
kjohnson Aug 27, 2024
20d4d8b
chore: Update to reflect base branch
kjohnson Aug 27, 2024
bd78664
fix: Add table prefix
kjohnson Aug 27, 2024
039008c
fix: add back campaign page id column
alaca Aug 27, 2024
4c75041
refactor: I hate global variables
alaca Aug 27, 2024
9498cec
fix: column name 🤦‍
alaca Aug 27, 2024
760cf56
fix: get campaign type
alaca Aug 28, 2024
1aa0079
fix: use correct repository
alaca Aug 28, 2024
2b54d45
fix: attribute type
alaca Aug 28, 2024
98924e1
chore: remove unused namespace
alaca Aug 28, 2024
9823bfe
refactor: campaign model test
alaca Aug 28, 2024
a4808a0
refactor: params
alaca Aug 28, 2024
b9ae5c9
fix: dates
alaca Aug 28, 2024
5b8f172
feature: add campaign repository test
alaca Aug 28, 2024
9752c8b
fix: get actual value
alaca Aug 28, 2024
78d24ce
refactor: add campaign type
alaca Aug 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Campaigns/Actions/ConvertQueryDataToCampaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Give\Campaigns\Actions;

use Give\Campaigns\Models\Campaign;
use Give\Campaigns\ValueObjects\CampaignStatus;
use Give\Framework\Support\Facades\DateTime\Temporal;

/**
* @unreleased
*/
class ConvertQueryDataToCampaign
{
/**
* @unreleased
*/
public function __invoke(object $queryObject): Campaign
{
return new Campaign([
'id' => (int)$queryObject->id,
'type' => $queryObject->type,
'title' => $queryObject->title,
'shortDescription' => $queryObject->shortDescription,
'longDescription' => $queryObject->longDescription,
'logo' => $queryObject->logo,
'image' => $queryObject->image,
'primaryColor' => $queryObject->primaryColor,
'secondaryColor' => $queryObject->secondaryColor,
'goal' => (int)$queryObject->goal,
'startDate' => $queryObject->startDate,
'endDate' => $queryObject->endDate,
'status' => new CampaignStatus($queryObject->status),
'createdAt' => Temporal::toDateTime($queryObject->createdAt),
]);
}
}
28 changes: 28 additions & 0 deletions src/Campaigns/Factories/CampaignFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Give\Campaigns\Factories;

use Give\Campaigns\ValueObjects\CampaignStatus;
use Give\Campaigns\ValueObjects\CampaignType;
use Give\Framework\Models\Factories\ModelFactory;

/**
* @unreleased
*/
class CampaignFactory extends ModelFactory
{
/**
* @inheritDoc
*/
public function definition(): array
{
return [
'type' => CampaignType::CORE(),
'title' => __('GiveWP Campaign', 'give'),
'shortDescription' => __('Campaign short description', 'give'),
'longDescription' => __('Campaign long description', 'give'),
'goal' => 1000,
'status' => CampaignStatus::ACTIVE(),
];
}
}
64 changes: 64 additions & 0 deletions src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Give\Campaigns\Migrations\Tables;

use Give\Framework\Database\DB;
use Give\Framework\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;

/**
* @unreleased
* Creates give_campaign_forms table
*/
class CreateCampaignFormsTable extends Migration
{
/**
* @inheritdoc
*/
public static function id(): string
{
return 'give-campaigns-create-give-campaign-forms-table';
}

/**
* @inheritdoc
*/
public static function title(): string
{
return 'Create give_campaign_forms table';
}

/**
* @inheritdoc
*/
public static function timestamp(): string
{
return strtotime('2024-08-26 00:00:01');
}

/**
* @inheritDoc
* @throws DatabaseMigrationException
*/
public function run(): void
{
global $wpdb;

$table = $wpdb->prefix . 'give_campaign_forms';
$charset = DB::get_charset_collate();

$sql = "CREATE TABLE $table (
campaign_id INT UNSIGNED NOT NULL,
form_id INT UNSIGNED NOT NULL,
PRIMARY KEY (campaign_id),
KEY form_id (form_id)
) $charset";

try {
DB::delta($sql);
} catch (DatabaseQueryException $exception) {
throw new DatabaseMigrationException("An error occurred while creating the $table table", 0, $exception);
}
}
}
77 changes: 77 additions & 0 deletions src/Campaigns/Migrations/Tables/CreateCampaignsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Give\Campaigns\Migrations\Tables;

use Give\Framework\Database\DB;
use Give\Framework\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;

/**
* @unreleased
* Creates give_campaigns table
*/
class CreateCampaignsTable extends Migration
{
/**
* @inheritdoc
*/
public static function id(): string
{
return 'give-campaigns-create-give-core-campaigns-table';
}

/**
* @inheritdoc
*/
public static function title(): string
{
return 'Create give_campaigns table from core';
}

/**
* @inheritdoc
*/
public static function timestamp(): string
{
return strtotime('2024-08-26 00:00:00');
}

/**
* @inheritDoc
* @throws DatabaseMigrationException
*/
public function run()
{
global $wpdb;

$table = $wpdb->prefix . 'give_campaigns';
$charset = DB::get_charset_collate();

$sql = "CREATE TABLE $table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
form_id INT NOT NULL,
campaign_type VARCHAR(12) NOT NULL DEFAULT '',
campaign_title TEXT NOT NULL,
campaign_url TEXT NOT NULL,
short_desc TEXT NOT NULL,
long_desc TEXT NOT NULL,
campaign_logo TEXT NOT NULL,
campaign_image TEXT NOT NULL,
primary_color VARCHAR(7) NOT NULL,
secondary_color VARCHAR(7) NOT NULL,
campaign_goal INT UNSIGNED NOT NULL,
status VARCHAR(12) NOT NULL,
start_date DATETIME NULL,
end_date DATETIME NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset";

try {
DB::delta($sql);
} catch (DatabaseQueryException $exception) {
throw new DatabaseMigrationException("An error occurred while creating the $table table", 0, $exception);
}
}
}
135 changes: 135 additions & 0 deletions src/Campaigns/Models/Campaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Give\Campaigns\Models;

use DateTime;
use Give\Campaigns\Actions\ConvertQueryDataToCampaign;
use Give\Campaigns\Factories\CampaignFactory;
use Give\Campaigns\Repositories\CampaignRepository;
use Give\Campaigns\ValueObjects\CampaignStatus;
use Give\Campaigns\ValueObjects\CampaignType;
use Give\DonationForms\Repositories\DonationFormRepository;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
use Give\Framework\Models\Contracts\ModelCrud;
use Give\Framework\Models\Contracts\ModelHasFactory;
use Give\Framework\Models\Model;
use Give\Framework\Models\ModelQueryBuilder;

/**
* @unreleased
*
* @property int $id
* @property CampaignType $type
* @property string $title
* @property string $url
* @property string $shortDescription
* @property string $longDescription
* @property string $logo
* @property string $image
* @property string $primaryColor
* @property string $secondaryColor
* @property int $goal
* @property CampaignStatus $status
* @property DateTime $startDate
* @property DateTime $endDate
* @property DateTime $createdAt
*/
class Campaign extends Model implements ModelCrud, ModelHasFactory
{
/**
* @inheritdoc
*/
protected $properties = [
'id' => 'int',
'type' => CampaignType::class,
'title' => 'string',
'shortDescription' => 'string',
'longDescription' => 'string',
'logo' => 'string',
'image' => 'string',
'primaryColor' => 'string',
'secondaryColor' => 'string',
'goal' => 'int',
'status' => CampaignStatus::class,
'startDate' => DateTime::class,
'endDate' => DateTime::class,
'createdAt' => DateTime::class,
];

/**
* @unreleased
*/
public static function factory(): CampaignFactory
{
return new CampaignFactory(static::class);
}

/**
* Find campaign by ID
*
* @unreleased
*/
public static function find($id): ?Campaign
{
return give(DonationFormRepository::class)->getById($id);
}

/**
* @unreleased
*
* @throws Exception
*/
public static function create(array $attributes): Campaign
{
$campaign = new static($attributes);

give(CampaignRepository::class)->insert($campaign);

return $campaign;
}

/**
* @unreleased
*
* @throws Exception|InvalidArgumentException
*/
public function save(): void
{
if ( ! $this->id) {
give(CampaignRepository::class)->insert($this);
} else {
give(CampaignRepository::class)->update($this);
}
}

/**
* @unreleased
*
* @throws Exception
*/
public function delete(): bool
{
return give(CampaignRepository::class)->delete($this);
}

/**
* @unreleased
*
* @return ModelQueryBuilder<Campaign>
*/
public static function query(): ModelQueryBuilder
{
return give(CampaignRepository::class)->prepareQuery();
}

/**
* @unreleased
*
* @param object $object
*/
public static function fromQueryBuilderObject($object): Campaign
{
return (new ConvertQueryDataToCampaign())($object);
}
}
Loading
Loading