Commit 9c18c671 authored by 4c00's avatar 4c00
Browse files

modified: model/foo.crud.go

	modified:   model/foo.go
parent b1801b86
Pipeline #48 canceled with stages
in 0 seconds
...@@ -13,6 +13,7 @@ func RegisterUser(name, email, pwd string) error { ...@@ -13,6 +13,7 @@ func RegisterUser(name, email, pwd string) error {
newUser.Email = email newUser.Email = email
newUser.Id = 0 newUser.Id = 0
newUser.Number = 0 newUser.Number = 0
//create the information of the user
err := DB.Create(&newUser) err := DB.Create(&newUser)
if err != nil { if err != nil {
var errReturn = errors.New("fail to register the user") var errReturn = errors.New("fail to register the user")
...@@ -21,6 +22,53 @@ func RegisterUser(name, email, pwd string) error { ...@@ -21,6 +22,53 @@ func RegisterUser(name, email, pwd string) error {
return nil return nil
} }
func Login(name, ip string, time int64) error {
targetUser := User{Name: name}
err := DB.Find(&targetUser)
if err != nil {
var errReturn = errors.New("fail to find the user")
return errReturn
}
//get the login information of the user
newRecord := Record{Name: name}
newRecord.Id = targetUser.Number
newRecord.Ip = ip
newRecord.Time = time
//set the record
err1 := DB.Create(&newRecord)
if err1 != nil {
var err1Return = errors.New("fail to register the user")
return err1Return
}
targetUser.Number = targetUser.Number + 1
targetUser.Status = true
//change the status of the user
err2 := DB.Updates(&targetUser)
if err2 != nil {
var err2Return = errors.New("fail to update the information of the user")
return err2Return
}
//update the information of the user
return nil
}
func Logout(name string) error {
targetUser := User{Name: name}
err := DB.Find(&targetUser)
if err != nil {
var errReturn = errors.New("fail to find the user")
return errReturn
}
targetUser.Status = false
//change the status of the user
err1 := DB.Updates(&targetUser)
if err1 != nil {
var err1Return = errors.New("fail to update the information of the user")
return err1Return
}
return nil
}
func CheckAuth(email, pwd string) bool { func CheckAuth(email, pwd string) bool {
targetUser := User{Email: email} targetUser := User{Email: email}
err := DB.Find(&targetUser) err := DB.Find(&targetUser)
...@@ -99,7 +147,12 @@ func GetUrl(name string) (interface{}, error) { ...@@ -99,7 +147,12 @@ func GetUrl(name string) (interface{}, error) {
func CreateLink(name string, link ShortLink) (int, error) { func CreateLink(name string, link ShortLink) (int, error) {
newLink := link newLink := link
newLink.Name = name newLink.Name = name
newLink.Status = true if link.StartTime < link.ExpireTime {
newLink.Status = true
} else {
newLink.Status = false
}
//set the status of the short link
master := User{Name: name} master := User{Name: name}
err := DB.Find(&master) err := DB.Find(&master)
if err != nil { if err != nil {
...@@ -110,9 +163,10 @@ func CreateLink(name string, link ShortLink) (int, error) { ...@@ -110,9 +163,10 @@ func CreateLink(name string, link ShortLink) (int, error) {
master.Id = master.Id + 1 master.Id = master.Id + 1
err1 := DB.Updates(&master) err1 := DB.Updates(&master)
if err1 != nil { if err1 != nil {
var err1Return = errors.New("fail to update the imformation of the user") var err1Return = errors.New("fail to update the information of the user")
return 0, err1Return return 0, err1Return
} }
//update the information of the user
err2 := DB.Create(&newLink) err2 := DB.Create(&newLink)
if err2 != nil { if err2 != nil {
var err2Return = errors.New("fail to create the link") var err2Return = errors.New("fail to create the link")
...@@ -168,12 +222,14 @@ func DeleteLink(name string, id int) error { ...@@ -168,12 +222,14 @@ func DeleteLink(name string, id int) error {
var err2Return = errors.New("fail to delete the short link") var err2Return = errors.New("fail to delete the short link")
return err2Return return err2Return
} }
//delete the targrt link
master.Id = master.Id - 1 master.Id = master.Id - 1
err3 := DB.Updates(&master) err3 := DB.Updates(&master)
if err3 != nil { if err3 != nil {
var err3Return = errors.New("fail to update the imformation of the user") var err3Return = errors.New("fail to update the imformation of the user")
return err3Return return err3Return
} }
//update the information of the user
return nil return nil
} }
...@@ -185,6 +241,7 @@ func PauseLink(name string, id int) error { ...@@ -185,6 +241,7 @@ func PauseLink(name string, id int) error {
return errReturn return errReturn
} }
targetLink.Status = false targetLink.Status = false
//set the status of the short link false
err1 := DB.Updates(&targetLink) err1 := DB.Updates(&targetLink)
if err1 != nil { if err1 != nil {
var err1Return = errors.New("fail to pause the short link") var err1Return = errors.New("fail to pause the short link")
...@@ -194,7 +251,7 @@ func PauseLink(name string, id int) error { ...@@ -194,7 +251,7 @@ func PauseLink(name string, id int) error {
} }
func ExistShort(str string) bool { func ExistShort(str string) bool {
targetLink := ShortLink{Short: str} targetLink := ShortLink{Short: str, Status: true}
err := DB.Find(&targetLink) err := DB.Find(&targetLink)
if err != nil { if err != nil {
return false return false
......
...@@ -6,15 +6,13 @@ type User struct { ...@@ -6,15 +6,13 @@ type User struct {
Name string `json:"name" form:"name" query:"name"` Name string `json:"name" form:"name" query:"name"`
Pwd string `json:"pwd" form:"pwd" query:"pwd"` Pwd string `json:"pwd" form:"pwd" query:"pwd"`
Email string `json:"email" form:"email" query:"email"` Email string `json:"email" form:"email" query:"email"`
Time int64 `json:"time" form:"time" query:"time"`
Ip string `json:"ip" form:"ip" query:"ip"`
Status bool `json:"status" form:"status" query:"status"` Status bool `json:"status" form:"status" query:"status"`
Id int Id int //count the number of short link of the user
Number int Number int //count the login number of the user
} }
type Record struct { type Record struct {
Name string `json:"name" form:"name" query:"name"` Name string `json:"name" form:"name" query:"name"` //set the user of the record
Time int64 `json:"time" form:"time" query:"time"` Time int64 `json:"time" form:"time" query:"time"`
Ip string `json:"ip" form:"ip" query:"ip"` Ip string `json:"ip" form:"ip" query:"ip"`
Id int Id int
...@@ -22,13 +20,13 @@ type Record struct { ...@@ -22,13 +20,13 @@ type Record struct {
type ShortLink struct { type ShortLink struct {
Id int `json:"id" form:"id" query:"id"` Id int `json:"id" form:"id" query:"id"`
Name string `json:"name" form:"name" query:"name"` Name string `json:"name" form:"name" query:"name"` //set the user of the short link
Origin string `json:"origin" form:"origin" query:"origin"` Origin string `json:"origin" form:"origin" query:"origin"`
Short string `json:"short" form:"short" query:"short"` Short string `json:"short" form:"short" query:"short"`
Comment string `json:"comment" form:"comment" query:"comment"` Comment string `json:"comment" form:"comment" query:"comment"`
StartTime int64 `json:"starttime" form:"starttime" query:"starttime"` StartTime int64 `json:"starttime" form:"starttime" query:"starttime"`
ExpireTime int64 `json:"expiretime" form:"expiretime" query:"expiretime"` ExpireTime int64 `json:"expiretime" form:"expiretime" query:"expiretime"`
Status bool `json:"status" form:"status" query:"status"` Status bool `json:"status" form:"status" query:"status"` //set the status of the short link, if true, we can use the short link, false, not
} }
type UpdateData struct { type UpdateData struct {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment