{"id":135440,"date":"2024-09-06T14:15:51","date_gmt":"2024-09-06T08:45:51","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135440"},"modified":"2024-09-06T14:15:51","modified_gmt":"2024-09-06T08:45:51","slug":"creating-a-table-for-post-likes","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/","title":{"rendered":"Creating a Table for Post Likes"},"content":{"rendered":"\n<p>To allow users to like or dislike posts in your social media application, you&#8217;ll need to create a separate table to store these interactions. This table will typically have a foreign key referencing the user and the post involved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating the <code>PostLikes<\/code> Table<\/strong><\/h2>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sqlalchemy import Column, Integer, ForeignKey\n\nclass PostLike(Base):\n    __tablename__ = \"post_likes\"\n\n    id = Column(Integer, primary_key=True, index=True)\n    user_id = Column(Integer, ForeignKey(\"users.id\")) &nbsp; \n    post_id = Column(Integer, ForeignKey(\"posts.id\"))\n\n    user = relationship(\"User\", back_populates=\"liked_posts\")\n    post = relationship(\"Post\", back_populates=\"likes\") &nbsp; \n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>PostLike<\/code> model represents a like or dislike interaction between a user and a post.<\/li>\n\n\n\n<li>The <code>user_id<\/code> and <code>post_id<\/code> columns reference the corresponding <code>User<\/code> and <code>Post<\/code> records.<\/li>\n\n\n\n<li>The <code>relationship<\/code> statements define the relationships between the <code>PostLike<\/code> model and the <code>User<\/code> and <code>Post<\/code> models.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Endpoints for Liking and Disliking Posts<\/strong><\/h2>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@router.post(\"\/posts\/{post_id}\/like\")\nasync def like_post(post_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):\n    # Check if the user has already liked or disliked the post\n    existing_like = await db.query(PostLike).filter(PostLike.user_id == current_user.id, PostLike.post_id == post_id).first()\n    if existing_like:\n        return {\"message\": \"You have already liked or disliked this post\"}\n\n    # Create a new like\n    like = PostLike(user_id=current_user.id, post_id=post_id)\n    db.add(like)\n    await db.commit()\n    await db.refresh(like)\n    return {\"message\": \"Post liked\"}\n\n@router.delete(\"\/posts\/{post_id}\/like\")\nasync def unlike_post(post_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):\n    # Find the existing like\n    existing_like = await db.query(PostLike).filter(PostLike.user_id == current_user.id, PostLike.post_id == post_id).first()\n    if not existing_like:\n        return {\"message\": \"You have not liked or disliked this post\"}\n\n    # Delete the like\n    db.delete(existing_like)\n    await db.commit()\n    return {\"message\": \"Post unliked\"}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Retrieving Post Likes<\/strong><\/h2>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@router.get(\"\/posts\/{post_id}\/likes\")\nasync def get_post_likes(post_id: int, db: Session = Depends(get_db)):\n    likes = await db.query(PostLike).filter(PostLike.post_id == post_id).all()\n    return likes\n<\/code><\/pre>\n\n\n\n<p>By following these steps, you can effectively implement post liking functionality in your FastAPI application, allowing users to interact with and engage with the content on your platform.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To allow users to like or dislike posts in your social media application, you&#8217;ll need to create a separate table to store these interactions. This table will typically have a foreign key referencing the user and the post involved. Creating the PostLikes Table Python Explanation: Creating Endpoints for Liking and Disliking Posts Python Retrieving Post&#8230;<\/p>\n","protected":false},"author":16,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-135440","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating a Table for Post Likes - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating a Table for Post Likes - Tutorial\" \/>\n<meta property=\"og:description\" content=\"To allow users to like or dislike posts in your social media application, you&#8217;ll need to create a separate table to store these interactions. This table will typically have a foreign key referencing the user and the post involved. Creating the PostLikes Table Python Explanation: Creating Endpoints for Liking and Disliking Posts Python Retrieving Post...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/\",\"name\":\"Creating a Table for Post Likes - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-06T08:45:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating a Table for Post Likes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating a Table for Post Likes - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/","og_locale":"en_US","og_type":"article","og_title":"Creating a Table for Post Likes - Tutorial","og_description":"To allow users to like or dislike posts in your social media application, you&#8217;ll need to create a separate table to store these interactions. This table will typically have a foreign key referencing the user and the post involved. Creating the PostLikes Table Python Explanation: Creating Endpoints for Liking and Disliking Posts Python Retrieving Post...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/","name":"Creating a Table for Post Likes - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-06T08:45:51+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-a-table-for-post-likes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Creating a Table for Post Likes"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=135440"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135440\/revisions"}],"predecessor-version":[{"id":135472,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135440\/revisions\/135472"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}