feat: add thumbnail upload support to course creation endpoint with multipart form data handling.
This commit is contained in:
parent
19844f343b
commit
cf12ef965e
3 changed files with 54 additions and 44 deletions
|
|
@ -4,6 +4,7 @@ import { config } from '../config';
|
|||
import { logger } from '../config/logger';
|
||||
import { UnauthorizedError, ValidationError, ForbiddenError, NotFoundError } from '../middleware/errorHandler';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { uploadFile, deleteFile } from '../config/minio';
|
||||
import {
|
||||
CreateCourseInput,
|
||||
UpdateCourseInput,
|
||||
|
|
@ -24,8 +25,22 @@ import {
|
|||
} from "../types/CoursesInstructor.types";
|
||||
|
||||
export class CoursesInstructorService {
|
||||
static async createCourse(courseData: CreateCourseInput, userId: number): Promise<createCourseResponse> {
|
||||
static async createCourse(courseData: CreateCourseInput, userId: number, thumbnailFile?: Express.Multer.File): Promise<createCourseResponse> {
|
||||
try {
|
||||
let thumbnailUrl: string | undefined;
|
||||
|
||||
// Upload thumbnail to MinIO if provided
|
||||
if (thumbnailFile) {
|
||||
const timestamp = Date.now();
|
||||
const uniqueId = Math.random().toString(36).substring(2, 15);
|
||||
const extension = thumbnailFile.originalname.split('.').pop() || 'jpg';
|
||||
const safeFilename = `${timestamp}-${uniqueId}.${extension}`;
|
||||
const filePath = `courses/thumbnails/${safeFilename}`;
|
||||
|
||||
await uploadFile(filePath, thumbnailFile.buffer, thumbnailFile.mimetype || 'image/jpeg');
|
||||
thumbnailUrl = filePath;
|
||||
}
|
||||
|
||||
// Use transaction to create course and instructor together
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
// Create the course
|
||||
|
|
@ -35,7 +50,7 @@ export class CoursesInstructorService {
|
|||
title: courseData.title,
|
||||
slug: courseData.slug,
|
||||
description: courseData.description,
|
||||
thumbnail_url: courseData.thumbnail_url,
|
||||
thumbnail_url: thumbnailUrl,
|
||||
price: courseData.price || 0,
|
||||
is_free: courseData.is_free ?? false,
|
||||
have_certificate: courseData.have_certificate ?? false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue