db

package module
v0.0.0-...-f79870c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package db provides functions and types relevant to the backend database for the article feed.

Package db provides functions and types relevant to the backend database for the article feed.

Package db provides functions and types relevant to the backend database for the article feed.

Package db provides functions and types relevant to the backend database for the article feed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendArticle

func AppendArticle(ctx context.Context, article *Article) error

AppendArticle appends a new article added to the web app database to a predefined, hard-coded Google Sheet.

func AppendArticleToOld

func AppendArticleToOld(ctx context.Context, article *Article) error

AppendArticleToOld appends a new article added to the web app database to a predefined, hard-coded Google Sheet.

func AppendQuestion

func AppendQuestion(ctx context.Context, qn Question) error

AppendQuestion appends a question to the initialised QuestionsDB.

func BackupArticles

func BackupArticles(ctx context.Context, database *ArticlesDBByDate) error

BackupArticles backs up the articles database to a predefined, hard-coded Google Sheet.

func BackupQuestions

func BackupQuestions(ctx context.Context, qnDB QuestionsDB) error

BackupQuestions backs up the questions database to a predefined, hard-coded Google Sheet.

func SendMail

func SendMail(from, to, replyTo, subject, msg string) error

Types

type Article

type Article struct {
	Title       string
	URL         string
	Topics      []Topic
	Questions   []Question
	DisplayDate string
	Date        int64
}

Article is a struct representing a single entry in the articlesdb.

func NewArticle

func NewArticle() (*Article, error)

NewArticle returns an Article in order to populate fields for adding to the articles database.

func (*Article) AddArticleToDB

func (a *Article) AddArticleToDB(db *ArticlesDBByDate, tm TopicsMap, qc QuestionCounter) error

AddArticleToDB takes a pointer to an article that has already had all its fields populated and adds it to the articles database, sorting by most recent published date.

func (*Article) SetDate

func (a *Article) SetDate(date string) error

SetDate is a wrapper around time.Parse to parse a date string to time.Time type, in order to call time.Unix() to return an int64 that makes the article sortable by date.

func (*Article) SetQuestions

func (a *Article) SetQuestions(year, number string, qnDB QuestionsDB) error

SetQuestions sets the []Question for the given Article item.

func (*Article) SetQuestionsNewArticle

func (a *Article) SetQuestionsNewArticle(year, number string, qnDB QuestionsDB) (QuestionsDB, error)

SetQuestionsNewArticle sets the []Question and increases the count for the question listing in the qnDB for the given Article item.

func (*Article) SetTopics

func (a *Article) SetTopics(topics ...string)

SetTopics is a wrapper around an append function to append multiple topics to the Article struct a.

type ArticlesDBByDate

type ArticlesDBByDate []Article

ArticlesDBByDate is the database of all entries in the articlesdb. Entries are sorted in reverse order of date, with the most recent at index 0.

func NewArticlesDBByDate

func NewArticlesDBByDate() *ArticlesDBByDate

NewArticlesDBByDate makes a slice of Articles to initialise the articles database.

func Search(term string, database *ArticlesDBByDate) *ArticlesDBByDate

Search implements a switch statement to check which kind of search to run.

func SearchAND

func SearchAND(term string, database *ArticlesDBByDate) *ArticlesDBByDate

SearchAND runs an AND boolean search.

func SearchAll

func SearchAll(term string, database *ArticlesDBByDate) *ArticlesDBByDate

SearchAll runs a search of the given term through all the items stored in the database.

func SearchNOT

func SearchNOT(term string, database *ArticlesDBByDate) *ArticlesDBByDate

SearchNOT runs a NOT boolean search.

func SearchOR

func SearchOR(term string, database *ArticlesDBByDate) *ArticlesDBByDate

SearchOR runs an OR boolean search.

func (ArticlesDBByDate) EditArticle

func (db ArticlesDBByDate) EditArticle(index string, article Article, tm TopicsMap, qc QuestionCounter) error

EditArticle is a function that the admin can invoke from the live app to edit a specific article.

func (*ArticlesDBByDate) InitArticlesDB

func (db *ArticlesDBByDate) InitArticlesDB(ctx context.Context, qnDB QuestionsDB, tm TopicsMap, qc QuestionCounter) error

