Skip to content

Commit

Permalink
chore: create a redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo committed Sep 6, 2024
1 parent f5fa502 commit 17b6e24
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
20 changes: 7 additions & 13 deletions server/src/repository/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import database from './database';

import type { ILink, ILinkForCreate, ILinkForUpdate } from '@linx/shared';

const Links = database<ILink>('links');
export class Link {
static async create(linkDTO: ILinkForCreate): Promise<ILink> {
const [link] = await database<ILink>('links')
.insert(linkDTO)
const [link] = await Links.insert(linkDTO)
.returning('*')
.orderBy('created_at');
return link;
Expand All @@ -14,41 +14,35 @@ export class Link {
static async getByShorterName(
shorter_name: string,
): Promise<ILink | undefined> {
const link = await database<ILink>('links')
.select('*')
const link = await Links.select('*')
.where({ shorter_name: shorter_name })
.first();

return link;
}

static async getById(id: string): Promise<ILink | undefined> {
const link = await database<ILink>('links')
.select('*')
.where({ id: id })
.first();
const link = await Links.select('*').where({ id: id }).first();
return link;
}

static async getUserLinks(userId: string): Promise<ILink[]> {
const links = await database<ILink>('links')
.select('*')
const links = await Links.select('*')
.where({ user_id: userId })
.orderBy('created_at', 'desc');

return links;
}

static async update(linkId: string, linkDTO: ILinkForUpdate): Promise<ILink> {
const [link] = await database<ILink>('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<string> {
const _link = await database<ILink>('links').where('id', linkId).delete();
const _link = await Links.where('id', linkId).delete();
return linkId;
}
}
12 changes: 12 additions & 0 deletions server/src/repository/Redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { IRedirect, IRedirectForCreate } from '@linx/shared';
import database from './database';

const Redirects = database<IRedirect>('redirects');

export class Redirect {
static async create(redirectDTO: IRedirectForCreate): Promise<IRedirect> {
const [redirect] = await Redirects.insert(redirectDTO).returning('*');

return redirect;
}
}
11 changes: 7 additions & 4 deletions server/src/repository/User.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { IUser, IUserForRegister } from '@linx/shared';
import database from './database';

const Users = database<IUser>('users');

export class User {
/**
* getUserByEmail - get a user with the email
* @param id string
* @returns string IUser
*/
static async getUserByEmail(email: string): Promise<IUser | undefined> {
const user = await database<IUser>('users').where({ email }).first();
const user = await Users.where({ email }).first();
return user;
}

Expand All @@ -18,7 +20,7 @@ export class User {
* @returns string IUser
*/
static async getUserByID(id: string): Promise<IUser | undefined> {
const user = await database('users').where({ id }).first();
const user = await Users.where({ id }).first();

return user;
}
Expand All @@ -29,7 +31,8 @@ export class User {
* @returns string id
*/
static async createUser(user: IUserForRegister): Promise<string> {
const [id] = await database('users').insert(user).returning('id');
return id;
const [id] = await Users.insert(user).select('id');

return id?.id;
}
}
2 changes: 2 additions & 0 deletions server/src/repository/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 4 additions & 3 deletions server/src/services/Redirect.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 17b6e24

Please sign in to comment.