Skip to content

Commit

Permalink
Release v5 (#6)
Browse files Browse the repository at this point in the history
* Change Likeable model API

* Add Liker contract and trait
  • Loading branch information
Anton Komarev authored Jan 16, 2018
1 parent daf03bb commit 7aa7935
Show file tree
Hide file tree
Showing 22 changed files with 973 additions and 405 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

All notable changes to `laravel-love` will be documented in this file.

## [5.0.0] - 2018-01-16

### Added

- Added `Cog\Contracts\Love\Liker\Models\Liker` contract with methods `like`, `dislike`, `unlike`, `undislike`, `toggleLike`, `toggleDislike`, `hasLiked`, `hasDisliked`

### Changed

- Method `like` renamed to `likeBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract
- Method `dislike` renamed to `dislikeBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract
- Method `unlike` renamed to `unlikeBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract
- Method `undislike` renamed to `undislikeBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract
- Method `liked` renamed to `likedBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract
- Method `disliked` renamed to `dislikedBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract
- Method `likeToggle` renamed to `toggleLikeBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract
- Method `dislikeToggle` renamed to `toggleDislikeBy` in `Cog\Contracts\Love\Likeable\Models\Likeable` contract

## [4.0.0] - 2018-01-04

### Changed
Expand Down Expand Up @@ -149,6 +166,7 @@ All notable changes to `laravel-love` will be documented in this file.

- Initial release

[5.0.0]: https://github.com/cybercog/laravel-love/compare/4.0.0...5.0.0
[4.0.0]: https://github.com/cybercog/laravel-love/compare/3.1.0...4.0.0
[3.1.0]: https://github.com/cybercog/laravel-love/compare/3.0.0...3.1.0
[3.0.0]: https://github.com/cybercog/laravel-love/compare/2.2.5...3.0.0
Expand Down
85 changes: 60 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ It completely changes package namespace architecture, aimed to API refactoring a
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Prepare likeable model](#prepare-likeable-model)
- [Available methods](#available-methods)
- [Prepare Liker Model](#prepare-liker-model)
- [Prepare Likeable Model](#prepare-likeable-model)
- [Available Methods](#available-methods)
- [Scopes](#scopes)
- [Events](#events)
- [Console commands](#console-commands)
- [Console Commands](#console-commands)
- [Extending](#extending)
- [Change log](#change-log)
- [Changelog](#changelog)
- [Upgrading](#upgrading)
- [Contributing](#contributing)
- [Testing](#testing)
Expand Down Expand Up @@ -78,9 +79,26 @@ $ php artisan vendor:publish --provider="Cog\Laravel\Love\Providers\LoveServiceP

## Usage

### Prepare likeable model
### Prepare Liker Model

Use `Likeable` contract in model which will get likes behavior and implement it or just use `Likeable` trait.
Use `Cog\Contracts\Love\Liker\Models\Liker` contract in model which will get likes
behavior and implement it or just use `Cog\Laravel\Love\Liker\Models\Traits\Liker` trait.

```php
use Cog\Contracts\Love\Liker\Models\Liker as LikerContract;
use Cog\Laravel\Love\Liker\Models\Traits\Liker;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements LikerContract
{
use Liker;
}
```

### Prepare Likeable Model

Use `Cog\Contracts\Love\Likeable\Models\Likeable` contract in model which will get likes
behavior and implement it or just use `Cog\Laravel\Love\Likeable\Models\Traits\Likeable` trait.

```php
use Cog\Contracts\Love\Likeable\Models\Likeable as LikeableContract;
Expand All @@ -93,29 +111,36 @@ class Article extends Model implements LikeableContract
}
```

### Available methods
### Available Methods

#### Likes

##### Like model


```php
$article->like(); // current user
$article->like($user->id);
$user->like($article);

$article->likeBy(); // current user
$article->likeBy($user->id);
```

##### Remove like mark from model

```php
$article->unlike(); // current user
$article->unlike($user->id);
$user->unlike($article);

$article->unlikeBy(); // current user
$article->unlikeBy($user->id);
```

##### Toggle like mark of model

```php
$article->likeToggle(); // current user
$article->likeToggle($user->id);
$user->toggleLike($article);

$article->toggleLikeBy(); // current user
$article->toggleLikeBy($user->id);
```

##### Get model likes count
Expand Down Expand Up @@ -145,9 +170,11 @@ $article->likes;
##### Boolean check if user liked model

```php
$user->hasLiked($article);

$article->liked; // current user
$article->liked(); // current user
$article->liked($user->id);
$article->isLikedBy(); // current user
$article->isLikedBy($user->id);
```

*Checks in eager loaded relations `likes` & `likesAndDislikes` first.*
Expand All @@ -169,22 +196,28 @@ $article->removeLikes();
##### Dislike model

```php
$article->dislike(); // current user
$article->dislike($user->id);
$user->dislike($article);

$article->dislikeBy(); // current user
$article->dislikeBy($user->id);
```

##### Remove dislike mark from model

```php
$article->undislike(); // current user
$article->undislike($user->id);
$user->undislike($article);

