Skip to content
Open
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
9 changes: 9 additions & 0 deletions backend/api/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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


}


111 changes: 111 additions & 0 deletions backend/api/router/note/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &notFoundError):
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()})
}
}












5 changes: 5 additions & 0 deletions backend/api/router/note/note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"github.com/stretchr/testify/assert"
)






func TestAddNoteHandler(t *testing.T) {
var mockApplication mock.MockApplication
var mockNotequeryService mock.MockNoteQueryService
Expand Down
13 changes: 12 additions & 1 deletion backend/api/router/note/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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



}
11 changes: 11 additions & 0 deletions backend/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions backend/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}