Controllers

// app/controllers/post_controller/postcontroller.go
package postcontroller

import (
	"context"
	"fmt"

	"sample_app/app/controllers"
	"sample_app/services/postservice"
)

type PostsController struct {
	controllers.BaseController
	posts postservice.Service
}

func New(ctx context.Context) (*PostsController, error) {
	base, err := controllers.NewBaseController(ctx)
	if err != nil {
		return nil, err
	}
	posts, ok := postservice.FromContext(ctx)
	if !ok {
		return nil, fmt.Errorf("post service is missing from application context")
	}
	return &PostsController{BaseController: base, posts: posts}, nil
}

func (c *PostsController) Show(postID int) error {
	post, err := c.posts.Find(postID)
	if err != nil {
		return err
	}
	c.Set("post", post)
	return nil
}