예제 #1
0
func allBooks() ([]models.Book, error) {
	//Retrieve
	books := []models.Book{}

	rows, err := db.Query(`SELECT id, name, author, pages, publication_date FROM books order by id`)
	defer rows.Close()
	if err == nil {
		for rows.Next() {
			var id int
			var name string
			var author string
			var pages int
			var publicationDate pq.NullTime

			err = rows.Scan(&id, &name, &author, &pages, &publicationDate)
			if err == nil {
				currentBook := models.Book{ID: id, Name: name, Author: author, Pages: pages}
				if publicationDate.Valid {
					currentBook.PublicationDate = publicationDate.Time
				}

				books = append(books, currentBook)
			} else {
				return books, err
			}
		}
	} else {
		return books, err
	}

	return books, err
}
예제 #2
0
func (c App) Book() revel.Result {
	var book = models.Book{}

	idStr := c.Params.Get("id")
	book.PublicationDate = time.Now()

	if len(idStr) > 0 {
		id, err := strconv.Atoi(idStr)
		if err != nil {
			panic(err)
		}

		book, err = getBook(id)
		if err != nil {
			panic(err)
		}
	}

	return c.Render(book)
}