update example queries. fix error on todo creation

This commit is contained in:
gilex-dev 2023-11-17 19:09:01 +01:00
parent a52aad5af1
commit 4851da8d19
4 changed files with 76 additions and 5 deletions

View File

@ -78,7 +78,7 @@ func (db CustomDB) createSQLite3Tables() error {
sql string sql string
}{ }{
{"User", "userId INTEGER PRIMARY KEY NOT NULL, userName VARCHAR NOT NULL UNIQUE CHECK(length(userName)!=0), passwordHash VARCHAR NOT NULL CHECK(length(passwordHash)!=0), fullName VARCHAR CHECK(length(fullName)!=0)"}, {"User", "userId INTEGER PRIMARY KEY NOT NULL, userName VARCHAR NOT NULL UNIQUE CHECK(length(userName)!=0), passwordHash VARCHAR NOT NULL CHECK(length(passwordHash)!=0), fullName VARCHAR CHECK(length(fullName)!=0)"},
{"Todo", "todoId INTEGER PRIMARY KEY NOT NULL, text VARCHAR NOT NULL CHECK(length(text)!=0), IS_done BOOL NOT NULL, FK_User_userId INTEGER NOT NULL, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, {"Todo", "todoId INTEGER PRIMARY KEY NOT NULL, text VARCHAR NOT NULL CHECK(length(text)!=0), IS_done BOOL NOT NULL DEFAULT false, FK_User_userId INTEGER NOT NULL, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"},
{"R_User_Role", "relationId INTEGER PRIMARY KEY NOT NULL, FK_Role_roleId INTEGER NOT NULL, FK_User_userId INTEGER NOT NULL, UNIQUE(FK_Role_roleId, FK_User_userId), FOREIGN KEY(FK_Role_roleId) REFERENCES Role(roleId) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, {"R_User_Role", "relationId INTEGER PRIMARY KEY NOT NULL, FK_Role_roleId INTEGER NOT NULL, FK_User_userId INTEGER NOT NULL, UNIQUE(FK_Role_roleId, FK_User_userId), FOREIGN KEY(FK_Role_roleId) REFERENCES Role(roleId) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"},
{"Role", "roleId INTEGER PRIMARY KEY NOT NULL, roleName VARCHAR NOT NULL UNIQUE CHECK(length(roleName)!=0), IS_admin BOOL NOT NULL, IS_userCreator BOOL NOT NULL"}, {"Role", "roleId INTEGER PRIMARY KEY NOT NULL, roleName VARCHAR NOT NULL UNIQUE CHECK(length(roleName)!=0), IS_admin BOOL NOT NULL, IS_userCreator BOOL NOT NULL"},
{"RefreshToken", "tokenId INTEGER PRIMARY KEY NOT NULL, FK_User_userId INTEGER NOT NULL, selector VARCHAR NOT NULL CHECK(length(selector)!=0) UNIQUE, tokenHash VARCHAR NOT NULL CHECK(length(tokenHash)!=0), expiryDate INTEGER NOT NULL, tokenName VARCHAR CHECK(length(tokenName)!=0), FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, {"RefreshToken", "tokenId INTEGER PRIMARY KEY NOT NULL, FK_User_userId INTEGER NOT NULL, selector VARCHAR NOT NULL CHECK(length(selector)!=0) UNIQUE, tokenHash VARCHAR NOT NULL CHECK(length(tokenHash)!=0), expiryDate INTEGER NOT NULL, tokenName VARCHAR CHECK(length(tokenName)!=0), FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"},

View File

@ -113,8 +113,8 @@ func (db CustomDB) GetAllTodos() ([]*model.Todo, error) {
return todos, nil return todos, nil
} }
// AddTodo adds a Todo to passed UserID. Check if the source has the rights to call this. // CreateTodo creates a Todo for the passed newTodo.UserID. Check if the source has the rights to call this.
func (db CustomDB) AddTodo(newTodo model.NewTodo) (*model.Todo, error) { func (db CustomDB) CreateTodo(newTodo model.NewTodo) (*model.Todo, error) {
statement, err := db.connection.Prepare("INSERT INTO Todo (text, FK_User_userID) VALUES (?, ?)") statement, err := db.connection.Prepare("INSERT INTO Todo (text, FK_User_userID) VALUES (?, ?)")
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -10,6 +10,12 @@ query getUser {
text text
done done
} }
roles {
id
roleName
isAdmin
isUserCreator
}
} }
} }
@ -65,7 +71,13 @@ mutation updateTodo {
} }
mutation createUser { mutation createUser {
createUser(input: { userName: "1234Lorem", fullName: "Lorem I." }) { createUser(
input: {
userName: "1234Lorem"
fullName: "Lorem I."
password: "t1meToDoSometh1ng"
}
) {
id id
} }
} }
@ -75,3 +87,62 @@ mutation updateUser {
fullName fullName
} }
} }
query getRefreshTokens {
refreshTokens {
id
userId
tokenName
}
}
mutation createRefreshToken {
createRefreshToken(input: { tokenName: "myNewDevice" }) {
userId
tokenName
selector
token
}
}
mutation deleteRefreshToken {
deleteRefreshToken(id: 1)
}
query getRoles {
roles {
id
roleName
}
}
query getRole {
role(id: 2) {
id
roleName
}
}
mutation createRole {
createRole(
input: { roleName: "moderator", isAdmin: false, isUserCreator: true }
) {
id
}
}
mutation deleteRole {
deleteRole(id: 2)
}
mutation addRole {
addRole(userId: 1, roleId: 2) {
roleName
}
}
mutation removeRole {
removeRole(userId: 2, roleId: 1) {
roleName
}
}

View File

@ -25,7 +25,7 @@ func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser)
// CreateTodo is the resolver for the createTodo field. // CreateTodo is the resolver for the createTodo field.
func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) { func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
todo, err := globals.DB.AddTodo(input) todo, err := globals.DB.CreateTodo(input)
if err != nil { if err != nil {
globals.Logger.Println("Failed to add new todo:", err) globals.Logger.Println("Failed to add new todo:", err)
return nil, errors.New("failed to add new todo") return nil, errors.New("failed to add new todo")