Merge branch 'dev/cedric' into dev/guerby
This commit is contained in:
1
backend/.gitignore
vendored
Normal file
1
backend/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
tmp
|
||||||
@@ -10,12 +10,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
|
"fr.latosa-escrima/utils"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
var MySigningKey = []byte("COUCOU")
|
var MySigningKey = []byte("COUCOU")
|
||||||
|
|
||||||
type LoginInformation struct {
|
type LoginArgs struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
@@ -26,14 +27,6 @@ type Claims struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
core.JSONError{
|
|
||||||
Status: core.Error,
|
|
||||||
Message: "Method is not allowed",
|
|
||||||
}.Respond(w, http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
@@ -50,7 +43,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusNoContent)
|
}.Respond(w, http.StatusNoContent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var login LoginInformation
|
var login LoginArgs
|
||||||
err = json.Unmarshal(body, &login)
|
err = json.Unmarshal(body, &login)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
@@ -151,3 +144,25 @@ func AuthJWT(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @param methods string -> HttpMethods separated by commas.
|
||||||
|
func Methods(methods string) core.Middleware {
|
||||||
|
_methods := strings.Split(strings.ToUpper(methods), ",")
|
||||||
|
middleware := func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if len(methods) > 0 {
|
||||||
|
if !utils.Contains(_methods, r.Method) {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: "Method is not allowed.",
|
||||||
|
}.Respond(w, http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return middleware
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type JSONStatus string
|
type JSONStatus string
|
||||||
@@ -55,33 +55,23 @@ func (r JSONSuccess) Respond(w http.ResponseWriter, code int) {
|
|||||||
defaultResponse(&r, w, code)
|
defaultResponse(&r, w, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleMiddlewareRoute(pattern string,
|
type Middleware func(http.Handler) http.Handler
|
||||||
handler func(w http.ResponseWriter, r *http.Request),
|
|
||||||
middleware func(http.Handler) http.Handler,
|
|
||||||
mux *http.ServeMux,
|
|
||||||
) {
|
|
||||||
// mux.HandleFunc(pattern, handler)
|
|
||||||
mux.Handle(pattern, middleware(http.HandlerFunc(handler)))
|
|
||||||
}
|
|
||||||
|
|
||||||
type HandlerFunc func(w http.ResponseWriter, r *http.Request)
|
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
Handler HandlerFunc
|
Handler http.HandlerFunc
|
||||||
Middleware func(http.Handler) http.Handler
|
Middlewares []Middleware
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleRoutes(mux *http.ServeMux, routes map[string]Handler) {
|
func HandleRoutes(mux *http.ServeMux, routes map[string]Handler) {
|
||||||
for pattern, handler := range routes {
|
for pattern, handler := range routes {
|
||||||
if handler.Middleware == nil {
|
if handler.Middlewares == nil {
|
||||||
mux.HandleFunc(pattern, handler.Handler)
|
mux.HandleFunc(pattern, handler.Handler)
|
||||||
} else {
|
} else {
|
||||||
HandleMiddlewareRoute(
|
h := http.HandlerFunc(handler.Handler)
|
||||||
pattern,
|
for _, middleware := range handler.Middlewares {
|
||||||
handler.Handler,
|
h = http.HandlerFunc(middleware(h).ServeHTTP)
|
||||||
handler.Middleware,
|
}
|
||||||
mux,
|
mux.Handle(pattern, h)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ type User struct {
|
|||||||
FirstName string `bun:"firstname,notnull" json:"firstname"`
|
FirstName string `bun:"firstname,notnull" json:"firstname"`
|
||||||
LastName string `bun:"lastname,notnull" json:"lastname"`
|
LastName string `bun:"lastname,notnull" json:"lastname"`
|
||||||
Email string `bun:"email,unique,notnull" json:"email"`
|
Email string `bun:"email,unique,notnull" json:"email"`
|
||||||
Password string `bun:"password,notnull" json:"password"`
|
Password string `bun:"password,notnull" json:"password,omitempty"`
|
||||||
Phone string `bun:"phone,notnull" json:"phone"`
|
Phone string `bun:"phone,notnull" json:"phone"`
|
||||||
Role Role `bun:"role,notnull,default:'user'" json:"role"`
|
Role Role `bun:"role,notnull,default:'user'" json:"role"`
|
||||||
CreatedAt time.Time `bun:"created_at,default:current_timestamp" json:"createdAt"`
|
CreatedAt time.Time `bun:"created_at,default:current_timestamp" json:"createdAt"`
|
||||||
|
|||||||
@@ -8,14 +8,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleDeleteUser(w http.ResponseWriter, r *http.Request) {
|
func HandleDeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodDelete {
|
|
||||||
core.JSONError{
|
|
||||||
Status: core.Error,
|
|
||||||
Message: "Method is not allowed.",
|
|
||||||
}.Respond(w, http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
uuid := r.PathValue("user_uuid")
|
uuid := r.PathValue("user_uuid")
|
||||||
_, err := core.DB.NewDelete().
|
_, err := core.DB.NewDelete().
|
||||||
Model((*core.User)(nil)).
|
Model((*core.User)(nil)).
|
||||||
|
|||||||
@@ -8,14 +8,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleGetUser(w http.ResponseWriter, r *http.Request) {
|
func HandleGetUser(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
core.JSONError{
|
|
||||||
Status: core.Error,
|
|
||||||
Message: "Method is not allowed.",
|
|
||||||
}.Respond(w, http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
uuid := r.PathValue("user_uuid")
|
uuid := r.PathValue("user_uuid")
|
||||||
var user core.User
|
var user core.User
|
||||||
count, err := core.DB.NewSelect().
|
count, err := core.DB.NewSelect().
|
||||||
@@ -40,6 +32,8 @@ func HandleGetUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user.Password = ""
|
||||||
|
|
||||||
// TODO : Remove password
|
// TODO : Remove password
|
||||||
core.JSONSuccess{
|
core.JSONSuccess{
|
||||||
Status: core.Success,
|
Status: core.Success,
|
||||||
|
|||||||
44
backend/api/get_users.go
Normal file
44
backend/api/get_users.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"fr.latosa-escrima/api/core"
|
||||||
|
"fr.latosa-escrima/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleGetUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var users []core.User
|
||||||
|
count, err := core.DB.NewSelect().
|
||||||
|
Model(&users).
|
||||||
|
ScanAndCount(context.Background())
|
||||||
|
|
||||||
|
users = utils.Map(users, func(user core.User) core.User {
|
||||||
|
user.Password = ""
|
||||||
|
return user
|
||||||
|
})
|
||||||
|
|
||||||
|
if count == 0 {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: "Not users.",
|
||||||
|
}.Respond(w, http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : Remove password
|
||||||
|
core.JSONSuccess{
|
||||||
|
Status: core.Success,
|
||||||
|
Message: "Users found.",
|
||||||
|
Data: users,
|
||||||
|
}.Respond(w, http.StatusOK)
|
||||||
|
}
|
||||||
@@ -11,15 +11,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
core.JSONError{
|
|
||||||
Status: core.Error,
|
|
||||||
Message: "This method is not allowed",
|
|
||||||
}.Respond(w, http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
@@ -41,7 +32,7 @@ func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
log.Println(user)
|
log.Println(user)
|
||||||
|
|
||||||
res, err := core.DB.NewInsert().Model(user).Exec(context.Background())
|
res, err := core.DB.NewInsert().Model(&user).Exec(context.Background())
|
||||||
if res == nil {
|
if res == nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
|
|||||||
88
backend/api/update_user.go
Normal file
88
backend/api/update_user.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"fr.latosa-escrima/api/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpdateUserArgs struct {
|
||||||
|
FirstName *string `json:"firstname,omitempty"`
|
||||||
|
LastName *string `json:"lastname,omitempty"`
|
||||||
|
Email *string `json:"email,omitempty"`
|
||||||
|
Password *string `json:"password,omitempty"`
|
||||||
|
Phone *string `json:"phone,omitempty"`
|
||||||
|
Role *core.Role `json:"role,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var updateArgs UpdateUserArgs
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(body, &updateArgs)
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var user core.User
|
||||||
|
updateQuery := core.DB.NewUpdate().Model(&user)
|
||||||
|
|
||||||
|
val := reflect.ValueOf(updateArgs)
|
||||||
|
typ := reflect.TypeOf(updateArgs)
|
||||||
|
|
||||||
|
for i := 0; i < val.NumField(); i++ {
|
||||||
|
field := val.Field(i)
|
||||||
|
|
||||||
|
tag := typ.Field(i).Tag.Get("bun")
|
||||||
|
if tag == "" {
|
||||||
|
tag = typ.Field(i).Tag.Get("json")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only add fields that are non-nil and non-zero
|
||||||
|
if field.IsValid() && !field.IsNil() && !field.IsZero() {
|
||||||
|
updateQuery.Set(fmt.Sprintf("%s = ?", strings.Split(tag, ",")[0]), field.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always update the `updated_at` field
|
||||||
|
updateQuery.Set("updated_at = ?", time.Now())
|
||||||
|
|
||||||
|
uuid := r.PathValue("user_uuid")
|
||||||
|
_, err = updateQuery.
|
||||||
|
Where("user_id = ?", uuid).
|
||||||
|
Returning("*").
|
||||||
|
Exec(context.Background())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user.Password = ""
|
||||||
|
|
||||||
|
core.JSONSuccess{
|
||||||
|
Status: core.Success,
|
||||||
|
Message: "User updated.",
|
||||||
|
Data: user,
|
||||||
|
}.Respond(w, http.StatusOK)
|
||||||
|
}
|
||||||
@@ -43,18 +43,33 @@ func main() {
|
|||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
core.HandleRoutes(mux, map[string]core.Handler{
|
core.HandleRoutes(mux, map[string]core.Handler{
|
||||||
"/": {Handler: handler, Middleware: nil},
|
"/": {
|
||||||
"/users/login": {Handler: api.HandleLogin, Middleware: nil},
|
Handler: handler,
|
||||||
"/users/new": {Handler: api.HandleCreateUser, Middleware: api.AuthJWT},
|
Middlewares: []core.Middleware{api.Methods("post")}},
|
||||||
"/users/{user_uuid}": {Handler: api.HandleGetUser, Middleware: api.AuthJWT},
|
"/users/login": {
|
||||||
"/users/{user_uuid}/delete": {Handler: api.HandleDeleteUser, Middleware: api.AuthJWT},
|
Handler: api.HandleLogin,
|
||||||
// "/users/{user_uuid}/update": {Handler: api.HandleUpdateUser, Middleware: api.AuthJWT},
|
Middlewares: []core.Middleware{api.Methods("POST")}},
|
||||||
|
"/users": {
|
||||||
|
Handler: api.HandleGetUsers,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
|
||||||
|
"/users/new": {
|
||||||
|
Handler: api.HandleCreateUser,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT}},
|
||||||
|
"/users/{user_uuid}": {
|
||||||
|
Handler: api.HandleGetUser,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
|
||||||
|
"/users/{user_uuid}/delete": {
|
||||||
|
Handler: api.HandleDeleteUser,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("DELETE"), api.AuthJWT}},
|
||||||
|
"/users/{user_uuid}/update": {
|
||||||
|
Handler: api.HandleUpdateUser,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}},
|
||||||
// "/users/{user_uuid}/events": {Handler: nil, Middleware: nil},
|
// "/users/{user_uuid}/events": {Handler: nil, Middleware: nil},
|
||||||
// "/users/{user_uuid}/events/{event_uuid}": {Handler: nil, Middleware: nil},
|
// "/users/{user_uuid}/events/{event_uuid}": {Handler: nil, Middleware: nil},
|
||||||
// "/users/{user_uuid}/events/{event_uuid}/delete": {Handler: nil, Middleware: nil},
|
// "/users/{user_uuid}/events/{event_uuid}/delete": {Handler: nil, Middleware: nil},
|
||||||
// "/users/{user_uuid}/events/{event_uuid}/update": {Handler: nil, Middleware: nil},
|
// "/users/{user_uuid}/events/{event_uuid}/update": {Handler: nil, Middleware: nil},
|
||||||
"/blogs/new": {Handler: api.HandleCreateBlog, Middleware: nil},
|
"/blogs/new": {Handler: api.HandleCreateBlog, Middlewares: nil},
|
||||||
"/blogs/{uuid}": {Handler: api.HandleGetBlog, Middleware: nil},
|
"/blogs/{uuid}": {Handler: api.HandleGetBlog, Middlewares: nil},
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Serving on port %s\n", port)
|
fmt.Printf("Serving on port %s\n", port)
|
||||||
|
|||||||
11
backend/utils/contains.go
Normal file
11
backend/utils/contains.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
func Contains[T comparable](arr []T, el T) bool {
|
||||||
|
for _, a := range arr {
|
||||||
|
if a == el {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
9
backend/utils/map.go
Normal file
9
backend/utils/map.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
func Map[T any, U any](input []T, fn func(T) U) []U {
|
||||||
|
var result []U
|
||||||
|
for _, v := range input {
|
||||||
|
result = append(result, fn(v))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user