Full-featured ORM library for Golang with associations, hooks, transactions, migrations, and developer-friendly chainable API
Overall
score
81%
Build a comment system that supports multiple media types (posts, videos, images) where comments can be attached to any of these media types through a single polymorphic association.
You need to implement a database-backed comment system with the following features:
Media Models: Create three types of media that can receive comments:
Comment Model: Create a comment model that can be attached to any media type through a polymorphic association:
Database Operations: Implement the following operations:
@generates
package main
// Post represents a blog post
type Post struct {
ID uint
Title string
Content string
Comments []Comment
}
// Video represents a video
type Video struct {
ID uint
Title string
URL string
Comments []Comment
}
// Image represents an image
type Image struct {
ID uint
Title string
URL string
Comments []Comment
}
// Comment represents a comment on any media type
type Comment struct {
ID uint
Body string
CommentableID uint
CommentableType string
}
// SetupDatabase initializes the database and migrates tables
func SetupDatabase() error
// CreatePost creates a new post with the given title and content
func CreatePost(title, content string) (*Post, error)
// CreateVideo creates a new video with the given title and URL
func CreateVideo(title, url string) (*Video, error)
// CreateImage creates a new image with the given title and URL
func CreateImage(title, url string) (*Image, error)
// AddCommentToPost adds a comment to a post
func AddCommentToPost(post *Post, body string) error
// AddCommentToVideo adds a comment to a video
func AddCommentToVideo(video *Video, body string) error
// AddCommentToImage adds a comment to an image
func AddCommentToImage(image *Image, body string) error
// GetCommentsForPost retrieves all comments for a post
func GetCommentsForPost(postID uint) ([]Comment, error)
// GetCommentsForVideo retrieves all comments for a video
func GetCommentsForVideo(videoID uint) ([]Comment, error)
// GetCommentsForImage retrieves all comments for an image
func GetCommentsForImage(imageID uint) ([]Comment, error)
// GetCommentWithType retrieves a comment and returns its type and ID
func GetCommentWithType(commentID uint) (*Comment, error)Provides ORM functionality with support for polymorphic associations.
Install with Tessl CLI
npx tessl i tessl/golang-gorm-io--gormdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10