import lab3 files
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
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
|
||||
default:
|
||||
return 500
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dto
|
||||
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
Text string `json:"text" binding:"required,min=1,max=4000"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user