$article->undislikeBy(); // current user
$article->undislikeBy($user->id);
```

##### Toggle dislike mark of model

```php
$article->dislikeToggle(); // current user
$article->dislikeToggle($user->id);
$user->toggleDislike($article);

$article->toggleDislikeBy(); // current user
$article->toggleDislikeBy($user->id);
```

##### Get model dislikes count
Expand Down Expand Up @@ -214,9 +247,11 @@ $article->dislikes;
##### Boolean check if user disliked model

```php
$user->hasDisliked($article);

$article->disliked; // current user
$article->disliked(); // current user
$article->disliked($user->id);
$article->isDislikedBy(); // current user
$article->isDislikedBy($user->id);
```

*Checks in eager loaded relations `dislikes` & `likesAndDislikes` first.*
Expand Down Expand Up @@ -299,7 +334,7 @@ On each dislike added `\Cog\Laravel\Love\Likeable\Events\LikeableWasDisliked` ev

On each dislike removed `\Cog\Laravel\Love\Likeable\Events\LikeableWasUndisliked` event is fired.

### Console commands
### Console Commands

##### Recount likes and dislikes of all model types

Expand Down Expand Up @@ -392,7 +427,7 @@ $model = app(\Cog\Contracts\Love\Like\Models\Like::class);
$service = app(\Cog\Contracts\Love\Likeable\Services\LikeableService::class);
```

## Change log
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Expand Down
14 changes: 14 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Upgrade Guide

- [From v4 to v5](#from-v4-to-v5)
- [From v3 to v4](#from-v3-to-v4)

## From v4 to v5

### Likeable model methods

- Find all `like` method and replace it with `likeBy`
- Find all `dislike` method and replace it with `dislikeBy`
- Find all `unlike` method and replace it with `unlikeBy`
- Find all `undislike` method and replace it with `undislikeBy`
- Find all `likeToggle` method and replace it with `toggleLikeBy`
- Find all `dislikeToggle` method and replace it with `toggleDislikeBy`
- Find all `liked` method and replace it with `isLikedBy`
- Find all `disliked` method and replace it with `isDislikedBy`

## From v3 to v4

Because there are many breaking changes an upgrade is not that easy. There are many edge cases this guide does not cover.
Expand Down
56 changes: 28 additions & 28 deletions contracts/Likeable/Models/Likeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,85 +93,85 @@ public function collectDislikers();
*
* @throws \Cog\Contracts\Love\Liker\Exceptions\InvalidLiker
*/
public function like($userId = null);
public function likeBy($userId = null);

/**
* Remove a like from this record for the given user.
* Add a dislike for model by the given user.
*
* @param null|string|int $userId If null will use currently logged in user.
* @return void
*
* @throws \Cog\Contracts\Love\Liker\Exceptions\InvalidLiker
*/
public function unlike($userId = null);
public function dislikeBy($userId = null);

/**
* Toggle like for model by the given user.
* Remove a like from this record for the given user.
*
* @param null|string|int $userId If null will use currently logged in user.
* @return void
*
* @throws \Cog\Contracts\Love\Liker\Exceptions\InvalidLiker
*/
public function likeToggle($userId = null);

/**
* Has the user already liked likeable model.
*
* @param null|string|int $userId
* @return bool
*/
public function liked($userId = null): bool;
public function unlikeBy($userId = null);

/**
* Delete likes related to the current record.
* Remove a dislike from this record for the given user.
*
* @param null|string|int $userId If null will use currently logged in user.
* @return void
*
* @throws \Cog\Contracts\Love\Liker\Exceptions\InvalidLiker
*/
public function removeLikes();
public function undislikeBy($userId = null);

/**
* Add a dislike for model by the given user.
* Toggle like for model by the given user.
*
* @param null|string|int $userId If null will use currently logged in user.
* @return void
*
* @throws \Cog\Contracts\Love\Liker\Exceptions\InvalidLiker
*/
public function dislike($userId = null);
public function toggleLikeBy($userId = null);

/**
* Remove a dislike from this record for the given user.
* Toggle dislike for model by the given user.
*
* @param null|string|int $userId If null will use currently logged in user.
* @return void
*
* @throws \Cog\Contracts\Love\Liker\Exceptions\InvalidLiker
*/
public function undislike($userId = null);
public function toggleDislikeBy($userId = null);

/**
* Toggle dislike for model by the given user.
* Delete likes related to the current record.
*
* @param null|string|int $userId If null will use currently logged in user.
* @return void
*/
public function removeLikes();

/**
* Delete dislikes related to the current record.
*
* @throws \Cog\Contracts\Love\Liker\Exceptions\InvalidLiker
* @return void
*/
public function dislikeToggle($userId = null);
public function removeDislikes();

/**
* Has the user already disliked likeable model.
* Has the user already liked likeable model.
*
* @param null|string|int $userId
* @return bool
*/
public function disliked($userId = null): bool;
public function isLikedBy($userId = null): bool;

/**
* Delete dislikes related to the current record.
* Has the user already disliked likeable model.
*
* @return void
* @param null|string|int $userId
* @return bool
*/
public function removeDislikes();
public function isDislikedBy($userId = null): bool;
}
Loading

0 comments on commit 7aa7935

Please sign in to comment.