Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: database model #1

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ type User struct {
UserName string
IsUserCompleted bool
CocktailID uint
Checkins []Checkin `gorm:"foreignKey:UserID;references:UserID"`
Checkins []Checkin
}

type Checkin struct {
Checkin uint `gorm:"primaryKey"`
CheckinID uint `gorm:"primaryKey;autoincrement"`
UserID string
LocationID string
User User `gorm:"foreignKey:UserID;references:UserID"`
}
8 changes: 4 additions & 4 deletions src/server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewSqlHandler(db *gorm.DB) interfaces.SqlHandler {

//Create obj in database
func (handler *SqlHandler) Create(obj interface{}) error {
result := handler.db.Session(&gorm.Session{}).Create(obj)
result := handler.db.Create(obj)
if result.Error != nil {
return result.Error
}
Expand All @@ -27,7 +27,7 @@ func (handler *SqlHandler) Create(obj interface{}) error {

//Update column of obj in database with column_name and value
func (handler *SqlHandler) UpdateColumn(obj interface{}, column_name string, value interface{}) error {
result := handler.db.Session(&gorm.Session{}).Model(&obj).Update(column_name, value)
result := handler.db.Model(&obj).Update(column_name, value)
if result.Error != nil {
return result.Error
}
Expand All @@ -36,7 +36,7 @@ func (handler *SqlHandler) UpdateColumn(obj interface{}, column_name string, val

//Get data of object by primary key
func (handler *SqlHandler) GetByPrimaryKey(obj interface{}) error {
result := handler.db.Session(&gorm.Session{}).First(obj)
result := handler.db.First(obj)
if result.Error != nil {
return result.Error
}
Expand All @@ -45,7 +45,7 @@ func (handler *SqlHandler) GetByPrimaryKey(obj interface{}) error {

//Get data of object with associations
func (handler *SqlHandler) GetWithAssociations(obj interface{}) error {
result := handler.db.Session(&gorm.Session{}).Preload(clause.Associations).First(obj)
result := handler.db.Preload(clause.Associations).First(obj)
if result.Error != nil {
return result.Error
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func New() *Server {
fmt.Printf("Error connecting to the database: %v\n", err)
os.Exit(0)
}
db = db.Exec(fmt.Sprintf("SET search_path TO %s", config.Database.Schema))
db = db.Exec(fmt.Sprintf("SET search_path TO %s", config.Database.Schema)).Session(&gorm.Session{})

sqlHandler := NewSqlHandler(db)

Expand Down
Loading