From 17b6e24cad884740b3b0e4a16ae222ff7d272054 Mon Sep 17 00:00:00 2001 From: Michael Liendo Date: Thu, 5 Sep 2024 23:22:33 -0400 Subject: [PATCH] chore: create a redirect --- server/src/repository/Link.ts | 20 +++++++------------- server/src/repository/Redirect.ts | 12 ++++++++++++ server/src/repository/User.ts | 11 +++++++---- server/src/repository/index.ts | 2 ++ server/src/services/Redirect.ts | 7 ++++--- 5 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 server/src/repository/Redirect.ts diff --git a/server/src/repository/Link.ts b/server/src/repository/Link.ts index 2ba77b6..0f7a668 100644 --- a/server/src/repository/Link.ts +++ b/server/src/repository/Link.ts @@ -2,10 +2,10 @@ import database from './database'; import type { ILink, ILinkForCreate, ILinkForUpdate } from '@linx/shared'; +const Links = database('links'); export class Link { static async create(linkDTO: ILinkForCreate): Promise { - const [link] = await database('links') - .insert(linkDTO) + const [link] = await Links.insert(linkDTO) .returning('*') .orderBy('created_at'); return link; @@ -14,8 +14,7 @@ export class Link { static async getByShorterName( shorter_name: string, ): Promise { - const link = await database('links') - .select('*') + const link = await Links.select('*') .where({ shorter_name: shorter_name }) .first(); @@ -23,16 +22,12 @@ export class Link { } static async getById(id: string): Promise { - const link = await database('links') - .select('*') - .where({ id: id }) - .first(); + const link = await Links.select('*').where({ id: id }).first(); return link; } static async getUserLinks(userId: string): Promise { - const links = await database('links') - .select('*') + const links = await Links.select('*') .where({ user_id: userId }) .orderBy('created_at', 'desc'); @@ -40,15 +35,14 @@ export class Link { } static async update(linkId: string, linkDTO: ILinkForUpdate): Promise { - const [link] = await database('links') - .update({ ...linkDTO, updated_at: new Date() }) + const [link] = await Links.update({ ...linkDTO, updated_at: new Date() }) .where({ id: linkId }) .returning('*'); return link; } static async deleteById(linkId: string): Promise { - const _link = await database('links').where('id', linkId).delete(); + const _link = await Links.where('id', linkId).delete(); return linkId; } } diff --git a/server/src/repository/Redirect.ts b/server/src/repository/Redirect.ts new file mode 100644 index 0000000..3df2aec --- /dev/null +++ b/server/src/repository/Redirect.ts @@ -0,0 +1,12 @@ +import type { IRedirect, IRedirectForCreate } from '@linx/shared'; +import database from './database'; + +const Redirects = database('redirects'); + +export class Redirect { + static async create(redirectDTO: IRedirectForCreate): Promise { + const [redirect] = await Redirects.insert(redirectDTO).returning('*'); + + return redirect; + } +} diff --git a/server/src/repository/User.ts b/server/src/repository/User.ts index 3bc5612..62947eb 100644 --- a/server/src/repository/User.ts +++ b/server/src/repository/User.ts @@ -1,6 +1,8 @@ import type { IUser, IUserForRegister } from '@linx/shared'; import database from './database'; +const Users = database('users'); + export class User { /** * getUserByEmail - get a user with the email @@ -8,7 +10,7 @@ export class User { * @returns string IUser */ static async getUserByEmail(email: string): Promise { - const user = await database('users').where({ email }).first(); + const user = await Users.where({ email }).first(); return user; } @@ -18,7 +20,7 @@ export class User { * @returns string IUser */ static async getUserByID(id: string): Promise { - const user = await database('users').where({ id }).first(); + const user = await Users.where({ id }).first(); return user; } @@ -29,7 +31,8 @@ export class User { * @returns string id */ static async createUser(user: IUserForRegister): Promise { - const [id] = await database('users').insert(user).returning('id'); - return id; + const [id] = await Users.insert(user).select('id'); + + return id?.id; } } diff --git a/server/src/repository/index.ts b/server/src/repository/index.ts index 1f564cc..8405822 100644 --- a/server/src/repository/index.ts +++ b/server/src/repository/index.ts @@ -1,7 +1,9 @@ import { Link } from './Link'; +import { Redirect } from './Redirect'; import { User } from './User'; export default class Repository { static user = User; static link = Link; + static redirect = Redirect; } diff --git a/server/src/services/Redirect.ts b/server/src/services/Redirect.ts index d491acb..2fc579c 100644 --- a/server/src/services/Redirect.ts +++ b/server/src/services/Redirect.ts @@ -1,8 +1,9 @@ -import type { IRedirect, IRedirectForCreate } from '@linx/shared'; +import type { IRedirectForCreate } from '@linx/shared'; +import Repository from '../repository'; export default class Redirect { - static async create(redirect: IRedirectForCreate) { - //todo: complete with the repository + static async create(redirectDTO: IRedirectForCreate) { + const redirect = await Repository.redirect.create(redirectDTO); return redirect; } }