func (self *AuthorRepository) GetAllAuthors() ([]library.Author, error) { authors := []library.Author{} rows, err := db.AppDB().Query(` SELECT id, first_name, last_name FROM author `) if err != nil { return authors, err } defer rows.Close() for rows.Next() { author := library.Author{} err := rows.Scan(&author.ID, &author.FirstName, &author.LastName) if err != nil { log.Println("Error scanning authors: ", err) continue } authors = append(authors, author) } err = rows.Err() if err != nil { log.Println("Error scanning authors: ", err) } return authors, err }
func (self *BookRepository) GetBooksByAuthor(authorID int) ([]library.Book, error) { books := []library.Book{} rows, err := db.AppDB().Query(` SELECT b.id, b.title, b.read, a.id, a.first_name, a.last_name FROM book b LEFT JOIN author a on a.ID = b.authorid WHERE b.authorid = $1 `, authorID) if err != nil { return books, err } defer rows.Close() for rows.Next() { book := library.Book{} author := library.Author{} err := rows.Scan(&book.ID, &book.Title, &book.Read, &author.ID, &author.FirstName, &author.LastName) if err != nil { log.Println("Error scanning books: ", err) continue } book.Author = author books = append(books, book) } err = rows.Err() if err != nil { log.Println("Error scanning books: ", err) } return books, err }
func (s *ProfileRepository) GetProfile() (*profile.Profile, error) { profile := profile.Profile{} jsonRow := json.RawMessage{} err := db.AppDB().QueryRow(` SELECT data FROM profile ORDER BY data->>'id' ASC LIMIT 1 `).Scan((*[]byte)(&jsonRow)) if err != nil { if err != sql.ErrNoRows { return &profile, errors.New(fmt.Sprintf("Error retrieving profile: %s", err.Error())) } } err = json.Unmarshal(jsonRow, &profile) if err != nil { if err != sql.ErrNoRows { return &profile, errors.New(fmt.Sprintf("Error unmarshalling json: %s", err.Error())) } } return &profile, nil }