Skip to content
Snippets Groups Projects
models.go 5.69 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis Poyen's avatar
    Alexis Poyen committed
    package models
    
    import (
    	"net/http"
    	"strings"
    
    	"time"
    
    Alexis Poyen's avatar
    Alexis Poyen committed
    
    	"github.com/jinzhu/gorm"
    
    	// Needed for sqlite
    	_ "github.com/jinzhu/gorm/dialects/sqlite"
    )
    
    // DataHandler init a gorm DB an presents API handlers
    type DataHandler struct {
    	db *gorm.DB
    }
    
    // ErrorIDDoesNotExist = "id does not exist"
    const ErrorIDDoesNotExist = "id does not exist"
    
    // ErrorIDIsMissing = "id is missing"
    const ErrorIDIsMissing = "id is missing"
    
    // ErrorCannotAccessRessource = "You can not access this ressource"
    const ErrorCannotAccessRessource = "You can not access this ressource"
    
    // ErrorRoleOfLoggedUser = "Could not get role of logged user"
    const ErrorRoleOfLoggedUser = "Could not get role of logged user"
    
    // ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
    const ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
    
    
    type election struct {
    	ID         uint       `gorm:"primary_key"`
    	CreatedAt  time.Time  `json:"-"`
    	UpdatedAt  time.Time  `json:"-"`
    	DeletedAt  *time.Time `json:"-"`
    	name       string
    	ballotType string
    	areas      []area
    	Rounds     []Round
    }
    
    type area struct {
    	ID         uint       `gorm:"primary_key"`
    	CreatedAt  time.Time  `json:"-"`
    	UpdatedAt  time.Time  `json:"-"`
    	DeletedAt  *time.Time `json:"-"`
    	electionID uint
    	name       string
    	seatNumber uint
    	mapID      string
    	sections   []section
    }
    
    type section struct {
    	ID         uint       `gorm:"primary_key"`
    	CreatedAt  time.Time  `json:"-"`
    	UpdatedAt  time.Time  `json:"-"`
    	DeletedAt  *time.Time `json:"-"`
    	areaID     uint
    	name       string
    	seatNumber uint
    	mapID      string
    	desks      []desk
    }
    
    type desk struct {
    	ID          uint       `gorm:"primary_key"`
    	CreatedAt   time.Time  `json:"-"`
    	UpdatedAt   time.Time  `json:"-"`
    	DeletedAt   *time.Time `json:"-"`
    	sectionID   uint
    	name        string
    	subscribed  uint
    	witnessDesk bool
    }
    
    type party struct {
    	ID            uint       `gorm:"primary_key"`
    	CreatedAt     time.Time  `json:"-"`
    	UpdatedAt     time.Time  `json:"-"`
    	DeletedAt     *time.Time `json:"-"`
    	name          string
    	color         string
    	candidateList []candidateList
    }
    
    type capturer struct {
    	ID         uint       `gorm:"primary_key"`
    	CreatedAt  time.Time  `json:"-"`
    	UpdatedAt  time.Time  `json:"-"`
    	DeletedAt  *time.Time `json:"-"`
    	userID     int        `gorm:"not null;unique"`
    	name       string
    	deskRounds []deskRound
    }
    
    type parameter struct {
    	ID                uint       `gorm:"primary_key"`
    	CreatedAt         time.Time  `json:"-"`
    	UpdatedAt         time.Time  `json:"-"`
    	DeletedAt         *time.Time `json:"-"`
    	roundID           round
    	countBalnkAndNull bool
    	showOnlyCompleted bool
    	showMap           bool
    }
    
    type round struct {
    	ID         uint       `gorm:"primary_key"`
    	CreatedAt  time.Time  `json:"-"`
    	UpdatedAt  time.Time  `json:"-"`
    	DeletedAt  *time.Time `json:"-"`
    	ElectionID uint
    	parameter  parameter
    	name       string
    	date       time.Time
    	tour       uint
    	deskRounds []deskRound
    }
    
    type deskRound struct {
    	ID             uint       `gorm:"primary_key"`
    	CreatedAt      time.Time  `json:"-"`
    	UpdatedAt      time.Time  `json:"-"`
    	DeletedAt      *time.Time `json:"-"`
    	RoundID        uint
    	completed      bool
    	dateCompletion time.Time
    	validated      bool
    	Votes          []Vote
    }
    type candidateList struct {
    	ID         uint       `gorm:"primary_key"`
    	CreatedAt  time.Time  `json:"-"`
    	UpdatedAt  time.Time  `json:"-"`
    	DeletedAt  *time.Time `json:"-"`
    	PartyID    uint
    	name       string
    	candidates []candidate
    	Votes      []Vote
    }
    
    type candidate struct {
    	ID                       uint       `gorm:"primary_key"`
    	CreatedAt                time.Time  `json:"-"`
    	UpdatedAt                time.Time  `json:"-"`
    	DeletedAt                *time.Time `json:"-"`
    	candidateListID          uint
    	fullName                 string
    	rank                     uint
    	communityCounseller      bool
    	birthdate                time.Time
    	potentialIncompatibility bool
    	refused                  bool
    	removed                  bool
    }
    
    type vote struct {
    	deskRoundID     uint       `gorm:"primary_key"`
    	candidateListID uint       `gorm:"primary_key"`
    	CreatedAt       time.Time  `json:"-"`
    	UpdatedAt       time.Time  `json:"-"`
    	DeletedAt       *time.Time `json:"-"`
    	voiceNumber     uint
    	blank           bool
    	null            bool
    }
    
    
    Alexis Poyen's avatar
    Alexis Poyen committed
    // NewDataHandler init a DataHandler and returns a pointer to it
    func NewDataHandler() *DataHandler {
    	db, err := gorm.Open("sqlite3", "./data/test.db")
    	if err != nil {
    		panic("failed to connect database")
    	}
    	db.LogMode(true)
    
    	// Add FK with SQLite doesn't work
    	// db.Model(&Operation{}).AddForeignKey("creditor", "bank_account(id)", "Set Null", "Set Null")
    
    	// Migrate the schema
    	return &DataHandler{db: db}
    }
    
    // ProcessAPI redirect API call to DataHandler to each correct API func
    func (d *DataHandler) ProcessAPI(w http.ResponseWriter, r *http.Request) {
    	api := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/"), "/")[0]
    	switch api {
    	}
    
    }
    
    func (d *DataHandler) getLoggedUser(w http.ResponseWriter, r *http.Request) interface{} {
    	// user := auth.GetLoggedUserTechnical(w, r)
    	// if user.Role != "" && (user.Role == "BANKER") {
    	// 	var o UserBanker
    	// 	if err := d.db.Where(reqUserID, user.ID).First(&o).Error; err != nil {
    	// 		o := UserBanker{UserID: user.ID, Name: user.Login}
    	// 		d.db.Create(&o)
    	// 		d.db.Where(reqUserID, user.ID).First(&o)
    	// 		return o
    	// 	}
    	// 	return o
    	// } else if user.Role != "" && (user.Role == "CLIENT") {
    	// 	var o UserClient
    	// 	if err := d.db.Where(reqUserID, user.ID).First(&o).Error; err != nil {
    	// 		o := UserClient{UserID: user.ID, Name: user.Login}
    	// 		d.db.Create(&o)
    	// 		d.db.Where(reqUserID, user.ID).First(&o)
    	// 		return o
    	// 	}
    
    	// 	return o
    	// }
    
    	return nil
    }