InitArticlesDB initialises the articles database at first run. Data is downloaded from the incumbent Google Sheets and parsed into the app's data structure. This is meant to be executed only once.

func (ArticlesDBByDate) Len

func (db ArticlesDBByDate) Len() int

Implement sort.Sort interface

func (ArticlesDBByDate) Less

func (db ArticlesDBByDate) Less(i, j int) bool

func (*ArticlesDBByDate) RemoveArticle

func (db *ArticlesDBByDate) RemoveArticle(index int, tm TopicsMap, qc QuestionCounter)

RemoveArticle is a function that the admin can invoke from the live app to remove any offending article.

func (ArticlesDBByDate) Swap

func (db ArticlesDBByDate) Swap(i, j int)

type Question

type Question struct {
	Year    string
	Number  string
	Wording string
}

Question is a struct that represents the question object in each article entry in the ArticlesDBByDate.

type QuestionCounter

type QuestionCounter map[string]int

QuestionCounter is an object to count number of articles per question.

func InitQuestionCounter

func InitQuestionCounter() QuestionCounter

InitQuestionCounter returns an initialised QuestionCounter map.

func (QuestionCounter) Decrement

func (qc QuestionCounter) Decrement(key string)

Decrement decreases the count for the question in the QuestionCounter map. Argument must be provided in the format qn.Year + " - Q" + qn.Number.

func (QuestionCounter) GetZeroArticleQns

func (qc QuestionCounter) GetZeroArticleQns(qnDB QuestionsDB)

GetZeroArticleQns gets the questions that have zero articles tagged.

func (QuestionCounter) Increment

func (qc QuestionCounter) Increment(key string)

Increment increases the count for the question in the QuestionCounter map. Argument must be provided in the format qn.Year + " - Q" + qn.Number.

type QuestionObject

type QuestionObject struct {
	Key   string
	Value int
}

QuestionObject is an object to enable ranking of questions by number of articles tagged.

type QuestionsByArticleCount

type QuestionsByArticleCount []QuestionObject

QuestionsByArticleCount is an object to rank questions by the number of articles tagged to each question.

func RankQuestionsByArticleCount

func RankQuestionsByArticleCount(counter QuestionCounter) QuestionsByArticleCount

RankQuestionsByArticleCount converts a QuestionsDB into a slice of Article in order to count and rank questions by the numebr of articles tagged to each question.

func (QuestionsByArticleCount) Len

func (q QuestionsByArticleCount) Len() int

Implement sort.Sort interface

func (QuestionsByArticleCount) Less

func (q QuestionsByArticleCount) Less(i, j int) bool

func (QuestionsByArticleCount) Swap

func (q QuestionsByArticleCount) Swap(i, j int)

type QuestionsDB

type QuestionsDB map[string]Question

QuestionsDB is a map of questions for quick searching.

func InitQuestionsDB

func InitQuestionsDB(ctx context.Context) (QuestionsDB, error)

InitQuestionsDB maps a list of questions in a file named by filename and maps them to a questions database.

type Topic

type Topic string

Topic represents a searchable tag for each article.

type TopicObject

type TopicObject struct {
	Key   Topic
	Value int
}

TopicObject provides an object to implement sorting by value in order to rank most and least tagged topics.

type TopicsCount

type TopicsCount []TopicObject

TopicsCount provides a type to sort the TopicsMap.

func GetTopicsCount

func GetTopicsCount(tm TopicsMap) TopicsCount

GetTopicsCount is a function to count topics on first initialisation of the Articles DB.

func (TopicsCount) Len

func (tc TopicsCount) Len() int

Implement sort.Sort interface.

func (TopicsCount) Less

func (tc TopicsCount) Less(i, j int) bool

func (TopicsCount) Swap

func (tc TopicsCount) Swap(i, j int)

type TopicsMap

type TopicsMap map[Topic]int

TopicsMap is a map to track the number of articles tagged with each topic in the database.

func InitTopicsMap

func InitTopicsMap() TopicsMap

InitTopicsMap returns an initialised TopicsMap.

func (TopicsMap) Decrement

func (tm TopicsMap) Decrement(key Topic)

Decrement decreases the count for the question in the TopicsMap map.

func (TopicsMap) Increment

func (tm TopicsMap) Increment(key Topic)

Increment increases the count for the question in the TopicsMap map.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL