import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Collection from 'App/Models/Collection'
import PaginationService from 'App/Services/PaginationService'
import { schema, rules } from '@ioc:Adonis/Core/Validator'
import Referrer from 'App/Models/Referrer'

export default class ReferrersController {
    private paginationService = new PaginationService(Referrer)
    public async index({ request }: HttpContextContract) {
        return await this.paginationService.paginate(request.all())
    }

    public async store({ request }: HttpContextContract) {
        const data: any = await request.validate({
            schema: schema.create({
                email: schema.string([rules.email()]),
                referrer: schema.number()
            })
        })
        return await Referrer.create(data)
    }

    public async get(email: string) {
        return await Referrer.findBy('email', email)
    }
}
