feat: session repository, user repository mappers flatten

This commit is contained in:
AmirMahdi Qiasvand 2025-09-06 14:53:14 +03:30
parent c3c7e908f0
commit 7e471fd8d9
13 changed files with 246 additions and 69 deletions

View File

@ -0,0 +1,28 @@
package domain
import (
"context"
"time"
"github.com/google/uuid"
)
type SessionRepo interface {
Create(ctx context.Context, session *UserSession) error
GetByID(ctx context.Context, id uuid.UUID) (*UserSession, error)
Delete(ctx context.Context, id uuid.UUID) error
GetUserSessions(ctx context.Context, userID uuid.UUID) ([]UserSession, error)
}
type UserSession struct {
ID uuid.UUID
UserID uuid.UUID
User User
WalletID uuid.UUID
IPaddress string
Agent string
ExpiresAt time.Time
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}

29
internal/domain/user.go Normal file
View File

@ -0,0 +1,29 @@
package domain
import (
"context"
"time"
"github.com/google/uuid"
)
type UserRepo interface {
Create(ctx context.Context, user *User) error
GetByID(ctx context.Context, id uuid.UUID) (*User, error)
GetByPhone(ctx context.Context, phone string) (*User, error)
Update(ctx context.Context, user *User) error
Delete(ctx context.Context, id uuid.UUID) error
}
type User struct {
ID uuid.UUID
PubKey string
Name string
LastName string
PhoneNumber string
NationalID string
LastLogin *time.Time
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}

View File

@ -1,7 +0,0 @@
package user
// Repo represents the user repository interface.
type Repo interface{}
// Service represents the user service interface.(UseCases)
type Service interface{}

View File

