Ejemplo n.º 1
0
func GetBooks() *Books {
	con := rest.Conn()

	var booksArray = []Book{}

	rows, err := con.Query("select b.id,l.title as library,b.title from books b LEFT JOIN libraries l on (l.id = b.library_id) where true ORDER BY id DESC")
	defer rows.Close()
	for rows.Next() {
		var book = Book{}
		err := rows.Scan(&book.Id, &book.Library, &book.Title)
		if err != nil {
			fmt.Println(err)
		}
		book.setAuthors()
		booksArray = append(booksArray, book)
	}
	err = rows.Err()
	if err != nil {
		fmt.Println(err)
	}

	books := Books{
		Books: booksArray,
	}
	return &books
}
Ejemplo n.º 2
0
func (a *Author) Save() int64 {
	con := rest.Conn()
	var author_id int64

	err := con.QueryRow(`INSERT INTO authors (name) VALUES ($1) RETURNING id`, html.EscapeString(a.Name)).Scan(&author_id)
	checkErr(err)

	a.Id = author_id
	return author_id
}
Ejemplo n.º 3
0
func (b *Book) Delete() int64 {
	con := rest.Conn()

	res, err := con.Exec("delete from books where id=$1", b.Id)
	checkErr(err)
	affect, err := res.RowsAffected()

	_, err = con.Exec("delete from books_to_authors where book_id=$1", b.Id)
	checkErr(err)
	return affect
}
Ejemplo n.º 4
0
func (b *Book) Save() int64 {
	con := rest.Conn()

	var bookid int64
	err := con.QueryRow(`INSERT INTO books (title,library_id) VALUES ($1,$2) RETURNING id`, html.EscapeString(b.Title), b.Library_id).Scan(&bookid)
	checkErr(err)

	for _, value := range b.Authors {
		_, err := con.Exec("INSERT INTO books_to_authors (book_id,author_id) VALUES ($1,$2)", bookid, value.Id)
		checkErr(err)
	}

	b.Id = bookid

	return bookid
}
Ejemplo n.º 5
0
func (l *Library) Save() int64 {
	con := rest.Conn()

	ins, err := con.Prepare("INSERT INTO libraries (title) VALUES (?)")
	checkErr(err)

	res, err := ins.Exec(html.EscapeString(l.Title))
	checkErr(err)

	id, err := res.LastInsertId()
	checkErr(err)

	l.Id = id

	return id
}
Ejemplo n.º 6
0
func (b *Book) setAuthors() {
	con := rest.Conn()
	var authorsArray = []Author{}

	query := fmt.Sprintf("SELECT a.id,a.name FROM books_to_authors b LEFT JOIN authors a on (a.id = b.author_id) WHERE b.book_id = %v", b.Id)
	rows, err := con.Query(query)
	defer rows.Close()
	for rows.Next() {
		var author = Author{}
		err := rows.Scan(&author.Id, &author.Name)
		if err != nil {
			fmt.Println(err)
		}
		authorsArray = append(authorsArray, author)
	}
	err = rows.Err()
	if err != nil {
		fmt.Println(err)
	}

	b.Authors = authorsArray
}
Ejemplo n.º 7
0
func GetAuthors() *[]Author {
	con := rest.Conn()

	var authorsArray = []Author{}

	rows, err := con.Query("SELECT a.id,a.name FROM  authors a WHERE true")
	defer rows.Close()
	for rows.Next() {
		var author = Author{}
		err := rows.Scan(&author.Id, &author.Name)
		if err != nil {
			fmt.Println(err)
		}
		authorsArray = append(authorsArray, author)
	}
	err = rows.Err()
	if err != nil {
		fmt.Println(err)
	}

	return &authorsArray
}
Ejemplo n.º 8
0
func (LibHandler) Get(values url.Values, id int) (int, interface{}) {
	return 200, models.GetLibraries(rest.Conn())
}