Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate to course v3 #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 112 additions & 33 deletions packages/mock-server/fixtures/course.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
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";
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 {
Expand All @@ -39,54 +47,125 @@ 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<{}> {
return Factory.extend({
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);
},
updatedAt() {
return faker.date.recent(1);
},
content() {
return faker.lorem.lines(5);
},
modules() {
return new Array(faker.datatype.number(1)).fill(0);
},
});
}

Expand Down
80 changes: 80 additions & 0 deletions packages/mock-server/fixtures/page.ts
Original file line number Diff line number Diff line change
@@ -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<AnyRegistry>): 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<AnyRegistry>): void {
server.createList(this.name(), 20);
}

model(): ModelDefinition {
return Model.extend({});
}
}
4 changes: 3 additions & 1 deletion packages/mock-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -13,6 +14,7 @@ const mocks: Mock[] = [
new VideoMock(),
new SearchMock(),
new CourseMock(),
new PageMock(),
];

function initModels(): { [key: string]: ModelDefinition } {
Expand Down Expand Up @@ -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));
},
});
Expand Down