Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save button draft #30

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions backend/src/models/user.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,6 +12,9 @@ class User extends BaseEntity {

@Column()
name: string;

@Column()
savedPosts: mongodb.ObjectId[];
}

export default User;
15 changes: 13 additions & 2 deletions backend/src/routes/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's change this to store the username - you can grab it by finding the user model first via id then grabbing the username from the user model. you'll also need to change (in the post model file) the author field to be a string instead of a user model

post.numLikes = 0;

const requestBody = ctx.request.body as any;
Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctx.state.user.id is just their id, so you'll have to use findOneBy with this id to grab the model first

await user.save();
ctx.body = user.savedPosts;
});
router.delete('/', async (ctx, next) => {
const post = await Post.find();
ctx.body = await Post.remove(post);
Expand Down
4 changes: 3 additions & 1 deletion lib/widgets/post_action_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class PostSongDetails extends StatelessWidget {
PostActionButton(
icon: Icons.bookmark_border,
text: "Save",
onPressed: () {},
onPressed: () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be filled in now - ahmed added the structure for making requests like this. essentially u can do this in steps:

  1. add a method to the PostRepository class called savePost - you can model it after the existing ones to make the post request to the route you added
  2. add a corresponding savePost method to the PostController class
  3. use the post controller in your post_action_button.dart file (in onPressed) to call savePost (example here)


},
),
],
)
Expand Down