49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
)
|
|
|
|
// CreateGroup inserts a new group into the database
|
|
func CreateGroup(db *sql.DB, groupName string) error {
|
|
query := `INSERT INTO groups (group_name) VALUES ($1)`
|
|
_, err := db.Exec(query, groupName)
|
|
return err
|
|
}
|
|
|
|
// GetGroupByID retrieves a group by its ID
|
|
func GetGroupByID(db *sql.DB, groupID int) (*Groups, error) {
|
|
query := `SELECT group_id, group_name FROM groups WHERE group_id = $1`
|
|
row := db.QueryRow(query, groupID)
|
|
|
|
var group Groups
|
|
err := row.Scan(&group.GroupId, &group.GroupName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group, nil
|
|
}
|
|
|
|
// RemoveGroup deletes a group and all its associated tasks
|
|
func RemoveGroup(db *sql.DB, groupID int) error {
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
// Delete all tasks in the group first
|
|
_, err = tx.Exec("DELETE FROM tasks WHERE group_id = $1", groupID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Delete the group
|
|
_, err = tx.Exec("DELETE FROM groups WHERE group_id = $1", groupID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
} |