func TestDeleteLibrary(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	l1 := testLibrary("l1", "/tmp/l1")
	err := database.SaveLibrary(l1)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	r, err := Delete(s.URL + "/api/libraries/" + strconv.FormatInt(l1.ID, 10))
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if r.StatusCode != http.StatusOK {
		t.Fatalf("expected status code 200, got %d", r.StatusCode)
	}

	l, err := database.GetLibrary(l1.ID)
	if err == nil {
		t.Fatalf("expected no results, got %v", l)
	}
}
Beispiel #2
0
// GET /libraries/:id/process
// Processes a library of files to fetch metadata
func LibraryProcess(w http.ResponseWriter, r *http.Request) (interface{}, error) {
	idstr := getPathParam(r, "id")
	id, err := strconv.ParseInt(idstr, 10, 64)
	if err != nil {
		return nil, err
	}

	library, err := database.GetLibrary(id)
	if err != nil {
		return nil, err
	}

	processor.ProcessLibrary(library)

	return nil, nil
}
Beispiel #3
0
// GET /api/libraries/:id
// Shows details for a library with :id
func LibraryShow(w http.ResponseWriter, r *http.Request) (interface{}, error) {
	idstr := getPathParam(r, "id")
	id, err := strconv.ParseInt(idstr, 10, 64)
	if err != nil {
		return nil, err
	}

	l, err := database.GetLibrary(id)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil, notFound{}
		} else {
			return nil, err
		}
	}

	return l, nil
}
Beispiel #4
0
// DELETE /libraries/:id
// Deletes a library identified by :id
func LibraryDelete(w http.ResponseWriter, r *http.Request) (interface{}, error) {
	idstr := getPathParam(r, "id")
	id, err := strconv.ParseInt(idstr, 10, 64)
	if err != nil {
		return nil, err
	}

	l, err := database.GetLibrary(id)
	if err != nil {
		return nil, err
	}

	err = database.DeleteLibrary(id)
	if err != nil {
		return nil, err
	}

	return l, nil
}
Beispiel #5
0
func LibraryProcess(w http.ResponseWriter, r *http.Request) error {
	idstr := r.URL.Query().Get(":id")

	log.Printf("Handling request to precess library: %s", idstr)

	id, err := strconv.ParseInt(idstr, 10, 64)
	if err != nil {
		return err
	}

	library, err := database.GetLibrary(id)
	if err != nil {
		return err
	}

	processor.ProcessLibrary(library)

	return RenderText(w, "Processing", 200)
}
func TestCreateLibrary(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	l1 := testLibrary("l1", "/tmp/l1")

	b, err := json.Marshal(l1)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	reader := strings.NewReader(fmt.Sprintf("%s", b))
	r, err := Put(s.URL+"/api/libraries", "application/json", reader)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var resp Library
	err = json.NewDecoder(r.Body).Decode(&resp)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if resp.ID == 0 {
		t.Errorf("expected non-zero id, got %d", resp.ID)
	}

	l2, err := database.GetLibrary(resp.ID)
	if err != nil {
		t.Errorf("library not saved to db with id %d", resp.ID)
	}

	if l2.Name != l1.Name {
		t.Errorf("expected name %q, got %q", l1.Name, l2.Name)
	}

	if l2.Path != l1.Path {
		t.Errorf("expected path %q, got %q", l1.Path, l2.Path)
	}
}