Skip to content

Commit

Permalink
* Ran prettier
Browse files Browse the repository at this point in the history
* Added in hits feature when you visit a url it updates a hits
  • Loading branch information
csprance committed Jul 28, 2022
1 parent 7ca5375 commit 1767e12
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from '@prisma/client';

import { Formula } from './graphtoy/types';

export function isNumber(s: string) {
Expand Down
11 changes: 9 additions & 2 deletions pages/[tinyurl].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PrismaClient } from '@prisma/client';
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import React from 'react';
import { PrismaClient } from '@prisma/client';

import Graph from '../components/Graph';
import Gui from '../components/Gui';
Expand All @@ -24,11 +24,18 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
url: tinyurl,
},
});
prisma.$disconnect()
// No Results
if (!results) {
return { props: {} };
}
prisma.tiny_url
.update({
where: { id: results.id },
data: { hits: results.hits + 1 },
})
.then(() => {
prisma.$disconnect();
});
// Return results
return {
props: { initialZustandState: results.value },
Expand Down
7 changes: 3 additions & 4 deletions pages/api/tinyurl.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { PrismaClient } from '@prisma/client';
import httpStatus from 'http-status';
import { NextApiRequest, NextApiResponse } from 'next';
import hash from 'object-hash';

import { PrismaClient } from '@prisma/client';

import { createUniqueRandomKey } from '../../lib/utils';

/**
Expand Down Expand Up @@ -37,7 +36,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
return res.status(httpStatus.OK).send({ ...existingTinyUrl });
}
// If it doesn't exist create it and return it
const tinyurl = await prisma.tiny_url.create({
const tinyurl = await prisma.tiny_url.create({
data: {
value: body.state,
url: await createUniqueRandomKey(),
Expand All @@ -46,7 +45,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
hits: 0,
},
});
prisma.$disconnect()
prisma.$disconnect();
return res.status(httpStatus.OK).send({ ...tinyurl });
}

Expand Down

0 comments on commit 1767e12

Please sign in to comment.