Domain Layer

Directory: domain/

The Domain layer is the very core of the application. It represents the business concepts and rules, completely independent of any technology or implementation detail. This layer is the most stable and should change the least often.

In this project, the domain/ package contains two main components:

  1. Entities (Models): These are the Go structs that represent the core data structures of the business. They contain the data fields and may include validation tags, but they do not contain logic related to saving, retrieving, or transmitting this data.
  2. Domain-Specific Errors: Custom error types that represent known business rule failures (e.g., ErrNotFound, ErrConflict).

Entities

Article

The Article entity defines the structure of an article within the system.

File: domain/article.go

package domain

import (
    "time"
)

// Article is representing the Article data struct
type Article struct {
    ID        int64     `json:"id"`
    Title     string    `json:"title" validate:"required"`
    Content   string    `json:"content" validate:"required"`
    Author    Author    `json:"author"`
    UpdatedAt time.Time `json:"updated_at"`
    CreatedAt time.Time `json:"created_at"`
}

  • json tags are used for serialization/deserialization, primarily in the Delivery layer.
  • validate tags are used for request body validation, also in the Delivery layer.
  • Notice how Article embeds the Author struct, representing the relationship between them.

Author

The Author entity defines the structure of an article's author.

File: domain/author.go

package domain

// Author representing the Author data struct
type Author struct {
    ID        int64  `json:"id"`
    Name      string `json:"name"`
    CreatedAt string `json:"created_at"`
    UpdatedAt string `json:"updated_at"`
}

Domain Errors

Defining standard errors at the domain level allows for consistent error handling across all layers of the application. The service layer can return these errors, and the delivery layer can map them to appropriate HTTP status codes.

File: domain/errors.go

package domain

import "errors"

var (
    // ErrInternalServerError will throw if any the Internal Server Error happen
    ErrInternalServerError = errors.New("internal Server Error")
    // ErrNotFound will throw if the requested item is not exists
    ErrNotFound = errors.New("your requested Item is not found")
    // ErrConflict will throw if the current action already exists
    ErrConflict = errors.New("your Item already exist")
    // ErrBadParamInput will throw if the given request-body or params is not valid
    ErrBadParamInput = errors.New("given Param is not valid")
)
These errors provide a clear contract for failure scenarios within the business logic, without leaking implementation-specific details (like a sql.ErrNoRows).