diff --git a/app/Controllers/Http/Api/v1/MeController.ts b/app/Controllers/Http/Api/v1/MeController.ts index 4fe9b8e..c9db38e 100644 --- a/app/Controllers/Http/Api/v1/MeController.ts +++ b/app/Controllers/Http/Api/v1/MeController.ts @@ -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); diff --git a/app/Repositories/ContactRepository.ts b/app/Repositories/ContactRepository.ts index 5656d07..73ce764 100644 --- a/app/Repositories/ContactRepository.ts +++ b/app/Repositories/ContactRepository.ts @@ -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; diff --git a/app/Repositories/LessonRepository.ts b/app/Repositories/LessonRepository.ts index 9fc4fd6..d655aca 100644 --- a/app/Repositories/LessonRepository.ts +++ b/app/Repositories/LessonRepository.ts @@ -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 { + 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, + }); + } + } } diff --git a/app/Validators/Contacts/UpdateContactsValidator.ts b/app/Validators/Contacts/UpdateContactsValidator.ts index adb087a..ff3c965 100644 --- a/app/Validators/Contacts/UpdateContactsValidator.ts +++ b/app/Validators/Contacts/UpdateContactsValidator.ts @@ -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}$/)]), diff --git a/routes/apis/v1/me.ts b/routes/apis/v1/me.ts index 25a6387..840c6d8 100644 --- a/routes/apis/v1/me.ts +++ b/routes/apis/v1/me.ts @@ -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');