<commit_before>// Copyright 2017 <NAME>. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package storage

import (
	"errors"
	"fmt"
	"time"

	"github.com/miniflux/miniflux/logger"
	"github.com/miniflux/miniflux/model"
	"github.com/miniflux/miniflux/timer"

	"github.com/lib/pq"
)

// NewEntryQueryBuilder returns a new EntryQueryBuilder
func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
	return NewEntryQueryBuilder(s, userID)
}

// createEntry add a new entry.
func (s *Storage) createEntry(entry *model.Entry) error {
	query := `
		INSERT INTO entries
		(title, hash, url, published_at, content, author, user_id, feed_id)
		VALUES
		($1, $2, $3, $4, $5, $6, $7, $8)
		RETURNING id
	`
	err := s.db.QueryRow(
		query,
		entry.Title,
		entry.Hash,
		entry.URL,
		entry.Date,
		entry.Content,
		entry.Author,
		entry.UserID,
		entry.FeedID,
	).Scan(&entry.ID)

	if err != nil {
		return fmt.Errorf("unable to create entry: %v", err)
	}

	entry.Status = "unread"
	for i := 0; i < len(entry.Enclosures); i++ {
		entry.Enclosures[i].EntryID = entry.ID
		entry.Enclosures[i].UserID = entry.UserID
		err := s.CreateEnclosure(entry.Enclosures[i])
		if err != nil {
			return err
		}
	}

	return nil
}

// UpdateEntryContent updates entry content.
func (s *Storage) UpdateEntryContent(entry *model.Entry) error {
	query := `
		UPDATE entries SET
		content=$1
		WHERE user_id=$2 AND id=$3
	`

	_, err := s.db.Exec(
		query,
		entry.Content,
		entry.UserID,
		entry.ID,
	)
	return err
}

// updateEntry update an entry when a feed is refreshed.
func (s *Storage) updateEntry(entry *model.Entry) error {
	query := `
		UPDATE entries SET
		title=$1, url=$2, published_at=$3, content=$4, author=$5
		WHERE user_id=$6 AND feed_id=$7 AND hash=$8
		RETURNING id
	`
	err := s.db.QueryRow(
		query,
		entry.Title,
		entry.URL,
		entry.Date,
		entry.Content,
		entry.Author,
		entry.UserID,
		entry.FeedID,<commit_msg>Do not update entry date while refreshing a feed<commit_after>// Copyright 2017 <NAME>. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package storage

import (
	"errors"
	"fmt"
	"time"

	"github.com/miniflux/miniflux/logger"
	"github.com/miniflux/miniflux/model"
	"github.com/miniflux/miniflux/timer"

	"github.com/lib/pq"
)

// NewEntryQueryBuilder returns a new EntryQueryBuilder
func (s *Storage) NewEntryQueryBuilder(userID int64) *EntryQueryBuilder {
	return NewEntryQueryBuilder(s, userID)
}

// createEntry add a new entry.
func (s *Storage) createEntry(entry *model.Entry) error {
	query := `
		INSERT INTO entries
		(title, hash, url, published_at, content, author, user_id, feed_id)
		VALUES
		($1, $2, $3, $4, $5, $6, $7, $8)
		RETURNING id
	`
	err := s.db.QueryRow(
		query,
		entry.Title,
		entry.Hash,
		entry.URL,
		entry.Date,
		entry.Content,
		entry.Author,
		entry.UserID,
		entry.FeedID,
	).Scan(&entry.ID)

	if err != nil {
		return fmt.Errorf("unable to create entry: %v", err)
	}

	entry.Status = "unread"
	for i := 0; i < len(entry.Enclosures); i++ {
		entry.Enclosures[i].EntryID = entry.ID
		entry.Enclosures[i].UserID = entry.UserID
		err := s.CreateEnclosure(entry.Enclosures[i])
		if err != nil {
			return err
		}
	}

	return nil
}

// UpdateEntryContent updates entry content.
func (s *Storage) UpdateEntryContent(entry *model.Entry) error {
	query := `
		UPDATE entries SET
		content=$1
		WHERE user_id=$2 AND id=$3
	`

	_, err := s.db.Exec(
		query,
		entry.Content,
		entry.UserID,
		entry.ID,
	)
	return err
}

// updateEntry updates an entry when a feed is refreshed.
// Note: we do not update the published date because some feeds do not contains any date,
// it default to time.Now() which could change the order of items on the history page.
func (s *Storage) updateEntry(entry *model.Entry) error {
	query := `
		UPDATE entries SET
		title=$1, url=$2, content=$3, author=$4
		WHERE user_id=$5 AND feed_id=$6 AND hash=$7
		RETURNING id
	`
	err := s.db.QueryRow(
		query,
		entry.Title,
		entry.URL,
		entry.Content,
		entry.Author,
		entry.UserID,
		entry.FeedID,
{'fraction_non_alphanumeric': 0.1310888252148997, 'fraction_numerical': 0.013132760267430755, 'mean_word_length': 3.5982436882546653, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799750', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1521, 'n_words': 430}