Skip to content
This repository was archived by the owner on Mar 12, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14,745 changes: 11,198 additions & 3,547 deletions backend/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"dependencies": {
"@prisma/client": "^2.27.0",
"axios": "^0.26.0",
"cron": "^1.8.2",
"debug": "^4.3.2",
"dotenv": "^10.0.0",
Expand Down
4 changes: 2 additions & 2 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ generator client {
}

model User {
id String @id @db.Uuid
id String @id

imageUrl String
name String
Expand All @@ -27,7 +27,7 @@ model Post {
id Int @id @default(autoincrement())

user User @relation("myPosts", fields: [userId], references: [id])
userId String @db.Uuid
userId String

timestampCreated DateTime @default(now())
timestampModified DateTime @updatedAt
Expand Down
6 changes: 5 additions & 1 deletion backend/src/controllers/posts-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { ParamsDictionary } from 'express-serve-static-core/index'
import { StatusCodes } from 'http-status-codes'
import { dbPostToPostDto } from '../data/post'
import repository from '../data/repository'
import Logger from '../util/logger'

const log = Logger.getLogger('posts-id')

type getPostById = (
req: Request,
Expand Down Expand Up @@ -58,7 +61,7 @@ const updatePostHandler: updatePost = async (req, res) => {
export const updatePost = asyncHandler(updatePostHandler)

const reactToPostHandler: reactToPost = async (req, res) => {
const postId = Number(req.params.postId)
const postId = Number(req.params.id)

try {
if (req.body.liked) {
Expand All @@ -67,6 +70,7 @@ const reactToPostHandler: reactToPost = async (req, res) => {
await repository.unlikePost(req.userId, postId)
}
} catch (error) {
log.error(error)
return res.status(StatusCodes.NOT_FOUND).end()
}

Expand Down
10 changes: 10 additions & 0 deletions backend/src/controllers/posts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OpenGraph, Post } from '@prisma/client'
import { Request, Response } from 'express'
import asyncHandler from 'express-async-handler'
import { ParamsDictionary } from 'express-serve-static-core/index'
Expand Down Expand Up @@ -30,6 +31,15 @@ const getPostsHandler: getPosts = async (req, res) => {
export const getPosts = asyncHandler(getPostsHandler)

const createPostsHandler: createPosts = async (req, res) => {
const promises: [Promise<{ count: number }>, Promise<OpenGraph>?] = [
repository.ensureCategoriesExist(req.body.categories as string[])
]

if (req.body.resource) {
promises.push(repository.getOrCreateOpenGraph(req.body.resource))
}

await Promise.all(promises)
const response = await repository.createPost(req.userId, req.body)

return res.status(StatusCodes.CREATED).end()
Expand Down
2 changes: 1 addition & 1 deletion backend/src/data/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const dbPostToPostDto = (post: dbPost, user: User): Components.Schemas.Po
}
: undefined,
reactions: {
isPersonallyLiked: post.likedUsers.includes(user),
isPersonallyLiked: post.likedUsers.map(user => user.id).includes(user.id),
likes: post.likedUsers.reduce((acc, _) => acc + 1, 0)
},
user: dbUserToUserDto(user)
Expand Down
21 changes: 17 additions & 4 deletions backend/src/data/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,37 @@ const repository = {
content: {
contains: query.searchQuery
},
categories: {
hasEvery: query.searchCategories
}
categories: query.searchCategories
? {
hasEvery: query.searchCategories
}
: undefined
},
include: {
user: true,
likedUsers: true,
openGraph: true
},
take: query.pageSize,
skip: query.pageSize && query.offset ? query.pageSize * query.offset : undefined
skip: query.pageSize && query.offset ? query.pageSize * query.offset : undefined,
orderBy: {
timestampCreated: 'desc'
}
})
},

async getCategories() {
return await prisma.category.findMany()
},

async ensureCategoriesExist(categories: string[]) {
const created = await prisma.category.createMany({
data: categories.map(category => ({ name: category })),
skipDuplicates: true
})
return created
},

async createCategory(name: string) {
return await prisma.category.create({
data: {
Expand Down
34 changes: 34 additions & 0 deletions backend/src/scripts/get-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import dotenv from 'dotenv'
dotenv.config()

import axios from 'axios'
import { applicationDefault, initializeApp } from 'firebase-admin/app'
import admin from 'firebase-admin'

const [, , ...params] = process.argv

initializeApp({
credential: applicationDefault(),
databaseURL: 'https://learning-drive-fe98e.firebaseio.com'
})

const getToken = async (uid: string) => {
try {
const customToken = await admin.auth().createCustomToken(uid)

const res = await axios({
url: `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${process.env.FIREBASE_API_KEY}`,
method: 'post',
data: {
token: customToken,
returnSecureToken: true
}
})

console.log(res.data.idToken)
} catch (error) {
console.log(error)
}
}

getToken(params[0])
1 change: 1 addition & 0 deletions backend/src/types/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare global {
PORT?: string
GOOGLE_APPLICATION_CREDENTIALS?: string
DEFAULT_PICTURE_LINK: string
FIREBASE_API_KEY: string
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/types/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ declare namespace Components {
/**
* The unique identifier of the user
* example:
* 7234a92f-185a-4e86-8276-54e3fa7b67c6
* 6SZoYTBzY6ewF5Rkko7hi1QMF8C3
*/
_id: string;
/**
Expand Down
1 change: 0 additions & 1 deletion backend/src/util/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ initializeApp({

const log = Logger.getLogger('verification')

// function used to determine user authentication
export default async (token: string) => {
try {
const decodedToken = await getAuth().verifyIdToken(token)
Expand Down
9 changes: 8 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"dependencies": {
"@emotion/react": "^11.8.1",
"@emotion/styled": "^11.8.1",
"@hookform/resolvers": "^2.8.8",
"@mui/lab": "^5.0.0-alpha.71",
"@mui/material": "^5.4.4",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
Expand All @@ -13,18 +15,23 @@
"@types/node": "^16.7.13",
"@types/react": "^17.0.20",
"@types/react-dom": "^17.0.9",
"axios": "^0.26.0",
"date-fns": "^2.28.0",
"firebase": "^9.6.7",
"jest-canvas-mock": "^2.3.1",
"notistack": "^2.0.3",
"react": "^17.0.2",
"react-dialog-async": "^1.0.8",
"react-dom": "^17.0.2",
"react-hook-form": "^7.27.1",
"react-infinite-scroll-component": "^6.1.0",
"react-router-dom": "6",
"react-scripts": "5.0.0",
"react-useanimations": "^2.0.8",
"swr": "^1.2.2",
"typescript": "^4.4.2",
"web-vitals": "^2.1.0"
"web-vitals": "^2.1.0",
"yup": "^0.32.11"
},
"scripts": {
"start": "react-scripts start",
Expand Down
5 changes: 1 addition & 4 deletions frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
Expand Down
2 changes: 1 addition & 1 deletion frontend/scripts/generateSwaggerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ console.log(`Generating client API from spec at ${specURL}`);

// Generate the client api
exec(
`openapi-generator-cli generate -g typescript-axios --additional-properties=useSingleRequestParameter=true,withoutPrefixEnums=true, -i ${specURL} -o ${tempDir}`,
`openapi-generator-cli generate -g typescript-axios --additional-properties=withoutPrefixEnums=true, -i ${specURL} -o ${tempDir}`,
(err, stdout, stderr) => {
if (err) {
console.log(err);
Expand Down
Loading