added backup for projects
This commit is contained in:
parent
8a1a5272d4
commit
4e7d37a23a
@ -28,6 +28,7 @@ func ApiRoutes() *http.ServeMux {
|
|||||||
mux.HandleFunc("PATCH /projects", handlers.UpdateProjectsHandler)
|
mux.HandleFunc("PATCH /projects", handlers.UpdateProjectsHandler)
|
||||||
mux.HandleFunc("GET /project/{id}", handlers.GetProjectHandler)
|
mux.HandleFunc("GET /project/{id}", handlers.GetProjectHandler)
|
||||||
mux.HandleFunc("GET /projects", handlers.GetProjectsHandler)
|
mux.HandleFunc("GET /projects", handlers.GetProjectsHandler)
|
||||||
|
mux.HandleFunc("GET /projects/backup", handlers.GetProjectsBackupHandler)
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,18 +6,18 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"portfolio/api/service/bcrypt"
|
"portfolio/api/service/bcrypt"
|
||||||
"portfolio/api/service/jwt"
|
"portfolio/api/service/jwt"
|
||||||
"portfolio/database/ent"
|
"portfolio/api/types"
|
||||||
"portfolio/database/query"
|
"portfolio/database/query"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Login(w http.ResponseWriter, r *http.Request) {
|
func Login(w http.ResponseWriter, r *http.Request) {
|
||||||
var u *ent.User
|
var u *types.LoginUser
|
||||||
|
|
||||||
isHtmx := r.Header.Get("HX-Request")
|
isHtmx := r.Header.Get("HX-Request")
|
||||||
|
|
||||||
if isHtmx == "true" {
|
if isHtmx == "true" {
|
||||||
u = &ent.User{
|
u = &types.LoginUser{
|
||||||
Email: r.PostFormValue("email"),
|
Email: r.PostFormValue("email"),
|
||||||
Password: r.PostFormValue("password"),
|
Password: r.PostFormValue("password"),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,12 +3,15 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"portfolio/api/service/jwt"
|
"portfolio/api/service/jwt"
|
||||||
"portfolio/api/service/parse"
|
"portfolio/api/service/parse"
|
||||||
"portfolio/database/ent"
|
"portfolio/database/ent"
|
||||||
"portfolio/database/query"
|
"portfolio/database/query"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {
|
func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -152,3 +155,27 @@ func GetProjectsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -6,22 +6,21 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"portfolio/api/service/bcrypt"
|
"portfolio/api/service/bcrypt"
|
||||||
"portfolio/api/service/validate"
|
"portfolio/api/service/validate"
|
||||||
"portfolio/database/ent"
|
"portfolio/api/types"
|
||||||
"portfolio/database/ent/user"
|
|
||||||
"portfolio/database/query"
|
"portfolio/database/query"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var u *ent.User
|
var u *types.RegisterUser
|
||||||
|
|
||||||
isHtmx := r.Header.Get("HX-Request")
|
isHtmx := r.Header.Get("HX-Request")
|
||||||
|
|
||||||
if isHtmx == "true" {
|
if isHtmx == "true" {
|
||||||
u = &ent.User{
|
u = &types.RegisterUser{
|
||||||
Name: r.PostFormValue("name"),
|
Name: r.PostFormValue("name"),
|
||||||
Role: user.Role(r.PostFormValue("role")),
|
//Role: user.Role(r.PostFormValue("role")),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := json.NewDecoder(r.Body).Decode(&u)
|
err := json.NewDecoder(r.Body).Decode(&u)
|
||||||
@ -29,7 +28,7 @@ func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
InternalServerErrorHandler(w, err)
|
InternalServerErrorHandler(w, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
u.Password = "123"
|
||||||
if !validate.UserIsValid(u) {
|
if !validate.UserIsValid(u) {
|
||||||
BadRequestHandler(w)
|
BadRequestHandler(w)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
package validate
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"portfolio/database/ent"
|
"portfolio/api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UserIsValid(u *ent.User) bool {
|
func UserIsValid(u *types.RegisterUser) bool {
|
||||||
if len(u.Name) > 0 &&
|
if len(u.Name) > 0 &&
|
||||||
len(u.Email) > 0 &&
|
len(u.Email) > 0 &&
|
||||||
len(u.Password) > 0 {
|
len(u.Password) > 0 {
|
||||||
|
|||||||
@ -9,8 +9,14 @@ type Username struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LoginUser struct {
|
type LoginUser struct {
|
||||||
Email string
|
Email string `json:"email,omitempty"`
|
||||||
Password string
|
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 {
|
type User struct {
|
||||||
|
|||||||
@ -4,12 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"portfolio/api/types"
|
||||||
"portfolio/database"
|
"portfolio/database"
|
||||||
"portfolio/database/ent"
|
"portfolio/database/ent"
|
||||||
"portfolio/database/ent/user"
|
"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.
|
u, err := database.Client.User.
|
||||||
Query().
|
Query().
|
||||||
Where(user.Email(U.Email)).
|
Where(user.Email(U.Email)).
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"portfolio/api/types"
|
||||||
"portfolio/database"
|
"portfolio/database"
|
||||||
"portfolio/database/ent"
|
"portfolio/database/ent"
|
||||||
"portfolio/database/ent/user"
|
"portfolio/database/ent/user"
|
||||||
@ -22,7 +23,7 @@ func GetUser(ctx context.Context, id int) (*ent.User, error) {
|
|||||||
return u, nil
|
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.
|
_, err := database.Client.User.
|
||||||
Create().
|
Create().
|
||||||
|
|||||||
@ -24,6 +24,8 @@ services:
|
|||||||
depends_on:
|
depends_on:
|
||||||
database:
|
database:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
volumes:
|
||||||
|
- ./backup:/web/assets/json
|
||||||
|
|
||||||
docs:
|
docs:
|
||||||
build:
|
build:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user