// ScheduleList returns a list of all saved schedules func (d *SQLite) ScheduleList() ([]*spec.Schedule, error) { rows, err := d.Connection.Query("SELECT * FROM "+schedulesTable, nil) if err != nil { return nil, err } defer rows.Close() var ss []*spec.Schedule for rows.Next() { var id, backup int var body []byte err = rows.Scan(&id, &backup, &body) if err != nil { return nil, err } var s spec.Schedule json.Unmarshal(body, &s) s.Backup = backup s.ID = id ss = append(ss, &s) } return ss, nil }
func updateSchedule(w http.ResponseWriter, r *http.Request) (*httpapi.APIResponse, error) { body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576)) if err != nil { return nil, httpapi.NewError("", "Request too large", 413) } if err := r.Body.Close(); err != nil { return nil, errors.New("") } s := new(spec.Schedule) if err := json.Unmarshal(body, &s); err != nil { return nil, httpapi.NewError(err.Error(), "Invalid Request", 400) } vars := mux.Vars(r) sID, err := strconv.ParseInt(vars["sID"], 10, 64) if err != nil { return nil, httpapi.NewError(err.Error(), "Invalid schedule ID", 400) } s.ID = int(sID) if err := manager.UpdateSchedule(s); err != nil { return nil, httpapi.NewError(err.Error(), "Could not update schedule", 400) } return &httpapi.APIResponse{HTTPCode: 200}, nil }
// GetSchedule returns single schedule func (d *SQLite) GetSchedule(id int) (*spec.Schedule, error) { var s []byte var backup int err := d.Connection.QueryRow("SELECT * FROM "+schedulesTable+" WHERE id=?", id).Scan(&id, &backup, &s) switch { case err == sql.ErrNoRows: return nil, errors.New("No schedule with that ID") case err != nil: return nil, err default: var sched spec.Schedule json.Unmarshal(s, &sched) sched.ID = id return &sched, nil } }
// AddSchedule saves a schedule to the DB func (d *SQLite) AddSchedule(s *spec.Schedule) error { sql := "INSERT INTO " + schedulesTable + " (backup, schedule) values(?, ?)" sched, err := json.Marshal(s) if err != nil { return errors.New("Could not marshal schedule") } result, err := d.Connection.Exec(sql, s.Backup, sched) if err != nil { return err } id, err := result.LastInsertId() if err != nil { return nil } s.ID = int(id) return nil }