feat: enable file upload support for announcement creation with multipart form data handling.
This commit is contained in:
parent
dd5a8c1cc8
commit
d2b3842564
3 changed files with 64 additions and 16 deletions
|
|
@ -110,17 +110,18 @@ export class AnnouncementsService {
|
|||
}
|
||||
|
||||
/**
|
||||
* สร้างประกาศใหม่
|
||||
* Create a new announcement
|
||||
* สร้างประกาศใหม่ (พร้อมอัปโหลดไฟล์แนบ)
|
||||
* Create a new announcement with optional file attachments
|
||||
*/
|
||||
async createAnnouncement(input: CreateAnnouncementInput): Promise<CreateAnnouncementResponse> {
|
||||
try {
|
||||
const { token, course_id, title, content, status, is_pinned } = input;
|
||||
const { token, course_id, title, content, status, is_pinned, files } = input;
|
||||
const decoded = jwt.verify(token, config.jwt.secret) as { id: number };
|
||||
|
||||
// Validate instructor access
|
||||
await CoursesInstructorService.validateCourseInstructor(token, course_id);
|
||||
|
||||
// Create announcement
|
||||
const announcement = await prisma.announcement.create({
|
||||
data: {
|
||||
course_id,
|
||||
|
|
@ -131,11 +132,52 @@ export class AnnouncementsService {
|
|||
published_at: status === 'PUBLISHED' ? new Date() : null,
|
||||
created_by: decoded.id,
|
||||
},
|
||||
include: {
|
||||
attachments: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Upload attachments if provided
|
||||
const attachments: {
|
||||
id: number;
|
||||
announcement_id: number;
|
||||
file_name: string;
|
||||
file_path: string;
|
||||
created_at: Date;
|
||||
updated_at: Date;
|
||||
}[] = [];
|
||||
|
||||
if (files && files.length > 0) {
|
||||
for (const file of files) {
|
||||
const timestamp = Date.now();
|
||||
const uniqueId = Math.random().toString(36).substring(2, 15);
|
||||
const fileName = file.originalname || 'file';
|
||||
const extension = fileName.split('.').pop() || '';
|
||||
const safeFilename = `${timestamp}-${uniqueId}.${extension}`;
|
||||
const filePath = `courses/${course_id}/announcements/${announcement.id}/${safeFilename}`;
|
||||
|
||||
// Upload to MinIO
|
||||
await uploadFile(filePath, file.buffer, file.mimetype || 'application/octet-stream');
|
||||
|
||||
// Create attachment record
|
||||
const attachment = await prisma.announcementAttachment.create({
|
||||
data: {
|
||||
announcement_id: announcement.id,
|
||||
file_name: fileName,
|
||||
file_path: filePath,
|
||||
file_size: file.size,
|
||||
mime_type: file.mimetype || 'application/octet-stream',
|
||||
},
|
||||
});
|
||||
|
||||
attachments.push({
|
||||
id: attachment.id,
|
||||
announcement_id: attachment.announcement_id,
|
||||
file_name: attachment.file_name,
|
||||
file_path: attachment.file_path,
|
||||
created_at: attachment.created_at,
|
||||
updated_at: attachment.created_at,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: 201,
|
||||
message: 'Announcement created successfully',
|
||||
|
|
@ -147,7 +189,7 @@ export class AnnouncementsService {
|
|||
is_pinned: announcement.is_pinned,
|
||||
created_at: announcement.created_at,
|
||||
updated_at: announcement.updated_at,
|
||||
attachments: [],
|
||||
attachments,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue