Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
李宇怀
Backend
Commits
e65e6399
Commit
e65e6399
authored
Aug 29, 2023
by
李宇怀
Browse files
完成task C
parent
3b79ce5f
Changes
5
Hide whitespace changes
Inline
Side-by-side
internal/controller/link.go
View file @
e65e6399
package
controller
package
controller
import
(
import
(
"fmt"
"go-svc-tpl/api/dto"
"go-svc-tpl/api/dto"
"go-svc-tpl/internal/dao"
"go-svc-tpl/internal/dao/model"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm"
)
)
const
USER_ID_KEY
=
"qwq"
const
LinkTable
=
"Links"
// >>>>>>>>>>>>>>>>>> Interface >>>>>>>>>>>>>>>>>>
// >>>>>>>>>>>>>>>>>> Interface >>>>>>>>>>>>>>>>>>
type
ILinkController
interface
{
//定义tag link 下的操作
type
ILinkController
interface
{
//定义tag link 下的操作
Create
(
*
gin
.
Context
,
*
dto
.
LinkCreateReq
)
(
*
dto
.
LinkCreateResp
,
error
)
Create
(
*
gin
.
Context
,
*
dto
.
LinkCreateReq
)
(
*
dto
.
LinkCreateResp
,
error
)
...
@@ -30,26 +36,140 @@ type LinkController struct {
...
@@ -30,26 +36,140 @@ type LinkController struct {
}
}
// create
// create
// 用gorm创建短链接
// short 如果为空字符串,则自动生成链接
// 如果起止时间为 null 则表示不设置起止时间
func
(
c
*
LinkController
)
Create
(
ctx
*
gin
.
Context
,
req
*
dto
.
LinkCreateReq
)
(
*
dto
.
LinkCreateResp
,
error
)
{
func
(
c
*
LinkController
)
Create
(
ctx
*
gin
.
Context
,
req
*
dto
.
LinkCreateReq
)
(
*
dto
.
LinkCreateResp
,
error
)
{
return
nil
,
nil
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
newLink
:=
&
model
.
Link
{
Short
:
req
.
Short
,
Comment
:
req
.
Comment
,
Origin
:
req
.
Origin
,
StartTime
:
req
.
StartTime
,
EndTime
:
req
.
EndTime
,
Active
:
true
,
OwnerID
:
userID
,
}
err
:=
dao
.
DB
(
ctx
)
.
Create
(
newLink
)
.
Error
if
err
!=
nil
{
return
nil
,
err
}
var
link
model
.
Link
err
=
dao
.
DB
(
ctx
)
.
First
(
&
link
,
"Short = ?"
,
newLink
.
Short
)
.
Error
if
err
!=
nil
{
if
err
==
gorm
.
ErrRecordNotFound
{
fmt
.
Println
(
"Short is not duplicate. You can insert."
)
}
return
nil
,
err
}
return
&
dto
.
LinkCreateResp
{
Short
:
newLink
.
Short
,
Origin
:
newLink
.
Origin
,
Comment
:
newLink
.
Comment
,
StartTime
:
newLink
.
StartTime
,
EndTime
:
newLink
.
EndTime
,
Active
:
newLink
.
Active
,
},
nil
}
}
// delete
// delete
func
(
c
*
LinkController
)
Delete
(
ctx
*
gin
.
Context
,
req
*
dto
.
LinkDeleteReq
)
error
{
func
(
c
*
LinkController
)
Delete
(
ctx
*
gin
.
Context
,
req
*
dto
.
LinkDeleteReq
)
error
{
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
deleteLink
:=
&
model
.
Link
{
Short
:
req
.
Short
,
OwnerID
:
userID
,
}
err
:=
dao
.
DB
(
ctx
)
.
Delete
(
&
deleteLink
)
.
Error
if
err
!=
nil
{
return
err
}
var
link
model
.
Link
err
=
dao
.
DB
(
ctx
)
.
First
(
&
link
,
"Short = ?"
,
deleteLink
.
Short
)
.
Error
if
err
!=
nil
{
if
err
==
gorm
.
ErrRecordNotFound
{
fmt
.
Println
(
"Short is not duplicate. You can insert."
)
}
return
err
}
return
nil
return
nil
}
}
// getinfo
// getinfo
func
(
c
*
LinkController
)
GetInfo
(
ctx
*
gin
.
Context
,
req
*
dto
.
GetLinkInfoReq
)
(
*
dto
.
GetLinkInfoResp
,
error
)
{
func
(
c
*
LinkController
)
GetInfo
(
ctx
*
gin
.
Context
,
req
*
dto
.
GetLinkInfoReq
)
(
*
dto
.
GetLinkInfoResp
,
error
)
{
return
nil
,
nil
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
getinfoLink
:=
&
model
.
Link
{
Short
:
req
.
Short
,
OwnerID
:
userID
,
}
var
link
model
.
Link
err
:=
dao
.
DB
(
ctx
)
.
First
(
&
link
,
"Short = ?"
,
getinfoLink
.
Short
)
.
Error
if
err
!=
nil
{
if
err
==
gorm
.
ErrRecordNotFound
{
fmt
.
Println
(
"Short is not duplicate. You can insert."
)
}
return
nil
,
err
}
return
&
dto
.
GetLinkInfoResp
{
Short
:
link
.
Short
,
Origin
:
link
.
Origin
,
Comment
:
link
.
Comment
,
StartTime
:
link
.
StartTime
,
EndTime
:
link
.
EndTime
,
Active
:
link
.
Active
,
},
nil
}
}
// update
// update
func
(
c
*
LinkController
)
Update
(
ctx
*
gin
.
Context
,
req
*
dto
.
LinkUpdateReq
)
error
{
func
(
c
*
LinkController
)
Update
(
ctx
*
gin
.
Context
,
req
*
dto
.
LinkUpdateReq
)
error
{
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
updateLink
:=
&
model
.
Link
{
Short
:
req
.
Short
,
Origin
:
req
.
Origin
,
Comment
:
req
.
Comment
,
StartTime
:
req
.
StartTime
,
EndTime
:
req
.
EndTime
,
Active
:
req
.
Active
,
OwnerID
:
userID
,
}
err
:=
dao
.
DB
(
ctx
)
.
Updates
(
&
updateLink
)
.
Error
if
err
!=
nil
{
return
err
}
var
link
model
.
Link
err
=
dao
.
DB
(
ctx
)
.
First
(
&
link
,
"Short = ?"
,
updateLink
.
Short
)
.
Error
if
err
!=
nil
{
if
err
==
gorm
.
ErrRecordNotFound
{
fmt
.
Println
(
"Short is not duplicate. You can insert."
)
}
return
err
}
return
nil
return
nil
}
}
// getlist
// getlist
func
(
c
*
LinkController
)
GetList
(
ctx
*
gin
.
Context
,
req
*
dto
.
GetLinkListReq
)
(
*
dto
.
GetLinkListResp
,
error
)
{
func
(
c
*
LinkController
)
GetList
(
ctx
*
gin
.
Context
,
req
*
dto
.
GetLinkListReq
)
(
*
dto
.
GetLinkListResp
,
error
)
{
return
nil
,
nil
var
links
[]
dto
.
ShortLinkModel
var
total
int64
query
:=
dao
.
DB
(
ctx
)
.
Model
(
&
dto
.
ShortLinkModel
{})
if
req
.
PageNumber
>
0
&&
req
.
PageSize
>
0
{
offset
:=
(
req
.
PageNumber
-
1
)
*
req
.
PageSize
query
=
query
.
Offset
(
int
(
offset
))
.
Limit
(
int
(
req
.
PageSize
))
}
if
result
:=
query
.
Find
(
&
links
);
result
.
Error
!=
nil
{
return
nil
,
result
.
Error
}
query
.
Count
(
&
total
)
resp
:=
&
dto
.
GetLinkListResp
{
Links
:
links
,
Total
:
total
,
}
return
resp
,
nil
}
}
internal/controller/server.go
View file @
e65e6399
...
@@ -2,6 +2,8 @@ package controller
...
@@ -2,6 +2,8 @@ package controller
import
(
import
(
"go-svc-tpl/api/dto"
"go-svc-tpl/api/dto"
"go-svc-tpl/internal/dao/model"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm"
...
@@ -24,11 +26,30 @@ type ServerController struct {
...
@@ -24,11 +26,30 @@ type ServerController struct {
db
*
gorm
.
DB
db
*
gorm
.
DB
}
}
// 重定向到相应链接
// 当出现问题时,会按标准 response 返回,由前端显示相应页面
func
(
c
*
ServerController
)
Link
(
ctx
*
gin
.
Context
,
req
*
dto
.
ServerLinkReq
)
error
{
func
(
c
*
ServerController
)
Link
(
ctx
*
gin
.
Context
,
req
*
dto
.
ServerLinkReq
)
error
{
//不是很懂这个函数要干什么
shortLink
:=
req
.
Short
// 在数据库中查找对应的长链接
var
link
model
.
Link
if
err
:=
c
.
db
.
Table
(
model
.
LinkTable
)
.
Where
(
"short = ?"
,
shortLink
)
.
First
(
&
link
)
.
Error
;
err
!=
nil
{
if
err
==
gorm
.
ErrRecordNotFound
{
// 如果找不到对应的长链接,返回404错误
ctx
.
JSON
(
http
.
StatusNotFound
,
gin
.
H
{
"error"
:
"Link not found"
})
}
else
{
// 如果发生其他错误,返回500错误
ctx
.
JSON
(
http
.
StatusInternalServerError
,
gin
.
H
{
"error"
:
"Internal server error"
})
}
return
err
}
// 如果找到了对应的长链接,进行重定向
ctx
.
Redirect
(
http
.
StatusMovedPermanently
,
link
.
Origin
)
return
nil
return
nil
}
}
func
(
c
*
ServerController
)
Veri
(
ctx
*
gin
.
Context
,
req
*
dto
.
ServerVeriReq
)
error
{
func
(
c
*
ServerController
)
Veri
(
ctx
*
gin
.
Context
,
req
*
dto
.
ServerVeriReq
)
error
{
//no
return
nil
return
nil
}
}
internal/controller/user.go
View file @
e65e6399
package
controller
package
controller
import
(
import
(
"fmt"
"go-svc-tpl/api/dto"
"go-svc-tpl/api/dto"
"go-svc-tpl/internal/dao"
"go-svc-tpl/internal/dao/model"
"regexp"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm"
...
@@ -29,37 +33,103 @@ type UserController struct {
...
@@ -29,37 +33,103 @@ type UserController struct {
// Register
// Register
func
(
c
*
UserController
)
Register
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserRegisterReq
)
error
{
func
(
c
*
UserController
)
Register
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserRegisterReq
)
error
{
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
//验证邮箱格式
emailPattern
:=
`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$`
_
,
err
:=
regexp
.
MatchString
(
emailPattern
,
req
.
Email
)
if
err
!=
nil
{
fmt
.
Println
(
"wrong email pattern"
,
err
)
return
err
}
newUser
:=
&
model
.
User
{
ID
:
userID
,
Email
:
req
.
Email
,
Name
:
req
.
Name
,
Password
:
req
.
Password
,
}
err
=
dao
.
DB
(
ctx
)
.
Create
(
newUser
)
.
Error
if
err
!=
nil
{
return
err
}
//验证邮箱唯一性
var
user
model
.
User
err
=
dao
.
DB
(
ctx
)
.
First
(
&
user
,
"Email = ?"
,
newUser
.
Email
)
.
Error
if
err
!=
nil
{
if
err
==
gorm
.
ErrRecordNotFound
{
fmt
.
Println
(
"Email is not duplicate. You can insert."
)
}
return
err
}
return
nil
return
nil
}
}
// GetVeri
// GetVeri
func
(
c
*
UserController
)
GetVeri
(
ctx
*
gin
.
Context
)
(
*
dto
.
GetVeriResp
,
error
)
{
func
(
c
*
UserController
)
GetVeri
(
ctx
*
gin
.
Context
)
(
*
dto
.
GetVeriResp
,
error
)
{
//no
return
nil
,
nil
return
nil
,
nil
}
}
// Login
// Login
func
(
c
*
UserController
)
Login
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserLoginReq
)
error
{
func
(
c
*
UserController
)
Login
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserLoginReq
)
error
{
//no
return
nil
return
nil
}
}
// GetInfo
// GetInfo
func
(
c
*
UserController
)
GetInfo
(
ctx
*
gin
.
Context
)
(
*
dto
.
GetUserInfoResp
,
error
)
{
func
(
c
*
UserController
)
GetInfo
(
ctx
*
gin
.
Context
)
(
*
dto
.
GetUserInfoResp
,
error
)
{
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
return
nil
,
nil
var
user
model
.
User
err
:=
dao
.
DB
(
ctx
)
.
First
(
&
user
,
"ID = ?"
,
userID
)
.
Error
if
err
!=
nil
{
return
nil
,
err
}
return
&
dto
.
GetUserInfoResp
{
ID
:
user
.
ID
,
Name
:
user
.
Name
,
Email
:
user
.
Email
,
},
nil
}
}
// ModifyInfo
// ModifyInfo
func
(
c
*
UserController
)
ModifyInfo
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserModifyInfoReq
)
error
{
func
(
c
*
UserController
)
ModifyInfo
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserModifyInfoReq
)
error
{
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
//验证邮箱格式
emailPattern
:=
`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$`
_
,
err
:=
regexp
.
MatchString
(
emailPattern
,
req
.
Email
)
if
err
!=
nil
{
fmt
.
Println
(
"wrong email pattern"
,
err
)
return
err
}
newUser
:=
&
model
.
User
{
ID
:
userID
,
Email
:
req
.
Email
,
Name
:
req
.
Name
,
}
err
=
dao
.
DB
(
ctx
)
.
Updates
(
&
newUser
)
.
Error
if
err
!=
nil
{
return
err
}
return
nil
return
nil
}
}
// ModifyPwd
// ModifyPwd
func
(
c
*
UserController
)
ModifyPwd
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserModifyPwdReq
)
error
{
func
(
c
*
UserController
)
ModifyPwd
(
ctx
*
gin
.
Context
,
req
*
dto
.
UserModifyPwdReq
)
error
{
userID
:=
ctx
.
GetUint
(
USER_ID_KEY
)
var
user
model
.
User
err
:=
dao
.
DB
(
ctx
)
.
First
(
&
user
,
"ID = ?"
,
userID
)
.
Error
if
err
!=
nil
{
return
err
}
if
user
.
Password
!=
req
.
OldPwd
{
fmt
.
Println
(
"wrong password"
)
}
newUser
:=
&
model
.
User
{
Password
:
req
.
NewPwd
,
}
err
=
dao
.
DB
(
ctx
)
.
Updates
(
&
newUser
)
.
Error
if
err
!=
nil
{
return
err
}
return
nil
return
nil
}
}
internal/dao/link.go
deleted
100644 → 0
View file @
3b79ce5f
package
dao
internal/dao/model/link.go
View file @
e65e6399
package
model
package
model
import
"time"
import
(
"gopkg.in/guregu/null.v4"
)
const
LinkTable
=
"Links"
const
LinkTable
=
"Links"
type
Link
struct
{
type
Link
struct
{
ID
uint
`json:"id"`
// 链接ID
ID
uint
`json:"ID"`
//ID
Title
string
`json:"title"`
// 链接标题
Active
bool
`json:"active"`
URL
string
`json:"url"`
// 链接URL
Comment
string
`json:"comment"`
OwnerID
uint
`json:"owner_id"`
// 拥有者ID
EndTime
null
.
Time
`json:"end_time"`
CreatedAt
time
.
Time
`json:"created_at"`
// 创建时间
Origin
string
`json:"origin"`
UpdatedAt
time
.
Time
`json:"updated_at"`
// 更新时间
Short
string
`json:"short"`
StartTime
null
.
Time
`json:"start_time"`
OwnerID
uint
`json:"owner_id"`
}
}
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