From 59b22801e099e74d20adb71d43223630fabfd365 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 31 Aug 2021 22:55:23 +0200 Subject: [PATCH] Add basic file migration web handler --- pkg/modules/migration/handler/common.go | 39 ++++++++++ pkg/modules/migration/handler/handler.go | 12 +-- pkg/modules/migration/handler/handler_file.go | 74 +++++++++++++++++++ pkg/modules/migration/migration_status.go | 4 +- pkg/modules/migration/migrator.go | 14 ++-- 5 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 pkg/modules/migration/handler/common.go create mode 100644 pkg/modules/migration/handler/handler_file.go diff --git a/pkg/modules/migration/handler/common.go b/pkg/modules/migration/handler/common.go new file mode 100644 index 000000000..358f3dca0 --- /dev/null +++ b/pkg/modules/migration/handler/common.go @@ -0,0 +1,39 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2021 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package handler + +import ( + "code.vikunja.io/api/pkg/modules/migration" + user2 "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web/handler" + "github.com/labstack/echo/v4" + "net/http" +) + +func status(ms migration.MigratorName, c echo.Context) error { + user, err := user2.GetCurrentUser(c) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + status, err := migration.GetMigrationStatus(ms, user) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + return c.JSON(http.StatusOK, status) +} diff --git a/pkg/modules/migration/handler/handler.go b/pkg/modules/migration/handler/handler.go index 158f680a2..6ff70e9c5 100644 --- a/pkg/modules/migration/handler/handler.go +++ b/pkg/modules/migration/handler/handler.go @@ -84,15 +84,5 @@ func (mw *MigrationWeb) Migrate(c echo.Context) error { func (mw *MigrationWeb) Status(c echo.Context) error { ms := mw.MigrationStruct() - user, err := user2.GetCurrentUser(c) - if err != nil { - return handler.HandleHTTPError(err, c) - } - - status, err := migration.GetMigrationStatus(ms, user) - if err != nil { - return handler.HandleHTTPError(err, c) - } - - return c.JSON(http.StatusOK, status) + return status(ms, c) } diff --git a/pkg/modules/migration/handler/handler_file.go b/pkg/modules/migration/handler/handler_file.go new file mode 100644 index 000000000..37a361f3e --- /dev/null +++ b/pkg/modules/migration/handler/handler_file.go @@ -0,0 +1,74 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2021 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package handler + +import ( + "code.vikunja.io/api/pkg/models" + "code.vikunja.io/api/pkg/modules/migration" + user2 "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web/handler" + "github.com/labstack/echo/v4" + "net/http" +) + +type FileMigratorWeb struct { + MigrationStruct func() migration.FileMigrator +} + +// RegisterRoutes registers all routes for migration +func (fw *FileMigratorWeb) RegisterRoutes(g *echo.Group) { + ms := fw.MigrationStruct() + g.GET("/"+ms.Name()+"/status", fw.Status) + g.POST("/"+ms.Name()+"/migrate", fw.Migrate) +} + +// Migrate calls the migration method +func (fw *FileMigratorWeb) Migrate(c echo.Context) error { + ms := fw.MigrationStruct() + + // Get the user from context + user, err := user2.GetCurrentUser(c) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + // Bind user request stuff + err = c.Bind(ms) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided: "+err.Error()) + } + + // Do the migration + err = ms.Migrate(user) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + err = migration.SetMigrationStatus(ms, user) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + return c.JSON(http.StatusOK, models.Message{Message: "Everything was migrated successfully."}) +} + +// Status returns whether or not a user has already done this migration +func (fw *FileMigratorWeb) Status(c echo.Context) error { + ms := fw.MigrationStruct() + + return status(ms, c) +} diff --git a/pkg/modules/migration/migration_status.go b/pkg/modules/migration/migration_status.go index c0421a0e1..e23a502e4 100644 --- a/pkg/modules/migration/migration_status.go +++ b/pkg/modules/migration/migration_status.go @@ -37,7 +37,7 @@ func (s *Status) TableName() string { } // SetMigrationStatus sets the migration status for a user -func SetMigrationStatus(m Migrator, u *user.User) (err error) { +func SetMigrationStatus(m MigratorName, u *user.User) (err error) { s := db.NewSession() defer s.Close() @@ -50,7 +50,7 @@ func SetMigrationStatus(m Migrator, u *user.User) (err error) { } // GetMigrationStatus returns the migration status for a migration and a user -func GetMigrationStatus(m Migrator, u *user.User) (status *Status, err error) { +func GetMigrationStatus(m MigratorName, u *user.User) (status *Status, err error) { s := db.NewSession() defer s.Close() diff --git a/pkg/modules/migration/migrator.go b/pkg/modules/migration/migrator.go index 256c66cf3..c14bb5d1e 100644 --- a/pkg/modules/migration/migrator.go +++ b/pkg/modules/migration/migrator.go @@ -22,8 +22,15 @@ import ( "code.vikunja.io/api/pkg/user" ) +type MigratorName interface { + // Name holds the name of the migration. + // This is used to show the name to users and to keep track of users who already migrated. + Name() string +} + // Migrator is the basic migrator interface which is shared among all migrators type Migrator interface { + MigratorName // Migrate is the interface used to migrate a user's tasks from another platform to vikunja. // The user object is the user who's tasks will be migrated. Migrate(user *user.User) error @@ -31,17 +38,12 @@ type Migrator interface { // The use case for this are Oauth flows, where the server token should remain hidden and not // known to the frontend. AuthURL() string - // Name holds the name of the migration. - // This is used to show the name to users and to keep track of users who already migrated. - Name() string } // FileMigrator handles importing Vikunja data from a file. The implementation of it determines the format. type FileMigrator interface { + MigratorName // Migrate is the interface used to migrate a user's tasks, list and other things from a file to vikunja. // The user object is the user who's tasks will be migrated. Migrate(user *user.User, file io.Reader) error - // Name holds the name of the migration. - // This is used to show the name to users and to keep track of users who already migrated. - Name() string }