func ListTasks() ([]icarus.TaskData, error) { InitStorage() rows, err := db.Query("SELECT task.id, task.handle, user.userid, user.password FROM task INNER JOIN user ON task.id = user.task_id") if err != nil { log.Warnf("storage.GetAllTasks(): Failed to fetch from database: %s.", err.Error()) return nil, err } defer rows.Close() res := make([]icarus.TaskData, 0) for rows.Next() { var entry icarus.TaskData entry.Courses = make([]icarus.CourseData, 0) err = rows.Scan(&entry.ID, &entry.Handle, &entry.User.UserID, &entry.User.Password) if err != nil { log.Warnf("storage.GetAllTasks(): Failed to fetch a task: %s.", err.Error()) continue } rowsCourse, err := db.Query("SELECT course.name, course.desc, course.token FROM course WHERE course.task_id = ?", entry.ID) if err != nil { log.Warnf("storage.GetAllTasks(): Task %d: Failed to fetch courses: %s.", entry.ID, err.Error()) continue } func(r *sql.Rows) { defer r.Close() for r.Next() { var cs icarus.CourseData err = r.Scan(&cs.Name, &cs.Desc, &cs.Token) if err != nil { log.Warnf("storage.GetAllTasks(): Task %d: Failed to fetch a course: %s.", entry.ID, err.Error()) continue } entry.Courses = append(entry.Courses, cs) } }(rowsCourse) res = append(res, entry) } return res, nil }
func CreateTask(taskdata icarus.TaskData) (ID int, t *task.Task, err error) { mu.Lock() defer mu.Unlock() ID, err = storage.CreateTask(taskdata) if err != nil { return } taskdata.ID = ID t, err = addTask(taskdata) return }