diff --git a/backend/src/models/user.ts b/backend/src/models/user.ts index b5a9482..6cd9dee 100644 --- a/backend/src/models/user.ts +++ b/backend/src/models/user.ts @@ -1,5 +1,6 @@ import { Entity, ObjectIdColumn, Column, BaseEntity } from 'typeorm'; import mongodb from 'mongodb'; +import Post from '../models/post'; @Entity() class User extends BaseEntity { @@ -11,6 +12,9 @@ class User extends BaseEntity { @Column() name: string; + + @Column() + savedPosts: mongodb.ObjectId[]; } export default User; diff --git a/backend/src/routes/post.ts b/backend/src/routes/post.ts index e625290..01fbf54 100644 --- a/backend/src/routes/post.ts +++ b/backend/src/routes/post.ts @@ -2,6 +2,7 @@ import Router from '@koa/router'; import Post from '../models/post'; import mongodb from 'mongodb'; import bodyParser from 'koa-bodyparser'; +import User from '../models/user'; const router = new Router({ prefix: '/posts', @@ -25,7 +26,7 @@ router.get('/:postID', async (ctx, next) => { router.post('/', async (ctx, next) => { const post = new Post(); post.id = new mongodb.ObjectId(); - post.author = null; //This is dependent on Auth to determine the author + post.author = ctx.state.user.id; post.numLikes = 0; const requestBody = ctx.request.body as any; @@ -58,7 +59,17 @@ router.post('/:postID/unlike', async (ctx, next) => { await post.save(); ctx.body = post; }); - +router.post('/:postID/save', async (ctx, next) => { + const post = await Post.findOneBy(mongodb.ObjectId(ctx.params.postID)); + if (post === null) { + ctx.status = 404; + return; + } + const user = ctx.state.user.id; + user.savedPosts.push(post.id); + await user.save(); + ctx.body = user.savedPosts; +}); router.delete('/', async (ctx, next) => { const post = await Post.find(); ctx.body = await Post.remove(post); diff --git a/lib/widgets/post_action_button.dart b/lib/widgets/post_action_button.dart index 2177282..f9f0224 100644 --- a/lib/widgets/post_action_button.dart +++ b/lib/widgets/post_action_button.dart @@ -47,7 +47,9 @@ class PostSongDetails extends StatelessWidget { PostActionButton( icon: Icons.bookmark_border, text: "Save", - onPressed: () {}, + onPressed: () { + + }, ), ], )