UPDATE: Add super admin
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package response
|
||||
|
||||
import "time"
|
||||
|
||||
type PreSignedResponse struct {
|
||||
UploadUrl string `json:"uploadUrl"`
|
||||
PublicUrl string `json:"publicUrl"`
|
||||
@@ -7,3 +9,17 @@ type PreSignedResponse struct {
|
||||
MediaId string `json:"mediaId"`
|
||||
SignedHeaders map[string]string `json:"signedHeaders"`
|
||||
}
|
||||
|
||||
type MediaResponse struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
StorageKey string `json:"storage_key"`
|
||||
OriginalName string `json:"original_name"`
|
||||
MimeType string `json:"mime_type"`
|
||||
Size int64 `json:"size"`
|
||||
TargetType string `json:"target_type"`
|
||||
TargetID string `json:"target_id"`
|
||||
FileMetadata []byte `json:"file_metadata"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
234
internal/gen/sqlc/files.sql.go
Normal file
234
internal/gen/sqlc/files.sql.go
Normal file
@@ -0,0 +1,234 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: files.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createMedia = `-- name: CreateMedia :one
|
||||
INSERT INTO medias (
|
||||
user_id, storage_key, original_name, mime_type, size, target_type, target_id, file_metadata
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8
|
||||
)
|
||||
RETURNING id, user_id, storage_key, original_name, mime_type, size, target_type, target_id, file_metadata, created_at, updated_at
|
||||
`
|
||||
|
||||
type CreateMediaParams struct {
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
StorageKey string `json:"storage_key"`
|
||||
OriginalName string `json:"original_name"`
|
||||
MimeType string `json:"mime_type"`
|
||||
Size int64 `json:"size"`
|
||||
TargetType string `json:"target_type"`
|
||||
TargetID pgtype.UUID `json:"target_id"`
|
||||
FileMetadata []byte `json:"file_metadata"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMedia(ctx context.Context, arg CreateMediaParams) (Media, error) {
|
||||
row := q.db.QueryRow(ctx, createMedia,
|
||||
arg.UserID,
|
||||
arg.StorageKey,
|
||||
arg.OriginalName,
|
||||
arg.MimeType,
|
||||
arg.Size,
|
||||
arg.TargetType,
|
||||
arg.TargetID,
|
||||
arg.FileMetadata,
|
||||
)
|
||||
var i Media
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.StorageKey,
|
||||
&i.OriginalName,
|
||||
&i.MimeType,
|
||||
&i.Size,
|
||||
&i.TargetType,
|
||||
&i.TargetID,
|
||||
&i.FileMetadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteMedia = `-- name: DeleteMedia :exec
|
||||
DELETE FROM medias
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteMedia(ctx context.Context, id pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteMedia, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getMediaByID = `-- name: GetMediaByID :one
|
||||
SELECT id, user_id, storage_key, original_name, mime_type, size, target_type, target_id, file_metadata, created_at, updated_at FROM medias
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetMediaByID(ctx context.Context, id pgtype.UUID) (Media, error) {
|
||||
row := q.db.QueryRow(ctx, getMediaByID, id)
|
||||
var i Media
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.StorageKey,
|
||||
&i.OriginalName,
|
||||
&i.MimeType,
|
||||
&i.Size,
|
||||
&i.TargetType,
|
||||
&i.TargetID,
|
||||
&i.FileMetadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getMediasByTarget = `-- name: GetMediasByTarget :many
|
||||
SELECT id, user_id, storage_key, original_name, mime_type, size, target_type, target_id, file_metadata, created_at, updated_at FROM medias
|
||||
WHERE target_type = $1 AND target_id = $2
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
type GetMediasByTargetParams struct {
|
||||
TargetType string `json:"target_type"`
|
||||
TargetID pgtype.UUID `json:"target_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetMediasByTarget(ctx context.Context, arg GetMediasByTargetParams) ([]Media, error) {
|
||||
rows, err := q.db.Query(ctx, getMediasByTarget, arg.TargetType, arg.TargetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Media{}
|
||||
for rows.Next() {
|
||||
var i Media
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.StorageKey,
|
||||
&i.OriginalName,
|
||||
&i.MimeType,
|
||||
&i.Size,
|
||||
&i.TargetType,
|
||||
&i.TargetID,
|
||||
&i.FileMetadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getMediasByUserID = `-- name: GetMediasByUserID :many
|
||||
SELECT id, user_id, storage_key, original_name, mime_type, size, target_type, target_id, file_metadata, created_at, updated_at FROM medias
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
func (q *Queries) GetMediasByUserID(ctx context.Context, userID pgtype.UUID) ([]Media, error) {
|
||||
rows, err := q.db.Query(ctx, getMediasByUserID, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Media{}
|
||||
for rows.Next() {
|
||||
var i Media
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.StorageKey,
|
||||
&i.OriginalName,
|
||||
&i.MimeType,
|
||||
&i.Size,
|
||||
&i.TargetType,
|
||||
&i.TargetID,
|
||||
&i.FileMetadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const searchMedias = `-- name: SearchMedias :many
|
||||
SELECT id, user_id, storage_key, original_name, mime_type, size, target_type, target_id, file_metadata, created_at, updated_at
|
||||
FROM medias
|
||||
WHERE
|
||||
($1::uuid IS NULL OR id > $1::uuid)
|
||||
AND ($2::varchar[] IS NULL OR target_type = ANY($2::varchar[]))
|
||||
AND (
|
||||
$3::text IS NULL OR
|
||||
original_name ILIKE '%' || $3::text || '%' OR
|
||||
storage_key ILIKE '%' || $3::text || '%'
|
||||
)
|
||||
ORDER BY id ASC
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type SearchMediasParams struct {
|
||||
Cursor pgtype.UUID `json:"cursor"`
|
||||
TargetTypes []string `json:"target_types"`
|
||||
SearchText pgtype.Text `json:"search_text"`
|
||||
Limit int32 `json:"limit"`
|
||||
}
|
||||
|
||||
func (q *Queries) SearchMedias(ctx context.Context, arg SearchMediasParams) ([]Media, error) {
|
||||
rows, err := q.db.Query(ctx, searchMedias,
|
||||
arg.Cursor,
|
||||
arg.TargetTypes,
|
||||
arg.SearchText,
|
||||
arg.Limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Media{}
|
||||
for rows.Next() {
|
||||
var i Media
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.StorageKey,
|
||||
&i.OriginalName,
|
||||
&i.MimeType,
|
||||
&i.Size,
|
||||
&i.TargetType,
|
||||
&i.TargetID,
|
||||
&i.FileMetadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -8,6 +8,20 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Media struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
StorageKey string `json:"storage_key"`
|
||||
OriginalName string `json:"original_name"`
|
||||
MimeType string `json:"mime_type"`
|
||||
Size int64 `json:"size"`
|
||||
TargetType string `json:"target_type"`
|
||||
TargetID pgtype.UUID `json:"target_id"`
|
||||
FileMetadata []byte `json:"file_metadata"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type Role struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
||||
36
internal/models/media.go
Normal file
36
internal/models/media.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"history-api/internal/dtos/response"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MediaEntity struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
StorageKey string `json:"storage_key"`
|
||||
OriginalName string `json:"original_name"`
|
||||
MimeType string `json:"mime_type"`
|
||||
Size int64 `json:"size"`
|
||||
TargetType string `json:"target_type"`
|
||||
TargetID string `json:"target_id"`
|
||||
FileMetadata []byte `json:"file_metadata"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (e *MediaEntity) ToResponse() *response.MediaResponse {
|
||||
return &response.MediaResponse{
|
||||
ID: e.ID,
|
||||
UserID: e.UserID,
|
||||
StorageKey: e.StorageKey,
|
||||
OriginalName: e.OriginalName,
|
||||
MimeType: e.MimeType,
|
||||
Size: e.Size,
|
||||
TargetType: e.TargetType,
|
||||
TargetID: e.TargetID,
|
||||
FileMetadata: e.FileMetadata,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
}
|
||||
}
|
||||
295
internal/repositories/mediaRepository.go
Normal file
295
internal/repositories/mediaRepository.go
Normal file
@@ -0,0 +1,295 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"history-api/internal/gen/sqlc"
|
||||
"history-api/internal/models"
|
||||
"history-api/pkg/cache"
|
||||
"history-api/pkg/constants"
|
||||
"history-api/pkg/convert"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type MediaRepository interface {
|
||||
GetByID(ctx context.Context, id pgtype.UUID) (*models.MediaEntity, error)
|
||||
GetByUserID(ctx context.Context, userId pgtype.UUID) ([]*models.MediaEntity, error)
|
||||
Search(ctx context.Context, params sqlc.SearchMediasParams) ([]*models.MediaEntity, error)
|
||||
Delete(ctx context.Context, id pgtype.UUID) error
|
||||
GetByTarget(ctx context.Context, params sqlc.GetMediasByTargetParams) ([]*models.MediaEntity, error)
|
||||
Create(ctx context.Context, params sqlc.CreateMediaParams) (*models.MediaEntity, error)
|
||||
}
|
||||
|
||||
type mediaRepository struct {
|
||||
q *sqlc.Queries
|
||||
c cache.Cache
|
||||
}
|
||||
|
||||
func NewMediaRepository(db sqlc.DBTX, c cache.Cache) MediaRepository {
|
||||
return &mediaRepository{
|
||||
q: sqlc.New(db),
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *mediaRepository) 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 *mediaRepository) getByIDsWithFallback(ctx context.Context, ids []string) ([]*models.MediaEntity, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*models.MediaEntity{}, nil
|
||||
}
|
||||
keys := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
keys[i] = fmt.Sprintf("media:id:%s", id)
|
||||
}
|
||||
raws := r.c.MGet(ctx, keys...)
|
||||
|
||||
var medias []*models.MediaEntity
|
||||
missingMediasToCache := make(map[string]any)
|
||||
|
||||
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 {
|
||||
pgId := pgtype.UUID{}
|
||||
err := pgId.Scan(ids[i])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
dbMedia, err := r.GetByID(ctx, pgId)
|
||||
if err == nil && dbMedia != nil {
|
||||
medias = append(medias, dbMedia)
|
||||
missingMediasToCache[keys[i]] = dbMedia
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(missingMediasToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, missingMediasToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
|
||||
return medias, nil
|
||||
}
|
||||
|
||||
func (r *mediaRepository) GetByID(ctx context.Context, id pgtype.UUID) (*models.MediaEntity, error) {
|
||||
cacheId := fmt.Sprintf("media:id:%s", convert.UUIDToString(id))
|
||||
var media models.MediaEntity
|
||||
err := r.c.Get(ctx, cacheId, &media)
|
||||
if err == nil {
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
row, err := r.q.GetMediaByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
media = models.MediaEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
StorageKey: row.StorageKey,
|
||||
OriginalName: row.OriginalName,
|
||||
MimeType: row.MimeType,
|
||||
Size: row.Size,
|
||||
TargetType: row.TargetType,
|
||||
TargetID: convert.UUIDToString(row.TargetID),
|
||||
FileMetadata: row.FileMetadata,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
|
||||
_ = r.c.Set(ctx, cacheId, media, constants.NormalCacheDuration)
|
||||
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
func (r *mediaRepository) Create(ctx context.Context, params sqlc.CreateMediaParams) (*models.MediaEntity, error) {
|
||||
row, err := r.q.CreateMedia(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
bgCtx := context.Background()
|
||||
|
||||
_ = r.c.DelByPattern(bgCtx, "media:target*")
|
||||
_ = r.c.DelByPattern(bgCtx, "media:userId:*")
|
||||
_ = r.c.DelByPattern(bgCtx, "media:search*")
|
||||
}()
|
||||
media := models.MediaEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
StorageKey: row.StorageKey,
|
||||
OriginalName: row.OriginalName,
|
||||
MimeType: row.MimeType,
|
||||
Size: row.Size,
|
||||
TargetType: row.TargetType,
|
||||
TargetID: convert.UUIDToString(row.TargetID),
|
||||
FileMetadata: row.FileMetadata,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
cacheId := fmt.Sprintf("media:id:%s", media.ID)
|
||||
_ = r.c.Set(ctx, cacheId, media, constants.NormalCacheDuration)
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
func (r *mediaRepository) Delete(ctx context.Context, id pgtype.UUID) error {
|
||||
err := r.q.DeleteMedia(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cacheId := fmt.Sprintf("media:id:%s", convert.UUIDToString(id))
|
||||
_ = r.c.Del(ctx, cacheId)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *mediaRepository) GetByTarget(ctx context.Context, params sqlc.GetMediasByTargetParams) ([]*models.MediaEntity, error) {
|
||||
queryKey := r.generateQueryKey("media:target", 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.GetMediasByTarget(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var medias []*models.MediaEntity
|
||||
var ids []string
|
||||
mediasToCache := make(map[string]any)
|
||||
|
||||
for _, row := range rows {
|
||||
media := &models.MediaEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
StorageKey: row.StorageKey,
|
||||
OriginalName: row.OriginalName,
|
||||
MimeType: row.MimeType,
|
||||
Size: row.Size,
|
||||
TargetType: row.TargetType,
|
||||
TargetID: convert.UUIDToString(row.TargetID),
|
||||
FileMetadata: row.FileMetadata,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
ids = append(ids, media.ID)
|
||||
medias = append(medias, media)
|
||||
|
||||
mediasToCache[fmt.Sprintf("media:id:%s", media.ID)] = media
|
||||
}
|
||||
|
||||
if len(mediasToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, mediasToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
|
||||
if len(ids) > 0 {
|
||||
_ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration)
|
||||
}
|
||||
|
||||
return medias, nil
|
||||
}
|
||||
|
||||
func (r *mediaRepository) Search(ctx context.Context, params sqlc.SearchMediasParams) ([]*models.MediaEntity, error) {
|
||||
queryKey := r.generateQueryKey("media: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.SearchMedias(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var medias []*models.MediaEntity
|
||||
var ids []string
|
||||
mediasToCache := make(map[string]any)
|
||||
|
||||
for _, row := range rows {
|
||||
media := &models.MediaEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
StorageKey: row.StorageKey,
|
||||
OriginalName: row.OriginalName,
|
||||
MimeType: row.MimeType,
|
||||
Size: row.Size,
|
||||
TargetType: row.TargetType,
|
||||
TargetID: convert.UUIDToString(row.TargetID),
|
||||
FileMetadata: row.FileMetadata,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
ids = append(ids, media.ID)
|
||||
medias = append(medias, media)
|
||||
|
||||
mediasToCache[fmt.Sprintf("media:id:%s", media.ID)] = media
|
||||
}
|
||||
|
||||
if len(mediasToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, mediasToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
|
||||
if len(ids) > 0 {
|
||||
_ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration)
|
||||
}
|
||||
|
||||
return medias, nil
|
||||
}
|
||||
|
||||
func (r *mediaRepository) GetByUserID(ctx context.Context, userId pgtype.UUID) ([]*models.MediaEntity, error) {
|
||||
queryKey := fmt.Sprintf("media:userId:%s", convert.UUIDToString(userId))
|
||||
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.GetMediasByUserID(ctx, userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var medias []*models.MediaEntity
|
||||
var ids []string
|
||||
mediasToCache := make(map[string]any)
|
||||
|
||||
for _, row := range rows {
|
||||
media := &models.MediaEntity{
|
||||
ID: convert.UUIDToString(row.ID),
|
||||
UserID: convert.UUIDToString(row.UserID),
|
||||
StorageKey: row.StorageKey,
|
||||
OriginalName: row.OriginalName,
|
||||
MimeType: row.MimeType,
|
||||
Size: row.Size,
|
||||
TargetType: row.TargetType,
|
||||
TargetID: convert.UUIDToString(row.TargetID),
|
||||
FileMetadata: row.FileMetadata,
|
||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
||||
}
|
||||
ids = append(ids, media.ID)
|
||||
medias = append(medias, media)
|
||||
|
||||
mediasToCache[fmt.Sprintf("media:id:%s", media.ID)] = media
|
||||
}
|
||||
|
||||
if len(mediasToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, mediasToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
|
||||
if len(ids) > 0 {
|
||||
_ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration)
|
||||
}
|
||||
|
||||
return medias, nil
|
||||
}
|
||||
77
internal/services/mediaService.go
Normal file
77
internal/services/mediaService.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"history-api/internal/dtos/request"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/repositories"
|
||||
"history-api/pkg/storage"
|
||||
"mime/multipart"
|
||||
)
|
||||
|
||||
type MediaService interface {
|
||||
GetMediaByID(ctx context.Context, mediaId string) (*response.MediaResponse, error)
|
||||
GetMediaByUserID(ctx context.Context, userId string) ([]*response.MediaResponse, error)
|
||||
SearchMedia(ctx context.Context, dto *request.SearchMediaDto) (*response.PaginatedResponse, error)
|
||||
DeleteMedia(ctx context.Context, mediaId string) error
|
||||
GetMediaByTarget(ctx context.Context, targetType string, targetId string) ([]*response.MediaResponse, error)
|
||||
UploadServerSide(ctx context.Context, userId string, fileHeader *multipart.FileHeader) (*response.MediaResponse, error)
|
||||
GeneratePresignedURL(ctx context.Context, userId string, dto *request.PreSignedDto) (*response.PreSignedResponse, error)
|
||||
PreSignedCompleted(ctx context.Context, userId string, dto *request.PreSignedCompleteDto) (*response.MediaResponse, error)
|
||||
}
|
||||
|
||||
type mediaService struct {
|
||||
mediaRepo repositories.MediaRepository
|
||||
s storage.Storage
|
||||
}
|
||||
|
||||
func NewMediaService(
|
||||
mediaRepo repositories.MediaRepository,
|
||||
s storage.Storage,
|
||||
) MediaService {
|
||||
return &mediaService{
|
||||
mediaRepo: mediaRepo,
|
||||
s: s,
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteMedia implements [MediaService].
|
||||
func (m *mediaService) DeleteMedia(ctx context.Context, mediaId string) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// GeneratePresignedURL implements [MediaService].
|
||||
func (m *mediaService) GeneratePresignedURL(ctx context.Context, userId string, dto *request.PreSignedDto) (*response.PreSignedResponse, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// GetMediaByID implements [MediaService].
|
||||
func (m *mediaService) GetMediaByID(ctx context.Context, mediaId string) (*response.MediaResponse, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// GetMediaByTarget implements [MediaService].
|
||||
func (m *mediaService) GetMediaByTarget(ctx context.Context, targetType string, targetId string) ([]*response.MediaResponse, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// GetMediaByUserID implements [MediaService].
|
||||
func (m *mediaService) GetMediaByUserID(ctx context.Context, userId string) ([]*response.MediaResponse, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// PreSignedCompleted implements [MediaService].
|
||||
func (m *mediaService) PreSignedCompleted(ctx context.Context, userId string, dto *request.PreSignedCompleteDto) (*response.MediaResponse, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// SearchMedia implements [MediaService].
|
||||
func (m *mediaService) SearchMedia(ctx context.Context, dto *request.SearchMediaDto) (*response.PaginatedResponse, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// UploadServerSide implements [MediaService].
|
||||
func (m *mediaService) UploadServerSide(ctx context.Context, userId string, fileHeader *multipart.FileHeader) (*response.MediaResponse, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user