chore: lab import lab4 backend and infrastructure as baseline

This commit is contained in:
Голиков Maximillian Антон
2026-11-06 20:21:23 +03:00
parent 276dbdad8a
commit 4bba40d098
68 changed files with 5315 additions and 0 deletions
@@ -0,0 +1,50 @@
package dto
import (
"encoding/hex"
"high-loud-apps-labs/internal/domain"
)
type VariantResponse struct {
Name string `json:"name"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
Width int `json:"width"`
Height int `json:"height"`
SHA256 string `json:"sha256"`
}
type AttachmentResponse struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
OwnerID string `json:"owner_id"`
Filename string `json:"filename"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
SHA256 string `json:"sha256"`
Status string `json:"status"`
Variants []VariantResponse `json:"variants"`
ErrorCode *string `json:"error_code"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
func AttachmentFromDomain(a *domain.Attachment) AttachmentResponse {
variants := make([]VariantResponse, 0)
errCode := a.ErrorCode
return AttachmentResponse{
ID: a.ID,
ChatID: a.ChatID,
OwnerID: a.OwnerID,
Filename: a.Filename,
ContentType: a.ContentType,
SizeBytes: a.SizeBytes,
SHA256: hex.EncodeToString(a.SHA256),
Status: string(a.Status),
Variants: variants,
ErrorCode: errCode,
CreatedAt: a.CreatedAt.UTC().Format(timeFormat),
UpdatedAt: a.UpdatedAt.UTC().Format(timeFormat),
}
}
+39
View File
@@ -0,0 +1,39 @@
package dto
import (
"high-loud-apps-labs/internal/domain"
)
type CreateChatRequest struct {
Title string `json:"title" binding:"required,min=1,max=100"`
MemberIDs []string `json:"member_ids" binding:"required,min=1,max=100,dive,uuid"`
}
type ChatResponse struct {
ID string `json:"id"`
Title string `json:"title"`
MemberIDs []string `json:"member_ids"`
CreatedAt string `json:"created_at"`
}
type ChatPageResponse struct {
Items []ChatResponse `json:"items"`
NextCursor *string `json:"next_cursor"`
}
func ChatFromDomain(c *domain.Chat) ChatResponse {
return ChatResponse{
ID: c.ID,
Title: c.Title,
MemberIDs: append([]string(nil), c.MemberIDs...),
CreatedAt: c.CreatedAt.UTC().Format(timeFormat),
}
}
func ChatsFromDomain(items []domain.Chat) []ChatResponse {
out := make([]ChatResponse, len(items))
for i := range items {
out[i] = ChatFromDomain(&items[i])
}
return out
}
+86
View File
@@ -0,0 +1,86 @@
package dto
import (
"errors"
"high-loud-apps-labs/internal/domain"
)
// делаем вложенный ответ через вкладку ErrorBody в обертку.
type ErrorBody struct {
Code string `json:"code"`
Message string `json:"message"`
RequestID string `json:"request_id"`
}
type ErrorResponse struct {
Error ErrorBody `json:"error"`
}
type coded interface {
Code() domain.ErrorCode
}
func ErrorFromDomain(err error, requestID string) ErrorResponse {
var c coded
if errors.As(err, &c) {
code := c.Code()
message := err.Error()
if code == domain.CodeInternalError {
message = "внутренняя ошибка"
}
return ErrorResponse{Error: ErrorBody{
Code: string(code),
Message: message,
RequestID: requestID,
}}
}
code := domain.CodeInternalError
switch {
case errors.Is(err, domain.ErrNotFound):
code = domain.CodeNotFound
case errors.Is(err, domain.ErrAlreadyExists):
code = domain.CodeConflict
case errors.Is(err, domain.ErrInvalidInput):
code = domain.CodeInvalidRequest
case errors.Is(err, domain.ErrUnauthorized):
code = domain.CodeUnauthorized
case errors.Is(err, domain.ErrUnsupportedMediaType):
code = domain.CodeUnsupportedMediaType
case errors.Is(err, domain.ErrUnavailable):
code = domain.CodeUnavailable
}
message := err.Error()
if code == domain.CodeInternalError {
message = "внутренняя ошибка"
}
return ErrorResponse{Error: ErrorBody{
Code: string(code),
Message: message,
RequestID: requestID,
}}
}
func StatusFromError(err error) int {
switch {
case errors.Is(err, domain.ErrNotFound):
return 404
case errors.Is(err, domain.ErrAlreadyExists):
return 409
case errors.Is(err, domain.ErrInvalidInput):
return 400
case errors.Is(err, domain.ErrUnauthorized):
return 401
case errors.Is(err, domain.ErrUnsupportedMediaType):
return 415
case errors.Is(err, domain.ErrUnavailable):
return 503
case errors.Is(err, domain.ErrPayloadTooLarge):
return 413
case errors.Is(err, domain.ErrConflict):
return 409
default:
return 500
}
}
+5
View File
@@ -0,0 +1,5 @@
package dto
type HealthResponse struct {
Status string `json:"status"`
}
+47
View File
@@ -0,0 +1,47 @@
package dto
import (
"high-loud-apps-labs/internal/domain"
)
type CreateMessageRequest struct {
Text string `json:"text" binding:"max=4000"`
AttachmentIDs []string `json:"attachment_ids" binding:"max=10,dive,uuid"`
}
type MessageResponse struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
AttachmentIDs []string `json:"attachment_ids"`
CreatedAt string `json:"created_at"`
}
type MessagePageResponse struct {
Items []MessageResponse `json:"items"`
NextCursor *string `json:"next_cursor"`
}
func MessageFromDomain(m *domain.Message) MessageResponse {
ids := m.AttachmentIDs
if ids == nil {
ids = []string{}
}
return MessageResponse{
ID: m.ID,
ChatID: m.ChatID,
SenderID: m.SenderID,
Text: m.Text,
AttachmentIDs: ids,
CreatedAt: m.CreatedAt.UTC().Format(timeFormat),
}
}
func MessagesFromDomain(items []domain.Message) []MessageResponse {
out := make([]MessageResponse, len(items))
for i := range items {
out[i] = MessageFromDomain(&items[i])
}
return out
}
+21
View File
@@ -0,0 +1,21 @@
package dto
import (
"high-loud-apps-labs/internal/domain"
)
type SessionResponse struct {
Token string `json:"token"`
TokenType string `json:"token_type"`
ExpiresAt string `json:"expires_at"`
User UserResponse `json:"user"`
}
func SessionResponseFromDomain(s *domain.Session, token string, u *domain.User) SessionResponse {
return SessionResponse{
Token: token,
TokenType: "Bearer",
ExpiresAt: s.ExpiresAt.UTC().Format(timeFormat),
User: UserFromDomain(u),
}
}
+29
View File
@@ -0,0 +1,29 @@
package dto
import (
"high-loud-apps-labs/internal/domain"
)
const timeFormat = "2006-01-02T15:04:05.000000Z07:00"
type CreateUserRequest struct {
Username string `json:"username" binding:"required,min=3,max=32"`
DisplayName string `json:"display_name" binding:"required,min=1,max=100"`
Password string `json:"password" binding:"required,min=12,max=512"`
}
type UserResponse struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
CreatedAt string `json:"created_at"`
}
func UserFromDomain(u *domain.User) UserResponse {
return UserResponse{
ID: u.ID,
Username: u.Username,
DisplayName: u.DisplayName,
CreatedAt: u.CreatedAt.UTC().Format(timeFormat),
}
}