Reorganization of backend + new routes
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user