db

package
v1.0.1-0...-6dc1f34 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2018 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Male is the constant that defines that a bot is male (value = 0)
	Male = Gender(0)
	// Female is the constant that defines that a bot is female (value = 1)
	Female = Gender(1)
)
View Source
const (
	// BotIsSender says that the message was sent by the bot
	BotIsSender = 0

	// UserIsSender says that the message was sent by the user
	UserIsSender = 1
)
View Source
const MessageMaxLength = 200

MessageMaxLength defines the maximum message length that a user can send to a bot

View Source
const Year time.Duration = time.Hour * 24 * 365

Year is the duraction of one year in nano seconds

Variables

View Source
var ErrBotDoesNotBelongToUser = UserError("the bot does not belong to you")

ErrBotDoesNotBelongToUser is thrown when the sent message is too long

View Source
var ErrConnectionClosed = errors.New("connection is closed or not established jet")

ErrConnectionClosed is thrown if a database request is made, but the connection to the database is closed

View Source
var ErrInternalServerError = UserError("internal server error")

ErrInternalServerError is thrown if something went wrong which is not ment to be shown to the user

View Source
var ErrMessageToLong = UserError("message is too long")

ErrMessageToLong is thrown when the sent message is too long

View Source
var ErrNoUserCredentials = UserError("missing user credentials")

ErrNoUserCredentials is shown if no user credentials are provided

View Source
var ErrUserNameTaken = UserError("username allready taken")

ErrUserNameTaken is thrown when user can not be created because the name is allready taken

View Source
var ErrUserNotExists = UserError("username not found")

ErrUserNotExists is thrown if the user does not exist

View Source
var ErrWrongPassword = UserError("wrong password")

ErrWrongPassword is thrown if the user password combination is invalid

Functions

func Close

func Close() error

Close closes the connection to the database

func Connect

func Connect(path string) error

Connect creates a connection to a sqlite3 database The given path is the location of the datbase file If the function runs without errors, the database is ready for requests

func CreateUser

func CreateUser(userName, password string) error

CreateUser adds a new user to the database It creates a new entry in the table "User" The password is hashed with bcrypt

func GetImages

func GetImages(gender int) (*[]Image, error)

GetImages returns image object with given id

func GetNames

func GetNames(gender int) (*[]Name, error)

GetNames returns all bots which belong to the given user

Types

type Bot

type Bot struct {
	// The bots unique id
	ID int

	// The bots name, which the user can see
	Name string

	// The path to the bots profile picture
	Image string

	Gender Gender

	// The user the bot belongs to. Only this user can communicate with the bot
	User int

	// The bots current affection to the user
	Affection float64

	// The bots current mood
	Mood float64

	// Pattern is the recognized pattern id in the last message
	Pattern *int

	Birthdate time.Time

	// FavoriteColor is the primary key of the bots favorite color
	FavoriteColor int

	// FatherName is the primary key of the bots fathers name
	FatherName int

	// FatherAge is the bots fathers age in years
	FatherAge int

	// MotherName is the primary key of the bots mothers name
	MotherName int

	// MotherAge is the bots mothers age in years
	MotherAge int

	// CreationDate is the point in time when the bot was created by a user
	CreationDate time.Time
}

Bot represents a bots entry in the database A bot is only accessible for one user The entry holds personal information about the bot but also information about the current mood and affection to the user.

func (*Bot) Create

func (b *Bot) Create() error

Create creates a bot entry in the database The following fields in the bot struct need to be filled: Name, Image, Gender, User, Affection and Mood If the insertion was successful the generated bot id is saved in the given bot struct.

func (*Bot) Delete

func (b *Bot) Delete() error

Delete removes bot from database

func (*Bot) GetFatherName

func (b *Bot) GetFatherName() string

GetFatherName returns the name of the father as string

func (*Bot) GetFavoriteColor

func (b *Bot) GetFavoriteColor() string

GetFavoriteColor returns the favorite color as string

func (*Bot) GetMessages

func (b *Bot) GetMessages() (*[]Message, error)

GetMessages returns a list of all messages, that the user and bot sent each other

func (*Bot) GetMotherName

func (b *Bot) GetMotherName() string

GetMotherName returns the name of the mother as string

func (*Bot) StoreMessages

func (b *Bot) StoreMessages(user *User, msgs []Message) error

StoreMessages stores messages in database The messages need to be sent between the given bot and user

func (*Bot) UpdateContext

func (b *Bot) UpdateContext(affection, mood float64, pattern *int) error

UpdateContext updates the bots fields affection and mood in the database and saves the value in the struct

type Gender

type Gender int

Gender defines the gender of a bot to be male or female

type Image

type Image struct {
	ImageID int
	Gender  Gender
	Path    string
}

Image represents database entry

func GetImage

func GetImage(id int) (*Image, error)

GetImage returns image object with given id

type Message

type Message struct {
	// ID is a unique id
	ID int

	// The bot who sent the message or received it
	Bot int

	// The sender of the message
	Sender MessageSender

	// The point in time that the message was sent
	Timestamp time.Time

	// The text that was sent
	Content string
}

Message represents database entry of a message A messsage is a text that was sent between the user and a bot Since a bot can only communicate with one user a message is always associated with the bot

type MessageSender

type MessageSender int

MessageSender defines who send a message

type Name

type Name struct {
	ID     int
	Text   string
	Gender int
}

Name represents database entry

func GetName

func GetName(id int) (*Name, error)

GetName returns all bots which belong to the given user

type User

type User struct {

	// UserID in database
	ID int

	// Username
	Name string
}

User represents a user entry in the database Every person that uses eve needs to have a user entry in the database, because it is used for authentication.

func CheckCredentials

func CheckCredentials(userName, password string) (*User, error)

CheckCredentials verifies if the combination of userName and password is valid. The function checks if a user with the given name exists and compares the password with the one in the database. The complete user data is returned if the credentials are valid.

func GetUserForSession

func GetUserForSession(sessionKey string) *User

GetUserForSession checks if the given sesssion key is associated with any user in the database. If the key exists in the database the User, which the session belongs to, is returned. An invalid key resolves in the return of a nil pointer

func (*User) GetBot

func (u *User) GetBot(botID int) (*Bot, error)

GetBot returns bot entry from database if bot belongs to user This funtion can be used to check if a bot belongs to the given user.

func (*User) GetBots

func (u *User) GetBots() (*[]Bot, error)

GetBots returns all bots which belong to the user

func (*User) StoreSessionKey

func (u *User) StoreSessionKey(key string) bool

StoreSessionKey saves the session key in the database in the "User" table This session key authenticates the user in further requests The function returns true if the storing was successfull

type UserError

type UserError string

UserError are errors that are shown to the error

func (UserError) Error

func (err UserError) Error() string

Jump to

Keyboard shortcuts

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