model

package
v0.0.0-...-0ad18f3 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2017 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package model implementiert die structs, die in die Datenbank abgebildet werden.

a couple of predefined data for development time

Index

Constants

View Source
const Base = "src/github.com/geobe/gostip"

the relative location of project files

View Source
const NQESTION = 10

of questions that could appear in one test

View Source
const U_ANY int = 1
View Source
const U_ENROL int = 2
View Source
const U_FULLADMIN int = 128
View Source
const U_POFF int = 4
View Source
const U_UADMIN int = 8

Variables

View Source
var InitialLanguages = map[Lang]string{
	NONE:  "_none",
	DE:    "_de",
	EN:    "_en",
	RU:    "_ru",
	KG:    "_kg",
	OTHER: "_other",
}
View Source
var InitialOblasts = []Oblast{
	{Name: "_bishkek"},
	{Name: "_osh_city"},
	{Name: "_batken"},
	{Name: "_chuy"},
	{Name: "_jalalabad"},
	{Name: "_naryn"},
	{Name: "_osh"},
	{Name: "_talas"},
	{Name: "_yssykköl"},
	{Name: "_foreign"},
}

Functions

func CanMail

func CanMail() bool

func ClearTestDb

func ClearTestDb(db *gorm.DB)

clear development db

func ConnectDb

func ConnectDb() *gorm.DB

connect to database using values from config file

func Db

func Db() *gorm.DB

make database connection available as a singleton

func Encrypt

func Encrypt(pw string) string

func GetMailer

func GetMailer() (string, string)

func InitProdDb

func InitProdDb(db *gorm.DB) *gorm.DB

create some test applicants in a developpment db

func InitTestDb

func InitTestDb(db *gorm.DB) *gorm.DB

create some test applicants in a developpment db

func SetMailer

func SetMailer(maccount, mpw string)

func Setup

func Setup(cfgfile string)

setting up viper configuration lib

Types

type Applicant

type Applicant struct {
	ID        int `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time    `sql:"index"`
	Data      ApplicantData // `gorm:"ForeignKey:DataID"`
}

an applicant for a place at university

func (*Applicant) BeforeUpdate

func (app *Applicant) BeforeUpdate(tx *gorm.DB) (err error)

to maintain a full history of changes of applicant data, old data are kept in the db using gorms 'DeletedAt' mechanism and a new record with updated data is saved to db. Tracebility is ensured by recording the identity of the user who initiated the update. concurrent conflicting updates are detected by comparing changed_at field.

type ApplicantData

type ApplicantData struct {
	Model
	ApplicantID    int
	Number         uint          `gorm:"AUTO_INCREMENT" form:"applid"`
	LastName       string        `form:"lastname"`
	FirstName      string        `form:"firstname"`
	FathersName    string        `form:"fathersname"`
	LastNameTx     string        `form:"lastnametx"`
	FirstNameTx    string        `form:"firstnametx"`
	FathersNameTx  string        `form:"fathersnametx"`
	Phone          string        `form:"phone"`
	Email          string        `form:"email"`
	Home           string        `form:"home"`
	School         string        `form:"school"`
	SchoolOk       bool          `form:"schoolok"`
	Oblast         Oblast        // Belongs To Association
	OblastID       uint          `form:"district"`
	OblastOk       bool          `form:"districtok"`
	OrtSum         int16         `form:"ort"`
	OrtMath        int16         `form:"ortmath"`
	OrtPhys        int16         `form:"ortphys"`
	OrtOk          bool          `form:"ortok"`
	Results        [NQESTION]int `gorm:"-" form:"result#"` // marks multiplied by 10
	Resultsave     string
	LanguageResult int       `form:"languageresult"`
	Language       Lang      `form:"language"`
	EnrolledAt     time.Time `form:"enrolledat"`
	CancelledAt    time.Time `form:"cancelledat"`
}

all data of an applicant are stored in a separate structure in order to maintain a full history of changes to these sensitive data form: tags are used to identify html form fields and request parameters

func (*ApplicantData) AfterFind

func (appdata *ApplicantData) AfterFind() (err error)

after loading struct fields from db, exam result values are converted back into an array of int

func (*ApplicantData) BeforeSave

func (appdata *ApplicantData) BeforeSave() (err error)

to easily store grant exam results in db with gorm, an array of (transient) integer result values is converted to a string before saving to db transcription fields are regenerated from original fields with every update

type ExamReference

type ExamReference struct {
	Model
	Year       int
	Results    [NQESTION]int `gorm:"-"`
	Resultsave string
}

the exam tasks and maximal results in a specific year's test. Results array records the maximal points achievable in a question. Zero values denote nonexisting questions

func (*ExamReference) AfterFind

func (exref *ExamReference) AfterFind() (err error)

after loading struct fields from db, exam reference values are converted back into an array of int

func (*ExamReference) BeforeSave

func (exref *ExamReference) BeforeSave() (err error)

to easily store grant exam reference values in db with gorm, an array of (transient) integer result values is converted to a string before saving to db. To maintain a short history of changes of exam reference values, tracebility is supported by recording timestamp and login of each user who initiated the update and last updaters id.

func (*ExamReference) QuestionsCount

func (exref *ExamReference) QuestionsCount() int

get the number of available results

type Lang

type Lang int

a type to classify language

const (
	NONE Lang = iota
	DE
	EN
	RU
	KG
	OTHER = 99
)

the languages relevant for the test

func (*Lang) Lang

func (l *Lang) Lang() string

type Model

type Model struct {
	// standard gorrm fields
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
	// additional fields to make all changes to the
	// database accountable
	UpdatedBy string
	Updater   uint
	DeletedBy string
	Deleter   uint
}

Model contains all fields for data management and accountability of changes for sensible data

type Oblast

type Oblast struct {
	ID   uint `gorm:"primary_key"`
	Name string
}

a district in Kyrgyzstan

func Oblasts

func Oblasts() []Oblast

cache oblasts in memory instead of reading every time from database

type User

type User struct {
	Model
	Login    string
	Fullname string
	Password string
	Role     int
}

func InitialValues

func InitialValues() (users []User, maxresults []int)

make some initial users available for testing and bootstrap

func NewUser

func NewUser(l, n, pw string, roles ...int) *User

func (*User) Authorize

func (u *User) Authorize(role int) bool

func (*User) ChangePw

func (u *User) ChangePw(oldpw, newpw string) bool

func (*User) String

func (u *User) String() string

func (*User) ValidatePw

func (u *User) ValidatePw(pw string) bool

Jump to

Keyboard shortcuts

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