From cb34f71dc6bbe66938eedcf9ec0b21cee309b4b1 Mon Sep 17 00:00:00 2001 From: leofvo Date: Fri, 17 Feb 2023 11:14:18 +0100 Subject: [PATCH] refactor: migrate mock to course v3 api specification --- packages/mock-server/fixtures/course.ts | 145 ++++++++++++++++++------ packages/mock-server/fixtures/page.ts | 80 +++++++++++++ packages/mock-server/index.ts | 4 +- 3 files changed, 195 insertions(+), 34 deletions(-) create mode 100644 packages/mock-server/fixtures/page.ts diff --git a/packages/mock-server/fixtures/course.ts b/packages/mock-server/fixtures/course.ts index 07493b78..4e939296 100644 --- a/packages/mock-server/fixtures/course.ts +++ b/packages/mock-server/fixtures/course.ts @@ -1,5 +1,3 @@ -import { Mock } from "./generic"; -import { BaseUsers, User } from "./user"; import { faker } from "@faker-js/faker"; import { Factory, Model, Server } from "miragejs"; import { @@ -7,27 +5,37 @@ import { FactoryDefinition, ModelDefinition, } from "miragejs/-types"; +import { Mock } from "./generic"; +import { BaseUsers, User } from "./user"; -export interface Watchtime { - videoId: string; - userId?: string; - watchedSeconds: number; - watchedPercent: number; - isWatched: boolean; +export enum VISIBILITY { + PUBLIC = "PUBLIC", + PROTECTED = "PROTECTED", + PRIVATE = "PRIVATE", } export interface Course { - id: string; - createdAt?: string; - updatedAt?: string; - visibility?: string; - draft?: boolean; - name: string; - slug: string; + title: string; description: string; - content: string; - user?: User; - modules?: any[]; // TODO + visibility: VISIBILITY; + sections: Section[]; + id: string; + userId: string; + user?: User; // INJECTED BY AGGREGATOR + createdAt: string; + updatedAt: string; +} + +export interface Section { + title: string; + elements: Element[]; + id: string; +} + +export interface Element { + type: "VIDEO" | "PAGE" | "QUIZ"; + id: string; + order: number; } export class CourseMock implements Mock { @@ -39,17 +47,47 @@ export class CourseMock implements Mock { server.timing = 1000; server.get("courses", (schema, request) => { - const { pageSize } = request.queryParams; + const { pageSize, page, visibility, userId } = request.queryParams; const { models } = (schema as any).courses.all(); + return { - data: models.slice(0, pageSize ?? 100), + page: parseInt(page) || 1, + pageSize: parseInt(pageSize) || 10, total: models.length, + data: models + .filter((course: Course) => { + if (visibility && course.visibility !== visibility) { + return false; + } + if (userId && course.userId !== userId) { + return false; + } + return true; + }) + .slice(0, parseInt(pageSize) || 10), }; }); server.get("courses/:id", (schema) => { return schema.first("course"); }); + + server.post("courses", (schema, request) => { + const attrs = JSON.parse(request.requestBody); + return schema.create("course", attrs); + }); + + server.put("courses/:id", (schema, request) => { + const attrs = JSON.parse(request.requestBody); + schema.findBy("course", { id: request.params.id })?.update(attrs); + return schema.findBy("course", { id: request.params.id }); + }); + + server.delete("courses/:id", (schema, request) => { + const course = schema.findBy("course", { id: request.params.id }); + course?.destroy(); + return course; + }); } factory(): FactoryDefinition<{}> { @@ -57,23 +95,70 @@ export class CourseMock implements Mock { id() { return faker.datatype.uuid(); }, - slug() { - return faker.datatype.uuid(); - }, - name() { + title() { return faker.name.jobTitle(); }, description() { return faker.lorem.lines(5); }, + userId() { + return BaseUsers[0].id; + }, user() { + // INJECTED BY AGGREGATOR return BaseUsers[0]; }, visibility() { - return Math.random() > 0.2 ? "public" : "private"; + return Math.random() > 0.2 ? "PUBLIC" : "PRIVATE"; }, - draft() { - return Math.random() < 0.4; + content() { + return faker.lorem.lines(5); + }, + sections() { + return [ + { + id: faker.datatype.uuid(), + title: faker.name.jobTitle(), + elements: [ + { + type: "VIDEO", + id: faker.datatype.uuid(), + order: 0, + }, + { + type: "PAGE", + id: faker.datatype.uuid(), + order: 1, + }, + { + type: "QUIZ", + id: faker.datatype.uuid(), + order: 2, + }, + ], + }, + { + id: faker.datatype.uuid(), + title: faker.name.jobTitle(), + elements: [ + { + type: "VIDEO", + id: faker.datatype.uuid(), + order: 0, + }, + { + type: "PAGE", + id: faker.datatype.uuid(), + order: 1, + }, + { + type: "VIDEO", + id: faker.datatype.uuid(), + order: 2, + }, + ], + }, + ]; }, createdAt() { return faker.date.past(2); @@ -81,12 +166,6 @@ export class CourseMock implements Mock { updatedAt() { return faker.date.recent(1); }, - content() { - return faker.lorem.lines(5); - }, - modules() { - return new Array(faker.datatype.number(1)).fill(0); - }, }); } diff --git a/packages/mock-server/fixtures/page.ts b/packages/mock-server/fixtures/page.ts new file mode 100644 index 00000000..b51a4070 --- /dev/null +++ b/packages/mock-server/fixtures/page.ts @@ -0,0 +1,80 @@ +import { Mock } from "./generic"; +import { BaseUsers, User } from "./user"; +import { faker } from "@faker-js/faker"; +import { Factory, Model, Server } from "miragejs"; +import { + AnyRegistry, + FactoryDefinition, + ModelDefinition, +} from "miragejs/-types"; + +export interface Page { + title: string; + content: string; + id: string; + userId: string; + createdAt: string; + updatedAt: string; +} + +export class PageMock implements Mock { + name(): string { + return "page"; + } + + routes(server: Server): void { + server.timing = 1000; + server.get("pages/:id", (schema, request) => { + return schema.findBy("page", { id: request.params.id }); + }); + + server.post("pages", (schema, request) => { + const attrs = JSON.parse(request.requestBody); + return schema.create("page", attrs); + }); + + server.put("pages/:id", (schema, request) => { + const attrs = JSON.parse(request.requestBody); + schema.findBy("page", { id: request.params.id })?.update(attrs); + return schema.findBy("page", { id: request.params.id }); + }); + + server.delete("pages/:id", (schema, request) => { + const page = schema.findBy("page", { id: request.params.id }); + page?.destroy(); + return page; + }); + } + + factory(): FactoryDefinition<{}> { + return Factory.extend({ + id() { + return faker.datatype.uuid(); + }, + + title() { + return faker.name.jobTitle(); + }, + content() { + return faker.lorem.lines(5); + }, + userId() { + return BaseUsers[0].id; + }, + createdAt() { + return faker.date.past(2); + }, + updatedAt() { + return faker.date.recent(1); + }, + }); + } + + seeds(server: Server): void { + server.createList(this.name(), 20); + } + + model(): ModelDefinition { + return Model.extend({}); + } +} diff --git a/packages/mock-server/index.ts b/packages/mock-server/index.ts index c3fc38e3..61659a65 100644 --- a/packages/mock-server/index.ts +++ b/packages/mock-server/index.ts @@ -2,6 +2,7 @@ import { createServer } from "miragejs"; import { FactoryDefinition, ModelDefinition } from "miragejs/-types"; import { CourseMock } from "./fixtures/course"; import { Mock } from "./fixtures/generic"; +import { PageMock } from "./fixtures/page"; import { SearchMock } from "./fixtures/search"; import { UserMock } from "./fixtures/user"; import { VideoMock } from "./fixtures/video"; @@ -13,6 +14,7 @@ const mocks: Mock[] = [ new VideoMock(), new SearchMock(), new CourseMock(), + new PageMock(), ]; function initModels(): { [key: string]: ModelDefinition } { @@ -40,7 +42,7 @@ export function initMockServer() { mocks.forEach((mock) => mock.seeds(server)); }, routes() { - this.urlPrefix = 'http://localhost:4000'; + this.urlPrefix = "http://localhost:4000"; mocks.forEach((mock) => mock.routes(this)); }, });