fix: saving materials for lesson

This commit is contained in:
Sergey Yarkov 2021-12-08 01:26:48 +03:00
parent d403b7b87a
commit 97171c1352
5 changed files with 34 additions and 27 deletions

View File

@ -96,7 +96,7 @@ export default class MeController extends BaseController {
/**
* Update contacts of authenticated user
* PUT /me/contacts
* PATCH /me/contacts
*/
public async updateContacts(ctx: HttpContextContract) {
const payload = await ctx.request.validate(UpdateContactsValidator);

View File

@ -63,12 +63,15 @@ export default class ContactRepository {
const contacts = await this.Contact.query().where('user_id', user.id).first();
if (contacts) {
contacts.phone_number = data.phone_number ?? null;
contacts.telegram_id = data.telegram_id?.substring(1) ?? null;
contacts.twitter_id = data.twitter_id?.substring(1) ?? null;
contacts.vk_id = data.vk_id ?? null;
await contacts
.merge({
phone_number: data.phone_number,
telegram_id: data.telegram_id?.substring(1),
twitter_id: data.twitter_id?.substring(1),
vk_id: data.vk_id,
})
.save();
await contacts.save();
await user.load('contacts');
return contacts;

View File

@ -1,7 +1,9 @@
import Drive from '@ioc:Adonis/Core/Drive';
import { MultipartFileContract } from '@ioc:Adonis/Core/BodyParser';
/**
* Models
*/
import Drive from '@ioc:Adonis/Core/Drive';
import Course from 'App/Models/Course';
import Lesson from 'App/Models/Lesson';
import LessonMaterial from 'App/Models/LessonMaterial';
@ -17,9 +19,12 @@ export default class LessonRepository {
private LessonMaterial: typeof LessonMaterial;
private Drive: typeof Drive;
constructor() {
this.Lesson = Lesson;
this.LessonMaterial = LessonMaterial;
this.Drive = Drive;
}
/**
@ -91,26 +96,13 @@ export default class LessonRepository {
*/
await lesson.related('content').create({ video_url: data.video_url });
/**
* Move each file to disk and then save in database
*/
if (data.materials) {
await Promise.all(
data.materials.map(async file => {
/**
* Move each file to disk and then save in database
*/
await file.moveToDisk('materials');
if (file.state === 'moved') {
const url = await Drive.getUrl(`materials/${file.fileName}`);
await lesson.related('materials').create({
name: file.fileName,
clientName: file.clientName,
ext: file.extname,
url,
});
}
})
);
const tasks = data.materials.map(file => this.saveMaterials(file, lesson));
await Promise.all(tasks);
}
/**
* Load data
*/
@ -158,4 +150,17 @@ export default class LessonRepository {
return null;
}
public async saveMaterials(file: MultipartFileContract, lesson: Lesson): Promise<void> {
await file.moveToDisk('materials');
if (file.state === 'moved') {
const url = await this.Drive.getUrl(`materials/${file.fileName}`);
await lesson.related('materials').create({
name: file.fileName,
clientName: file.clientName,
ext: file.extname,
url,
});
}
}
}

View File

@ -10,7 +10,6 @@ export default class UpdateContactsValidator {
locales: ['ru-RU', 'en-US'],
strict: true,
}),
// TODO validate correctly
rules.unique({ table: 'contacts', column: 'phone_number' }),
]),
vk_id: schema.string.optional({}, [rules.regex(/^([a-zA-Z0-9_]){1,64}$/)]),

View File

@ -11,7 +11,7 @@ Route.group(() => {
Route.patch('/password', 'Api/v1/MeController.changePassword')
.middleware('role:admin,teacher,student')
.as('me.change-password');
Route.put('/contacts', 'Api/v1/MeController.updateContacts')
Route.patch('/contacts', 'Api/v1/MeController.updateContacts')
.middleware('role:admin,teacher,student')
.as('me.update-contacts');
}).prefix('me');