diff --git a/.gitignore b/.gitignore index 88b6f0d..f6d5910 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,5 @@ yarn-error.log* # typescript *.tsbuildinfo + +.env \ No newline at end of file diff --git a/README.md b/README.md index c87e042..1e1f5e7 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,22 @@ -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). +- Add created_at and updated_at for all tables +- Feed some users -## Getting Started +# Pages -First, run the development server: +- Create user (Done) +- List users (Done) +- Update user +- Delete user -```bash -npm run dev -# or -yarn dev -``` +- Create apps +- List app +- Update app +- Delete app -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. +1. Autogenerate certificate +2. Request validation +3. Add logs to all request -You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file. +SAML certificate (PEM format) -[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`. - -The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. - -## Learn More - -To learn more about Next.js, take a look at the following resources: - -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. - -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! - -## Deploy on Vercel - -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. - -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. +you need to generate a set of public and private keys and an X.509 certificate that contains the public key. The public keys and certificates must be generated with either the RSA or DSA algorithm and registered with Google. diff --git a/package-lock.json b/package-lock.json index a34e555..f97054e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,7 @@ "": { "name": "fake", "dependencies": { + "@prisma/client": "^3.7.0", "next": "12.0.7", "react": "17.0.2", "react-dom": "17.0.2" @@ -1114,6 +1115,31 @@ "node": ">= 8" } }, + "node_modules/@prisma/client": { + "version": "3.7.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-3.7.0.tgz", + "integrity": "sha512-fUJMvBOX5C7JPc0e3CJD6Gbelbu4dMJB4ScYpiht8HMUnRShw20ULOipTopjNtl6ekHQJ4muI7pXlQxWS9nMbw==", + "hasInstallScript": true, + "dependencies": { + "@prisma/engines-version": "3.7.0-31.8746e055198f517658c08a0c426c7eec87f5a85f" + }, + "engines": { + "node": ">=12.6" + }, + "peerDependencies": { + "prisma": "*" + }, + "peerDependenciesMeta": { + "prisma": { + "optional": true + } + } + }, + "node_modules/@prisma/engines-version": { + "version": "3.7.0-31.8746e055198f517658c08a0c426c7eec87f5a85f", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-3.7.0-31.8746e055198f517658c08a0c426c7eec87f5a85f.tgz", + "integrity": "sha512-+qx2b+HK7BKF4VCa0LZ/t1QCXsu6SmvhUQyJkOD2aPpmOzket4fEnSKQZSB0i5tl7rwCDsvAiSeK8o7rf+yvwg==" + }, "node_modules/@rushstack/eslint-patch": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@rushstack/eslint-patch/-/eslint-patch-1.1.0.tgz", @@ -6183,6 +6209,19 @@ "fastq": "^1.6.0" } }, + "@prisma/client": { + "version": "3.7.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-3.7.0.tgz", + "integrity": "sha512-fUJMvBOX5C7JPc0e3CJD6Gbelbu4dMJB4ScYpiht8HMUnRShw20ULOipTopjNtl6ekHQJ4muI7pXlQxWS9nMbw==", + "requires": { + "@prisma/engines-version": "3.7.0-31.8746e055198f517658c08a0c426c7eec87f5a85f" + } + }, + "@prisma/engines-version": { + "version": "3.7.0-31.8746e055198f517658c08a0c426c7eec87f5a85f", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-3.7.0-31.8746e055198f517658c08a0c426c7eec87f5a85f.tgz", + "integrity": "sha512-+qx2b+HK7BKF4VCa0LZ/t1QCXsu6SmvhUQyJkOD2aPpmOzket4fEnSKQZSB0i5tl7rwCDsvAiSeK8o7rf+yvwg==" + }, "@rushstack/eslint-patch": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@rushstack/eslint-patch/-/eslint-patch-1.1.0.tgz", diff --git a/package.json b/package.json index 98e285a..1fc1977 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "lint": "next lint" }, "dependencies": { + "@prisma/client": "^3.7.0", "next": "12.0.7", "react": "17.0.2", "react-dom": "17.0.2" diff --git a/pages/api/apps.ts b/pages/api/apps.ts new file mode 100644 index 0000000..3b4c541 --- /dev/null +++ b/pages/api/apps.ts @@ -0,0 +1,73 @@ +import { PrismaClient } from '@prisma/client'; +import type { NextApiRequest, NextApiResponse } from 'next'; + +const prisma = new PrismaClient(); + +type ServiceProvider = { + sp_acs_url: string; + sp_entity_id: string; +}; + +type IdentityProvider = { + idp_sso_url: string; + idp_entity_id: string; +}; + +type App = { + id: string; + name: string; + description?: string; + certificate: string; +} & ServiceProvider; + +const createApp = async (body: Omit): Promise => { + return await prisma.app.create({ data: body }); +}; + +const getAllApps = async (): Promise => { + return await prisma.app.findMany(); +}; + +const getAppById = async (id: string): Promise => { + return await prisma.app.findUnique({ + where: { + id, + }, + }); +}; + +const createKeyPairs = (): any => {}; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + + // if (method === 'GET') { + // const apps = await getAllApps(); + + // return res.status(200).json(apps); + // } + + if (method === 'GET') { + const app = await getAppById('287f8e3d-234c-425c-bac3-cc68878582f5'); + + return res.status(200).json(app); + } + + if (method === 'POST') { + const { name, sp_acs_url, sp_entity_id, description = null } = req.body; + const certificate = 'certificate'; + + const app = await createApp({ + name, + description, + certificate, + sp_acs_url, + sp_entity_id, + }); + + return res.status(200).json(app); + } +} diff --git a/pages/api/hello.ts b/pages/api/hello.ts index f8bcc7e..beadf93 100644 --- a/pages/api/hello.ts +++ b/pages/api/hello.ts @@ -1,13 +1,12 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next' +import type { NextApiRequest, NextApiResponse } from 'next'; type Data = { - name: string -} + name: string; +}; -export default function handler( +export default async function handler( req: NextApiRequest, res: NextApiResponse ) { - res.status(200).json({ name: 'John Doe' }) + res.status(200).json({ name: 'Kiran' }); } diff --git a/pages/api/users/[id].ts b/pages/api/users/[id].ts new file mode 100644 index 0000000..2744de4 --- /dev/null +++ b/pages/api/users/[id].ts @@ -0,0 +1,34 @@ +import { PrismaClient } from '@prisma/client'; +import type { NextApiRequest, NextApiResponse } from 'next'; + +const prisma = new PrismaClient(); + +type User = { + id: number; + first_name: string; + last_name: string; + email: string; +}; + +const getUserById = async (id: number): Promise => { + return await prisma.user.findUnique({ + where: { + id, + }, + }); +}; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + + if (method === 'GET') { + const { id } = req.query; + + const user = await getUserById(Number(id)); + + return res.status(200).json(user); + } +} diff --git a/pages/api/users/index.ts b/pages/api/users/index.ts new file mode 100644 index 0000000..b0049fd --- /dev/null +++ b/pages/api/users/index.ts @@ -0,0 +1,40 @@ +import { PrismaClient } from '@prisma/client'; +import type { NextApiRequest, NextApiResponse } from 'next'; + +const prisma = new PrismaClient(); + +type User = { + id: number, + first_name: string, + last_name: string, + email: string, +}; + +const createUser = async (body: Omit): Promise => { + return await prisma.user.create({ data: body }); +}; + +const fetchAllUsers = async (): Promise => { + return await prisma.user.findMany(); +}; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + + if (method === 'GET') { + const users = await fetchAllUsers(); + + return res.status(200).json(users); + } + + if (method === 'POST') { + const { first_name, last_name, email } = req.body; + + const user = await createUser({ first_name, last_name, email }); + + return res.status(200).json(user); + } +} diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..64f66eb --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,25 @@ +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} + +model User { + id Int @id @default(autoincrement()) + first_name String + last_name String + email String +} + +model App { + id String @id @default(uuid()) + name String + description String? + certificate String + sp_acs_url String + sp_entity_id String +} +