models

package
v0.0.0-...-43927cf Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Db

func Db(r *http.Request) *gorm.DB

fetches the database connection from the request context

func GetByIdQuery

func GetByIdQuery(db *gorm.DB, r *http.Request) *gorm.DB

func Paginate

func Paginate[T any](query *gorm.DB, r *http.Request, paginationOptions *utils.PaginationParams, mapResponseFunc func(T) any, queryInstance any) (*utils.PaginatedResponse, error)

func PaginateQuery

func PaginateQuery(db *gorm.DB, paginationParams *utils.PaginationParams) *gorm.DB

Types

type Answer

type Answer struct {
	AppModel
	UserID          uint             `json:"user_id" gorm:"not null" validate:"required"`
	QuestionID      uint             `json:"question_id" gorm:"not null" validate:"required,id_of=question"`
	Content         AnswerOfQuestion `json:"content" gorm:"not null" validate:"required"`
	Points          uint             `json:"points" gorm:"not null;default:0" validate:"eq=0"` // validation is set so it is not possible to set points manually
	PointsGrantedAt *time.Time       `json:"points_granted_at"`
}

func (Answer) BuildResponse

func (m Answer) BuildResponse() any

func (Answer) GetAllQuery

func (m Answer) GetAllQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (Answer) GetQuery

func (m Answer) GetQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (Answer) Validate

func (m Answer) Validate(r *http.Request) error

type AnswerOfQuestion

type AnswerOfQuestion struct {
	datatypes.JSON
}

func (*AnswerOfQuestion) Validate

func (a *AnswerOfQuestion) Validate(questionType string) error

type AppModel

type AppModel struct {
	ID        uint      `gorm:"primarykey" json:"id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

defines the base model for all models in the application

func (AppModel) AfterCreateCtx

func (m AppModel) AfterCreateCtx(ctx context.Context, tx *gorm.DB) error

func (AppModel) AfterDeleteCtx

func (m AppModel) AfterDeleteCtx(ctx context.Context, tx *gorm.DB) error

func (AppModel) AfterUpdateCtx

func (m AppModel) AfterUpdateCtx(ctx context.Context, tx *gorm.DB) error

func (AppModel) BeforeCreateCtx

func (m AppModel) BeforeCreateCtx(ctx context.Context, tx *gorm.DB) error

func (AppModel) BeforeDeleteCtx

func (m AppModel) BeforeDeleteCtx(ctx context.Context, tx *gorm.DB) error

func (AppModel) BeforeUpdateCtx

func (m AppModel) BeforeUpdateCtx(ctx context.Context, tx *gorm.DB) error

func (AppModel) DeleteQuery

func (m AppModel) DeleteQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (AppModel) GetAllQuery

func (m AppModel) GetAllQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (AppModel) GetID

func (m AppModel) GetID() uint

func (AppModel) GetQuery

func (m AppModel) GetQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (AppModel) UpdateQuery

func (m AppModel) UpdateQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (AppModel) Validate

func (m AppModel) Validate(r *http.Request) error

used for custom validation logic, which can't be defined in the struct tags

type Athlete

type Athlete struct {
	AppModel
	FirstName   *string         `json:"first_name"`
	LastName    *string         `json:"last_name"`
	Birthday    *datatypes.Date `json:"birthday"`
	Country     *string         `json:"country"`
	Gender      string          `json:"gender" gorm:"not null"`
	Disciplines []Discipline    `json:"disciplines" gorm:"many2many:athletes_disciplines;constraint:OnDelete:CASCADE"`
}

func (Athlete) BuildResponse

func (m Athlete) BuildResponse() any

func (Athlete) GetAllQuery

func (m Athlete) GetAllQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (Athlete) GetQuery

func (m Athlete) GetQuery(db *gorm.DB, r *http.Request) *gorm.DB

type AthleteAnswer

type AthleteAnswer struct {
	Value string `json:"value" validate:"required"`
}

type AthleteResponse

type AthleteResponse struct {
	ID          uint     `json:"id"`
	FirstName   *string  `json:"first_name"`
	LastName    *string  `json:"last_name"`
	Gender      string   `json:"gender"`
	Country     *string  `json:"country"`
	Disciplines []string `json:"disciplines"`
}

type AthletesIdsAnswer

type AthletesIdsAnswer struct {
	AthleteIdOne   uint `json:"athlete_id_one" validate:"required,id_of=athlete"`
	AthleteIdTwo   uint `json:"athlete_id_two" validate:"required,id_of=athlete"`
	AthleteIdThree uint `json:"athlete_id_three" validate:"required,id_of=athlete"`
}

type Discipline

type Discipline struct {
	AppModel
	Name     string    `json:"name" gorm:"not null"`
	Type     string    `json:"type" gorm:"not null"`
	Athletes []Athlete `json:"athletes" gorm:"many2many:athletes_disciplines;constraint:OnDelete:CASCADE"`
}

func (Discipline) BuildResponse

func (m Discipline) BuildResponse() any

type DisciplineResponse

type DisciplineResponse struct {
	ID   uint   `json:"id"`
	Name string `json:"name"`
	Type string `json:"type"`
}

type Event

type Event struct {
	AppModel
	Name        string     `json:"name" gorm:"not null" validate:"required"`
	Description *string    `json:"description"`
	Deadline    time.Time  `json:"deadline" gorm:"not null" validate:"required"`
	Questions   []Question `json:"questions,omitempty" gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE"`
}

