import {Controller, view, Inject, Injectable, HTTPStatus, http, redirect} from '@extollo/lib'
import {GoLink} from '../../models/GoLink.model'

/**
 * GoLinks Controller
 */
@Injectable()
export class GoLinks extends Controller {
    async launch() {
        const short = this.request.safe('short').string()
        const link = await GoLink.query<GoLink>()
            .where('active', '=', true)
            .where('short', '=', short)
            .first()

        if ( !link ) {
            return http(HTTPStatus.http404, 'Invalid or expired link.')
        }

        return redirect(link.url)
    }
}
