<commit_before><?php

namespace Api\V1\Http\Resources;

use App\Core\Validator\Validator;
use App\Models\DB\Photo;
use Api\V1\Core\Resource\Contracts\Resource;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
use Throwable;

/**
 * Class UploadedPhotoResource.
 *
 * @property ConnectionInterface db
 * @property Photo $photo
 * @package Api\V1\Http\Resources
 */
class UploadedPhotoResource implements Resource
{
    use Validator;

    const VALIDATION_CREATE = 'validation.create';
    const VALIDATION_UPDATE = 'validation.update';

    /**
     * UploadedPhotoResource constructor.
     *
     * @param ConnectionInterface $db
     * @param Photo $photo
     */
    public function __construct(ConnectionInterface $db, Photo $photo)
    {
        $this->db = $db;
        $this->photo = $photo;
    }

    /**
     * @inheritdoc
     */
    protected function getValidationRules() : array
    {
        return [
            static::VALIDATION_CREATE => [
                'user_id' => ['required', 'filled', 'integer'],
                'path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails' => ['required', 'filled', 'array'],
                'thumbnails.*.width' => ['required', 'filled', 'int'],
                'thumbnails.*.height' => ['required', 'filled', 'int'],
                'thumbnails.*.path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails.*.relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
            ],
            static::VALIDATION_UPDATE => [
                'path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails' => ['required', 'filled', 'array'],
                'thumbnails.*.width' => ['required', 'filled', 'int'],
                'thumbnails.*.height' => ['required', 'filled', 'int'],
                'thumbnails.*.path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails.*.relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
            ],
        ];
    }

    /**
     * Get a resource by unique ID.
     *
     * @param int $id
     * @return Photo
     */
    public function getById($id) : Photo
    {
        $photo = $this->photo
            ->withThumbnails()
            ->whereIsUploaded()
            ->whereId($id)
            ->first();

        if ($photo === null) {
            throw new ModelNotFoundException('Uploaded photo not found.');
        };

        return $photo;
    }

    /**
     * @inheritdoc
     */
    public function getCollection($take, $skip, array $parameters)
    {
        throw new Exception('Method not implemented.');
    }

    /**
     * Create a resource.
     *
     * @param array $attributes
     * @return Photo
     * @throws Throwable
     */
    public function create(array $attributes) : Photo
    {
        $attributes = $this->validate($attributes, static::VALIDATION_CREATE);

        $photo = $this->photo->newInstance(['is_published' => false] + $attributes);

        try {
            $this->db->beginTransaction();
            $photo->save();
            $photo->thumbnails()->delete();
            $photo->thumbnails()->detach();
            $photo->thumbnails()->createMany($attributes['thumbnails']);
            $this->db->commit();<commit_msg>[BE]. Fixed photo resource is_uploaded attribute issue<commit_after><?php

namespace Api\V1\Http\Resources;

use App\Core\Validator\Validator;
use App\Models\DB\Photo;
use Api\V1\Core\Resource\Contracts\Resource;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
use Throwable;

/**
 * Class UploadedPhotoResource.
 *
 * @property ConnectionInterface db
 * @property Photo $photo
 * @package Api\V1\Http\Resources
 */
class UploadedPhotoResource implements Resource
{
    use Validator;

    const VALIDATION_CREATE = 'validation.create';
    const VALIDATION_UPDATE = 'validation.update';

    /**
     * UploadedPhotoResource constructor.
     *
     * @param ConnectionInterface $db
     * @param Photo $photo
     */
    public function __construct(ConnectionInterface $db, Photo $photo)
    {
        $this->db = $db;
        $this->photo = $photo;
    }

    /**
     * @inheritdoc
     */
    protected function getValidationRules() : array
    {
        return [
            static::VALIDATION_CREATE => [
                'user_id' => ['required', 'filled', 'integer'],
                'path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails' => ['required', 'filled', 'array'],
                'thumbnails.*.width' => ['required', 'filled', 'int'],
                'thumbnails.*.height' => ['required', 'filled', 'int'],
                'thumbnails.*.path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails.*.relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
            ],
            static::VALIDATION_UPDATE => [
                'path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails' => ['required', 'filled', 'array'],
                'thumbnails.*.width' => ['required', 'filled', 'int'],
                'thumbnails.*.height' => ['required', 'filled', 'int'],
                'thumbnails.*.path' => ['required', 'filled', 'string', 'min:1', 'max:255'],
                'thumbnails.*.relative_url' => ['required', 'filled', 'string', 'min:1', 'max:255'],
            ],
        ];
    }

    /**
     * Get a resource by unique ID.
     *
     * @param int $id
     * @return Photo
     */
    public function getById($id) : Photo
    {
        $photo = $this->photo
            ->withThumbnails()
            ->whereIsUploaded()
            ->whereId($id)
            ->first();

        if ($photo === null) {
            throw new ModelNotFoundException('Uploaded photo not found.');
        };

        return $photo;
    }

    /**
     * @inheritdoc
     */
    public function getCollection($take, $skip, array $parameters)
    {
        throw new Exception('Method not implemented.');
    }

    /**
     * Create a resource.
     *
     * @param array $attributes
     * @return Photo
     * @throws Throwable
     */
    public function create(array $attributes) : Photo
    {
        $attributes = $this->validate($attributes, static::VALIDATION_CREATE);

        $photo = $this->photo->newInstance($attributes);
        $photo->setIsPublishedAttribute(false);

        try {
            $this->db->beginTransaction();
            $photo->save();
            $photo->thumbnails()->delete();
            $photo->thumbnails()->detach();
            $photo->thumbnails()->createMany($attributes['thumbnails']);
            $this->db->commit();
{'fraction_non_alphanumeric': 0.17375640847997784, 'fraction_numerical': 0.009699321047526674, 'mean_word_length': 2.271985494106981, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6198720', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2071, 'n_words': 432}