Skip to content
Snippets Groups Projects
Commit 1cd078ba authored by Sercan Yesildal's avatar Sercan Yesildal
Browse files

wip migrate to db

parent 8f5bb64d
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
docker run -d -p 3306:3306 --name mariadb -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=myaktion mariadb:10.5
\ No newline at end of file
#!/bin/bash
docker kill database && docker rm database
\ No newline at end of file
package db
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/model"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func init() {
// init database
dsn := fmt.Sprintf("root:root@tcp(%s)/myaktion?charset=utf8&parseTime=True&loc=Local", os.Getenv("DB_CONNECT"))
log.Info("Using DSN for DB:", dsn)
var err error
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to database")
}
log.Info("Starting automatic migrations")
if err := DB.Debug().AutoMigrate(&model.Campaign{}); err != nil {
panic(err)
}
if err := DB.Debug().AutoMigrate(&model.Donation{}); err != nil {
panic(err)
}
log.Info("Automatic migrations finished")
}
...@@ -5,6 +5,13 @@ go 1.20 ...@@ -5,6 +5,13 @@ go 1.20
require ( require (
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0
gorm.io/driver/mysql v1.5.0
gorm.io/gorm v1.25.1
) )
require golang.org/x/sys v0.7.0 // indirect require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
golang.org/x/sys v0.7.0 // indirect
)
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
...@@ -16,3 +23,8 @@ golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= ...@@ -16,3 +23,8 @@ golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.0 h1:6hSAT5QcyIaty0jfnff0z0CLDjyRgZ8mlMHLqSt7uXM=
gorm.io/driver/mysql v1.5.0/go.mod h1:FFla/fJuCvyTi7rJQd27qlNX2v3L6deTR1GgTjSOLPo=
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64=
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
package model package model
type Account struct { type Account struct {
Name string `json:"name"` Name string `json:"name" gorm:"notNull"`
BankName string `json:"bankName"` BankName string `json:"bankName" gorm:"notNull"`
Number string `json:"number"` Number string `json:"number" gorm:"notNull"`
} }
package model package model
type Campaign struct { type Campaign struct {
ID uint `json:"id"` ID uint `json:"id" gorm:"primaryKey"`
Name string `json:"name"` Name string `json:"name" gorm:"notNull"`
OrganizerName string `json:"organizerName"` OrganizerName string `json:"organizerName" gorm:"notNull"`
TargetAmount float64 `json:"targetAmount"` TargetAmount float64 `json:"targetAmount" gorm:"notNull; check:target_amount >= 10.0"`
DonationMinimum float64 `json:"donationMinimum"` DonationMinimum float64 `json:"donationMinimum" gorm:"notNull; check:donation_minimum >= 1.0"`
AmountDonatedSoFar float64 `json:"amountDonatedSoFar"` AmountDonatedSoFar float64 `json:"amountDonatedSoFar" gorm:"-"`
Account Account `json:"account"` Account Account `json:"account" gorm:"embedded; embeddedPrefix:account_"`
Donations []Donation `json:"donations"` Donations []Donation `json:"donations" gorm:"foreignKey: CampaignID; constraint:OnUpdate:CASCADE, OnDelete:CASCADE"`
} }
...@@ -6,11 +6,12 @@ const ( ...@@ -6,11 +6,12 @@ const (
) )
type Donation struct { type Donation struct {
Amount float64 `json:"amount"` CampaignID uint `json:"campaignId"`
DonorName string `json:"donorName"` Amount float64 `json:"amount" gorm:"notNull"`
ReceiptRequested bool `json:"receiptRequested"` DonorName string `json:"donorName" gorm:"notNull"`
Account Account `json:"account"` ReceiptRequested bool `json:"receiptRequested" gorm:"notNull"`
Status Status `json:"status"` Account Account `json:"account" gorm:"embedded; embeddedPrefix:account_; foreignKey: CampaignID; constraint:OnUpdate:CASCADE, OnDelete:CASCADE"`
Status Status `json:"status" gorm:"notNull; type:ENUM('TRANSFERRED','IN_PROCESS')"`
} }
type Status string type Status string
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/db"
"gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/model" "gitlab.reutlingen-university.de/yesildas/myaktion-go/src/myaktion/model"
) )
...@@ -18,9 +19,11 @@ func init() { ...@@ -18,9 +19,11 @@ func init() {
} }
func CreateCampaign(campaign *model.Campaign) error { func CreateCampaign(campaign *model.Campaign) error {
campaign.ID = actCampaignId result := db.DB.Create(campaign)
campaignStore[actCampaignId] = campaign if result.Error != nil {
actCampaignId += 1 log.Info("Campaign creation failed.")
return result.Error
}
log.Infof("Successfully stored new campaign with ID %v in database.", campaign.ID) log.Infof("Successfully stored new campaign with ID %v in database.", campaign.ID)
log.Tracef("Stored: %v", campaign) log.Tracef("Stored: %v", campaign)
...@@ -40,8 +43,9 @@ func GetCampaign(id uint) (*model.Campaign, error) { ...@@ -40,8 +43,9 @@ func GetCampaign(id uint) (*model.Campaign, error) {
func GetCampaigns() ([]model.Campaign, error) { func GetCampaigns() ([]model.Campaign, error) {
var campaigns []model.Campaign var campaigns []model.Campaign
for _, campaign := range campaignStore { result := db.DB.Preload("Donations").Find(&campaigns)
campaigns = append(campaigns, *campaign) if result.Error != nil {
return nil, result.Error
} }
log.Infof("Successfully retrieved %d campaigns.", len(campaigns)) log.Infof("Successfully retrieved %d campaigns.", len(campaigns))
log.Tracef("Retrieved: %v", campaigns) log.Tracef("Retrieved: %v", campaigns)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment