-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Z00One/feat/quiz
クイズCRUDの実装
- Loading branch information
Showing
25 changed files
with
1,182 additions
and
49 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
prisma/migrations/20240404111628_create_quiz_list_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Warnings: | ||
- The primary key for the `ClassUserQuiz` table will be changed. If it partially fails, the table could be left without primary key constraint. | ||
- The primary key for the `SetQuiz` table will be changed. If it partially fails, the table could be left without primary key constraint. | ||
- You are about to drop the column `id` on the `SetQuiz` table. All the data in the column will be lost. | ||
- Added the required column `updated_at` to the `ClassUserQuiz` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- DropForeignKey | ||
ALTER TABLE "ClassUserQuiz" DROP CONSTRAINT "ClassUserQuiz_q_id_fkey"; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE "ClassUserQuiz" DROP CONSTRAINT "ClassUserQuiz_s_id_fkey"; | ||
|
||
-- DropForeignKey | ||
ALTER TABLE "QuizFeedback" DROP CONSTRAINT "QuizFeedback_s_id_fkey"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "ClassUserQuiz" DROP CONSTRAINT "ClassUserQuiz_pkey", | ||
ADD COLUMN "updated_at" TIMESTAMP(3) NOT NULL, | ||
ADD CONSTRAINT "ClassUserQuiz_pkey" PRIMARY KEY ("u_id", "c_id"); | ||
|
||
-- AlterTable | ||
ALTER TABLE "SetQuiz" DROP CONSTRAINT "SetQuiz_pkey", | ||
DROP COLUMN "id", | ||
ADD CONSTRAINT "SetQuiz_pkey" PRIMARY KEY ("m_id"); | ||
|
||
-- CreateTable | ||
CREATE TABLE "QuizList" ( | ||
"q_id" BIGINT NOT NULL, | ||
"s_id" BIGINT NOT NULL, | ||
|
||
CONSTRAINT "QuizList_pkey" PRIMARY KEY ("q_id","s_id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "ClassUserQuiz" ADD CONSTRAINT "ClassUserQuiz_q_id_s_id_fkey" FOREIGN KEY ("q_id", "s_id") REFERENCES "QuizList"("q_id", "s_id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "QuizFeedback" ADD CONSTRAINT "QuizFeedback_s_id_fkey" FOREIGN KEY ("s_id") REFERENCES "SetQuiz"("m_id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "QuizList" ADD CONSTRAINT "QuizList_q_id_fkey" FOREIGN KEY ("q_id") REFERENCES "Quiz"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "QuizList" ADD CONSTRAINT "QuizList_s_id_fkey" FOREIGN KEY ("s_id") REFERENCES "SetQuiz"("m_id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
export enum Key { | ||
ROLES = 'roles', | ||
MODELS = 'models', | ||
ID_PARAM = 'id', | ||
} | ||
|
||
export const MetaData = <T>(key: Key, models: T) => | ||
SetMetadata(key, models); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/common/decorators/use-role-or-owner-guards.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { applyDecorators, UseGuards } from '@nestjs/common'; | ||
import { Prisma, Role } from '@prisma/client'; | ||
import { RoleOrOwnerGuard } from '@common/guards/role-or-owner.guard'; | ||
import { Key, MetaData } from './metadata.decorator'; | ||
|
||
export const UseRoleOrOwnerGuards = ( | ||
role: Role[], | ||
model: Prisma.ModelName, | ||
) => { | ||
return applyDecorators( | ||
MetaData<Prisma.ModelName>(Key.MODELS, model), | ||
MetaData<Role[]>(Key.ROLES, role), | ||
UseGuards(RoleOrOwnerGuard), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
} from '@nestjs/common'; | ||
import { OwnerGuard } from './owner.guard'; | ||
import { RolesGuard } from './role.guard'; | ||
|
||
@Injectable() | ||
export class RoleOrOwnerGuard implements CanActivate { | ||
constructor( | ||
private readonly ownerGuard: OwnerGuard, | ||
private readonly roleGuard: RolesGuard, | ||
) {} | ||
|
||
async canActivate( | ||
context: ExecutionContext, | ||
): Promise<boolean> { | ||
const hasRole = await this.checkUserRole(context); | ||
if (hasRole) return true; | ||
|
||
const isOwner = await this.checkOwnership(context); | ||
if (isOwner) return true; | ||
|
||
return false; | ||
} | ||
|
||
private checkUserRole(context: ExecutionContext) { | ||
return this.roleGuard.canActivate(context); | ||
} | ||
|
||
private checkOwnership(context: ExecutionContext) { | ||
return this.ownerGuard.canActivate(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.