refactor: add check for note not found
parent
53503cb9f3
commit
5801d9ecca
|
@ -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 ¬esv1.EditNoteResponse{Note: editedNote}, nil
|
||||
}
|
||||
|
|
|
@ -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(), ¬esv1.EditNoteRequest{
|
||||
Note: ¬esv1.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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue