Skip to content

Commit

Permalink
chore: update migrations and create the services for the links
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo committed Aug 6, 2024
1 parent 15105d1 commit 8775fe5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/migrations/20240806135204_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function up(knex: Knex): Promise<void> {
.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);
});
}
Expand Down
6 changes: 4 additions & 2 deletions server/src/repository/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export class Link {
return link;
}

static async getByID(ID: string): Promise<ILink> {
const [link] = await database<ILink>('links').select('*').where({ id: ID });
static async getByID(ID: string): Promise<ILink[]> {
const link = await database<ILink>('links')
.select('*')
.where({ user_id: ID });
return link;
}

Expand Down
21 changes: 21 additions & 0 deletions server/src/services/Link.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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<ILink[]> {
const links = await Repository.link.getByID(user_id);
return links;
}
}
2 changes: 2 additions & 0 deletions server/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8775fe5

Please sign in to comment.