Yimaru-BackEnd/internal/repository/practices.go

115 lines
2.4 KiB
Go

package repository
import (
dbgen "Yimaru-Backend/gen/db"
"Yimaru-Backend/internal/domain"
"context"
"github.com/jackc/pgx/v5/pgtype"
)
func (s *Store) CreatePractice(
ctx context.Context,
ownerType string,
ownerID int64,
title string,
description *string,
bannerImage *string,
persona *string,
isActive *bool,
) (domain.Practice, error) {
row, err := s.queries.CreatePractice(ctx, dbgen.CreatePracticeParams{
OwnerType: ownerType,
OwnerID: ownerID,
Title: title,
Description: pgtype.Text{String: *description},
BannerImage: pgtype.Text{String: *bannerImage},
Persona: pgtype.Text{String: *persona},
Column7: isActive,
})
if err != nil {
return domain.Practice{}, err
}
return domain.Practice{
ID: row.ID,
OwnerType: row.OwnerType,
OwnerID: row.OwnerID,
Title: row.Title,
Description: &row.Description.String,
BannerImage: &row.BannerImage.String,
Persona: &row.Persona.String,
IsActive: row.IsActive,
}, nil
}
func (s *Store) GetPracticesByOwner(
ctx context.Context,
ownerType string,
ownerID int64,
) ([]domain.Practice, error) {
rows, err := s.queries.GetPracticesByOwner(ctx, dbgen.GetPracticesByOwnerParams{
OwnerType: ownerType,
OwnerID: ownerID,
})
if err != nil {
return nil, err
}
practices := make([]domain.Practice, 0, len(rows))
for _, row := range rows {
practices = append(practices, domain.Practice{
ID: row.ID,
OwnerType: row.OwnerType,
OwnerID: row.OwnerID,
Title: row.Title,
Description: &row.Description.String,
BannerImage: &row.BannerImage.String,
Persona: &row.Persona.String,
IsActive: row.IsActive,
})
}
return practices, nil
}
func (s *Store) UpdatePractice(
ctx context.Context,
id int64,
title *string,
description *string,
bannerImage *string,
persona *string,
isActive *bool,
) error {
return s.queries.UpdatePractice(ctx, dbgen.UpdatePracticeParams{
Title: func() string {
if title != nil {
return *title
}
return ""
}(),
Description: pgtype.Text{String: *description},
BannerImage: pgtype.Text{String: *bannerImage},
Persona: pgtype.Text{String: *persona},
IsActive: func() bool {
if isActive != nil {
return *isActive
}
return false
}(),
ID: id,
})
}
func (s *Store) DeletePractice(
ctx context.Context,
id int64,
) error {
return s.queries.DeletePractice(ctx, id)
}