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 * Update contacts of authenticated user
* PUT /me/contacts * PATCH /me/contacts
*/ */
public async updateContacts(ctx: HttpContextContract) { public async updateContacts(ctx: HttpContextContract) {
const payload = await ctx.request.validate(UpdateContactsValidator); 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(); const contacts = await this.Contact.query().where('user_id', user.id).first();
if (contacts) { if (contacts) {
contacts.phone_number = data.phone_number ?? null; await contacts
contacts.telegram_id = data.telegram_id?.substring(1) ?? null; .merge({
contacts.twitter_id = data.twitter_id?.substring(1) ?? null; phone_number: data.phone_number,
contacts.vk_id = data.vk_id ?? null; 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'); await user.load('contacts');
return contacts; return contacts;

View File

@ -1,7 +1,9 @@
import Drive from '@ioc:Adonis/Core/Drive';
import { MultipartFileContract } from '@ioc:Adonis/Core/BodyParser';
/** /**
* Models * Models
*/ */
import Drive from '@ioc:Adonis/Core/Drive';
import Course from 'App/Models/Course'; import Course from 'App/Models/Course';
import Lesson from 'App/Models/Lesson'; import Lesson from 'App/Models/Lesson';
import LessonMaterial from 'App/Models/LessonMaterial'; import LessonMaterial from 'App/Models/LessonMaterial';
@ -17,9 +19,12 @@ export default class LessonRepository {
private LessonMaterial: typeof LessonMaterial; private LessonMaterial: typeof LessonMaterial;
private Drive: typeof Drive;
constructor() { constructor() {
this.Lesson = Lesson; this.Lesson = Lesson;
this.LessonMaterial = LessonMaterial; this.LessonMaterial = LessonMaterial;
this.Drive = Drive;
} }
/** /**
@ -91,26 +96,13 @@ export default class LessonRepository {
*/ */
await lesson.related('content').create({ video_url: data.video_url }); await lesson.related('content').create({ video_url: data.video_url });
/**
* Move each file to disk and then save in database
*/
if (data.materials) { if (data.materials) {
await Promise.all( const tasks = data.materials.map(file => this.saveMaterials(file, lesson));
data.materials.map(async file => { await Promise.all(tasks);
/**
* 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,
});
}
})
);
} }
/** /**
* Load data * Load data
*/ */
@ -158,4 +150,17 @@ export default class LessonRepository {
return null; 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'], locales: ['ru-RU', 'en-US'],
strict: true, strict: true,
}), }),
// TODO validate correctly
rules.unique({ table: 'contacts', column: 'phone_number' }), rules.unique({ table: 'contacts', column: 'phone_number' }),
]), ]),
vk_id: schema.string.optional({}, [rules.regex(/^([a-zA-Z0-9_]){1,64}$/)]), 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') Route.patch('/password', 'Api/v1/MeController.changePassword')
.middleware('role:admin,teacher,student') .middleware('role:admin,teacher,student')
.as('me.change-password'); .as('me.change-password');
Route.put('/contacts', 'Api/v1/MeController.updateContacts') Route.patch('/contacts', 'Api/v1/MeController.updateContacts')
.middleware('role:admin,teacher,student') .middleware('role:admin,teacher,student')
.as('me.update-contacts'); .as('me.update-contacts');
}).prefix('me'); }).prefix('me');