@ -1,28 +0,0 @@
package user
import (
"time"
"github.com/google/uuid"
)
type User struct {
ID uuid.UUID
Name string
LastName string
PhoneNumber string
NationalID string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
type UserSession struct {
ID uuid.UUID
UserID uuid.UUID
WalletID uuid.UUID
IPaddress string
Agent string
ExpiresAt time.Time
CreatedAt time.Time
}

View File

@ -1,34 +0,0 @@
package mapper
import (
"backend/internal/domain/user"
"backend/internal/repository/storage/types"
)
func UserDomain2Storage(u user.User) *types.User {
return &types.User{
Base: types.Base{
ID: u.ID,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
DeletedAt: u.DeletedAt,
},
Name: u.Name,
LastName: u.LastName,
Phone: u.PhoneNumber,
NationalID: u.NationalID,
}
}
func UserStorage2Domain(u types.User) *user.User {
return &user.User{
ID: u.ID,
Name: u.Name,
LastName: u.LastName,
PhoneNumber: u.Phone,
NationalID: u.NationalID,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
DeletedAt: u.DeletedAt,
}
}

View File

@ -0,0 +1,56 @@
package storage
import (
"backend/internal/domain"
"backend/internal/repository/storage/types"
"context"
"github.com/google/uuid"
"gorm.io/gorm"
)
type SessionRepository struct {
db *gorm.DB
}
func NewSessionRepository(db *gorm.DB) domain.SessionRepo {
return &SessionRepository{
db: db,
}
}
func (r *SessionRepository) Create(ctx context.Context, session *domain.UserSession) error {
model := types.CastSessionToStorage(*session)
return r.db.WithContext(ctx).Create(&model).Error
}
func (r *SessionRepository) GetByID(ctx context.Context, id uuid.UUID) (*domain.UserSession, error) {
var session types.UserSession
if err := r.db.WithContext(ctx).Preload("User").First(&session, "id = ?", id).Error; err != nil {
return nil, err
}
return types.CastSessionToDomain(session), nil
}
func (r *SessionRepository) Delete(ctx context.Context, id uuid.UUID) error {
var session types.UserSession
if err := r.db.WithContext(ctx).First(&session, "id = ?", id).Error; err != nil {
return err
}
return r.db.WithContext(ctx).Delete(&session).Error
}
func (r *SessionRepository) GetUserSessions(ctx context.Context, userID uuid.UUID) ([]domain.UserSession, error) {
var sessions []types.UserSession
if err := r.db.WithContext(ctx).Preload("User").Find(&sessions, "user_id = ?", userID).Error; err != nil {
return nil, err
}
result := make([]domain.UserSession, len(sessions))
for i, session := range sessions {
result[i] = *types.CastSessionToDomain(session)
}
return result, nil
}

View File

@ -0,0 +1,50 @@
package types
import (
"backend/internal/domain"
"time"
"github.com/google/uuid"
)
type UserSession struct {
Base
UserID uuid.UUID
User *User
WalletID uuid.UUID
IP string
Agent string
ExpireAt time.Time
}
func CastSessionToStorage(s domain.UserSession) *UserSession {
return &UserSession{
Base: Base{
ID: s.ID,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
DeletedAt: s.DeletedAt,
},
UserID: s.UserID,
User: CastUserToStorage(s.User),
WalletID: s.WalletID,
IP: s.IPaddress,
Agent: s.Agent,
ExpireAt: s.ExpiresAt,
}
}
func CastSessionToDomain(s UserSession) *domain.UserSession {
return &domain.UserSession{
ID: s.ID,
UserID: s.UserID,
User: *CastUserToDomain(*s.User),
WalletID: s.WalletID,
IPaddress: s.IP,
Agent: s.Agent,
ExpiresAt: s.ExpireAt,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
DeletedAt: s.DeletedAt,
}
}

View File

@ -1,5 +1,9 @@
package types package types
import (
"backend/internal/domain"
)
type User struct { type User struct {
Base Base
Name string Name string
@ -7,3 +11,31 @@ type User struct {
Phone string Phone string
NationalID string NationalID string
} }
func CastUserToStorage(u domain.User) *User {
return &User{
Base: Base{
ID: u.ID,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
DeletedAt: u.DeletedAt,
},
Name: u.Name,
LastName: u.LastName,
Phone: u.PhoneNumber,
NationalID: u.NationalID,
}
}
func CastUserToDomain(u User) *domain.User {
return &domain.User{
ID: u.ID,
Name: u.Name,
LastName: u.LastName,
PhoneNumber: u.Phone,
NationalID: u.NationalID,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
DeletedAt: u.DeletedAt,
}
}

View File

@ -0,0 +1,50 @@
package storage
import (
"backend/internal/domain"
"backend/internal/repository/storage/types"
"context"
"github.com/google/uuid"
"gorm.io/gorm"
)
type UserRepository struct {
db *gorm.DB
}
func NewUserRepository(db *gorm.DB) domain.UserRepo {
return &UserRepository{
db: db,
}
}
func (r *UserRepository) Create(ctx context.Context, user *domain.User) error {
var model types.User
return r.db.WithContext(ctx).Create(&model).Error
}
func (r *UserRepository) GetByID(ctx context.Context, id uuid.UUID) (*domain.User, error) {
var user types.User
if err := r.db.WithContext(ctx).First(&user, "id = ?", id).Error; err != nil {
return nil, err
}
return types.CastUserToDomain(user), nil
}
func (r *UserRepository) GetByPhone(ctx context.Context, phone string) (*domain.User, error) {
var user types.User
if err := r.db.WithContext(ctx).First(&user, "phone = ?", phone).Error; err != nil {
return nil, err
}
return types.CastUserToDomain(user), nil
}
func (r *UserRepository) Update(ctx context.Context, user *domain.User) error {
userModel := types.CastUserToStorage(*user)
return r.db.WithContext(ctx).Save(&userModel).Error
}
func (r *UserRepository) Delete(ctx context.Context, id uuid.UUID) error {
return r.db.WithContext(ctx).Delete(&types.User{}, "id = ?", id).Error
}

View File

1
pkg/logger/logger.go Normal file
View File

@ -0,0 +1 @@
package logger

View File

View File