feat: make max-calls of schedule nullable

This commit is contained in:
Acbox
2026-02-02 22:48:06 +08:00
parent 78c6758f34
commit 2b8b537523
6 changed files with 48 additions and 16 deletions
+8 -4
View File
@@ -77,8 +77,8 @@ func (s *Service) Create(ctx context.Context, userID string, req CreateRequest)
return Schedule{}, err
}
maxCalls := pgtype.Int4{Valid: false}
if req.MaxCalls != nil {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls), Valid: true}
if req.MaxCalls.Set && req.MaxCalls.Value != nil {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls.Value), Valid: true}
}
enabled := true
if req.Enabled != nil {
@@ -164,8 +164,12 @@ func (s *Service) Update(ctx context.Context, id string, req UpdateRequest) (Sch
command = *req.Command
}
maxCalls := existing.MaxCalls
if req.MaxCalls != nil {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls), Valid: true}
if req.MaxCalls.Set {
if req.MaxCalls.Value == nil {
maxCalls = pgtype.Int4{Valid: false}
} else {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls.Value), Valid: true}
}
}
enabled := existing.Enabled
if req.Enabled != nil {
+36 -3
View File
@@ -1,6 +1,9 @@
package schedule
import "time"
import (
"encoding/json"
"time"
)
type Schedule struct {
ID string `json:"id"`
@@ -16,11 +19,41 @@ type Schedule struct {
UserID string `json:"user_id"`
}
type NullableInt struct {
Value *int
Set bool
}
func (n NullableInt) IsZero() bool {
return !n.Set
}
func (n NullableInt) MarshalJSON() ([]byte, error) {
if !n.Set || n.Value == nil {
return []byte("null"), nil
}
return json.Marshal(*n.Value)
}
func (n *NullableInt) UnmarshalJSON(data []byte) error {
n.Set = true
if string(data) == "null" {
n.Value = nil
return nil
}
var value int
if err := json.Unmarshal(data, &value); err != nil {
return err
}
n.Value = &value
return nil
}
type CreateRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Pattern string `json:"pattern"`
MaxCalls *int `json:"max_calls,omitempty"`
MaxCalls NullableInt `json:"max_calls,omitempty"`
Command string `json:"command"`
Enabled *bool `json:"enabled,omitempty"`
}
@@ -29,7 +62,7 @@ type UpdateRequest struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Pattern *string `json:"pattern,omitempty"`
MaxCalls *int `json:"max_calls,omitempty"`
MaxCalls NullableInt `json:"max_calls,omitempty"`
Command *string `json:"command,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
}