Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wielopolski committed Nov 8, 2024
1 parent ee2897a commit d0e83b4
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 10 deletions.
14 changes: 13 additions & 1 deletion apps/api/src/courses/api/courses.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import { RolesGuard } from "src/common/guards/roles.guard";
import { USER_ROLES } from "src/users/schemas/user-roles";

import { CoursesService } from "../courses.service";
import { allCoursesSchema, type AllCoursesResponse } from "../schemas/course.schema";
import { SortCourseFieldsOptions } from "../schemas/courseQuery";
import { type CreateCourseBody, createCourseSchema } from "../schemas/createCourse.schema";
import { type CommonShowCourse, commonShowCourseSchema } from "../schemas/showCourseCommon.schema";
import { type UpdateCourseBody, updateCourseSchema } from "../schemas/updateCourse.schema";

import { allCoursesValidation } from "./validations";

import type { AllCoursesResponse } from "../schemas/course.schema";
import type { CoursesFilterSchema } from "../schemas/courseQuery";

@Controller("courses")
Expand Down Expand Up @@ -146,6 +146,18 @@ export class CoursesController {
return new PaginatedResponse(data);
}

@Get("tutor-courses")
@Validate({
request: [{ type: "query", name: "authorId", schema: UUIDSchema, required: true }],
response: baseResponse(allCoursesSchema),
})
async getTutorCourses(
@Query("authorId") authorId: string,
@CurrentUser("userId") currentUserId: string,
): Promise<BaseResponse<AllCoursesResponse>> {
return new BaseResponse(await this.coursesService.getTutorCourses(authorId, currentUserId));
}

