From 4e7d37a23a5f806c4ce2cfcd69e4139eb3379e2d Mon Sep 17 00:00:00 2001 From: darius Date: Thu, 12 Sep 2024 15:34:45 +0200 Subject: [PATCH] added backup for projects --- api/apiRoutes.go | 1 + api/handlers/authHandler.go | 6 +++--- api/handlers/projectHandler.go | 27 +++++++++++++++++++++++++++ api/handlers/userHandler.go | 11 +++++------ api/service/validate/validateUser.go | 4 ++-- api/types/userTypes.go | 10 ++++++++-- database/query/authQuery.go | 3 ++- database/query/userQuery.go | 3 ++- docker-compose.yml | 2 ++ 9 files changed, 52 insertions(+), 15 deletions(-) diff --git a/api/apiRoutes.go b/api/apiRoutes.go index 1c9426f..85d63be 100644 --- a/api/apiRoutes.go +++ b/api/apiRoutes.go @@ -28,6 +28,7 @@ func ApiRoutes() *http.ServeMux { mux.HandleFunc("PATCH /projects", handlers.UpdateProjectsHandler) mux.HandleFunc("GET /project/{id}", handlers.GetProjectHandler) mux.HandleFunc("GET /projects", handlers.GetProjectsHandler) + mux.HandleFunc("GET /projects/backup", handlers.GetProjectsBackupHandler) return mux } diff --git a/api/handlers/authHandler.go b/api/handlers/authHandler.go index 5ef952e..384f520 100644 --- a/api/handlers/authHandler.go +++ b/api/handlers/authHandler.go @@ -6,18 +6,18 @@ import ( "net/http" "portfolio/api/service/bcrypt" "portfolio/api/service/jwt" - "portfolio/database/ent" + "portfolio/api/types" "portfolio/database/query" "time" ) func Login(w http.ResponseWriter, r *http.Request) { - var u *ent.User + var u *types.LoginUser isHtmx := r.Header.Get("HX-Request") if isHtmx == "true" { - u = &ent.User{ + u = &types.LoginUser{ Email: r.PostFormValue("email"), Password: r.PostFormValue("password"), } diff --git a/api/handlers/projectHandler.go b/api/handlers/projectHandler.go index aff27f0..cfbefcd 100644 --- a/api/handlers/projectHandler.go +++ b/api/handlers/projectHandler.go @@ -3,12 +3,15 @@ package handlers import ( "context" "encoding/json" + "fmt" "net/http" + "os" "portfolio/api/service/jwt" "portfolio/api/service/parse" "portfolio/database/ent" "portfolio/database/query" "strconv" + "time" ) func CreateProjectHandler(w http.ResponseWriter, r *http.Request) { @@ -152,3 +155,27 @@ func GetProjectsHandler(w http.ResponseWriter, r *http.Request) { return } } + +func GetProjectsBackupHandler(w http.ResponseWriter, r *http.Request) { + fmt.Print("test") + p, err := query.GetProjects(context.Background()) + if err != nil { + UnprocessableEntityHandler(w, err) + return + } + + w.Header().Set("Content-Type", "application/json") + + backup, _ := json.Marshal(p) + + err = os.WriteFile("/web/assets/json/backup-"+strconv.Itoa(int(time.Now().Unix()))+".json", backup, 0644) + if err != nil { + UnprocessableEntityHandler(w, err) + return + } + + err = json.NewEncoder(w).Encode(p) + if err != nil { + return + } +} diff --git a/api/handlers/userHandler.go b/api/handlers/userHandler.go index 0beb8f0..5c78e1c 100644 --- a/api/handlers/userHandler.go +++ b/api/handlers/userHandler.go @@ -6,22 +6,21 @@ import ( "net/http" "portfolio/api/service/bcrypt" "portfolio/api/service/validate" - "portfolio/database/ent" - "portfolio/database/ent/user" + "portfolio/api/types" "portfolio/database/query" "strconv" ) func CreateUserHandler(w http.ResponseWriter, r *http.Request) { - var u *ent.User + var u *types.RegisterUser isHtmx := r.Header.Get("HX-Request") if isHtmx == "true" { - u = &ent.User{ + u = &types.RegisterUser{ Name: r.PostFormValue("name"), - Role: user.Role(r.PostFormValue("role")), + //Role: user.Role(r.PostFormValue("role")), } } else { err := json.NewDecoder(r.Body).Decode(&u) @@ -29,7 +28,7 @@ func CreateUserHandler(w http.ResponseWriter, r *http.Request) { InternalServerErrorHandler(w, err) } } - + u.Password = "123" if !validate.UserIsValid(u) { BadRequestHandler(w) return diff --git a/api/service/validate/validateUser.go b/api/service/validate/validateUser.go index f338446..1833060 100644 --- a/api/service/validate/validateUser.go +++ b/api/service/validate/validateUser.go @@ -1,10 +1,10 @@ package validate import ( - "portfolio/database/ent" + "portfolio/api/types" ) -func UserIsValid(u *ent.User) bool { +func UserIsValid(u *types.RegisterUser) bool { if len(u.Name) > 0 && len(u.Email) > 0 && len(u.Password) > 0 { diff --git a/api/types/userTypes.go b/api/types/userTypes.go index adf0960..ee6fddf 100644 --- a/api/types/userTypes.go +++ b/api/types/userTypes.go @@ -9,8 +9,14 @@ type Username struct { } type LoginUser struct { - Email string - Password string + Email string `json:"email,omitempty"` + Password string `json:"password,omitempty"` +} + +type RegisterUser struct { + Name string `json:"name,omitempty"` + Email string `json:"email,omitempty"` + Password string `json:"password,omitempty"` } type User struct { diff --git a/database/query/authQuery.go b/database/query/authQuery.go index af20e27..dc551d4 100644 --- a/database/query/authQuery.go +++ b/database/query/authQuery.go @@ -4,12 +4,13 @@ import ( "context" "fmt" "log" + "portfolio/api/types" "portfolio/database" "portfolio/database/ent" "portfolio/database/ent/user" ) -func GetLogin(ctx context.Context, U *ent.User) (*ent.User, error) { +func GetLogin(ctx context.Context, U *types.LoginUser) (*ent.User, error) { u, err := database.Client.User. Query(). Where(user.Email(U.Email)). diff --git a/database/query/userQuery.go b/database/query/userQuery.go index 28b0d89..582a0ad 100644 --- a/database/query/userQuery.go +++ b/database/query/userQuery.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "portfolio/api/types" "portfolio/database" "portfolio/database/ent" "portfolio/database/ent/user" @@ -22,7 +23,7 @@ func GetUser(ctx context.Context, id int) (*ent.User, error) { return u, nil } -func CreateUser(ctx context.Context, user ent.User) error { +func CreateUser(ctx context.Context, user types.RegisterUser) error { _, err := database.Client.User. Create(). diff --git a/docker-compose.yml b/docker-compose.yml index f817cf6..9db8d50 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,6 +24,8 @@ services: depends_on: database: condition: service_healthy + volumes: + - ./backup:/web/assets/json docs: build: