Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/server/src/modules/monitor/errs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package monitor

import "errors"

var (
ErrNotFound = errors.New("not found")
)

var (
ErrMonitorNotFound = errors.New("monitor not found")
)
5 changes: 5 additions & 0 deletions apps/server/src/modules/monitor/monitor.controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package monitor

import (
"errors"
"fmt"
"net/http"
"peekaping/src/modules/monitor_notification"
Expand Down Expand Up @@ -285,6 +286,10 @@ func (ic *MonitorController) UpdateFull(ctx *gin.Context) {
updatedMonitor, err := ic.monitorService.UpdateFull(ctx, id, &monitor)
if err != nil {
ic.logger.Errorw("Failed to update monitor", "error", err)
if errors.Is(err, ErrMonitorNotFound) {
ctx.JSON(http.StatusNotFound, utils.NewFailResponse(err.Error()))
return
}
ctx.JSON(http.StatusInternalServerError, utils.NewFailResponse("Internal server error"))
return
}
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/modules/monitor/monitor.mongo.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package monitor
import (
"context"
"errors"
"fmt"
"peekaping/src/config"
"peekaping/src/modules/heartbeat"
"time"
Expand Down Expand Up @@ -385,6 +386,9 @@ func (r *MonitorRepositoryImpl) UpdateFull(ctx context.Context, id string, monit
if err != nil {
return err
}
if existingMonitor == nil {
return fmt.Errorf("%w: monitor %s", ErrMonitorNotFound, id)
}

filter := bson.M{"_id": objectID}
update := bson.M{}
Expand Down
17 changes: 16 additions & 1 deletion apps/server/src/modules/monitor/monitor.sql.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitor

import (
"context"
"fmt"
"time"

"peekaping/src/modules/shared"
Expand Down Expand Up @@ -209,11 +210,25 @@ func (r *SQLRepositoryImpl) UpdateFull(ctx context.Context, id string, monitor *
sm := toSQLModel(monitor)
sm.UpdatedAt = time.Now()

_, err := r.db.NewUpdate().
result, err := r.db.NewUpdate().
Model(sm).
Where("id = ?", id).
ExcludeColumn("id", "created_at").
Exec(ctx)

if err != nil {
return err
}

rowAffected, err := result.RowsAffected()
if err != nil {
return err
}

if rowAffected == 0 {
return fmt.Errorf("%w: monitor %s", ErrMonitorNotFound, id)
}

return err
}

Expand Down
Loading