add resume update method

This commit is contained in:
iIronside 2023-11-22 12:42:36 +03:00
parent 75329a8835
commit 835147af37
5 changed files with 230 additions and 8 deletions

View File

@ -2,11 +2,26 @@
namespace frontend\modules\api\controllers;
use frontend\modules\api\models\resume\forms\ChangeResumeForm;
use frontend\modules\api\models\resume\Resume;
use frontend\modules\api\services\ResumeService;
use Yii;
use yii\web\BadRequestHttpException;
class ResumeController extends ApiController
{
public ResumeService $resumeService;
public function __construct(
$id,
$module,
ResumeService $resumeService,
$config = []
)
{
$this->resumeService = $resumeService;
parent::__construct($id, $module, $config);
}
/**
* @OA\Get(path="/resume",
@ -42,4 +57,87 @@ class ResumeController extends ApiController
{
return Resume::findOne($userId ?? Yii::$app->user->identity->id);
}
/**
*
* @OA\Put(path="/resume/edit-skills",
* summary="Изменить скилы",
* description="Метод полностью удалит старые скилы и используя переданые id скилов запишет новые",
* security={
* {"bearerAuth": {}}
* },
* tags={"Resume"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="json",
* @OA\Schema(
* required={"UserCard"},
* @OA\Property(
* property="UserCard",
* type="object",
* @OA\Property(
* property="skill",
* type="array",
* @OA\Items(
* type="integer",
* example={1,2,3,4}
* ),
* ),
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает сообщение об успехе",
* ),
* )
* )
*
* @return array|ChangeResumeForm
* @throws BadRequestHttpException
*/
public function actionEditSkills()
{
return $this->resumeService->editSkills(Yii::$app->request->post(), Yii::$app->user->identity->getId());
}
/**
*
* @OA\Put(path="/resume/edit-text",
* summary="Изменить резюме",
* description="Метод для изменения текста резюме",
* security={
* {"bearerAuth": {}}
* },
* tags={"Resume"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* required={"resume"},
* @OA\Property(
* property="resume",
* type="string",
* description="Текст резюме",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает сообщение об успехе",
* ),
* )
* )
*
* @return array|ChangeResumeForm
* @throws BadRequestHttpException
*/
public function actionEditText(): ChangeResumeForm|array
{
return $this->resumeService->editText(Yii::$app->request->post(), Yii::$app->user->identity->getId());
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace frontend\modules\api\models\resume\forms;
use yii\base\Model;
class ChangeResumeForm extends Model
{
public $resume;
/**
* @return array
*/
public function rules()
{
return [
[['resume'], 'string'],
[['resume'], 'required'],
];
}
/**
* @return string
*/
public function formName(): string
{
return '';
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace frontend\modules\api\models\resume\forms;
use frontend\modules\api\models\Skill;
use yii\base\Model;
class SkillsResumeForm extends Model
{
public array $skill;
/**
* @return array
*/
public function rules()
{
return [
[['skill'], 'required'],
['skill', 'each', 'rule' => ['integer']],
['skill', 'each', 'rule' => ['in', 'range' => Skill::find()->select('id')->column()]]
];
}
/**
* @return string
*/
public function formName(): string
{
return '';
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace frontend\modules\api\services;
use frontend\modules\api\models\resume\forms\ChangeResumeForm;
use frontend\modules\api\models\resume\forms\SkillsResumeForm;
use frontend\modules\card\models\UserCard;
use yii\web\BadRequestHttpException;
class ResumeService
{
/**
* @param array $params
* @param int $userId
* @return SkillsResumeForm|string[]
* @throws BadRequestHttpException
*/
public function editSkills(array $params, int $userId): array|SkillsResumeForm
{
$model = new SkillsResumeForm();
$model->load($params['UserCard']);
if (!$model->validate()) {
return $model;
}
$card = UserCard::findOne(['id_user' => $userId]);
if (!$card->save()) {
$errors = $card->getErrors();
throw new BadRequestHttpException(array_shift($errors)[0]);
}
return ['status' => 'success'];
}
/**
* @param array $params
* @param int $userId
* @return array|ChangeResumeForm
* @throws BadRequestHttpException
*/
public function editText(array $params, int $userId): array|ChangeResumeForm
{
$model = new ChangeResumeForm();
$model->load($params);
if (!$model->validate()) {
return $model;
}
$card = UserCard::findOne(['id_user' => $userId]);
$card->vc_text = $model->resume;
if (!$card->save()) {
$errors = $card->getErrors();
throw new BadRequestHttpException(array_shift($errors)[0]);
}
return ['status' => 'success'];
}
}

View File

@ -32,17 +32,19 @@ class UserCard extends \common\models\UserCard
public function afterSave($insert, $changedAttributes)
{
$post = Yii::$app->request->post('UserCard');
if (Yii::$app->request->post('UserCard')) {
$post = Yii::$app->request->post('UserCard');
if ($post['skill']) {
CardSkill::deleteAll(['card_id' => $this->id]);
if ($post['skill']) {
CardSkill::deleteAll(['card_id' => $this->id]);
foreach ($post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
foreach ($post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
$skill->save();
$skill->save();
}
}
}