diff --git a/server/migrations/20240806135204_links.ts b/server/migrations/20240806135204_links.ts index aea9b5a..9c8b40e 100644 --- a/server/migrations/20240806135204_links.ts +++ b/server/migrations/20240806135204_links.ts @@ -10,7 +10,7 @@ export async function up(knex: Knex): Promise { .primary(); table.string('url').notNullable(); table.string('user_id').references('id').inTable('user'); - table.string('shorter_name').notNullable(); + table.string('shorter_name').unique().notNullable(); table.timestamps(true, true); }); } diff --git a/server/src/repository/Link.ts b/server/src/repository/Link.ts index 1da627e..4f42496 100644 --- a/server/src/repository/Link.ts +++ b/server/src/repository/Link.ts @@ -15,8 +15,10 @@ export class Link { return link; } - static async getByID(ID: string): Promise { - const [link] = await database('links').select('*').where({ id: ID }); + static async getByID(ID: string): Promise { + const link = await database('links') + .select('*') + .where({ user_id: ID }); return link; } diff --git a/server/src/services/Link.ts b/server/src/services/Link.ts new file mode 100644 index 0000000..e1b6573 --- /dev/null +++ b/server/src/services/Link.ts @@ -0,0 +1,21 @@ +import Repository from '../repository'; +import { BadRequestError } from '../utils/errorHandler'; + +import type { ILink, ILinkForCreate } from '@linx/shared'; + +export default class Link { + static async create(link_dto: ILinkForCreate): Promise { + const check = await Repository.link.getByShorterName(link_dto.shorter_name); + if (check) + throw new BadRequestError('Exists the shorter name, please select other'); + + const link = await Repository.link.create(link_dto); + + return link; + } + + static async getAllUserLinks(user_id: string): Promise { + const links = await Repository.link.getByID(user_id); + return links; + } +} diff --git a/server/src/services/index.ts b/server/src/services/index.ts index b6625c8..6bf24ca 100644 --- a/server/src/services/index.ts +++ b/server/src/services/index.ts @@ -1,7 +1,9 @@ import Auth from './Auth'; +import Link from './Link'; import User from './User'; export default class Services { static auth = Auth; static user = User; + static link = Link; }