(ctx context.Context, param params.CreateTemplateParams)
| 144 | } |
| 145 | |
| 146 | func (s *sqlDatabase) CreateTemplate(ctx context.Context, param params.CreateTemplateParams) (template params.Template, err error) { |
| 147 | if param.IsSystem && !auth.IsAdmin(ctx) { |
| 148 | return params.Template{}, runnerErrors.ErrUnauthorized |
| 149 | } |
| 150 | var userID *uuid.UUID |
| 151 | if !param.IsSystem { |
| 152 | parsedID, err := getUIDFromContext(ctx) |
| 153 | if err != nil { |
| 154 | return params.Template{}, fmt.Errorf("error creating template: %w", err) |
| 155 | } |
| 156 | userID = &parsedID |
| 157 | } |
| 158 | defer func() { |
| 159 | if err == nil { |
| 160 | s.sendNotify(common.TemplateEntityType, common.CreateOperation, template) |
| 161 | } |
| 162 | }() |
| 163 | if err := param.Validate(); err != nil { |
| 164 | return params.Template{}, fmt.Errorf("failed to validate create params: %w", err) |
| 165 | } |
| 166 | |
| 167 | sealed, err := s.marshalAndSeal(param.Data) |
| 168 | if err != nil { |
| 169 | return params.Template{}, fmt.Errorf("failed to seal data: %w", err) |
| 170 | } |
| 171 | tpl := Template{ |
| 172 | UserID: userID, |
| 173 | Name: param.Name, |
| 174 | Description: param.Description, |
| 175 | OSType: param.OSType, |
| 176 | Data: sealed, |
| 177 | ForgeType: param.ForgeType, |
| 178 | } |
| 179 | |
| 180 | if err := s.conn.Create(&tpl).Error; err != nil { |
| 181 | if errors.Is(err, gorm.ErrDuplicatedKey) { |
| 182 | return params.Template{}, runnerErrors.NewConflictError("a template name already exists with the specified name") |
| 183 | } |
| 184 | return params.Template{}, fmt.Errorf("error creating template: %w", err) |
| 185 | } |
| 186 | |
| 187 | return s.GetTemplate(ctx, tpl.ID) |
| 188 | } |
| 189 | |
| 190 | func (s *sqlDatabase) UpdateTemplate(ctx context.Context, id uint, param params.UpdateTemplateParams) (template params.Template, err error) { |
| 191 | var hasChange bool |
no test coverage detected