Skip to content

Commit

Permalink
defer db close
Browse files Browse the repository at this point in the history
  • Loading branch information
inciner8r committed Apr 4, 2024
1 parent 6fb7891 commit 85ddc9d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions api/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func AddProject(c *gin.Context) {
return
}
db := dbconfig.GetDb()
instance, _ := db.DB()
defer instance.Close()

var project models.Project
id := uuid.New()
project.Id = id.String()
Expand All @@ -55,6 +58,8 @@ func AddProject(c *gin.Context) {

func GetAllProjects(c *gin.Context) {
db := dbconfig.GetDb()
instance, _ := db.DB()
defer instance.Close()
var projects []models.Project
err := db.Model(&models.Project{}).Find(&projects).Error
if err != nil {
Expand All @@ -69,6 +74,9 @@ func GetAllProjects(c *gin.Context) {

func getProjectById(c *gin.Context) {
db := dbconfig.GetDb()
instance, _ := db.DB()
defer instance.Close()

id := c.Query("id")

var project models.Project
Expand Down
3 changes: 2 additions & 1 deletion config/dbconfig/dbconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func GetDb() *gorm.DB {

func Init() error {
db := GetDb()

instance, _ := db.DB()
defer instance.Close()
err := db.AutoMigrate(&models.Project{})
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 85ddc9d

Please sign in to comment.