Reorganization of backend + new routes

This commit is contained in:
cdricms
2025-01-29 18:09:41 +01:00
parent 7c66353e63
commit 8110172a38
67 changed files with 1124 additions and 400 deletions

View File

@@ -1 +1,62 @@
package roles
import (
"context"
"net/http"
"strconv"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
"github.com/google/uuid"
)
func HandleAddPermission(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
role_id := r.PathValue("role_uuid")
permission_id := r.PathValue("permission_id")
var permission models.Permission
count, err := core.DB.NewSelect().Model(&permission).
Where("id = ?", permission_id).
Limit(1).ScanAndCount(ctx)
if count == 0 {
core.JSONError{
Status: core.Error,
Message: "Permission doesn't exist.",
}.Respond(w, http.StatusNotFound)
return
}
var role models.Role
count, err = core.DB.NewSelect().Model(&role).
Where("id = ?", role_id).
Limit(1).ScanAndCount(ctx)
if count == 0 {
core.JSONError{
Status: core.Error,
Message: "Role doesn't exist.",
}.Respond(w, http.StatusNotFound)
return
}
pid, err := strconv.Atoi(permission_id)
rid, err := uuid.Parse(role_id)
permissionRole := models.PermissionToRole{
PermissionID: pid,
RoleID: rid,
}
_, err = core.DB.NewInsert().Model(&permissionRole).Ignore().
Exec(ctx)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Permission added.",
}.Respond(w, http.StatusCreated)
}

View File

@@ -1 +1,30 @@
package roles
import (
"context"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
)
func HandleDelete(w http.ResponseWriter, r *http.Request) {
uuid := r.PathValue("role_uuid")
_, err := core.DB.NewDelete().
Model((*models.Role)(nil)).
Where("id = ?", uuid).
Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
// TODO: Check SQL Result
core.JSONSuccess{
Status: core.Success,
Message: "Role deteled",
}.Respond(w, http.StatusOK)
}

View File

@@ -1 +1,37 @@
package roles
import (
"context"
"encoding/json"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
)
func HandleNew(w http.ResponseWriter, r *http.Request) {
var role models.Role
err := json.NewDecoder(r.Body).Decode(&role)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
_, err = core.DB.NewInsert().Model(&role).Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Role inserted",
Data: role,
}.Respond(w, http.StatusCreated)
}

View File

@@ -1 +1,35 @@
package roles
import (
"context"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
)
func HandleRolePermissions(w http.ResponseWriter, r *http.Request) {
uuid := r.PathValue("role_uuid")
var role models.Role
err := core.DB.NewSelect().
Model(&role).
Where("id = ?", uuid).
Relation("Permissions").
Limit(1).
Scan(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Permissions.",
Data: role.Permissions,
}.Respond(w, http.StatusOK)
}

View File

@@ -1 +1,32 @@
package roles
import (
"context"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
)
func HandleRemovePermission(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
role_id := r.PathValue("role_uuid")
permission_id := r.PathValue("permission_id")
_, err := core.DB.NewDelete().Model((*models.PermissionToRole)(nil)).
Where("permission_id = ? AND role_id = ?", permission_id, role_id).
Exec(ctx)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Permission removed.",
}.Respond(w, http.StatusOK)
}

View File

@@ -1 +1,40 @@
package roles
import (
"context"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
)
func HandleRole(w http.ResponseWriter, r *http.Request) {
uuid := r.PathValue("role_uuid")
var role models.Role
count, err := core.DB.NewSelect().
Model(&role).
Where("id = ?", uuid).
Relation("Permissions").
Limit(1).
ScanAndCount(context.Background())
if count == 0 {
core.JSONError{
Status: core.Error,
Message: "The role requested was not found.",
}.Respond(w, http.StatusNotFound)
return
}
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Role found.",
Data: role,
}.Respond(w, http.StatusOK)
}

View File

@@ -1 +1,39 @@
package roles
import (
"context"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
)
func HandleRoles(w http.ResponseWriter, r *http.Request) {
var roles []models.Role
count, err := core.DB.NewSelect().
Model(&roles).
Relation("Permissions").
ScanAndCount(context.Background())
if count == 0 {
core.JSONError{
Status: core.Error,
Message: "No role found.",
}.Respond(w, http.StatusNotFound)
return
}
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Roles found.",
Data: roles,
}.Respond(w, http.StatusOK)
}

View File

@@ -1 +1,52 @@
package roles
import (
"context"
"encoding/json"
"net/http"
"fr.latosa-escrima/core"
"fr.latosa-escrima/core/models"
"github.com/google/uuid"
)
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
var role models.Role
err := json.NewDecoder(r.Body).Decode(&role)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusBadRequest)
return
}
role_uuid := r.PathValue("role_uuid")
role.ID, err = uuid.Parse(role_uuid)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusBadRequest)
return
}
_, err = core.DB.NewUpdate().
Model(&role).
OmitZero().
WherePK().
Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: "Event not found.",
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Event updated",
Data: role,
}.Respond(w, http.StatusOK)
}