Skip to content

Commit

Permalink
add user profile activation update
Browse files Browse the repository at this point in the history
  • Loading branch information
huntmori committed Jan 25, 2024
1 parent 485d468 commit 3b5d91d
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 7 deletions.
18 changes: 14 additions & 4 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@
ProfileController::class . ':createUserProfile'
)->add(JwtMiddleware::class);

$group->get("/{uid}", ProfileController::class.":getProfile")
->add(JwtMiddleware::class);
$group->get("", ProfileController::class.":getProfiles")
->add(JwtMiddleware::class);
$group->get(
"/{uid}",
ProfileController::class.":getProfile"
)->add(JwtMiddleware::class);

$group->get(
"",
ProfileController::class.":getProfiles"
)->add(JwtMiddleware::class);

$group->patch(
"/{uid}/activation",
ProfileController::class.":updateProfileActivation"
)->add(JwtMiddleware::class);
});
/*
$app->get("/test", function(Request $request, Response $response) use ($app) {
Expand Down
4 changes: 2 additions & 2 deletions app/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
SettingsInterface::class => function () {
$ROOT_PATH = __DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR;

$stringProfile = require("../env.profile.php");
$config = require "../env.local.php";
$stringProfile = require($ROOT_PATH. DIRECTORY_SEPARATOR. "env.profile.php");
$config = require($ROOT_PATH. DIRECTORY_SEPARATOR. "env.{$stringProfile}.php");
return new Settings([
'displayErrorDetails' => true, // Should be set to false in production
'logError' => false,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
},
"scripts": {
"start": "php ./public/index.php",
"test": "phpunit"
"test": "phpunit",
"build":" php -d phar.readonly=off .\\vendor\\bin\\phar-composer build . .\\app.phar "
},
"bin": ["public/index.php", ".env.profile", ".env.local.json"]
}
17 changes: 17 additions & 0 deletions src/Domain/Common/repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@ public function selectList(string $sql, array $paramMap, string $returnType) : a
$this->disposePdo($pdo);
return $array;
}

public function update(string $sql, array $paramMap): bool
{
$pdo = $this->getPdo();
echo $sql.PHP_EOL;
$stmt = $pdo->prepare($sql);

$keys = array_keys($paramMap);
for ($i=0; $i<count($keys); $i++) {
$key = $keys[$i];
$stmt->bindValue($key, $paramMap[$key]);
}

$result = $stmt->execute();
$this->disposePdo($pdo);
return $result;
}
}
2 changes: 2 additions & 0 deletions src/Domain/Profile/Repository/ProfileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public function getUserProfileByProfileUid(string $uid);

public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated) : array;
public function checkNicknameCount(string $nickname) : int;

public function updateProfileActivation(Profile $profile, bool $activation) : ?Profile;
}
20 changes: 20 additions & 0 deletions src/Domain/Profile/Repository/ProfileRepositoryImplement.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,24 @@ public function checkNicknameCount(string $nickname) : int

return count($result);
}

public function updateProfileActivation(Profile $profile, bool $activation) : ?Profile
{
$result = $this->update("
UPDATE profile
SET activated = :activated,
updated_at = NOW()
WHERE uid = :uid",
[
'activated' => ($activation ? 1 : 0),
'uid' => $profile->getUid()
]
);

if (!$result) {
return null;
}

return $this->getUserProfileByProfileUid($profile->getUid());
}
}
16 changes: 16 additions & 0 deletions src/Domain/Profile/controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Domain\Profile\models\ProfileCreateRequest;
use App\Domain\Profile\models\ProfileGetByIdRequest;
use App\Domain\Profile\models\ProfileGetListRequest;
use App\Domain\Profile\models\ProfilePatchActivationRequest;
use App\Domain\Profile\service\ProfileService;
use App\Domain\User\service\UserService;
use Psr\Http\Message\ResponseInterface as Response;
Expand Down Expand Up @@ -83,4 +84,19 @@ public function getProfiles(Request $request, Response $response, array $args):
200
);
}

public function updateProfileActivation(Request $request, Response $response, array $args): Response
{
$requestDto = new ProfilePatchActivationRequest($request, $args);

$claims = $this->tokenService->getClaimFromToken($requestDto->getToken());
$user = $this->userService->getUserByUserId($claims->userId);
$profile = $this->profileService->getUserProfileByProfileUid($requestDto->getProfileUid());

$result = $this->profileService->updateProfileActivation($user, $profile, $requestDto->getActivate());


return $this->respondWithData($response, $result->toArray(), 200);
}

}
53 changes: 53 additions & 0 deletions src/Domain/Profile/models/ProfilePatchActivationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Domain\Profile\models;

use App\Domain\Common\models\AuthorizationRequestTrait;
use Psr\Http\Message\RequestInterface;

class ProfilePatchActivationRequest
{
private ?string $token;
private ?string $profileUid;
private ?bool $activate;

use AuthorizationRequestTrait;

public function __construct(RequestInterface $request, array $arg)
{
$this->token = $this->extractToken($request);
$this->profileUid = $arg['uid'];
$this->activate = json_decode($request->getBody(), true)['activate'];
}

public function getToken(): ?string
{
return $this->token;
}

public function setToken(?string $token): void
{
$this->token = $token;
}

public function getProfileUid(): ?string
{
return $this->profileUid;
}

public function setProfileUid(?string $profileUid): void
{
$this->profileUid = $profileUid;
}

public function getActivate(): ?bool
{
return $this->activate;
}

public function setActivate(?bool $activate): void
{
$this->activate = $activate;
}

}
7 changes: 7 additions & 0 deletions src/Domain/Profile/service/ProfileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Domain\Profile\entities\Profile;
use App\Domain\Profile\models\ProfileCreateRequest;
use App\Domain\Profile\models\ProfileGetByIdRequest;
use App\Domain\User\entities\User;

interface ProfileService
{
Expand All @@ -16,4 +17,10 @@ public function checkNickNameDuplicate(string $nickName): bool;
public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody): ?Profile;

public function getUserProfilesByRequest(ProfileGetByIdRequest $request) : ?Profile;

public function updateProfileActivation(User $user, Profile $profile, bool $activation) : ?Profile;


public function getUserProfileByProfileUid(string $uid) : ?Profile;

}
18 changes: 18 additions & 0 deletions src/Domain/Profile/service/ProfileServiceImplement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Domain\Profile\models\ProfileCreateRequest;
use App\Domain\Profile\models\ProfileGetByIdRequest;
use App\Domain\Profile\Repository\ProfileRepository;
use App\Domain\User\entities\User;
use App\Domain\User\repository\UserRepository;
use App\Domain\User\service\UserService;
use Cassandra\Uuid;
Expand Down Expand Up @@ -120,4 +121,21 @@ public function getUserProfilesByRequest(ProfileGetByIdRequest $request) : ?Prof
var_dump($profileUid);
return $this->profileRepository->getUserProfileByProfileUid($profileUid);
}

/**
* @throws Exception
*/
public function updateProfileActivation(User $user, Profile $profile, bool $activation): ?Profile
{
if ($user->getUid() !== $profile->getUserUid()) {
throw new Exception("자신의 프로필만 수정 할 수 있습니다.");
}

return $this->profileRepository->updateProfileActivation($profile, $activation);
}

public function getUserProfileByProfileUid(string $uid): ?Profile
{
return $this->profileRepository->getUserProfileByProfileUid($uid);
}
}

0 comments on commit 3b5d91d

Please sign in to comment.