controller

package
v0.0.0-...-a1c72dd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package controller handles HTTP request/response flow. Converts external input (JSON, URL params) into domain objects and calls use cases. Returns results or errors to clients.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BookController

type BookController struct {
	// contains filtered or unexported fields
}

BookController handles http requests, validates them and transform them into domain objects. The domain objects are then passed to the usecase layer, executing the business logic.

func NewBookController

func NewBookController(
	logger *slog.Logger,
	interactor BookInteractor,
	bookPresenter BookPresenter,
	errPresenter ErrorPresenter,
) *BookController

NewBookController creates a new instance of BookController.

func (*BookController) CreateBook

func (bc *BookController) CreateBook(w http.ResponseWriter, r *http.Request)

CreateBook handles CreateBookRequest over http.

func (*BookController) DeleteBook

func (bc *BookController) DeleteBook(w http.ResponseWriter, r *http.Request)

DeleteBook handles delete book by ID requests over http.

func (*BookController) GetBook

func (bc *BookController) GetBook(w http.ResponseWriter, r *http.Request)

GetBook handles read book by ID requests over http.

func (*BookController) ListBooks

func (bc *BookController) ListBooks(w http.ResponseWriter, _ *http.Request)

ListBooks handles read books requests over http.

func (*BookController) UpdateBook

func (bc *BookController) UpdateBook(w http.ResponseWriter, r *http.Request)

UpdateBook handles UpdateBookRequest over http.

type BookInteractor

type BookInteractor interface {
	// CreateBook sends the book to be created to the underlying repository.
	CreateBook(book *domain.Book) error
	// GetBook retrieves a domain.Book by its ID.
	GetBook(id string) (*domain.Book, error)
	// ListBooks retrieves a list of books.
	ListBooks() ([]*domain.Book, error)
	// UpdateBook updates a single book by its ID.
	UpdateBook(book *domain.Book) error
	// DeleteBook removes a book from the repository.
	DeleteBook(id string) error
}

BookInteractor is the interface an interactor must implement to be used by the BookController to execute business logic.

type BookPresenter

type BookPresenter interface {
	// Present prepares the domain.Book message to be returned.
	Present(book *domain.Book) map[string]any
}

BookPresenter is the interface a presenter must implement to be used by the BookController to return successful responses.

type CreateBookRequest

type CreateBookRequest struct {
	Title  string `json:"title"`
	Author string `json:"author"`
	Price  int    `json:"price"`
}

CreateBookRequest defines the expected request for create.

func (*CreateBookRequest) Validate

func (r *CreateBookRequest) Validate() error

Validate a CreateBookRequest.

type ErrorPresenter

type ErrorPresenter interface {
	// Present prepares the error message to be returned through w.
	Present(w http.ResponseWriter, err error, code int)
}

ErrorPresenter is the interface a presenter must implement to be used by the BookController to return error responses.

type UpdateBookRequest

type UpdateBookRequest struct {
	ID    string `json:"id"`
	Price int    `json:"price"`
}

UpdateBookRequest defines the expected request for update.

func (*UpdateBookRequest) Validate

func (r *UpdateBookRequest) Validate() error

Validate an UpdateBookRequest.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL