38 lines
790 B
Go
38 lines
790 B
Go
package customer
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Profile struct {
|
|
ID int64
|
|
FirstName string
|
|
LastName string
|
|
Email string
|
|
}
|
|
|
|
type Service struct {
|
|
db *gorm.DB
|
|
prefix string
|
|
}
|
|
|
|
func NewService(db *gorm.DB, prefix string) *Service {
|
|
return &Service{db: db, prefix: prefix}
|
|
}
|
|
|
|
func (s *Service) GetByID(ctx context.Context, id int64) (*Profile, error) {
|
|
var profile Profile
|
|
query := fmt.Sprintf("SELECT id_customer AS id, firstname AS first_name, lastname AS last_name, email FROM %scustomer WHERE id_customer = ? LIMIT 1", s.prefix)
|
|
result := s.db.WithContext(ctx).Raw(query, id).Scan(&profile)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
return &profile, nil
|
|
}
|