123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package repositories
- //go:generate mkdir -p mocks
- //go:generate rm -rf ./mocks/*_minimock.go
- //go:generate minimock -i github.com/dmitriygnatenko/internal/interfaces.IArticleRepository -o ./mocks/ -s "_minimock.go"
- import (
- "context"
- "database/sql"
- "github.com/dmitriygnatenko/internal/interfaces"
- "github.com/dmitriygnatenko/internal/models"
- )
- type articleRepository struct {
- db *sql.DB
- }
- func InitArticleRepository(db *sql.DB) interfaces.IArticleRepository {
- return articleRepository{db: db}
- }
- func (a articleRepository) GetAllPreview(ctx context.Context) ([]models.ArticlePreview, error) {
- var res []models.ArticlePreview
- query := "SELECT id, url, publish_time, title, preview_text, image " +
- "FROM " + articleTableName + " " +
- "WHERE is_active = 1 " +
- "ORDER BY publish_time DESC"
- rows, err := a.db.QueryContext(ctx, query)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- for rows.Next() {
- row := models.ArticlePreview{}
- err = rows.Scan(
- &row.ID,
- &row.URL,
- &row.PublishTime,
- &row.Title,
- &row.PreviewText,
- &row.Image,
- )
- if err != nil {
- return nil, err
- }
- res = append(res, row)
- }
- if err = rows.Err(); err != nil {
- return nil, err
- }
- return res, nil
- }
- func (a articleRepository) GetAll(ctx context.Context) ([]models.Article, error) {
- var res []models.Article
- query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
- "FROM " + articleTableName + " " +
- "ORDER BY publish_time DESC"
- rows, err := a.db.QueryContext(ctx, query)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- for rows.Next() {
- row := models.Article{}
- err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.Image, &row.Text, &row.PreviewText, &row.MetaKeywords, &row.MetaDescription, &row.IsActive)
- if err != nil {
- return nil, err
- }
- res = append(res, row)
- }
- if err = rows.Err(); err != nil {
- return nil, err
- }
- return res, nil
- }
- func (a articleRepository) GetPreviewByTagID(ctx context.Context, tagID int) ([]models.ArticlePreview, error) {
- var res []models.ArticlePreview
- query := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
- "FROM " + articleTableName + " a, " + articleTagTableName + " at " +
- "WHERE a.is_active = 1 AND at.article_id = a.id AND at.tag_id = ? " +
- "ORDER BY a.publish_time DESC"
- rows, err := a.db.QueryContext(ctx, query, tagID)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- for rows.Next() {
- row := models.ArticlePreview{}
- err = rows.Scan(
- &row.ID,
- &row.URL,
- &row.PublishTime,
- &row.Title,
- &row.PreviewText,
- &row.Image,
- )
- if err != nil {
- return nil, err
- }
- res = append(res, row)
- }
- if err = rows.Err(); err != nil {
- return nil, err
- }
- return res, nil
- }
- func (a articleRepository) GetByURL(ctx context.Context, url string) (*models.Article, error) {
- var res models.Article
- query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
- "FROM " + articleTableName + " " +
- "WHERE url = ? LIMIT 1"
- err := a.db.QueryRowContext(ctx, query, url).
- Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
- if err != nil {
- return nil, err
- }
- return &res, nil
- }
- func (a articleRepository) GetByID(ctx context.Context, id int) (*models.Article, error) {
- var res models.Article
- query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
- "FROM " + articleTableName + " " +
- "WHERE id = ? LIMIT 1"
- err := a.db.QueryRowContext(ctx, query, id).
- Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
- if err != nil {
- return nil, err
- }
- return &res, nil
- }
- func (a articleRepository) Add(ctx context.Context, m models.Article) (int, error) {
- query := "INSERT INTO " + articleTableName + " " +
- "(url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active) " +
- "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
- res, err := a.db.ExecContext(ctx, query, m.URL, m.PublishTime, m.Title, m.Image, m.Text, m.PreviewText, m.MetaKeywords, m.MetaDescription, m.IsActive)
- if err != nil {
- return 0, err
- }
- id, err := res.LastInsertId()
- if err != nil {
- return 0, err
- }
- return int(id), err
- }
- func (a articleRepository) Update(ctx context.Context, m models.Article) error {
- query := "UPDATE " + articleTableName + " " +
- "SET url = ?, publish_time = ?, title = ?, image = ?, text = ?, preview_text = ?, meta_keywords = ?, " +
- " meta_desc = ?, is_active = ? WHERE id = ?"
- _, err := a.db.ExecContext(ctx, query, m.URL, m.PublishTime, m.Title, m.Image, m.Text, m.PreviewText,
- m.MetaKeywords, m.MetaDescription, m.IsActive, m.ID)
- return err
- }
- func (a articleRepository) Delete(ctx context.Context, id int) error {
- query := "DELETE FROM " + articleTableName + " WHERE id = ?"
- _, err := a.db.ExecContext(ctx, query, id)
- return err
- }
|