Skip to content

refactor: drop type Author from comments table#83

Merged
ayush00git merged 10 commits into
mainfrom
fix/post-author
Jun 7, 2026
Merged

refactor: drop type Author from comments table#83
ayush00git merged 10 commits into
mainfrom
fix/post-author

Conversation

@ayush00git

@ayush00git ayush00git commented Jun 4, 2026

Copy link
Copy Markdown
Owner

References #80 #85

Design issues -
the comment struct holds

Author      Admin

to allow authors of types (faculty, warden and centrehead) to comment we need to change the struct design.


Claude documentation of the PR -

PR #83 — refactor: drop type Author from comments table

Branch: fix/post-authormain
Merged: Jun 7, 2026
Label: refactor
References: #80, #85


Problem

The Comment struct held a hard-typed Author Admin association field:

type Comment struct {
    AuthorID uint
    Author   Admin `gorm:"foreignKey:AuthorID"`
    // ...
}

This restricted comment authorship to only Admin users. Faculty, Warden, and CentreHead users could not comment on their own posts since they are not of type Admin.


Design Decision

Replaced AuthorID and Author Admin fields with flat email and role string fields stored directly on the comment at insert time.

Before:

{
  "id": 1,
  "author_id": 1,
  "Author": {
    "id": 1,
    "email": "xen.civil@nith.ac.in",
    "password": "",
    "position": "XEN_Civil",
    "is_verified": false,
    "created_at": "0001-01-01T00:00:00Z"
  }
}

After:

{
  "id": 1,
  "email": "xen.civil@nith.ac.in",
  "role": "XEN_Civil",
  "comment_text": "Inspection scheduled.",
  "created_at": "2026-05-28T13:53:08.190581+05:30"
}

Changes

models/comment.go

  • Dropped AuthorID uint and Author Admin fields
  • Added AuthorEmail string and AuthorRole string stored at insert time
  • Removes the need for any Preload("Author") on comment queries

handlers/ — comment handlers

  • Updated all comment creation handlers (AdminPostComment, FacultyPostComment, WardenPostComment, CentreHeadPostComment) to populate AuthorEmail and AuthorRole from the logged-in user at insert time
  • Added warden and centrehead post comment handlers (feat: add warden and centrehead post comment handlers)

routes/

  • Registered new comment routes for warden and centrehead post types

handlers/admin_post.go

  • AdminGetPost now returns a server-driven response instead of raw model dump

Side Effect Discovered

Removing the Preload("Author") on comments revealed that CommentableType and CommentableID are the actual fields GORM uses internally to resolve polymorphic associations — these were previously being implicitly resolved via the nested .Select on Author.


Commits

Commit Message
e7a0930 fix: allow author to comment
4054f96 fix: struct now drops Author admin model
bd2a147 feat: refined the preloaded author
403bc8e added tests
3c0eb0e feat: add warden and centrehead post comment handlers
c770c6c feat: registered comment routes
8ae5f3d feat: add test cases
39d4f87 fix: AdminGetPost now returns server-driven response
65534f6 fix: api response refactored
357ffbb fix: comment box for users to add comments

Why This Approach

  • Simpler reads — no joins or preloads needed to get author info on comments
  • Multi-role support — any user type (admin, faculty, warden, centrehead) can now comment
  • Cleaner API response — removes nested object with zero-value fields like password, is_verified
  • Slight performance gain — one less DB query per comment fetch (no author lookup)
  • Trade-off: denormalized data — if a user changes their email, old comments won't reflect it (acceptable for a CMS)

@ayush00git

Copy link
Copy Markdown
Owner Author

I propose replacing the struct fields AuthorID and Author with Email and role only. reason - these are the most basics fields that we want to know about the comment author.

api response -

 "comments": [
      {
        "id": 1,
        "CommentableID": 1,
        "CommentableType": "faculty_posts",
        "comment_text": "Inspection scheduled; the maintenance team will visit the site shortly.",
        "author_id": 1,
        "Author": {
          "id": 1,
          "email": "xen.civil@nith.ac.in",
          "password": "",
          "position": "XEN_Civil",
          "is_verified": false,
          "created_at": "0001-01-01T00:00:00Z"
        },
        "created_at": "2026-05-28T13:53:08.190581+05:30",
        "updated_at": "2026-05-28T13:53:08.190581+05:30"
      },

resolves to -

 "comments": [
      {
        "id": 1,
        "CommentableID": 1,
        "CommentableType": "faculty_posts",
        "comment_text": "Inspection scheduled; the maintenance team will visit the site shortly.",
        "email": "xen.civil@nith.ac.in",
        "role": "XEN",
        "created_at": "2026-05-28T13:53:08.190581+05:30",
        "updated_at": "2026-05-28T13:53:08.190581+05:30"
      },

@ayush00git ayush00git added refactor and removed design labels Jun 7, 2026
@ayush00git

Copy link
Copy Markdown
Owner Author

After the design changes were made there is no need to define the Preload field on comments' Author that was done previously. This also showed a hidden logic, that for fetching comments we needed the CommentableType and CommentableID which helps pointing to the exact table. previously these fields were automatically fetched because the .Select method was used on the nested field Author of 'Comment'.

@ayush00git ayush00git changed the title fix: allow author to comment refactor: drop type Author from comments table Jun 7, 2026
@ayush00git ayush00git merged commit e2246b8 into main Jun 7, 2026
1 check passed
@ayush00git ayush00git deleted the fix/post-author branch June 7, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant