forked from mtusi2026/mtusi2026lab2
Реализация на 5
This commit is contained in:
@@ -2,9 +2,15 @@ package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/auth/jwtprovider"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
@@ -29,6 +35,8 @@ func NewUserHandler(r router.Router, provider jwtprovider.Provider, v *validator
|
||||
}
|
||||
}
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
func (h *UserHandler) RegisterRoutes() {
|
||||
h.router.Public(http.MethodPost, "/api/v1/users", h.Create)
|
||||
h.router.Private(http.MethodGet, "/api/v1/users/me", h.GetMe)
|
||||
@@ -51,6 +59,17 @@ func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
toCreate.Password = *req.Password
|
||||
}
|
||||
|
||||
if !usernamePattern.MatchString(req.Username) || !utf8.ValidString(req.DisplayName) ||
|
||||
strings.TrimSpace(req.DisplayName) == "" || utf8.RuneCountInString(req.DisplayName) > 100 {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid user fields"))
|
||||
return
|
||||
}
|
||||
passwordLen := utf8.RuneCountInString(toCreate.Password)
|
||||
if !utf8.ValidString(toCreate.Password) || strings.TrimSpace(toCreate.Password) == "" || passwordLen < 12 || passwordLen > 128 {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid password"))
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.user.Create(ctx, toCreate)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
@@ -74,6 +93,11 @@ func (h *UserHandler) GetMe(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if userData.UserID == uuid.Nil {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid user id"))
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.user.GetByID(ctx, userData.UserID)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package outside
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/cache"
|
||||
)
|
||||
|
||||
const (
|
||||
// chatRedisKey equals "chat:{chat_id}" path
|
||||
chatRedisKey = "chat:%s"
|
||||
)
|
||||
|
||||
type ChatCache struct {
|
||||
client *cache.Client
|
||||
}
|
||||
|
||||
func NewChatCache(client *cache.Client) *ChatCache {
|
||||
return &ChatCache{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
type chat struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []uuid.UUID `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (r *ChatCache) Create(ctx context.Context, req dto.Chat) error {
|
||||
data, err := json.Marshal(chat{
|
||||
ID: req.ID,
|
||||
Title: req.Title,
|
||||
MemberIDs: req.MemberIDs,
|
||||
CreatedAt: req.CreatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return apperr.Wrap(err, apperr.CodeInternal, "failed to marshal chat cache")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, cache.OperationTimeout)
|
||||
defer cancel()
|
||||
err = r.client.Set(ctx, fmt.Sprintf(chatRedisKey, req.ID), data, 0).Err()
|
||||
if err != nil {
|
||||
return apperr.WrapRedis(err, "failed to create chat cache")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ChatCache) Get(ctx context.Context, chatID uuid.UUID) (*dto.Chat, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, cache.OperationTimeout)
|
||||
defer cancel()
|
||||
data, err := r.client.Get(ctx, fmt.Sprintf(chatRedisKey, chatID.String())).Bytes()
|
||||
if err != nil {
|
||||
return nil, apperr.WrapRedis(err, "failed to read chat cache")
|
||||
}
|
||||
|
||||
var item chat
|
||||
if err = json.Unmarshal(data, &item); err != nil {
|
||||
return nil, apperr.Wrap(err, apperr.CodeInternal, "failed to unmarshal chat cache")
|
||||
}
|
||||
|
||||
return &dto.Chat{
|
||||
ID: item.ID,
|
||||
Title: item.Title,
|
||||
MemberIDs: item.MemberIDs,
|
||||
CreatedAt: item.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user