-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcreate.go
34 lines (26 loc) · 913 Bytes
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SPDX-License-Identifier: Apache-2.0
//nolint:dupl // ignore similar code with update.go
package schedule
import (
"context"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// CreateSchedule creates a new schedule in the database.
func (e *engine) CreateSchedule(ctx context.Context, s *library.Schedule) (*library.Schedule, error) {
e.logger.WithFields(logrus.Fields{
"schedule": s.GetName(),
}).Tracef("creating schedule %s in the database", s.GetName())
// cast the library type to database type
schedule := database.ScheduleFromLibrary(s)
// validate the necessary fields are populated
err := schedule.Validate()
if err != nil {
return nil, err
}
// send query to the database
result := e.client.Table(constants.TableSchedule).Create(schedule)
return schedule.ToLibrary(), result.Error
}