refactor: add check for note not found

pull/3/head
sundowndev 2024-02-27 23:55:12 +04:00
parent 53503cb9f3
commit 5801d9ecca
2 changed files with 30 additions and 0 deletions

View File

@ -71,12 +71,18 @@ func (s *NotesService) EditNote(_ context.Context, req *notesv1.EditNoteRequest)
return nil, fmt.Errorf("validation failed: %v", err)
}
var found bool
for i := range s.notes {
if s.notes[i].Id == req.Note.Id {
s.notes[i] = editedNote
found = true
break
}
}
if !found {
return nil, fmt.Errorf("couldn't find note with id: %s", req.Note.Id)
}
return &notesv1.EditNoteResponse{Note: editedNote}, nil
}

View File

@ -166,3 +166,27 @@ func TestNotesService_EditNote(t *testing.T) {
assert.Equal(t, "january groceries", res2.Note.Title)
assert.True(t, res2.Note.Archived)
}
func TestNotesService_EditNote_NotFound(t *testing.T) {
srv, conn, err := newTestServer()
if err != nil {
t.Fatal(err)
}
client := notesv1.NewNotesServiceClient(conn)
defer srv.Close()
defer conn.Close()
res, err := client.EditNote(context.Background(), &notesv1.EditNoteRequest{
Note: &notesv1.Note{Id: "5cbb1dab-4789-4f32-971d-bdcb60f190b1", Title: "test"},
})
assert.Nil(t, res)
// Retrieve the status from the rpc error
s, ok := status.FromError(err)
if ok {
assert.Equal(t, "couldn't find note with id: 5cbb1dab-4789-4f32-971d-bdcb60f190b1", s.Message())
} else {
log.Fatal(err)
}
}