package database import ( "crowsnest/internal/model" "database/sql" "net/url" "time" ) type ArticlePageViewModelRepository struct { DB *sql.DB } // Gets all the view model for the article pages given a article id. This may // throw an error if the connection to the database fails. func (m *ArticlePageViewModelRepository) ById(id int64) (*model.ArticlePageViewModel, error) { stmt := ` SELECT a.sourceUrl, a.publishDate, a.title, a.content, d.summary FROM articles a JOIN documents d ON a.document_id = d.id WHERE a.id = $1 ` rows := m.DB.QueryRow(stmt, id) a := &model.ArticlePageViewModel{} var date time.Time if err := rows.Scan(&a.SourceUrl, &date, &a.Title, &a.Content, &a.Summary); err != nil { return nil, err } // short url parsedURL, err := url.Parse(a.SourceUrl) if err == nil { a.ShortSource = parsedURL.Hostname() } else { a.ShortSource = "" } // publish date a.PublishDate = date.Format("02.01.2006 15:04") return a, nil }