@Get("course")
@Validate({
request: [{ type: "query", name: "id", schema: UUIDSchema, required: true }],
Expand Down
49 changes: 40 additions & 9 deletions apps/api/src/courses/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

import { DatabasePg } from "src/common";
import { addPagination, DEFAULT_PAGE_SIZE } from "src/common/pagination";
import { STATES } from "src/common/states";
import { S3Service } from "src/file/s3.service";
import { LessonProgress } from "src/lessons/schemas/lesson.types";

Expand Down Expand Up @@ -42,7 +43,7 @@ import type { CoursesQuery } from "./api/courses.types";
import type { AllCoursesResponse } from "./schemas/course.schema";
import type { CreateCourseBody } from "./schemas/createCourse.schema";
import type { UpdateCourseBody } from "./schemas/updateCourse.schema";
import type { Pagination } from "src/common";
import type { Pagination, UUIDType } from "src/common";

@Injectable()
export class CoursesService {
Expand Down Expand Up @@ -168,7 +169,9 @@ export class CoursesService {
.innerJoin(categories, eq(courses.categoryId, categories.id))
.leftJoin(users, eq(courses.authorId, users.id))
.leftJoin(courseLessons, eq(courses.id, courseLessons.courseId))
.where(and(...conditions, eq(courses.state, "published"), eq(courses.archived, false)));
.where(
and(...conditions, eq(courses.state, STATES.published), eq(courses.archived, false)),
);

const dataWithS3SignedUrls = await this.addS3SignedUrls(data);

Expand Down Expand Up @@ -197,7 +200,7 @@ export class CoursesService {

return this.db.transaction(async (tx) => {
const conditions = [
eq(courses.state, "published"),
eq(courses.state, STATES.published),
eq(courses.archived, false),
isNull(studentCourses.studentId),
];
Expand Down Expand Up @@ -358,7 +361,7 @@ export class CoursesService {
and(
eq(courseLessons.courseId, id),
eq(lessons.archived, false),
eq(lessons.state, "published"),
eq(lessons.state, STATES.published),
isNotNull(lessons.id),
isNotNull(lessons.title),
isNotNull(lessons.description),
Expand Down Expand Up @@ -420,7 +423,7 @@ export class CoursesService {
and(
eq(courseLessons.courseId, id),
eq(lessons.archived, false),
eq(lessons.state, "published"),
eq(lessons.state, STATES.published),
isNotNull(lessons.id),
isNotNull(lessons.title),
isNotNull(lessons.description),
Expand All @@ -440,7 +443,35 @@ export class CoursesService {
};
}

async createCourse(createCourseBody: CreateCourseBody, authorId: string) {
async getTutorCourses(authorId: UUIDType, userId: UUIDType) {
return await this.db
.select(this.getSelectField(userId))
.from(courses)
.leftJoin(studentCourses, eq(studentCourses.courseId, courses.id))
.leftJoin(categories, eq(courses.categoryId, categories.id))
.leftJoin(users, eq(courses.authorId, users.id))
.leftJoin(courseLessons, eq(courses.id, courseLessons.courseId))
.where(
and(
eq(courses.state, STATES.published),
eq(courses.archived, false),
eq(courses.authorId, authorId),
),
)
.groupBy(
courses.id,
courses.title,
courses.imageUrl,
courses.description,
users.firstName,
users.lastName,
studentCourses.studentId,
categories.title,
)
.orderBy(courses.title);
}

async createCourse(createCourseBody: CreateCourseBody, authorId: UUIDType) {
return this.db.transaction(async (tx) => {
const [category] = await tx
.select()
Expand All @@ -463,7 +494,7 @@ export class CoursesService {
title: createCourseBody.title,
description: createCourseBody.description,
imageUrl: createCourseBody.imageUrl,
state: createCourseBody.state || "draft",
state: createCourseBody.state || STATES.draft,
priceInCents: createCourseBody.priceInCents,
currency: createCourseBody.currency || "usd",
authorId: authorId,
Expand Down Expand Up @@ -587,7 +618,7 @@ export class CoursesService {
and(
eq(courseLessons.courseId, course.id),
eq(lessons.archived, false),
eq(lessons.state, "published"),
eq(lessons.state, STATES.published),
eq(lessons.type, "quiz"),
),
)
Expand Down Expand Up @@ -763,7 +794,7 @@ export class CoursesService {
}

if (publishedOnly) {
conditions.push(eq(courses.state, "published"));
conditions.push(eq(courses.state, STATES.published));
}

return conditions;
Expand Down
200 changes: 200 additions & 0 deletions apps/api/src/swagger/api-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,33 @@
}
}
},
"/api/users/user-details": {
"get": {
"operationId": "UsersController_getUserDetails",
"parameters": [
{
"name": "userId",
"required": false,
"in": "query",
"schema": {
"format": "uuid",
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetUserDetailsResponse"
}
}
}
}
}
}
},
"/api/users/admin/{id}": {
"patch": {
"operationId": "UsersController_adminUpdateUser",
Expand Down Expand Up @@ -1266,6 +1293,33 @@
}
}
},
"/api/courses/tutor-courses": {
"get": {
"operationId": "CoursesController_getTutorCourses",
"parameters": [
{
"name": "authorId",
"required": true,
"in": "query",
"schema": {
"format": "uuid",
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetTutorCoursesResponse"
}
}
}
}
}
}
},
"/api/courses/course": {
"get": {
"operationId": "CoursesController_getCourse",
Expand Down Expand Up @@ -2914,6 +2968,70 @@
"data"
]
},
"GetUserDetailsResponse": {
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"id": {
"format": "uuid",
"type": "string"
},
"description": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"contactEmail": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"contactPhone": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"jobTitle": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
},
"required": [
"id",
"description",
"contactEmail",
"contactPhone",
"jobTitle"
]
}
},
"required": [
"data"
]
},
"AdminUpdateUserBody": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -3619,6 +3737,88 @@
"pagination"
]
},
"GetTutorCoursesResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"format": "uuid",
"type": "string"
},
"title": {
"type": "string"
},
"imageUrl": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"description": {
"type": "string"
},
"author": {
"type": "string"
},
"category": {
"type": "string"
},
"courseLessonCount": {
"type": "number"
},
"completedLessonCount": {
"type": "number"
},
"enrolled": {
"type": "boolean"
},
"enrolledParticipantCount": {
"type": "number"
},
"priceInCents": {
"type": "number"
},
"currency": {
"type": "string"
},
"state": {
"type": "string"
},
"archived": {
"type": "boolean"
},
"createdAt": {
"type": "string"
}
},
"required": [
"id",
"title",
"imageUrl",
"description",
"author",
"category",
"courseLessonCount",
"completedLessonCount",
"enrolledParticipantCount",
"priceInCents",
"currency"
]
}
}
},
"required": [
"data"
]
},
"GetCourseResponse": {
"type": "object",
"properties": {
Expand Down
Loading

0 comments on commit d0e83b4

Please sign in to comment.