init
This commit is contained in:
125
internal/store/mongo/admin_profile_store.go
Normal file
125
internal/store/mongo/admin_profile_store.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
mongodriver "go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"scheduler-backend/internal/store/model"
|
||||
)
|
||||
|
||||
const defaultAdminPassword = "admin123"
|
||||
|
||||
type AdminProfileStore struct {
|
||||
collection *mongodriver.Collection
|
||||
}
|
||||
|
||||
func NewAdminProfileStore(db *mongodriver.Database) *AdminProfileStore {
|
||||
return &AdminProfileStore{
|
||||
collection: db.Collection("admin_profiles"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AdminProfileStore) Get(ctx context.Context) (*model.AdminProfile, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var item model.AdminProfile
|
||||
err := s.collection.FindOne(findCtx, bson.D{}, options.FindOne().SetSort(bson.D{{Key: "updatedAt", Value: -1}})).Decode(&item)
|
||||
if err == nil {
|
||||
return &item, nil
|
||||
}
|
||||
if !errors.Is(err, mongodriver.ErrNoDocuments) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
item = model.AdminProfile{
|
||||
ID: primitive.NewObjectID(),
|
||||
Account: "admin",
|
||||
Nickname: "调度管理员",
|
||||
Avatar: "",
|
||||
Remark: "default admin profile",
|
||||
PasswordHash: hashPassword(defaultAdminPassword),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if _, err := s.collection.InsertOne(findCtx, item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s *AdminProfileStore) Upsert(ctx context.Context, profile *model.AdminProfile) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
now := time.Now()
|
||||
profile.UpdatedAt = now
|
||||
if profile.CreatedAt.IsZero() {
|
||||
profile.CreatedAt = now
|
||||
}
|
||||
|
||||
if profile.ID.IsZero() {
|
||||
current, err := s.Get(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
profile.ID = current.ID
|
||||
if profile.PasswordHash == "" {
|
||||
profile.PasswordHash = current.PasswordHash
|
||||
}
|
||||
if profile.CreatedAt.IsZero() {
|
||||
profile.CreatedAt = current.CreatedAt
|
||||
}
|
||||
}
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, profile.ID, bson.M{
|
||||
"$set": bson.M{
|
||||
"account": profile.Account,
|
||||
"nickname": profile.Nickname,
|
||||
"avatar": profile.Avatar,
|
||||
"remark": profile.Remark,
|
||||
"passwordHash": profile.PasswordHash,
|
||||
"createdAt": profile.CreatedAt,
|
||||
"updatedAt": profile.UpdatedAt,
|
||||
},
|
||||
}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *AdminProfileStore) ChangePassword(ctx context.Context, oldPassword, newPassword string) error {
|
||||
current, err := s.Get(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current.PasswordHash != hashPassword(oldPassword) {
|
||||
return ErrInvalidPassword
|
||||
}
|
||||
|
||||
current.PasswordHash = hashPassword(newPassword)
|
||||
return s.Upsert(ctx, current)
|
||||
}
|
||||
|
||||
func (s *AdminProfileStore) Authenticate(ctx context.Context, account, password string) (*model.AdminProfile, error) {
|
||||
current, err := s.Get(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if current.Account != account || current.PasswordHash != hashPassword(password) {
|
||||
return nil, ErrInvalidPassword
|
||||
}
|
||||
return current, nil
|
||||
}
|
||||
|
||||
func hashPassword(password string) string {
|
||||
sum := sha256.Sum256([]byte(password))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
152
internal/store/mongo/admin_user_store.go
Normal file
152
internal/store/mongo/admin_user_store.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
mongodriver "go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"scheduler-backend/internal/store/model"
|
||||
)
|
||||
|
||||
type AdminUserStore struct {
|
||||
collection *mongodriver.Collection
|
||||
}
|
||||
|
||||
func NewAdminUserStore(db *mongodriver.Database) *AdminUserStore {
|
||||
return &AdminUserStore{
|
||||
collection: db.Collection("admin_users"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AdminUserStore) List(ctx context.Context) ([]model.AdminUser, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cursor, err := s.collection.Find(findCtx, bson.D{}, options.Find().SetSort(bson.D{{Key: "updatedAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(findCtx)
|
||||
|
||||
items := make([]model.AdminUser, 0)
|
||||
for cursor.Next(findCtx) {
|
||||
var item model.AdminUser
|
||||
if err := cursor.Decode(&item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(items) == 0 {
|
||||
defaultAdmin := model.AdminUser{
|
||||
Account: "admin",
|
||||
Nickname: "调度管理员",
|
||||
Status: "active",
|
||||
Role: "super_admin",
|
||||
Remark: "default admin user",
|
||||
PasswordHash: hashPassword(defaultAdminPassword),
|
||||
}
|
||||
if err := s.Create(ctx, &defaultAdmin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, defaultAdmin)
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *AdminUserStore) GetByID(ctx context.Context, id primitive.ObjectID) (*model.AdminUser, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var item model.AdminUser
|
||||
err := s.collection.FindOne(findCtx, bson.M{"_id": id}).Decode(&item)
|
||||
if err != nil {
|
||||
if errors.Is(err, mongodriver.ErrNoDocuments) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s *AdminUserStore) Create(ctx context.Context, item *model.AdminUser) error {
|
||||
insertCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
now := time.Now()
|
||||
item.CreatedAt = now
|
||||
item.UpdatedAt = now
|
||||
if item.Status == "" {
|
||||
item.Status = "active"
|
||||
}
|
||||
if item.Role == "" {
|
||||
item.Role = "super_admin"
|
||||
}
|
||||
|
||||
result, err := s.collection.InsertOne(insertCtx, item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if oid, ok := result.InsertedID.(primitive.ObjectID); ok {
|
||||
item.ID = oid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AdminUserStore) Update(ctx context.Context, item *model.AdminUser) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
item.UpdatedAt = time.Now()
|
||||
_, err := s.collection.UpdateByID(updateCtx, item.ID, bson.M{
|
||||
"$set": bson.M{
|
||||
"account": item.Account,
|
||||
"nickname": item.Nickname,
|
||||
"status": item.Status,
|
||||
"role": item.Role,
|
||||
"remark": item.Remark,
|
||||
"updatedAt": item.UpdatedAt,
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *AdminUserStore) ChangePassword(ctx context.Context, id primitive.ObjectID, newPassword string) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, id, bson.M{
|
||||
"$set": bson.M{
|
||||
"passwordHash": hashPassword(newPassword),
|
||||
"updatedAt": time.Now(),
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *AdminUserStore) Authenticate(ctx context.Context, account, password string) (*model.AdminUser, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var item model.AdminUser
|
||||
err := s.collection.FindOne(findCtx, bson.M{"account": account}).Decode(&item)
|
||||
if err != nil {
|
||||
if errors.Is(err, mongodriver.ErrNoDocuments) {
|
||||
return nil, ErrInvalidPassword
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if item.Status == "disabled" || item.PasswordHash != hashPassword(password) {
|
||||
return nil, ErrInvalidPassword
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
37
internal/store/mongo/client.go
Normal file
37
internal/store/mongo/client.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
mongodriver "go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"scheduler-backend/pkg/config"
|
||||
)
|
||||
|
||||
type Databases struct {
|
||||
Client *mongodriver.Client
|
||||
MetaDB *mongodriver.Database
|
||||
BusinessDB *mongodriver.Database
|
||||
}
|
||||
|
||||
func Connect(ctx context.Context, cfg config.Config) (*Databases, error) {
|
||||
client, err := mongodriver.Connect(ctx, options.Client().ApplyURI(cfg.MongoURI()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := client.Ping(pingCtx, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Databases{
|
||||
Client: client,
|
||||
MetaDB: client.Database(cfg.SchedulerMongoDatabase),
|
||||
BusinessDB: client.Database(cfg.BusinessMongoDatabase),
|
||||
}, nil
|
||||
}
|
||||
5
internal/store/mongo/errors.go
Normal file
5
internal/store/mongo/errors.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package mongo
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrInvalidPassword = errors.New("invalid password")
|
||||
222
internal/store/mongo/job_config_store.go
Normal file
222
internal/store/mongo/job_config_store.go
Normal file
@@ -0,0 +1,222 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
mongodriver "go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
"scheduler-backend/internal/store/model"
|
||||
)
|
||||
|
||||
type JobConfigStore struct {
|
||||
collection *mongodriver.Collection
|
||||
}
|
||||
|
||||
func NewJobConfigStore(db *mongodriver.Database) *JobConfigStore {
|
||||
return &JobConfigStore{
|
||||
collection: db.Collection("job_configs"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) List(ctx context.Context) ([]model.JobConfig, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cursor, err := s.collection.Find(
|
||||
findCtx,
|
||||
bson.D{},
|
||||
options.Find().SetSort(bson.D{{Key: "updatedAt", Value: -1}}),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(findCtx)
|
||||
|
||||
items := make([]model.JobConfig, 0)
|
||||
for cursor.Next(findCtx) {
|
||||
var item model.JobConfig
|
||||
if err := cursor.Decode(&item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
if err := cursor.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) ListEnabled(ctx context.Context) ([]model.JobConfig, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cursor, err := s.collection.Find(
|
||||
findCtx,
|
||||
bson.M{"enabled": true},
|
||||
options.Find().SetSort(bson.D{{Key: "updatedAt", Value: -1}}),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(findCtx)
|
||||
|
||||
items := make([]model.JobConfig, 0)
|
||||
for cursor.Next(findCtx) {
|
||||
var item model.JobConfig
|
||||
if err := cursor.Decode(&item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
if err := cursor.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) GetByID(ctx context.Context, id primitive.ObjectID) (*model.JobConfig, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var item model.JobConfig
|
||||
err := s.collection.FindOne(findCtx, bson.M{"_id": id}).Decode(&item)
|
||||
if err != nil {
|
||||
if errors.Is(err, mongodriver.ErrNoDocuments) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) Create(ctx context.Context, item *model.JobConfig) error {
|
||||
insertCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
now := time.Now()
|
||||
item.CreatedAt = now
|
||||
item.UpdatedAt = now
|
||||
if item.LastStatus == "" {
|
||||
item.LastStatus = "idle"
|
||||
}
|
||||
|
||||
result, err := s.collection.InsertOne(insertCtx, item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if oid, ok := result.InsertedID.(primitive.ObjectID); ok {
|
||||
item.ID = oid
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) Update(ctx context.Context, item *model.JobConfig) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
item.UpdatedAt = time.Now()
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"name": item.Name,
|
||||
"handlerKey": item.HandlerKey,
|
||||
"enabled": item.Enabled,
|
||||
"scheduleType": item.ScheduleType,
|
||||
"scheduleValue": item.ScheduleValue,
|
||||
"defaultParams": item.DefaultParams,
|
||||
"updatedAt": item.UpdatedAt,
|
||||
},
|
||||
}
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, item.ID, update)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) ToggleEnabled(ctx context.Context, id primitive.ObjectID, enabled bool) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, id, bson.M{
|
||||
"$set": bson.M{
|
||||
"enabled": enabled,
|
||||
"updatedAt": time.Now(),
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) UpdateRunState(ctx context.Context, id primitive.ObjectID, status string, lastRunAt *time.Time) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
set := bson.M{
|
||||
"lastStatus": status,
|
||||
"updatedAt": time.Now(),
|
||||
}
|
||||
if lastRunAt != nil {
|
||||
set["lastRunAt"] = *lastRunAt
|
||||
}
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, id, bson.M{
|
||||
"$set": set,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) UpsertByHandlerKey(ctx context.Context, item *model.JobConfig) error {
|
||||
upsertCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
now := time.Now()
|
||||
_, err := s.collection.UpdateOne(
|
||||
upsertCtx,
|
||||
bson.M{"handlerKey": item.HandlerKey},
|
||||
bson.M{
|
||||
"$set": bson.M{
|
||||
"name": item.Name,
|
||||
"handlerKey": item.HandlerKey,
|
||||
"scheduleType": item.ScheduleType,
|
||||
"scheduleValue": item.ScheduleValue,
|
||||
"defaultParams": item.DefaultParams,
|
||||
"updatedAt": now,
|
||||
},
|
||||
"$setOnInsert": bson.M{
|
||||
"enabled": false,
|
||||
"lastStatus": "idle",
|
||||
"createdAt": now,
|
||||
},
|
||||
},
|
||||
options.Update().SetUpsert(true),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *JobConfigStore) UpdateNextRunAt(ctx context.Context, id primitive.ObjectID, nextRunAt *time.Time) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
set := bson.M{
|
||||
"updatedAt": time.Now(),
|
||||
}
|
||||
if nextRunAt != nil {
|
||||
set["nextRunAt"] = *nextRunAt
|
||||
} else {
|
||||
set["nextRunAt"] = nil
|
||||
}
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, id, bson.M{
|
||||
"$set": set,
|
||||
})
|
||||
return err
|
||||
}
|
||||
109
internal/store/mongo/job_execution_store.go
Normal file
109
internal/store/mongo/job_execution_store.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
mongodriver "go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"scheduler-backend/internal/store/model"
|
||||
)
|
||||
|
||||
type JobExecutionStore struct {
|
||||
collection *mongodriver.Collection
|
||||
}
|
||||
|
||||
func NewJobExecutionStore(db *mongodriver.Database) *JobExecutionStore {
|
||||
return &JobExecutionStore{
|
||||
collection: db.Collection("job_executions"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *JobExecutionStore) List(ctx context.Context) ([]model.JobExecution, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cursor, err := s.collection.Find(
|
||||
findCtx,
|
||||
bson.D{},
|
||||
options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(findCtx)
|
||||
|
||||
items := make([]model.JobExecution, 0)
|
||||
for cursor.Next(findCtx) {
|
||||
var item model.JobExecution
|
||||
if err := cursor.Decode(&item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
if err := cursor.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *JobExecutionStore) GetByID(ctx context.Context, id primitive.ObjectID) (*model.JobExecution, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var item model.JobExecution
|
||||
err := s.collection.FindOne(findCtx, bson.M{"_id": id}).Decode(&item)
|
||||
if err != nil {
|
||||
if errors.Is(err, mongodriver.ErrNoDocuments) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s *JobExecutionStore) Create(ctx context.Context, item *model.JobExecution) error {
|
||||
insertCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if item.CreatedAt.IsZero() {
|
||||
item.CreatedAt = time.Now()
|
||||
}
|
||||
|
||||
result, err := s.collection.InsertOne(insertCtx, item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if oid, ok := result.InsertedID.(primitive.ObjectID); ok {
|
||||
item.ID = oid
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *JobExecutionStore) UpdateResult(ctx context.Context, item *model.JobExecution) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, item.ID, bson.M{
|
||||
"$set": bson.M{
|
||||
"status": item.Status,
|
||||
"startedAt": item.StartedAt,
|
||||
"finishedAt": item.FinishedAt,
|
||||
"durationMs": item.DurationMs,
|
||||
"resultSummary": item.ResultSummary,
|
||||
"errorMessage": item.ErrorMessage,
|
||||
"logText": item.LogText,
|
||||
"logFile": item.LogFile,
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
113
internal/store/mongo/system_config_store.go
Normal file
113
internal/store/mongo/system_config_store.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
mongodriver "go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"scheduler-backend/internal/store/model"
|
||||
)
|
||||
|
||||
type SystemConfigStore struct {
|
||||
collection *mongodriver.Collection
|
||||
}
|
||||
|
||||
func NewSystemConfigStore(db *mongodriver.Database) *SystemConfigStore {
|
||||
return &SystemConfigStore{
|
||||
collection: db.Collection("system_configs"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SystemConfigStore) List(ctx context.Context) ([]model.SystemConfig, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cursor, err := s.collection.Find(findCtx, bson.D{}, options.Find().SetSort(bson.D{{Key: "updatedAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(findCtx)
|
||||
|
||||
items := make([]model.SystemConfig, 0)
|
||||
for cursor.Next(findCtx) {
|
||||
var item model.SystemConfig
|
||||
if err := cursor.Decode(&item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := cursor.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *SystemConfigStore) GetByID(ctx context.Context, id primitive.ObjectID) (*model.SystemConfig, error) {
|
||||
findCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var item model.SystemConfig
|
||||
err := s.collection.FindOne(findCtx, bson.M{"_id": id}).Decode(&item)
|
||||
if err != nil {
|
||||
if errors.Is(err, mongodriver.ErrNoDocuments) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (s *SystemConfigStore) Create(ctx context.Context, item *model.SystemConfig) error {
|
||||
insertCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
now := time.Now()
|
||||
item.CreatedAt = now
|
||||
item.UpdatedAt = now
|
||||
|
||||
result, err := s.collection.InsertOne(insertCtx, item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if oid, ok := result.InsertedID.(primitive.ObjectID); ok {
|
||||
item.ID = oid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SystemConfigStore) Update(ctx context.Context, item *model.SystemConfig) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
item.UpdatedAt = time.Now()
|
||||
_, err := s.collection.UpdateByID(updateCtx, item.ID, bson.M{
|
||||
"$set": bson.M{
|
||||
"key": item.Key,
|
||||
"title": item.Title,
|
||||
"value": item.Value,
|
||||
"valueType": item.ValueType,
|
||||
"description": item.Description,
|
||||
"enabled": item.Enabled,
|
||||
"updatedAt": item.UpdatedAt,
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SystemConfigStore) ToggleEnabled(ctx context.Context, id primitive.ObjectID, enabled bool) error {
|
||||
updateCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.collection.UpdateByID(updateCtx, id, bson.M{
|
||||
"$set": bson.M{
|
||||
"enabled": enabled,
|
||||
"updatedAt": time.Now(),
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user