added backup for projects

This commit is contained in:
darius 2024-09-12 15:34:45 +02:00
parent 8a1a5272d4
commit 4e7d37a23a
9 changed files with 52 additions and 15 deletions

View File

@ -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
}

View File

@ -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"),
}

View File

@ -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
}
}

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -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)).

View File

@ -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().

View File

@ -24,6 +24,8 @@ services:
depends_on:
database:
condition: service_healthy
volumes:
- ./backup:/web/assets/json
docs:
build: