refactor(backend): switch to DDD

This commit is contained in:
2025-10-09 00:59:30 +03:00
parent 4e0fc431e2
commit c39d82fafd
9 changed files with 461 additions and 343 deletions
@@ -0,0 +1,44 @@
package rest
import (
"fmt"
"net/http"
"tanabata/internal/domain"
)
type ErrorResponse struct {
Error string `json:"error"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
type ErrorMapper struct{}
func (m *ErrorMapper) MapError(err domain.DomainError) (int, ErrorResponse) {
switch err.Code {
case domain.ErrFileNotFound:
return http.StatusNotFound, ErrorResponse{
Error: "Not Found",
Code: string(err.Code),
Message: fmt.Sprintf("File %q not found", err.Details...),
}
case domain.ErrMIMENotSupported:
return http.StatusNotFound, ErrorResponse{
Error: "MIME not supported",
Code: string(err.Code),
Message: fmt.Sprintf("MIME not supported: %q", err.Details...),
}
case domain.ErrValidation:
return http.StatusNotFound, ErrorResponse{
Error: "Bad Request",
Code: string(err.Code),
Message: fmt.Sprintf("Invalid %s: %s", err.Details...),
}
}
return http.StatusInternalServerError, ErrorResponse{
Error: "Internal Server Error",
Code: string(err.Code),
Message: "An unexpected error occured",
}
}