Skip to content

Commit

Permalink
chore: some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo committed Aug 7, 2024
1 parent 8775fe5 commit a06eb1f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/migrations/20240806135204_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function up(knex: Knex): Promise<void> {
.defaultTo(knex.raw('uuid_generate_v4()'))
.primary();
table.string('url').notNullable();
table.string('user_id').references('id').inTable('user');
table.uuid('user_id').references('id').inTable('users');
table.string('shorter_name').unique().notNullable();
table.timestamps(true, true);
});
Expand Down
22 changes: 22 additions & 0 deletions server/src/controllers/Link/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Services from '../../services';
import { BadRequestError } from '../../utils/errorHandler';

import type { ILinkForCreate } from '@linx/shared';
import type { Reply, Request } from '../../types';

export default async function create(request: Request, reply: Reply) {
const { shorter_name, url } = request.body as ILinkForCreate;
const user = request.user;

if (!shorter_name || !url) {
throw new BadRequestError('Please provide all required fields');
}

const link = await Services.link.create({
shorter_name,
url,
user_id: user.id,
});

return reply.code(201).send({ message: 'Link created', data: { id: link } });
}
2 changes: 2 additions & 0 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import auth from './auth';
import user from './user';
import link from './link';

import type {
DoneFuncWithErrOrRes,
Expand Down Expand Up @@ -33,6 +34,7 @@ export default function routes(

fastify.register(auth, { prefix: '/auth' });
fastify.register(user, { prefix: '/user' });
fastify.register(link, { prefix: '/link' });

done();
}
21 changes: 21 additions & 0 deletions server/src/routes/link/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import create from '../../controllers/Link/create';
import checkJwt from '../../middlewares/checkJwt';

import type { IReply } from '@linx/shared';
import type {
DoneFuncWithErrOrRes,
FastifyInstance,
RegisterOptions,
} from 'fastify';

export default function link(
fastify: FastifyInstance,
_: RegisterOptions,
done: DoneFuncWithErrOrRes,
) {
fastify.register(checkJwt);

fastify.post<{ Reply: IReply }>('/create', create);

done();
}

0 comments on commit a06eb1f

Please sign in to comment.