Skip to content

Commit 3fab720

Browse files
authored
Fix/update monitor 200 if monitor not found (#193)
* refactor(monitor): update buildSetMapFromModel function to preserve created_at timestamp during updates * fix(monitor): handle monitor not found error in update operations for MongoDB and SQL repositories
1 parent a77456f commit 3fab720

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package monitor
2+
3+
import "errors"
4+
5+
var (
6+
ErrNotFound = errors.New("not found")
7+
)
8+
9+
var (
10+
ErrMonitorNotFound = errors.New("monitor not found")
11+
)

apps/server/src/modules/monitor/monitor.controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package monitor
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67
"peekaping/src/modules/monitor_notification"
@@ -285,6 +286,10 @@ func (ic *MonitorController) UpdateFull(ctx *gin.Context) {
285286
updatedMonitor, err := ic.monitorService.UpdateFull(ctx, id, &monitor)
286287
if err != nil {
287288
ic.logger.Errorw("Failed to update monitor", "error", err)
289+
if errors.Is(err, ErrMonitorNotFound) {
290+
ctx.JSON(http.StatusNotFound, utils.NewFailResponse(err.Error()))
291+
return
292+
}
288293
ctx.JSON(http.StatusInternalServerError, utils.NewFailResponse("Internal server error"))
289294
return
290295
}

apps/server/src/modules/monitor/monitor.mongo.repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package monitor
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"peekaping/src/config"
78
"peekaping/src/modules/heartbeat"
89
"time"
@@ -385,6 +386,9 @@ func (r *MonitorRepositoryImpl) UpdateFull(ctx context.Context, id string, monit
385386
if err != nil {
386387
return err
387388
}
389+
if existingMonitor == nil {
390+
return fmt.Errorf("%w: monitor %s", ErrMonitorNotFound, id)
391+
}
388392

389393
filter := bson.M{"_id": objectID}
390394
update := bson.M{}

apps/server/src/modules/monitor/monitor.sql.repository.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package monitor
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

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

212-
_, err := r.db.NewUpdate().
213+
result, err := r.db.NewUpdate().
213214
Model(sm).
214215
Where("id = ?", id).
215216
ExcludeColumn("id", "created_at").
216217
Exec(ctx)
218+
219+
if err != nil {
220+
return err
221+
}
222+
223+
rowAffected, err := result.RowsAffected()
224+
if err != nil {
225+
return err
226+
}
227+
228+
if rowAffected == 0 {
229+
return fmt.Errorf("%w: monitor %s", ErrMonitorNotFound, id)
230+
}
231+
217232
return err
218233
}
219234

0 commit comments

Comments
 (0)