(ctx context.Context, tx *gorm.DB, id uint, preload ...string)
| 69 | } |
| 70 | |
| 71 | func (s *sqlDatabase) getTemplate(ctx context.Context, tx *gorm.DB, id uint, preload ...string) (Template, error) { |
| 72 | var template Template |
| 73 | q := tx.Model(&Template{}).Where("id = ?", id) |
| 74 | |
| 75 | if len(preload) > 0 { |
| 76 | for _, item := range preload { |
| 77 | q = q.Preload(item) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if !auth.IsAdmin(ctx) { |
| 82 | userID, err := getUIDFromContext(ctx) |
| 83 | if err != nil { |
| 84 | return Template{}, fmt.Errorf("error listing templates: %w", err) |
| 85 | } |
| 86 | q = q.Where("user_id = ? or user_id IS NULL", userID) |
| 87 | } |
| 88 | |
| 89 | q = q.First(&template) |
| 90 | if q.Error != nil { |
| 91 | if errors.Is(q.Error, gorm.ErrRecordNotFound) { |
| 92 | return Template{}, runnerErrors.ErrNotFound |
| 93 | } |
| 94 | return Template{}, fmt.Errorf("failed to get template: %w", q.Error) |
| 95 | } |
| 96 | return template, nil |
| 97 | } |
| 98 | |
| 99 | func (s *sqlDatabase) GetTemplate(ctx context.Context, id uint) (params.Template, error) { |
| 100 | template, err := s.getTemplate(ctx, s.conn, id, "User") |
no test coverage detected