Skip to content

Commit 8a10e6c

Browse files
committed
Merge 'feature/paylist_with_postgre' to develop
2 parents 7a83b11 + 5fac49d commit 8a10e6c

18 files changed

+236
-186
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
gin-bin
2+
archieve-bin/
3+
*.idea

config/config.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,18 @@ import (
88
"os"
99

1010
"github.com/jinzhu/gorm"
11-
_ "github.com/jinzhu/gorm/dialects/mysql"
11+
_ "github.com/jinzhu/gorm/dialects/postgres"
1212

1313
"github.com/ariebrainware/paylist-api/model"
1414
)
1515

1616
// Config is a configuration model
1717
type Config struct {
18-
Db struct {
19-
Host string
20-
User string
21-
Password string
22-
Database string
23-
}
24-
Listen struct {
25-
Address string
26-
Port string
27-
}
18+
Host string
19+
User string
20+
Password string
21+
Database string
22+
Port int
2823
}
2924

3025
var (
@@ -47,12 +42,14 @@ func Conf() {
4742
if err != nil {
4843
log.Fatal("can't decode config json", err)
4944
}
50-
log.Println(config.Db.Database)
51-
connString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", config.Db.User, config.Db.Password, config.Db.Host, config.Listen.Port, config.Db.Database)
52-
DB, err = gorm.Open("mysql", connString)
45+
log.Println(config.Database)
46+
connString := fmt.Sprintf(`user=%s password=%s host=%s port=%d dbname=%s sslmode=disable`, config.User, config.Password, config.Host, config.Port, config.Database)
47+
DB, err = gorm.Open("postgres", connString)
48+
DB.LogMode(true)
5349
if err != nil {
50+
fmt.Println(err)
5451
panic("failed connect to database")
5552
}
56-
DB.AutoMigrate(&model.Paylist{}, &model.User{})
53+
DB.AutoMigrate(&model.Paylist{}, &model.User{}, &model.Logging{})
5754
fmt.Println("Schema migrated!!")
5855
}

config/config.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
{
2-
"Db": {
3-
"Host": "localhost",
4-
"User": "rob0ne",
5-
"Password": "@_L0c4lDB",
6-
"Database": "paylist"
7-
},
8-
"Listen": {
9-
"Address": "*",
10-
"Port": "3306"
11-
}
2+
"Host": "localhost",
3+
"User": "postgres",
4+
"Password": "",
5+
"Database": "paylist",
6+
"Port": 5432
127
}

endpoint/paylist.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func CreateUserPaylist(c *gin.Context) {
8080
return
8181
}
8282
username := tk.Username
83-
8483
// Decrease user balance
8584
amount, _ := strconv.Atoi(c.PostForm("amount"))
8685
err = config.DB.Model(&users).Where("username = ?", username).First(&users).Error
@@ -162,7 +161,6 @@ func UpdateUserPaylist(c *gin.Context) {
162161

163162
// UpdateUserPaylistStatus is a function to mark the paylist completed or not
164163
func UpdateUserPaylistStatus(c *gin.Context) {
165-
status := c.PostForm("status")
166164
paylist := model.Paylist{}
167165
user := model.User{}
168166
tk := User{}
@@ -179,14 +177,6 @@ func UpdateUserPaylistStatus(c *gin.Context) {
179177
username := tk.Username
180178

181179
// Check User balance
182-
if err = config.DB.Model(&paylist).Where("username = ?", username).First(&paylist).Error; err != nil {
183-
util.CallErrorNotFound(c, "no paylist found", nil)
184-
return
185-
}
186-
if tk.Username != paylist.Username {
187-
util.CallServerError(c, "not authorized", nil)
188-
return
189-
}
190180
id, _ := strconv.Atoi(c.Param("id"))
191181
if err := config.DB.Model(&paylist).Where("ID = ? AND username = ?", id, username).First(&paylist).Error; err != nil || err == gorm.ErrRecordNotFound {
192182
util.CallErrorNotFound(c, "no paylist found", nil)
@@ -198,24 +188,21 @@ func UpdateUserPaylistStatus(c *gin.Context) {
198188
}
199189

200190
// Update the paylist status
201-
s, _ := strconv.ParseBool(status)
202-
if err = config.DB.Model(&paylist).Where("username = ?", tk.Username).Update("completed", s).Error; err != nil {
203-
fmt.Println(err)
204-
util.CallServerError(c, "fail to update paylist status", err)
205-
return
191+
if user.Balance >= 0 && paylist.Completed == false {
192+
paylist.Completed = true
193+
config.DB.Model(&paylist).Where("ID = ? and username = ?", id, username).Update(&paylist)
194+
} else if user.Balance < 0 && paylist.Completed == false {
195+
paylist.Completed = false
196+
config.DB.Model(&paylist).Where("ID = ? and username = ?", id, username).Update(&paylist)
206197
}
207-
if !s {
208-
util.CallSuccessOK(c, "paylist uncompleted!", paylist)
209-
return
210-
}
211-
util.CallSuccessOK(c, "paylist completed!", paylist)
198+
util.CallSuccessOK(c, "successfully update user paylist", paylist.Completed)
212199
}
213200

214201
//DeleteUserPaylist handle deleted user paylist
215202
func DeleteUserPaylist(c *gin.Context) {
216203
paylistID, _ := strconv.Atoi(c.Param("id"))
217-
paylist := model.Paylist{}
218-
user := model.User{}
204+
paylist := &model.Paylist{}
205+
user := &model.User{}
219206
tk := User{}
220207

221208
if paylistID == 0 {
@@ -232,15 +219,15 @@ func DeleteUserPaylist(c *gin.Context) {
232219
util.CallUserError(c, "fail to parse the token, make sure the token is valid", err)
233220
}
234221
username := tk.Username
235-
config.DB.Model(&paylist).Where("username = ?", username).First(&paylist)
236-
if tk.Username != paylist.Username {
237-
util.CallServerError(c, "user not authorized", nil)
222+
if err = config.DB.Where("ID = ?", paylistID).Find(&paylist).Error; err != nil {
223+
util.CallErrorNotFound(c, "no paylist found!", err)
238224
c.Abort()
239225
return
240226
}
241227

242-
if err = config.DB.Where("id = ?", paylistID).First(&paylist).Error; err != nil {
243-
util.CallErrorNotFound(c, "no paylist found!", nil)
228+
config.DB.Model(&paylist).Where("username = ?", username).First(&paylist)
229+
if tk.Username != paylist.Username {
230+
util.CallServerError(c, "user not authorized", nil)
244231
c.Abort()
245232
return
246233
}

0 commit comments

Comments
 (0)