diff --git a/backend/api/app/app.go b/backend/api/app/app.go index fef9514..1f15840 100644 --- a/backend/api/app/app.go +++ b/backend/api/app/app.go @@ -14,4 +14,13 @@ type Application interface { AddNote(ctx context.Context, title, content string) (domain.Note, error) UpdateNote(ctx context.Context, noteUUID uuid.UUID, title, content string) (domain.Note, error) DeleteNote(ctx context.Context, noteUUID uuid.UUID) error + + +// New methods added +MarkNote(ctx context.Context, noteUUID uuid.UUID) error +UnarchiveNote(ctx context.Context, noteUUID uuid.UUID) error + + } + + diff --git a/backend/api/router/note/note.go b/backend/api/router/note/note.go index 06de66e..22a1075 100644 --- a/backend/api/router/note/note.go +++ b/backend/api/router/note/note.go @@ -246,3 +246,114 @@ func GetNoteHandler(w http.ResponseWriter, r *http.Request) { panic(err.Error()) } } + + + + + + + + + + +// MarkNoteHandler handles marking a note as a favorite. +func MarkNoteHandler(w http.ResponseWriter, r *http.Request) { + noteUUIDString := chi.URLParam(r, "noteUUID") + slog.Info("Note uuid from request", "noteUUID", noteUUIDString) + noteUUID, err := uuid.Parse(noteUUIDString) + if err != nil { + render.Status(r, 422) + render.JSON(w, r, map[string]string{"msg": "Invalid note uuid"}) + return + } + + application := r.Context().Value("app").(app.Application) + authUserUUID := r.Context().Value("authUserUUID").(uuid.UUID) + + ctx := context.Background() + ctx = context.WithValue(ctx, "auth", a.Auth{UserUUID: authUserUUID}) + + // Assuming you have a method to mark a note as a favorite in your app.Application interface + err = application.MarkNote(ctx, noteUUID) + + if err != nil { + slog.Info("Unable to mark note as favorite", "error", err.Error()) + handleNoteUpdateError(w, r, err) + return + } + + render.Status(r, 200) + render.JSON(w, r, map[string]string{"msg": "Note marked as favorite successfully"}) +} + + + +// UnarchiveNoteHandler handles unarchiving a note. +func UnarchiveNoteHandler(w http.ResponseWriter, r *http.Request) { + noteUUIDString := chi.URLParam(r, "noteUUID") + slog.Info("Note uuid from request", "noteUUID", noteUUIDString) + noteUUID, err := uuid.Parse(noteUUIDString) + if err != nil { + render.Status(r, 422) + render.JSON(w, r, map[string]string{"msg": "Invalid note uuid"}) + return + } + + application := r.Context().Value("app").(app.Application) + authUserUUID := r.Context().Value("authUserUUID").(uuid.UUID) + + ctx := context.Background() + ctx = context.WithValue(ctx, "auth", a.Auth{UserUUID: authUserUUID}) + + // Assuming you have a method to unarchive a note in your app.Application interface + err = application.UnarchiveNote(ctx, noteUUID) + + if err != nil { + slog.Info("Unable to unarchive note", "error", err.Error()) + handleNoteUpdateError(w, r, err) + return + } + + render.Status(r, 200) + render.JSON(w, r, map[string]string{"msg": "Note unarchived successfully"}) +} + + + + + +// handleNoteUpdateError handles the error response for note updates. +func handleNoteUpdateError(w http.ResponseWriter, r *http.Request, err error) { + slog.Error("Note update error", "error", err.Error()) + + var authError *a.AuthenticationError + var authorizationError *a.AuthorizationError + var notFoundError *a.EntityNotFoundError + + switch { + case errors.As(err, &authError): + render.Status(r, 401) + render.JSON(w, r, map[string]string{"msg": authError.Message}) + case errors.As(err, &authorizationError): + render.Status(r, 403) + render.JSON(w, r, map[string]string{"msg": "Not allowed"}) + case errors.As(err, ¬FoundError): + render.Status(r, 404) + render.JSON(w, r, map[string]string{"msg": notFoundError.Message}) + default: + render.Status(r, 400) + render.JSON(w, r, map[string]string{"msg": err.Error()}) + } +} + + + + + + + + + + + + diff --git a/backend/api/router/note/note_test.go b/backend/api/router/note/note_test.go index d1d4280..8119698 100644 --- a/backend/api/router/note/note_test.go +++ b/backend/api/router/note/note_test.go @@ -23,6 +23,11 @@ import ( "github.com/stretchr/testify/assert" ) + + + + + func TestAddNoteHandler(t *testing.T) { var mockApplication mock.MockApplication var mockNotequeryService mock.MockNoteQueryService diff --git a/backend/api/router/note/router.go b/backend/api/router/note/router.go index 7ed8857..7f138cb 100644 --- a/backend/api/router/note/router.go +++ b/backend/api/router/note/router.go @@ -14,5 +14,16 @@ func GetNoteRouter() chi.Router { router.Delete("/{noteUUID}", middleware.AuthenticationMiddleware(http.HandlerFunc(DeleteNoteHandler)).(http.HandlerFunc)) router.Get("/", middleware.AuthenticationMiddleware(http.HandlerFunc(FetchNotesHandler)).(http.HandlerFunc)) router.Get("/{noteUUID}", middleware.AuthenticationMiddleware(http.HandlerFunc(GetNoteHandler)).(http.HandlerFunc)) - return router + + + + + // New routes for marking as favorite and unarchiving + router.Put("/{noteUUID}/mark-as-favorite", http.HandlerFunc(MarkNoteHandler)) + router.Put("/{noteUUID}/unarchive", http.HandlerFunc(UnarchiveNoteHandler)) + + return router + + + } diff --git a/backend/app/app.go b/backend/app/app.go index 9b75870..65381e9 100644 --- a/backend/app/app.go +++ b/backend/app/app.go @@ -174,3 +174,14 @@ func (app *Application) DeleteNote(ctx context.Context, noteUUID uuid.UUID) erro } return nil } + + +func (app *Application) MarkNote(ctx context.Context, noteUUID uuid.UUID) error { + // Implement the logic to mark a note as favorite + return nil +} + +func (app *Application) UnarchiveNote(ctx context.Context, noteUUID uuid.UUID) error { + // Implement the logic to unarchive a note + return nil +} diff --git a/backend/mock/app.go b/backend/mock/app.go index 08c76ec..3313196 100644 --- a/backend/mock/app.go +++ b/backend/mock/app.go @@ -39,3 +39,15 @@ func (app *MockApplication) DeleteNote(ctx context.Context, noteUUID uuid.UUID) args := app.Called(ctx, noteUUID) return args.Error(0) } + + + +func (app *MockApplication) MarkNote(ctx context.Context, noteUUID uuid.UUID) error { + args := app.Called(ctx, noteUUID) + return args.Error(0) +} + +func (app *MockApplication) UnarchiveNote(ctx context.Context, noteUUID uuid.UUID) error { + args := app.Called(ctx, noteUUID) + return args.Error(0) +}