UPDATE: Project Module
All checks were successful
Build and Release / release (push) Successful in 1m15s
All checks were successful
Build and Release / release (push) Successful in 1m15s
This commit is contained in:
@@ -55,22 +55,46 @@ func (r *entityRepository) getByIDsWithFallback(ctx context.Context, ids []strin
|
||||
var entities []*models.EntityEntity
|
||||
missingToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var e models.EntityEntity
|
||||
if err := json.Unmarshal(b, &e); err == nil {
|
||||
entities = append(entities, &e)
|
||||
}
|
||||
} else {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
dbEntity, err := r.GetByID(ctx, pgId)
|
||||
if err == nil && dbEntity != nil {
|
||||
entities = append(entities, dbEntity)
|
||||
missingToCache[keys[i]] = dbEntity
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.EntityEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetEntitiesByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.EntityEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Name: row.Name,
|
||||
Description: convert.TextToString(row.Description),
|
||||
ThumbnailUrl: convert.TextToString(row.ThumbnailUrl),
|
||||
IsDeleted: row.IsDeleted,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var u models.EntityEntity
|
||||
if err := json.Unmarshal(b, &u); err == nil {
|
||||
entities = append(entities, &u)
|
||||
}
|
||||
} else {
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
entities = append(entities, item)
|
||||
missingToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,22 +58,54 @@ func (r *geometryRepository) getByIDsWithFallback(ctx context.Context, ids []str
|
||||
var geometries []*models.GeometryEntity
|
||||
missingToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var g models.GeometryEntity
|
||||
if err := json.Unmarshal(b, &g); err == nil {
|
||||
geometries = append(geometries, &g)
|
||||
}
|
||||
} else {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
dbGeometry, err := r.GetByID(ctx, pgId)
|
||||
if err == nil && dbGeometry != nil {
|
||||
geometries = append(geometries, dbGeometry)
|
||||
missingToCache[keys[i]] = dbGeometry
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.GeometryEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetGeometriesByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.GeometryEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
GeoType: constants.ParseGeoType(row.GeoType),
|
||||
DrawGeometry: row.DrawGeometry,
|
||||
Binding: row.Binding,
|
||||
TimeStart: convert.Int4ToInt32(row.TimeStart),
|
||||
TimeEnd: convert.Int4ToInt32(row.TimeEnd),
|
||||
Bbox: &response.Bbox{
|
||||
MinLng: row.MinLng,
|
||||
MinLat: row.MinLat,
|
||||
MaxLng: row.MaxLng,
|
||||
MaxLat: row.MaxLat,
|
||||
},
|
||||
IsDeleted: row.IsDeleted,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var u models.GeometryEntity
|
||||
if err := json.Unmarshal(b, &u); err == nil {
|
||||
geometries = append(geometries, &u)
|
||||
}
|
||||
} else {
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
geometries = append(geometries, item)
|
||||
missingToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,7 +137,7 @@ func (r *geometryRepository) GetByID(ctx context.Context, id pgtype.UUID) (*mode
|
||||
|
||||
geometry = models.GeometryEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
GeoType: row.GeoType,
|
||||
GeoType: constants.ParseGeoType(row.GeoType),
|
||||
DrawGeometry: row.DrawGeometry,
|
||||
Binding: row.Binding,
|
||||
TimeStart: convert.Int4ToInt32(row.TimeStart),
|
||||
@@ -143,7 +175,7 @@ func (r *geometryRepository) Search(ctx context.Context, params sqlc.SearchGeome
|
||||
for _, row := range rows {
|
||||
geometry := &models.GeometryEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
GeoType: row.GeoType,
|
||||
GeoType: constants.ParseGeoType(row.GeoType),
|
||||
DrawGeometry: row.DrawGeometry,
|
||||
Binding: row.Binding,
|
||||
TimeStart: convert.Int4ToInt32(row.TimeStart),
|
||||
@@ -181,7 +213,7 @@ func (r *geometryRepository) Create(ctx context.Context, params sqlc.CreateGeome
|
||||
|
||||
geometry := models.GeometryEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
GeoType: row.GeoType,
|
||||
GeoType: constants.ParseGeoType(row.GeoType),
|
||||
DrawGeometry: row.DrawGeometry,
|
||||
Binding: row.Binding,
|
||||
TimeStart: convert.Int4ToInt32(row.TimeStart),
|
||||
@@ -212,7 +244,7 @@ func (r *geometryRepository) Update(ctx context.Context, params sqlc.UpdateGeome
|
||||
}
|
||||
geometry := models.GeometryEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
GeoType: row.GeoType,
|
||||
GeoType: constants.ParseGeoType(row.GeoType),
|
||||
DrawGeometry: row.DrawGeometry,
|
||||
Binding: row.Binding,
|
||||
TimeStart: convert.Int4ToInt32(row.TimeStart),
|
||||
|
||||
@@ -56,22 +56,48 @@ func (r *mediaRepository) getByIDsWithFallback(ctx context.Context, ids []string
|
||||
var medias []*models.MediaEntity
|
||||
missingMediasToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var m models.MediaEntity
|
||||
if err := json.Unmarshal(b, &m); err == nil {
|
||||
medias = append(medias, &m)
|
||||
}
|
||||
} else {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
dbMedia, err := r.GetByID(ctx, pgId)
|
||||
if err == nil && dbMedia != nil {
|
||||
medias = append(medias, dbMedia)
|
||||
missingMediasToCache[keys[i]] = dbMedia
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.MediaEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetMediaByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.MediaEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
StorageKey: row.StorageKey,
|
||||
OriginalName: row.OriginalName,
|
||||
MimeType: row.MimeType,
|
||||
Size: row.Size,
|
||||
FileMetadata: row.FileMetadata,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var u models.MediaEntity
|
||||
if err := json.Unmarshal(b, &u); err == nil {
|
||||
medias = append(medias, &u)
|
||||
}
|
||||
} else {
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
medias = append(medias, item)
|
||||
missingMediasToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
345
internal/repositories/projectRepository.go
Normal file
345
internal/repositories/projectRepository.go
Normal file
@@ -0,0 +1,345 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"history-api/internal/gen/sqlc"
|
||||
"history-api/internal/models"
|
||||
"history-api/pkg/cache"
|
||||
"history-api/pkg/constants"
|
||||
"history-api/pkg/convert"
|
||||
)
|
||||
|
||||
type ProjectRepository interface {
|
||||
GetByID(ctx context.Context, id pgtype.UUID) (*models.ProjectEntity, error)
|
||||
GetByIDs(ctx context.Context, ids []string) ([]*models.ProjectEntity, error)
|
||||
GetByUserID(ctx context.Context, params sqlc.GetProjectsByUserIdParams) ([]*models.ProjectEntity, error)
|
||||
Search(ctx context.Context, params sqlc.SearchProjectsParams) ([]*models.ProjectEntity, error)
|
||||
Count(ctx context.Context, params sqlc.CountProjectsParams) (int64, error)
|
||||
Create(ctx context.Context, params sqlc.CreateProjectParams) (*models.ProjectEntity, error)
|
||||
Update(ctx context.Context, params sqlc.UpdateProjectParams) (*models.ProjectEntity, error)
|
||||
Delete(ctx context.Context, id pgtype.UUID) error
|
||||
}
|
||||
|
||||
type projectRepository struct {
|
||||
q *sqlc.Queries
|
||||
c cache.Cache
|
||||
}
|
||||
|
||||
func NewProjectRepository(db sqlc.DBTX, c cache.Cache) ProjectRepository {
|
||||
return &projectRepository{
|
||||
q: sqlc.New(db),
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *projectRepository) generateQueryKey(prefix string, params any) string {
|
||||
b, _ := json.Marshal(params)
|
||||
hash := fmt.Sprintf("%x", md5.Sum(b))
|
||||
return fmt.Sprintf("%s:%s", prefix, hash)
|
||||
}
|
||||
|
||||
|
||||
func (r *projectRepository) getByIDsWithFallback(ctx context.Context, ids []string) ([]*models.ProjectEntity, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*models.ProjectEntity{}, nil
|
||||
}
|
||||
keys := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
keys[i] = fmt.Sprintf("project:id:%s", id)
|
||||
}
|
||||
raws := r.c.MGet(ctx, keys...)
|
||||
|
||||
var projects []*models.ProjectEntity
|
||||
missingToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.ProjectEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetProjectsByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.ProjectEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Title: row.Title,
|
||||
Description: convert.TextToString(row.Description),
|
||||
LatestRevisionID: convert.UUIDToStringPtr(row.LatestRevisionID),
|
||||
VersionCount: row.VersionCount,
|
||||
ProjectStatus: constants.ParseProjectStatusType(row.ProjectStatus),
|
||||
LockedBy: convert.UUIDToStringPtr(row.LockedBy),
|
||||
IsDeleted: row.IsDeleted,
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
CommitIds: convert.ListUUIDToString(row.CommitIds),
|
||||
SubmissionIds: convert.ListUUIDToString(row.SubmissionIds),
|
||||
}
|
||||
_ = item.ParseUser(row.User)
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var p models.ProjectEntity
|
||||
if err := json.Unmarshal(b, &p); err == nil {
|
||||
projects = append(projects, &p)
|
||||
}
|
||||
} else {
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
projects = append(projects, item)
|
||||
missingToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(missingToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, missingToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func (r *projectRepository) GetByIDs(ctx context.Context, ids []string) ([]*models.ProjectEntity, error) {
|
||||
return r.getByIDsWithFallback(ctx, ids)
|
||||
}
|
||||
|
||||
func (r *projectRepository) GetByID(ctx context.Context, id pgtype.UUID) (*models.ProjectEntity, error) {
|
||||
cacheId := fmt.Sprintf("project:id:%s", convert.UUIDToString(id))
|
||||
var project models.ProjectEntity
|
||||
err := r.c.Get(ctx, cacheId, &project)
|
||||
if err == nil {
|
||||
_ = r.c.Set(ctx, cacheId, project, constants.NormalCacheDuration)
|
||||
return &project, nil
|
||||
}
|
||||
|
||||
row, err := r.q.GetProjectById(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
project = models.ProjectEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Title: row.Title,
|
||||
Description: convert.TextToString(row.Description),
|
||||
LatestRevisionID: convert.UUIDToStringPtr(row.LatestRevisionID),
|
||||
VersionCount: row.VersionCount,
|
||||
ProjectStatus: constants.ParseProjectStatusType(row.ProjectStatus),
|
||||
LockedBy: convert.UUIDToStringPtr(row.LockedBy),
|
||||
IsDeleted: row.IsDeleted,
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
CommitIds: convert.ListUUIDToString(row.CommitIds),
|
||||
SubmissionIds: convert.ListUUIDToString(row.SubmissionIds),
|
||||
}
|
||||
_ = project.ParseUser(row.User)
|
||||
|
||||
_ = r.c.Set(ctx, cacheId, project, constants.NormalCacheDuration)
|
||||
|
||||
return &project, nil
|
||||
}
|
||||
|
||||
func (r *projectRepository) GetByUserID(ctx context.Context, params sqlc.GetProjectsByUserIdParams) ([]*models.ProjectEntity, error) {
|
||||
queryKey := r.generateQueryKey("project:user", params)
|
||||
var cachedIDs []string
|
||||
if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 {
|
||||
return r.getByIDsWithFallback(ctx, cachedIDs)
|
||||
}
|
||||
|
||||
rows, err := r.q.GetProjectsByUserId(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var projects []*models.ProjectEntity
|
||||
var ids []string
|
||||
projectToCache := make(map[string]any)
|
||||
|
||||
for _, row := range rows {
|
||||
project := &models.ProjectEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Title: row.Title,
|
||||
Description: convert.TextToString(row.Description),
|
||||
LatestRevisionID: convert.UUIDToStringPtr(row.LatestRevisionID),
|
||||
VersionCount: row.VersionCount,
|
||||
ProjectStatus: constants.ParseProjectStatusType(row.ProjectStatus),
|
||||
LockedBy: convert.UUIDToStringPtr(row.LockedBy),
|
||||
IsDeleted: row.IsDeleted,
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
CommitIds: convert.ListUUIDToString(row.CommitIds),
|
||||
SubmissionIds: convert.ListUUIDToString(row.SubmissionIds),
|
||||
}
|
||||
_ = project.ParseUser(row.User)
|
||||
|
||||
ids = append(ids, project.ID)
|
||||
projects = append(projects, project)
|
||||
projectToCache[fmt.Sprintf("project:id:%s", project.ID)] = project
|
||||
}
|
||||
|
||||
if len(projectToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, projectToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
_ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration)
|
||||
}
|
||||
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func (r *projectRepository) Search(ctx context.Context, params sqlc.SearchProjectsParams) ([]*models.ProjectEntity, error) {
|
||||
queryKey := r.generateQueryKey("project:search", params)
|
||||
var cachedIDs []string
|
||||
if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 {
|
||||
return r.getByIDsWithFallback(ctx, cachedIDs)
|
||||
}
|
||||
|
||||
rows, err := r.q.SearchProjects(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var projects []*models.ProjectEntity
|
||||
var ids []string
|
||||
projectToCache := make(map[string]any)
|
||||
|
||||
for _, row := range rows {
|
||||
project := &models.ProjectEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Title: row.Title,
|
||||
Description: convert.TextToString(row.Description),
|
||||
LatestRevisionID: convert.UUIDToStringPtr(row.LatestRevisionID),
|
||||
VersionCount: row.VersionCount,
|
||||
ProjectStatus: constants.ParseProjectStatusType(row.ProjectStatus),
|
||||
LockedBy: convert.UUIDToStringPtr(row.LockedBy),
|
||||
IsDeleted: row.IsDeleted,
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
CommitIds: convert.ListUUIDToString(row.CommitIds),
|
||||
SubmissionIds: convert.ListUUIDToString(row.SubmissionIds),
|
||||
}
|
||||
_ = project.ParseUser(row.User)
|
||||
|
||||
ids = append(ids, project.ID)
|
||||
projects = append(projects, project)
|
||||
projectToCache[fmt.Sprintf("project:id:%s", project.ID)] = project
|
||||
}
|
||||
|
||||
if len(projectToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, projectToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
_ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration)
|
||||
}
|
||||
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func (r *projectRepository) Count(ctx context.Context, params sqlc.CountProjectsParams) (int64, error) {
|
||||
queryKey := r.generateQueryKey("project:count", params)
|
||||
var count int64
|
||||
if err := r.c.Get(ctx, queryKey, &count); err == nil {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
count, err := r.q.CountProjects(ctx, params)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
_ = r.c.Set(ctx, queryKey, count, constants.NormalCacheDuration)
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (r *projectRepository) Create(ctx context.Context, params sqlc.CreateProjectParams) (*models.ProjectEntity, error) {
|
||||
row, err := r.q.CreateProject(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
project := models.ProjectEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Title: row.Title,
|
||||
Description: convert.TextToString(row.Description),
|
||||
LatestRevisionID: convert.UUIDToStringPtr(row.LatestRevisionID),
|
||||
VersionCount: row.VersionCount,
|
||||
ProjectStatus: constants.ParseProjectStatusType(row.ProjectStatus),
|
||||
LockedBy: convert.UUIDToStringPtr(row.LockedBy),
|
||||
IsDeleted: row.IsDeleted,
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
CommitIds: convert.ListUUIDToString(row.CommitIds),
|
||||
SubmissionIds: convert.ListUUIDToString(row.SubmissionIds),
|
||||
}
|
||||
_ = project.ParseUser(row.User)
|
||||
|
||||
_ = r.c.Set(ctx, fmt.Sprintf("project:id:%s", project.ID), project, constants.NormalCacheDuration)
|
||||
|
||||
go func() {
|
||||
bgCtx := context.Background()
|
||||
_ = r.c.DelByPattern(bgCtx, "project:search*")
|
||||
_ = r.c.DelByPattern(bgCtx, "project:user*")
|
||||
_ = r.c.DelByPattern(bgCtx, "project:count*")
|
||||
}()
|
||||
return &project, nil
|
||||
}
|
||||
|
||||
func (r *projectRepository) Update(ctx context.Context, params sqlc.UpdateProjectParams) (*models.ProjectEntity, error) {
|
||||
row, err := r.q.UpdateProject(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
project := models.ProjectEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Title: row.Title,
|
||||
Description: convert.TextToString(row.Description),
|
||||
LatestRevisionID: convert.UUIDToStringPtr(row.LatestRevisionID),
|
||||
VersionCount: row.VersionCount,
|
||||
ProjectStatus: constants.ParseProjectStatusType(row.ProjectStatus),
|
||||
LockedBy: convert.UUIDToStringPtr(row.LockedBy),
|
||||
IsDeleted: row.IsDeleted,
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
CommitIds: convert.ListUUIDToString(row.CommitIds),
|
||||
SubmissionIds: convert.ListUUIDToString(row.SubmissionIds),
|
||||
}
|
||||
_ = project.ParseUser(row.User)
|
||||
|
||||
_ = r.c.Set(ctx, fmt.Sprintf("project:id:%s", project.ID), project, constants.NormalCacheDuration)
|
||||
return &project, nil
|
||||
}
|
||||
|
||||
func (r *projectRepository) Delete(ctx context.Context, id pgtype.UUID) error {
|
||||
err := r.q.DeleteProject(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = r.c.Del(ctx, fmt.Sprintf("project:id:%s", convert.UUIDToString(id)))
|
||||
go func() {
|
||||
bgCtx := context.Background()
|
||||
_ = r.c.DelByPattern(bgCtx, "project:search*")
|
||||
_ = r.c.DelByPattern(bgCtx, "project:user*")
|
||||
_ = r.c.DelByPattern(bgCtx, "project:count*")
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
@@ -61,6 +61,34 @@ func (r *roleRepository) getByIDsWithFallback(ctx context.Context, ids []string)
|
||||
var roles []*models.RoleEntity
|
||||
missingRolesToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.RoleEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetRolesByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.RoleEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Name: row.Name,
|
||||
IsDeleted: row.IsDeleted,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var u models.RoleEntity
|
||||
@@ -68,15 +96,9 @@ func (r *roleRepository) getByIDsWithFallback(ctx context.Context, ids []string)
|
||||
roles = append(roles, &u)
|
||||
}
|
||||
} else {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
dbRole, err := r.GetByID(ctx, pgId)
|
||||
if err == nil && dbRole != nil {
|
||||
roles = append(roles, dbRole)
|
||||
missingRolesToCache[keys[i]] = dbRole
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
roles = append(roles, item)
|
||||
missingRolesToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (t *tokenRepository) CheckVerified(ctx context.Context, email string, token
|
||||
}
|
||||
|
||||
func (t *tokenRepository) CreateUploadToken(ctx context.Context, userId string, token *models.TokenUploadEntity) error {
|
||||
cacheKey := fmt.Sprintf("token:%d:%s:%s", constants.TokenUpload.Value(), userId, token.ID)
|
||||
cacheKey := fmt.Sprintf("token:%d:%s:%s", constants.TokenTypeUpload.Value(), userId, token.ID)
|
||||
err := t.c.Set(ctx, cacheKey, token, constants.TokenUploadDuration)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -58,7 +58,7 @@ func (t *tokenRepository) CreateUploadToken(ctx context.Context, userId string,
|
||||
}
|
||||
|
||||
func (t *tokenRepository) GetUploadToken(ctx context.Context, userId string, id string) (*models.TokenUploadEntity, error) {
|
||||
cacheKey := fmt.Sprintf("token:%d:%s:%s", constants.TokenUpload.Value(), userId, id)
|
||||
cacheKey := fmt.Sprintf("token:%d:%s:%s", constants.TokenTypeUpload.Value(), userId, id)
|
||||
var token models.TokenUploadEntity
|
||||
err := t.c.Get(ctx, cacheKey, &token)
|
||||
if err != nil {
|
||||
@@ -68,7 +68,7 @@ func (t *tokenRepository) GetUploadToken(ctx context.Context, userId string, id
|
||||
}
|
||||
|
||||
func (t *tokenRepository) DeleteUploadToken(ctx context.Context, userId string, id string) error {
|
||||
cacheKey := fmt.Sprintf("token:%d:%s:%s", constants.TokenUpload.Value(), userId, id)
|
||||
cacheKey := fmt.Sprintf("token:%d:%s:%s", constants.TokenTypeUpload.Value(), userId, id)
|
||||
return t.c.Del(ctx, cacheKey)
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,38 @@ func (r *userRepository) getByIDsWithFallback(ctx context.Context, ids []string)
|
||||
var users []*models.UserEntity
|
||||
missingUsersToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.UserEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetUsersByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.UserEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Email: row.Email,
|
||||
PasswordHash: convert.TextToString(row.PasswordHash),
|
||||
TokenVersion: row.TokenVersion,
|
||||
IsDeleted: row.IsDeleted,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
_ = item.ParseRoles(row.Roles)
|
||||
_ = item.ParseProfile(row.Profile)
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var u models.UserEntity
|
||||
@@ -70,15 +102,9 @@ func (r *userRepository) getByIDsWithFallback(ctx context.Context, ids []string)
|
||||
users = append(users, &u)
|
||||
}
|
||||
} else {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
dbUser, err := r.GetByID(ctx, pgId)
|
||||
if err == nil && dbUser != nil {
|
||||
users = append(users, dbUser)
|
||||
missingUsersToCache[keys[i]] = dbUser
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
users = append(users, item)
|
||||
missingUsersToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,40 @@ func (v *verificationRepository) getByIDsWithFallback(ctx context.Context, ids [
|
||||
var verification []*models.UserVerificationEntity
|
||||
missingVerificationToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.UserVerificationEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := v.q.GetUserVerificationsByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.UserVerificationEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
VerifyType: constants.ParseVerifyType(row.VerifyType),
|
||||
Content: convert.TextToString(row.Content),
|
||||
IsDeleted: row.IsDeleted,
|
||||
Status: constants.ParseStatusType(row.Status),
|
||||
ReviewNote: convert.TextToString(row.ReviewNote),
|
||||
ReviewedAt: convert.TimeToPtr(row.ReviewedAt),
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
}
|
||||
_ = item.ParseMedia(row.Medias)
|
||||
_ = item.ParseUser(row.User)
|
||||
_ = item.ParseReviewer(row.Reviewer)
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var u models.UserVerificationEntity
|
||||
@@ -106,15 +140,9 @@ func (v *verificationRepository) getByIDsWithFallback(ctx context.Context, ids [
|
||||
verification = append(verification, &u)
|
||||
}
|
||||
} else {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
dbUser, err := v.GetByID(ctx, pgId)
|
||||
if err == nil && dbUser != nil {
|
||||
verification = append(verification, dbUser)
|
||||
missingVerificationToCache[keys[i]] = dbUser
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
verification = append(verification, item)
|
||||
missingVerificationToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,22 +57,45 @@ func (r *wikiRepository) getByIDsWithFallback(ctx context.Context, ids []string)
|
||||
var wikis []*models.WikiEntity
|
||||
missingToCache := make(map[string]any)
|
||||
|
||||
var missingPgIds []pgtype.UUID
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var w models.WikiEntity
|
||||
if err := json.Unmarshal(b, &w); err == nil {
|
||||
wikis = append(wikis, &w)
|
||||
}
|
||||
} else {
|
||||
if len(b) == 0 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
if err == nil {
|
||||
missingPgIds = append(missingPgIds, pgId)
|
||||
}
|
||||
dbWiki, err := r.GetByID(ctx, pgId)
|
||||
if err == nil && dbWiki != nil {
|
||||
wikis = append(wikis, dbWiki)
|
||||
missingToCache[keys[i]] = dbWiki
|
||||
}
|
||||
}
|
||||
|
||||
dbMap := make(map[string]*models.WikiEntity)
|
||||
if len(missingPgIds) > 0 {
|
||||
dbRows, err := r.q.GetWikisByIDs(ctx, missingPgIds)
|
||||
if err == nil {
|
||||
for _, row := range dbRows {
|
||||
item := models.WikiEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
Title: convert.TextToString(row.Title),
|
||||
Content: convert.TextToString(row.Content),
|
||||
IsDeleted: row.IsDeleted,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
dbMap[item.ID] = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var u models.WikiEntity
|
||||
if err := json.Unmarshal(b, &u); err == nil {
|
||||
wikis = append(wikis, &u)
|
||||
}
|
||||
} else {
|
||||
if item, ok := dbMap[ids[i]]; ok {
|
||||
wikis = append(wikis, item)
|
||||
missingToCache[keys[i]] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user