169 lines
4.2 KiB
Go
169 lines
4.2 KiB
Go
/*
|
|
YetAnotherToDoList
|
|
Copyright © 2023 gilex-dev gilex-dev@proton.me
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, version 3.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package database
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph/model"
|
|
)
|
|
|
|
func (db CustomDB) CreateRole(newRole *model.NewRole) (role *model.Role, err error) {
|
|
statement, err := db.connection.Prepare("INSERT INTO Role (roleName, IS_admin, IS_userCreator) VALUES (?, ?, ?)")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rows, err := statement.Exec(newRole.RoleName, newRole.IsAdmin, newRole.IsUserCreator)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
num, err := rows.RowsAffected()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if num < 1 {
|
|
return nil, errors.New("no rows affected")
|
|
}
|
|
|
|
insertId, err := rows.LastInsertId()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &model.Role{ID: strconv.FormatInt(insertId, 10), RoleName: newRole.RoleName, IsAdmin: newRole.IsAdmin, IsUserCreator: newRole.IsUserCreator}, nil
|
|
}
|
|
|
|
func (db CustomDB) GetRole(role *model.Role) (*model.Role, error) {
|
|
id, err := strconv.Atoi(role.ID)
|
|
if err != nil {
|
|
return nil, errors.New("invalid roleId")
|
|
}
|
|
statement, err := db.connection.Prepare("SELECT roleName, IS_admin, IS_userCreator FROM Role WHERE roleId = ?")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := statement.QueryRow(id)
|
|
if err := result.Scan(&role.RoleName, &role.IsAdmin, &role.IsUserCreator); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return role, nil
|
|
}
|
|
|
|
func (db CustomDB) GetRolesFrom(userId string) ([]*model.Role, error) {
|
|
numUserId, err := strconv.Atoi(userId)
|
|
if err != nil {
|
|
return nil, errors.New("invalid userId")
|
|
}
|
|
statement, err := db.connection.Prepare("SELECT Role.roleId, Role.roleName, Role.IS_admin, Role.IS_userCreator FROM Role INNER JOIN R_User_Role ON R_User_Role.FK_Role_roleId = Role.roleId WHERE R_User_Role.FK_User_userId = ?")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rows, err := statement.Query(numUserId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var all []*model.Role
|
|
for rows.Next() {
|
|
role := model.Role{}
|
|
if err := rows.Scan(&role.ID, &role.RoleName, &role.IsAdmin, &role.IsUserCreator); err != nil {
|
|
return nil, err
|
|
}
|
|
all = append(all, &role)
|
|
}
|
|
return all, nil
|
|
}
|
|
|
|
func (db CustomDB) GetAllRoles() ([]*model.Role, error) {
|
|
rows, err := db.connection.Query("SELECT roleId, roleName, IS_admin, IS_userCreator FROM Role")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var all []*model.Role
|
|
for rows.Next() {
|
|
var role model.Role
|
|
if err := rows.Scan(&role.ID, &role.RoleName, &role.IsAdmin, &role.IsUserCreator); err != nil {
|
|
return nil, err
|
|
}
|
|
all = append(all, &role)
|
|
}
|
|
return all, nil
|
|
}
|
|
|
|
func (db CustomDB) UpdateRole(roleId string, changes *model.UpdateRole) (*model.Role, error) {
|
|
|
|
id, err := strconv.Atoi(roleId)
|
|
if err != nil {
|
|
return nil, errors.New("invalid userId")
|
|
}
|
|
|
|
statement, err := db.connection.Prepare("UPDATE Role SET roleName = IFNULL(?, roleName), IS_admin = IFNULL(?, IS_admin), IS_userCreator = IFNULL(?, IS_userCreator) WHERE roleId = ?")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rows, err := statement.Exec(changes.RoleName, changes.IsAdmin, changes.IsUserCreator, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
num, err := rows.RowsAffected()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if num < 1 {
|
|
return nil, errors.New("no rows affected")
|
|
}
|
|
|
|
return db.GetRole(&model.Role{ID: roleId})
|
|
}
|
|
|
|
func (db CustomDB) DeleteRole(roleId string) (*string, error) {
|
|
statement, err := db.connection.Prepare("DELETE FROM Role WHERE roleId = ?")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rows, err := statement.Exec(roleId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
num, err := rows.RowsAffected()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if num < 1 {
|
|
return nil, errors.New("no rows affected")
|
|
}
|
|
|
|
return &roleId, nil
|
|
}
|