Skip to content

majiayu000/spellbook / skills/golang-web

golang-web

Unverified

Modern Go Web application architecture guide. Use when creating new Go web projects, APIs, or microservices. Covers project structure, tech stack selection, and best practices based on Go standards.

templates/project/internal/handlers/user.go

View on GitHub
// internal/handlers/user.go
package handlers

import (
	"github.com/gin-gonic/gin"
	"github.com/yourname/myapp/internal/services"
	"github.com/yourname/myapp/pkg/errors"
	"github.com/yourname/myapp/pkg/response"
)

// UserHandler handles user-related HTTP requests
type UserHandler struct {
	service services.UserService
}

// NewUserHandler creates a new UserHandler
func NewUserHandler(service services.UserService) *UserHandler {
	return &UserHandler{service: service}
}

// Create handles POST /users
func (h *UserHandler) Create(c *gin.Context) {
	var input services.CreateUserInput
	if err := c.ShouldBindJSON(&input); err != nil {
		response.Error(c, errors.ErrInvalidParams)
		return
	}

	user, err := h.service.Create(c.Request.Context(), input)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.Created(c, user)
}

// Get handles GET /users/:id
func (h *UserHandler) Get(c *gin.Context) {
	id := c.Param("id")

	user, err := h.service.GetByID(c.Request.Context(), id)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.Success(c, user)
}

// Update handles PUT /users/:id
func (h *UserHandler) Update(c *gin.Context) {
	id := c.Param("id")

	var input services.UpdateUserInput
	if err := c.ShouldBindJSON(&input); err != nil {
		response.Error(c, errors.ErrInvalidParams)
		return
	}

	user, err := h.service.Update(c.Request.Context(), id, input)
	if err != nil {
		response.Error(c, err)
		return
	}

	response.Success(c, user)
}

// Delete handles DELETE /users/:id
func (h *UserHandler) Delete(c *gin.Context) {
	id := c.Param("id")

	if err := h.service.Delete(c.Request.Context(), id); err != nil {
		response.Error(c, err)
		return
	}

	response.NoContent(c)
}
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall