func findEntryByID(db *sql.DB, entryID int) (dash.Entry, error) {
	var entry = dash.Entry{
		ID: entryID,
	}
	var err = db.QueryRow(`SELECT
				e.title,
				e.type,
				e.anchor,
				e.body,
				e.body_rendered,
				e.score,
				e.user_id,
				e.removed_from_public,
				e.public,
				u.username
			FROM entries AS e
			INNER JOIN users AS u ON u.id = e.user_id
			WHERE e.id = ?`, entryID,
	).Scan(
		&entry.Title,
		&entry.Type,
		&entry.Anchor,
		&entry.Body,
		&entry.BodyRendered,
		&entry.Score,
		&entry.UserID,
		&entry.RemovedFromPublic,
		&entry.Public,
		&entry.AuthorUsername)
	if err != nil {
		return entry, err
	}

	var rows *sql.Rows
	rows, err = db.Query(`select t.name FROM teams AS t inner join entry_team ON t.id = entry_team.team_id where entry_team.entry_id = ?`, entryID)
	if err != nil {
		return entry, err
	}
	defer rows.Close()
	entry.Teams = make([]string, 0)

	for rows.Next() {
		var teamName string
		if err := rows.Scan(&teamName); err != nil {
			return entry, err
		}
		entry.Teams = append(entry.Teams, teamName)
	}

	return entry, err
}
func updateEntryVoteScore(db *sql.DB, entry *dash.Entry) error {
	var score = 0
	var err = db.QueryRow(`SELECT SUM(type) FROM votes WHERE entry_id = ?`, entry.ID).Scan(&score)
	if err != nil {
		return err
	}

	_, err = db.Exec(`UPDATE entries SET score = ? WHERE id = ?`, score, entry.ID)
	entry.Score = score
	return err
}
// EntryCreate creates a new entry with the current user as author
func EntryCreate(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
	var db = ctx.Value(DBKey).(*sql.DB)
	var user = ctx.Value(UserKey).(*dash.User)

	var payload entrySaveRequest
	json.NewDecoder(req.Body).Decode(&payload)

	var entry = dash.Entry{
		ID:         payload.EntryID,
		Title:      payload.Title,
		Type:       payload.Type,
		Body:       payload.Body,
		Public:     payload.Public,
		Identifier: payload.Identifier,
		Anchor:     payload.Anchor,
		Teams:      payload.Teams,
	}

	if entry.Title == "" {
		return ErrMissingTitle
	}
	if entry.Body == "" {
		return ErrMissingBody
	}
	if entry.Anchor == "" {
		return ErrMissingAnchor
	}
	if err := upsertIdentifier(db, &entry.Identifier); err != nil {
		return err
	}
	if entry.Public && entry.Identifier.BannedFromPublic {
		return ErrPublicAnnotationForbidden
	}
	entry.IdentifierID = entry.Identifier.ID
	entry.BodyRendered = string(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.MarkdownCommon([]byte(entry.Body))))

	var res, err = db.Exec(`INSERT INTO entries (title, body, body_rendered, type, identifier_id, anchor, public, removed_from_public, score, user_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
		entry.Title, entry.Body, entry.BodyRendered, entry.Type, entry.IdentifierID, entry.Anchor, entry.Public, entry.RemovedFromPublic, entry.Score, user.ID, time.Now(), time.Now())
	if err != nil {
		return err
	}
	var insertID int64
	insertID, err = res.LastInsertId()
	if err != nil {
		return err
	}
	entry.ID = int(insertID)

	var vote = dash.Vote{
		EntryID: entry.ID,
		UserID:  user.ID,
		Type:    dash.VoteUp,
	}
	if _, err := db.Exec(`INSERT INTO votes (type, entry_id, user_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)`, vote.Type, vote.EntryID, vote.UserID, time.Now(), time.Now()); err != nil {
		return err
	}

	for _, t := range entry.Teams {
		var teamID int64
		db.QueryRow(`SELECT id FROM teams WHERE name = ? LIMIT 1`, t).Scan(&teamID)
		db.Exec(`INSERT INTO entry_team (entry_id, team_id) VALUES (?, ?)`, entry.ID, teamID)
	}

	updateEntryVoteScore(db, &entry)

	json.NewEncoder(w).Encode(entrySaveResponse{
		Entry:  entry,
		Status: "success",
	})
	return nil
}