Newer
Older
"forge.grandlyon.com/apoyen/elections/internal/auth"
"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" with 404 http.StatusNotFound
// ErrorIDIsMissing = "id is missing" with 404 http.StatusNotFound
// ErrorCannotAccessRessource = "You can not access this ressource" with 403 http.StatusForbidden
const ErrorCannotAccessRessource = "You can not access this ressource"
// ErrorRoleOfLoggedUser = "Could not get role of logged user" with 500 http.StatusInternalServerError
const ErrorRoleOfLoggedUser = "Could not get role of logged user"
// ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource." with 405 http.StatusMethodNotAllowed
const ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
// ErrorParentNotFound = "Could not get the parent associated to the object" with 500 http.StatusInternalServerError
const ErrorParentNotFound = "Could not get the parent associated to the object"
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
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
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
AreaID uint
Name string
MapID string
Desks []Desk
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
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
Name string
Color string
CandidateList []CandidateList
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 `gorm:"many2many:capturer_deskrounds;"`
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
CountBalnkAndNull bool
ShowOnlyCompleted bool
ShowMap bool
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
CandidateLists []CandidateList
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
RoundID uint
Capturers []Capturer `gorm:"many2many:capturer_deskrounds;"`
Completed bool
DateCompletion time.Time
Validated bool
Votes []Vote
// CandidateList
type CandidateList struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
PartyID uint
RoundID uint
Area Area `gorm:"foreignkey:AreaRefer"`
Name string
Candidates []Candidate
Votes []Vote
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
}
// Vote
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
// 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)
// Migrate the schema
db.AutoMigrate(&Capturer{})
db.AutoMigrate(&Election{})
db.AutoMigrate(&Area{})
db.AutoMigrate(&Section{})
db.AutoMigrate(&Desk{})
db.AutoMigrate(&Round{})
db.AutoMigrate(&DeskRound{})
db.AutoMigrate(&Party{})
db.AutoMigrate(&CandidateList{})
db.AutoMigrate(&Candidate{})
db.AutoMigrate(&Vote{})
db.AutoMigrate(&Parameter{})
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 == "CAPTURER") {
var o Capturer
if err := d.db.Where("user_id = ?", user.ID).First(&o).Error; err != nil {
o := Capturer{UserID: user.ID, Name: user.Login}
d.db.Create(&o)
d.db.First(&o, user.ID)
return o
}
return o
}