Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
际垚 王
ShortLink
Commits
9c18c671
Commit
9c18c671
authored
Dec 17, 2022
by
4c00
Browse files
modified: model/foo.crud.go
modified: model/foo.go
parent
b1801b86
Pipeline
#48
canceled with stages
in 0 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
model/foo.crud.go
View file @
9c18c671
...
...
@@ -13,6 +13,7 @@ func RegisterUser(name, email, pwd string) error {
newUser
.
Email
=
email
newUser
.
Id
=
0
newUser
.
Number
=
0
//create the information of the user
err
:=
DB
.
Create
(
&
newUser
)
if
err
!=
nil
{
var
errReturn
=
errors
.
New
(
"fail to register the user"
)
...
...
@@ -21,6 +22,53 @@ func RegisterUser(name, email, pwd string) error {
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
{
targetUser
:=
User
{
Email
:
email
}
err
:=
DB
.
Find
(
&
targetUser
)
...
...
@@ -99,7 +147,12 @@ func GetUrl(name string) (interface{}, error) {
func
CreateLink
(
name
string
,
link
ShortLink
)
(
int
,
error
)
{
newLink
:=
link
newLink
.
Name
=
name
if
link
.
StartTime
<
link
.
ExpireTime
{
newLink
.
Status
=
true
}
else
{
newLink
.
Status
=
false
}
//set the status of the short link
master
:=
User
{
Name
:
name
}
err
:=
DB
.
Find
(
&
master
)
if
err
!=
nil
{
...
...
@@ -110,9 +163,10 @@ func CreateLink(name string, link ShortLink) (int, error) {
master
.
Id
=
master
.
Id
+
1
err1
:=
DB
.
Updates
(
&
master
)
if
err1
!=
nil
{
var
err1Return
=
errors
.
New
(
"fail to update the i
m
formation of the user"
)
var
err1Return
=
errors
.
New
(
"fail to update the i
n
formation of the user"
)
return
0
,
err1Return
}
//update the information of the user
err2
:=
DB
.
Create
(
&
newLink
)
if
err2
!=
nil
{
var
err2Return
=
errors
.
New
(
"fail to create the link"
)
...
...
@@ -168,12 +222,14 @@ func DeleteLink(name string, id int) error {
var
err2Return
=
errors
.
New
(
"fail to delete the short link"
)
return
err2Return
}
//delete the targrt link
master
.
Id
=
master
.
Id
-
1
err3
:=
DB
.
Updates
(
&
master
)
if
err3
!=
nil
{
var
err3Return
=
errors
.
New
(
"fail to update the imformation of the user"
)
return
err3Return
}
//update the information of the user
return
nil
}
...
...
@@ -185,6 +241,7 @@ func PauseLink(name string, id int) error {
return
errReturn
}
targetLink
.
Status
=
false
//set the status of the short link false
err1
:=
DB
.
Updates
(
&
targetLink
)
if
err1
!=
nil
{
var
err1Return
=
errors
.
New
(
"fail to pause the short link"
)
...
...
@@ -194,7 +251,7 @@ func PauseLink(name string, id int) error {
}
func
ExistShort
(
str
string
)
bool
{
targetLink
:=
ShortLink
{
Short
:
str
}
targetLink
:=
ShortLink
{
Short
:
str
,
Status
:
true
}
err
:=
DB
.
Find
(
&
targetLink
)
if
err
!=
nil
{
return
false
...
...
model/foo.go
View file @
9c18c671
...
...
@@ -6,15 +6,13 @@ type User struct {
Name
string
`json:"name" form:"name" query:"name"`
Pwd
string
`json:"pwd" form:"pwd" query:"pwd"`
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"`
Id
int
Number
int
Id
int
//count the number of short link of the user
Number
int
//count the login number of the user
}
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"`
Ip
string
`json:"ip" form:"ip" query:"ip"`
Id
int
...
...
@@ -22,13 +20,13 @@ type Record struct {
type
ShortLink
struct
{
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"`
Short
string
`json:"short" form:"short" query:"short"`
Comment
string
`json:"comment" form:"comment" query:"comment"`
StartTime
int64
`json:"starttime" form:"starttime" query:"starttime"`
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
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment