feat: port backend implementation from lab1

This commit is contained in:
Голиков Maximillian Антон
2026-09-30 22:32:04 +03:00
parent 65003b6119
commit 380d0ef710
34 changed files with 1795 additions and 0 deletions
+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
}
+69
View File
@@ -0,0 +1,69 @@
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) {
return ErrorResponse{Error: ErrorBody{
Code: string(c.Code()),
Message: err.Error(),
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.ErrForbidden):
code = domain.CodeUnauthorized
case errors.Is(err, domain.ErrUnsupportedMediaType):
code = domain.CodeUnsupportedMediaType
}
return ErrorResponse{Error: ErrorBody{
Code: string(code),
Message: err.Error(),
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.ErrForbidden):
return 401
case errors.Is(err, domain.ErrUnsupportedMediaType):
return 415
default:
return 500
}
}
+5
View File
@@ -0,0 +1,5 @@
package dto
type HealthResponse struct {
Status string `json:"status"`
}
+40
View File
@@ -0,0 +1,40 @@
package dto
import (
"high-loud-apps-labs/internal/domain"
)
type CreateMessageRequest struct {
Text string `json:"text" binding:"required,min=1,max=4000"`
}
type MessageResponse struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
}
type MessagePageResponse struct {
Items []MessageResponse `json:"items"`
NextCursor *string `json:"next_cursor"`
}
func MessageFromDomain(m *domain.Message) MessageResponse {
return MessageResponse{
ID: m.ID,
ChatID: m.ChatID,
SenderID: m.SenderID,
Text: m.Text,
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
}
+28
View File
@@ -0,0 +1,28 @@
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"`
}
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),
}
}