AddProject database interface with tests
This commit is contained in:
parent
6f51151d64
commit
229ebc3a08
2 changed files with 11 additions and 5 deletions
|
@ -14,6 +14,7 @@ type Db struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
|
const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
|
||||||
|
const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?"
|
||||||
|
|
||||||
// DbConnect connects to the database
|
// DbConnect connects to the database
|
||||||
func DbConnect() *Db {
|
func DbConnect() *Db {
|
||||||
|
@ -65,11 +66,8 @@ func (d *Db) GetUserId(username string) (int, error) {
|
||||||
return id, err
|
return id, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates a new project in the database, associated with a user
|
||||||
func (d *Db) AddProject(name string, description string, username string) error {
|
func (d *Db) AddProject(name string, description string, username string) error {
|
||||||
userId, err := d.GetUserId(username)
|
_, err := d.Exec(projectInsert, name, description, username)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = d.Exec("INSERT INTO projects (name, description, user_id) VALUES (?, ?, ?)", name, description, userId)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,14 @@ func TestDbGetUserId(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDbAddProject(t *testing.T) {
|
||||||
|
db := DbConnect()
|
||||||
|
err := db.AddProject("test", "description", "test")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("AddProject failed:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDbRemoveUser(t *testing.T) {
|
func TestDbRemoveUser(t *testing.T) {
|
||||||
db := DbConnect()
|
db := DbConnect()
|
||||||
err := db.RemoveUser("test")
|
err := db.RemoveUser("test")
|
||||||
|
|
Loading…
Add table
Reference in a new issue