CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/golang-gorm-io--gorm

tessl install tessl/golang-gorm-io--gorm@1.31.0

Full-featured ORM library for Golang with associations, hooks, transactions, migrations, and developer-friendly chainable API

Agent Success

Agent success rate when using this tile

81%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.13x

Baseline

Agent success rate without this tile

72%

task.mdevals/scenario-9/

Media Comment System

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.

Requirements

You need to implement a database-backed comment system with the following features:

  1. Media Models: Create three types of media that can receive comments:

    • Post: Has a title and content
    • Video: Has a title and URL
    • Image: Has a title and URL
  2. Comment Model: Create a comment model that can be attached to any media type through a polymorphic association:

    • Each comment has a body (text content)
    • Each comment is associated with a commentable entity (Post, Video, or Image)
    • Comments should track which type of entity they belong to and the entity's ID
  3. Database Operations: Implement the following operations:

    • Create posts, videos, and images
    • Add comments to posts, videos, and images
    • Query all comments for a specific media item
    • Retrieve a comment and identify which media type it belongs to

Implementation Guidelines

  • Use appropriate database schema design to support polymorphic relationships
  • Ensure comments can be efficiently queried for any media type
  • Handle the relationship such that a single comment model works for all media types

Test Cases

  • Creating a post with a comment retrieves the comment correctly @test
  • Creating a video with a comment retrieves the comment correctly @test
  • Creating an image with a comment retrieves the comment correctly @test
  • Querying a comment reveals its associated media type and ID @test

@generates

API

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)

Dependencies { .dependencies }

gorm.io/gorm { .dependency }

Provides ORM functionality with support for polymorphic associations.

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/gorm.io/gorm@v1.31.1
tile.json