From b48fe599369e33e0cff0aa779f0b2a703701bd31 Mon Sep 17 00:00:00 2001 From: Daniel Heidemann Date: Wed, 31 Jul 2024 20:07:22 +0200 Subject: [PATCH] cleanup models (#44) --- server/db/init.go | 1 - server/models/answer.go | 25 ------------------------- server/models/building.go | 14 +++++++------- server/models/event.go | 8 ++++---- server/models/label.go | 6 +++--- server/models/room.go | 8 ++++---- server/models/setting.go | 2 +- server/models/student.go | 7 +++---- server/models/tutor.go | 2 +- server/models/user.go | 16 ++++++++-------- 10 files changed, 31 insertions(+), 58 deletions(-) delete mode 100644 server/models/answer.go diff --git a/server/db/init.go b/server/db/init.go index 088be6f..af7d46c 100644 --- a/server/db/init.go +++ b/server/db/init.go @@ -42,7 +42,6 @@ func Init(ctx context.Context, tracer *trace.TracerProvider) (*bun.DB, *sql.DB, (*models.User)(nil), (*models.Building)(nil), (*models.Room)(nil), - (*models.Answer)(nil), (*models.Setting)(nil)} for _, relation := range relations { diff --git a/server/models/answer.go b/server/models/answer.go deleted file mode 100644 index b6484a4..0000000 --- a/server/models/answer.go +++ /dev/null @@ -1,25 +0,0 @@ -package models - -import ( - "context" - - "github.com/uptrace/bun" -) - -type Answer struct { - bun.BaseModel `bun:"table:answers,alias:a"` - - Number int8 `bun:",pk"` - StudentMail string `bun:",pk"` - Text string - Score int8 `bun:",notnull"` - - Student *Student `bun:"rel:belongs-to,join:student_mail=mail"` -} - -var _ bun.BeforeCreateTableHook = (*Answer)(nil) - -func (*Answer) BeforeCreateTable(ctx context.Context, query *bun.CreateTableQuery) error { - query.ForeignKey(`("student_mail") REFERENCES "users" ("mail") ON DELETE CASCADE`) - return nil -} diff --git a/server/models/building.go b/server/models/building.go index c1a5c1a..3a7c7fd 100644 --- a/server/models/building.go +++ b/server/models/building.go @@ -7,13 +7,13 @@ import ( type Building struct { bun.BaseModel `bun:"table:buildings,alias:b"` - ID int32 `bun:"id,pk,autoincrement"` - Name string `bun:"name,notnull"` - Street string `bun:"street,notnull"` - Number string `bun:"number,notnull"` - City string `bun:"city,notnull"` - Zip int32 `bun:"zip,notnull"` - Osm string `bun:"osm,notnull"` + ID int32 `bun:",pk,autoincrement"` + Name string `bun:",notnull,type:varchar(255)"` + Street string `bun:",notnull,type:varchar(255)"` + Number string `bun:",notnull,type:varchar(255)"` + City string `bun:",notnull,type:varchar(255)"` + Zip int32 `bun:",notnull"` + Osm string `bun:",notnull,type:varchar(255)"` Rooms []*Room `bun:"rel:has-many,join:id=building_id"` } diff --git a/server/models/event.go b/server/models/event.go index 5f7a11a..db8a552 100644 --- a/server/models/event.go +++ b/server/models/event.go @@ -11,10 +11,10 @@ type Event struct { bun.BaseModel `bun:"table:events,alias:e"` ID int32 `bun:",pk,autoincrement"` - Title string `bun:",notnull"` + Title string `bun:",notnull,type:varchar(255)"` Description string - TopicName string `bun:",notnull"` - TypeName string `bun:",notnull"` + TopicName string `bun:",notnull,type:varchar(50)"` + TypeName string `bun:",notnull,type:varchar(50)"` From time.Time `bun:",notnull"` To time.Time `bun:",notnull"` NeedsTutors bool `bun:",notnull"` @@ -39,7 +39,7 @@ type EventToTutor struct { Event *Event `bun:"rel:belongs-to,join:event_id=id"` TutorMail string `bun:",pk"` Tutor *Tutor `bun:"rel:belongs-to,join:tutor_mail=mail"` - RoomNumber string `bun:",pk"` + RoomNumber string `bun:",pk,type:varchar(50)"` BuildingID int32 `bun:",pk"` Room *Room `bun:"rel:belongs-to,join:room_number=number,join:building_id=building_id"` } diff --git a/server/models/label.go b/server/models/label.go index 0ff3328..0daddc1 100644 --- a/server/models/label.go +++ b/server/models/label.go @@ -7,7 +7,7 @@ import ( type Label struct { bun.BaseModel `bun:"table:labels,alias:l"` - Name string `bun:",pk"` - Color string - Kind string `bun:",notnull"` + Name string `bun:",pk,type:varchar(50)"` + Color string `bun:",type:varchar(7)"` + Kind string `bun:",notnull,type:varchar(20)"` } diff --git a/server/models/room.go b/server/models/room.go index 4983211..545af1c 100644 --- a/server/models/room.go +++ b/server/models/room.go @@ -9,8 +9,8 @@ import ( type Room struct { bun.BaseModel `bun:"table:rooms,alias:r"` - Number string `bun:",pk,notnull"` - Name string + Number string `bun:",pk,notnull,type:varchar(50)"` + Name string `bun:",type:varchar(255)"` Capacity int16 Floor int8 BuildingID int32 `bun:",pk"` @@ -26,10 +26,10 @@ func (*Room) BeforeCreateTable(ctx context.Context, query *bun.CreateTableQuery) } type RoomToEvent struct { - RoomNumber string `bun:",pk"` + RoomNumber string `bun:",pk,type:varchar(50)"` BuildingID int32 `bun:",pk"` Room *Room `bun:"rel:belongs-to,join:room_number=number,join:building_id=building_id"` - EventID int32 `bun:"event_id,pk"` + EventID int32 `bun:",pk"` Event *Event `bun:"rel:belongs-to,join:event_id=id"` } diff --git a/server/models/setting.go b/server/models/setting.go index 6155722..b0fdafc 100644 --- a/server/models/setting.go +++ b/server/models/setting.go @@ -7,5 +7,5 @@ type Setting struct { Key string `bun:",pk,type:varchar(255)"` Value string `bun:",notnull"` - Type string `bun:"notnull,type:varchar(50)"` + Type string `bun:",notnull,type:varchar(50)"` } diff --git a/server/models/student.go b/server/models/student.go index 8c801a5..ef033f5 100644 --- a/server/models/student.go +++ b/server/models/student.go @@ -12,18 +12,17 @@ type Student struct { Score int8 Accepted bool - EventsRegistered []Event `bun:"m2m:student_to_events,join:Student=Event"` - Answers []Answer `bun:"rel:has-many,join:mail=student_mail"` + EventsRegistered []Event `bun:"m2m:student_to_events,join:Student=Event"` } func (Student) IsUser() {} type StudentToEvent struct { - StudentMail string `bun:",pk"` + StudentMail string `bun:",pk,type:varchar(255)"` Student *Student `bun:"rel:belongs-to,join:student_mail=mail"` EventID int32 `bun:",pk"` Event *Event `bun:"rel:belongs-to,join:event_id=id"` - RoomNumber string `bun:",pk"` + RoomNumber string `bun:",pk,type:varchar(50)"` BuildingID int32 `bun:",pk"` Room *Room `bun:"rel:belongs-to,join:room_number=number,join:building_id=building_id"` } diff --git a/server/models/tutor.go b/server/models/tutor.go index c4538fd..ca5253f 100644 --- a/server/models/tutor.go +++ b/server/models/tutor.go @@ -16,7 +16,7 @@ type Tutor struct { func (Tutor) IsUser() {} type TutorToEvent struct { - TutorMail string `bun:",pk"` + TutorMail string `bun:",pk,type:varchar(255)"` Tutor *Tutor `bun:"rel:belongs-to,join:tutor_mail=mail"` EventID int32 `bun:",pk"` Event *Event `bun:"rel:belongs-to,join:event_id=id"` diff --git a/server/models/user.go b/server/models/user.go index 4a58d3e..c85ff66 100644 --- a/server/models/user.go +++ b/server/models/user.go @@ -9,12 +9,12 @@ import ( type User struct { bun.BaseModel `bun:"table:users,alias:u"` - Mail string `bun:"mail,pk,notnull"` - Fn string `bun:"fn,notnull"` - Sn string `bun:"sn"` - Confirmed bool `bun:"confirmed,notnull"` - SessionID int32 `bun:"session_id"` - LastLogin time.Time `bun:"last_login,default:current_timestamp"` - PasswordHash string `bun:"password_hash"` - CreatedAt time.Time `bun:"created_at,default:current_timestamp"` + Mail string `bun:",pk,notnull,type:varchar(255)"` + Fn string `bun:",notnull,type:varchar(255)"` + Sn string `bun:"type:varchar(255)"` + Confirmed bool `bun:"confirmed,notnull"` + SessionID int32 + LastLogin time.Time `bun:",default:current_timestamp"` + PasswordHash string `bun:"type:varchar(64)"` + CreatedAt time.Time `bun:",default:current_timestamp"` }