Files
mtusi2026lab3/internal/service/production/creation.go
T

65 lines
2.0 KiB
Go

package production
import (
"github.com/google/uuid"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service"
)
// PrepareUser validates registration fields and assigns the new user identifier.
func PrepareUser(request dto.UsersCreateRequestDTO) (dto.UserDTO, error) {
if !validUsername(request.Username) || !validText(request.DisplayName, maxDisplayNameRunes) {
return dto.UserDTO{}, service.ErrInvalid
}
return dto.UserDTO{
ID: uuid.New(),
Username: request.Username,
DisplayName: request.DisplayName,
}, nil
}
// prepareChat validates the title and member identifiers before storage access.
func prepareChat(request dto.ChatsCreateRequestDTO, caller uuid.UUID) (dto.ChatDTO, error) {
if !validText(request.Title, maxChatTitleRunes) {
return dto.ChatDTO{}, service.ErrInvalid
}
members, err := parseMembers(request.MemberIDs, caller)
if err != nil {
return dto.ChatDTO{}, err
}
return dto.ChatDTO{
ID: uuid.New(),
Title: request.Title,
MemberIDs: members,
}, nil
}
// prepareMessage validates text and creates a record for the authorized sender and chat.
func prepareMessage(request dto.MessagesCreateRequestDTO, sender, chatID uuid.UUID) (dto.MessageDTO, error) {
if !validText(request.Text, maxMessageRunes) || !validIdempotencyKey(request.IdempotencyKey) {
return dto.MessageDTO{}, service.ErrInvalid
}
return dto.MessageDTO{
ID: uuid.New(),
ChatID: chatID,
SenderID: sender,
Text: request.Text,
IdempotencyKey: request.IdempotencyKey,
}, nil
}
// validIdempotencyKey accepts only the bounded ASCII alphabet from the contract.
func validIdempotencyKey(key string) bool {
if len(key) < 1 || len(key) > 128 {
return false
}
for _, c := range key {
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '.' || c == '_' || c == ':' || c == '-' {
continue
}
return false
}
return true
}