func (Event) BuildResponse

func (m Event) BuildResponse() any

func (Event) GetAllQuery

func (m Event) GetAllQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (*Event) IsPresent

func (m *Event) IsPresent() error

type Pokemon

type Pokemon struct {
	AppModel
	PokemonName string `json:"pokemon_name" validate:"required,oneof=D Pikachu Bulbasaur Charmander Squirtle,min=2,max=100"`
	Age         int    `json:"age" validate:"required,gte=0,lte=130" gorm:"type:int"`
	Email       string `json:"email" validate:"required,email" gorm:"not null;type:varchar(100)"`
	Attack      string `json:"attack" validate:"required,oneof=Thunderbolt Ember Vine_Whip Water_Gun,min=2,max=100" gorm:"not null"`
}

func (Pokemon) BuildResponse

func (m Pokemon) BuildResponse() any

func (Pokemon) GetAllQuery

func (m Pokemon) GetAllQuery(db *gorm.DB, r *http.Request) *gorm.DB

type Question

type Question struct {
	AppModel
	EventID       uint              `json:"event_id" gorm:"not null" validate:"required,id_of=event"`
	Content       string            `json:"content" gorm:"not null" validate:"required"`
	CorrectAnswer *AnswerOfQuestion `json:"correct_answer"`
	Type          string            `json:"type" gorm:"not null" validate:"oneof=athlete country"`
	Answers       []Answer          `json:"answers,omitempty" gorm:"foreignKey:QuestionID;constraint:OnDelete:CASCADE"`
	Points        uint              `json:"points" gorm:"not null;default:1" validate:"required,gte=1"`
}

func (Question) BeforeUpdateCtx

func (m Question) BeforeUpdateCtx(ctx context.Context, tx *gorm.DB) error

func (Question) BuildResponse

func (m Question) BuildResponse() any

func (Question) GetAllQuery

func (m Question) GetAllQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (Question) Validate

func (m Question) Validate(r *http.Request) error

type Role

type Role struct {
	AppModel
	Name  string `json:"name" validate:"required,oneof=admin user organizer" gorm:"not null;unique"`
	Users []User `json:"users" gorm:"many2many:user_roles;constraint:OnDelete:CASCADE"`
}

type User

type User struct {
	AppModel
	Email    string   `json:"email" validate:"required,email" gorm:"not null;unique"`
	Password string   `json:"password" validate:"required,min=6" gorm:"not null"`
	Roles    []Role   `json:"roles" gorm:"many2many:user_roles;constraint:OnDelete:CASCADE"`
	Answers  []Answer `json:"answers,omitempty" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
}

func (*User) BeforeCreate

func (u *User) BeforeCreate(tx *gorm.DB) error

func (User) BuildResponse

func (m User) BuildResponse() any

func (User) GetAllQuery

func (m User) GetAllQuery(db *gorm.DB, r *http.Request) *gorm.DB

func (User) GetQuery

func (m User) GetQuery(db *gorm.DB, r *http.Request) *gorm.DB

type UserResponse

type UserResponse struct {
	ID        uint      `json:"id"`
	Email     string    `json:"email"`
	Roles     []string  `json:"roles"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

Jump to

Keyboard shortcuts

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