-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: post checkin api
- Loading branch information
Showing
12 changed files
with
162 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
database: | ||
supabase: | ||
host: 127.0.0.1 | ||
user: postgres | ||
password: postgres | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,30 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/esc-chula/gearfest-backend/src/interfaces" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/esc-chula/gearfest-backend/src/config" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
type SqlHandler struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewSqlHandler(db *gorm.DB) interfaces.SqlHandler { | ||
SqlHandler := new(SqlHandler) | ||
SqlHandler.db = db | ||
return SqlHandler | ||
} | ||
|
||
//Create obj in database | ||
func (handler *SqlHandler) Create(obj interface{}) error { | ||
result := handler.db.Create(obj) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
//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.Model(&obj).Update(column_name, value) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
//Get data of object by primary key | ||
func (handler *SqlHandler) GetByPrimaryKey(obj interface{}) error { | ||
result := handler.db.First(obj) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
func LoadSupabase(config config.SupabaseConfig) *gorm.DB { | ||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", | ||
config.Host, | ||
config.User, | ||
config.Password, | ||
config.DBName, | ||
config.Port, | ||
config.SSLMode, | ||
config.Timezone, | ||
) | ||
|
||
//Get data of object with associations | ||
func (handler *SqlHandler) GetWithAssociations(obj interface{}) error { | ||
result := handler.db.Preload(clause.Associations).First(obj) | ||
if result.Error != nil { | ||
return result.Error | ||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
fmt.Printf("Error connecting to the database: %v\n", err) | ||
os.Exit(0) | ||
} | ||
return nil | ||
db = db.Exec(fmt.Sprintf("SET search_path TO %s", config.Schema)).Session(&gorm.Session{}) | ||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package repository | ||
|
||
import ( | ||
"github.com/esc-chula/gearfest-backend/src/domain" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type UserRepository struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewUserRepository(db *gorm.DB) *UserRepository { | ||
return &UserRepository{ | ||
db: db, | ||
} | ||
} | ||
|
||
// Create user in database | ||
func (repo *UserRepository) CreateUser(user *domain.User) error { | ||
result := repo.db.Create(user) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
// Create checkin | ||
func (repo *UserRepository) Checkin(checkin *domain.Checkin) error { | ||
result := repo.db.Create(checkin) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
// Update column of user in database with column_name and value | ||
func (repo *UserRepository) UpdateColumn(user *domain.User, column_name string, value interface{}) error { | ||
result := repo.db.Model(user).Update(column_name, value) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
// Get user by id | ||
func (repo *UserRepository) GetById(user *domain.User, id string) error { | ||
result := repo.db.First(user, "user_id = ?", id) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
// Get user with checkins | ||
func (repo *UserRepository) GetWithCheckins(user *domain.User, id string) error { | ||
result := repo.db.Model(&domain.User{}).Preload("Checkins").First(user, "user_id = ?", id) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
alter table "public"."checkins" drop constraint "unique_user_location"; | ||
|
||
drop index if exists "public"."unique_user_location"; | ||
|
||
alter table "public"."checkins" alter column "location_id" set data type bigint using "location_id"::bigint; | ||
|
||
alter table "public"."checkins" add constraint "checkins_location_id_check" CHECK ((location_id >= 0)) not valid; | ||
|
||
alter table "public"."checkins" validate constraint "checkins_location_id_check"; | ||
|
||
|