refactor: modify upload file structure

This commit is contained in:
lbaf23 2021-08-23 15:29:14 +08:00
parent 8f2ccdf5e0
commit 27c17eb0ed
10 changed files with 385 additions and 15 deletions

View File

@ -11,7 +11,7 @@ runmode=dev
httpport = 5000
driverName = mysql
dataSourceName = root:123@tcp(localhost:3306)/
dbName = openpbl_db
dbName = openpbl
redisEndpoint =
jwtSecret = CasdoorSecret

View File

@ -2,6 +2,7 @@ package controllers
import (
"OpenPBL/models"
"OpenPBL/util"
"encoding/json"
"strconv"
)
@ -41,6 +42,31 @@ func (p *ProjectController) GetSectionDetail() {
p.ServeJSON()
}
// GetSectionFiles
// @Title
// @Description
// @Param sectionId path string true ""
// @Param projectId path string true ""
// @Success 200 {object}
// @Failure 403 body is empty
// @router /:projectId/section/:sectionId/files [get]
func (p *ProjectController) GetSectionFiles() {
sid := p.GetString(":sectionId")
files, err := models.GetSectionFiles(sid)
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Data: files,
}
}
p.ServeJSON()
}
// GetChapterSections
// @Title
// @Description
@ -222,3 +248,124 @@ func (p *ProjectController) UpdateSectionsMinute() {
p.ServeJSON()
}
// UploadSectionFile
// @Title
// @Description
// @Param filePath body string true ""
// @Success 200 {object} models.TeacherProject
// @Failure 400
// @router /:projectId/section/:sectionId/file [post]
func (p *ProjectController) UploadSectionFile() {
user := p.GetSessionUser()
if !util.IsTeacher(user) {
p.Data["json"] = Response{
Code: 403,
Msg: "非法用户",
}
p.ServeJSON()
return
}
sid, err := p.GetInt64(":sectionId")
name := p.GetString("name")
url := p.GetString("url")
filePath := p.GetString("filePath")
r := &models.SectionFile{
SectionId: sid,
Name: name,
FilePath: filePath,
Url: url,
}
err = r.Create()
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Msg: "上传成功",
Data: r,
}
}
p.ServeJSON()
}
// UpdateSectionFile
// @Title
// @Description
// @Param filePath body string true ""
// @Success 200 {object} models.TeacherProject
// @Failure 400
// @router /:projectId/section/:sectionId/file/:fileId/update [post]
func (p *ProjectController) UpdateSectionFile() {
user := p.GetSessionUser()
if !util.IsTeacher(user) {
p.Data["json"] = Response{
Code: 403,
Msg: "非法用户",
}
p.ServeJSON()
return
}
sid, err := p.GetInt64(":sectionId")
fileId, err := p.GetInt64(":fileId")
name := p.GetString("name")
url := p.GetString("url")
filePath := p.GetString("filePath")
r := &models.SectionFile{
Id: fileId,
SectionId: sid,
Name: name,
FilePath: filePath,
Url: url,
}
err = r.Update()
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Msg: "上传成功",
Data: r,
}
}
p.ServeJSON()
}
// DeleteSectionFile
// @Title
// @Description
// @Param filePath body string true ""
// @Success 200 {object} models.TeacherProject
// @Failure 400
// @router /:projectId/section/:sectionId/file/:fileId/delete [post]
func (p *ProjectController) DeleteSectionFile() {
user := p.GetSessionUser()
if !util.IsTeacher(user) {
p.Data["json"] = Response{
Code: 403,
Msg: "非法用户",
}
p.ServeJSON()
return
}
fid := p.GetString(":fileId")
err := models.DeleteSectionFile(fid)
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Msg: "删除成功",
}
}
p.ServeJSON()
}

View File

@ -39,7 +39,6 @@ func (p *ProjectController) CreateSubmit() {
SubmitTitle: p.GetString("submitTitle"),
SubmitIntroduce: p.GetString("submitIntroduce"),
SubmitContent: p.GetString("submitContent"),
FilePath: p.GetString("filePath"),
CreateAt: time.Now(),
Scored: true,
Score: 100,
@ -68,6 +67,141 @@ func (p *ProjectController) CreateSubmit() {
p.ServeJSON()
}
// UploadSubmitFile
// @Title
// @Description
// @Param filePath body string true ""
// @Success 200 {object} models.TeacherProject
// @Failure 400
// @router /:projectId/task/:taskId/submit/:submitId/file [post]
func (p *ProjectController) UploadSubmitFile() {
user := p.GetSessionUser()
if !util.IsStudent(user) {
p.Data["json"] = Response{
Code: 403,
Msg: "非法用户",
}
p.ServeJSON()
return
}
uid := util.GetUserId(user)
tid, err := p.GetInt64(":taskId")
pid, err := p.GetInt64(":projectId")
url := p.GetString("url")
fileName := p.GetString("name")
filePath := p.GetString("filePath")
sid, err := p.GetInt64(":submitId")
if sid == 0 {
submit := &models.Submit{
ProjectId: pid,
StudentId: uid,
TaskId: tid,
SubmitType: "file",
CreateAt: time.Now(),
Scored: false,
Score: 0,
}
err = submit.Create(make([]models.Choice, 0))
sid = submit.Id
}
r := &models.SubmitFile{
SubmitId: sid,
Name: fileName,
FilePath: filePath,
Url: url,
}
err = r.Create()
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Msg: "上传成功",
}
}
p.ServeJSON()
}
// UpdateSubmitFile
// @Title
// @Description
// @Param filePath body string true ""
// @Success 200 {object} models.TeacherProject
// @Failure 400
// @router /:projectId/task/:taskId/submit/:submitId/file/:fileId/update [post]
func (p *ProjectController) UpdateSubmitFile() {
user := p.GetSessionUser()
if !util.IsStudent(user) {
p.Data["json"] = Response{
Code: 403,
Msg: "非法用户",
}
p.ServeJSON()
return
}
url := p.GetString("url")
fileName := p.GetString("name")
sid, err := p.GetInt64(":submitId")
fid, err := p.GetInt64(":fileId")
filePath := p.GetString("filePath")
r := &models.SubmitFile{
Id: fid,
SubmitId: sid,
Name: fileName,
FilePath: filePath,
Url: url,
}
err = r.Update()
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Msg: "上传成功",
}
}
p.ServeJSON()
}
// DeleteSubmitFile
// @Title
// @Description
// @Param filePath body string true ""
// @Success 200 {object} models.TeacherProject
// @Failure 400
// @router /:projectId/task/:taskId/submit/:submitId/file/:fileId/delete [post]
func (p *ProjectController) DeleteSubmitFile() {
user := p.GetSessionUser()
if !util.IsStudent(user) {
p.Data["json"] = Response{
Code: 403,
Msg: "非法用户",
}
p.ServeJSON()
return
}
fid := p.GetString(":fileId")
err := models.DeleteSubmitFile(fid)
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Msg: "删除成功",
}
}
p.ServeJSON()
}
// UpdateSubmit
// @Title
// @Description
@ -95,7 +229,6 @@ func (p *ProjectController) UpdateSubmit() {
SubmitTitle: p.GetString("submitTitle"),
SubmitIntroduce: p.GetString("submitIntroduce"),
SubmitContent: p.GetString("submitContent"),
FilePath: p.GetString("filePath"),
CreateAt: time.Now(),
Score: score,
Scored: scored,
@ -118,3 +251,29 @@ func (p *ProjectController) UpdateSubmit() {
}
p.ServeJSON()
}
// GetSubmitFiles
// @Title
// @Description
// @Param body body models.Submit true ""
// @Success 200 {object}
// @Failure 403 body is empty
// @router /:projectId/task/:taskId/submit/:submitId/files [get]
func (p *ProjectController) GetSubmitFiles() {
sid := p.GetString(":submitId")
files, err := models.GetSubmitFiles(sid)
if err != nil {
p.Data["json"] = Response{
Code: 400,
Msg: err.Error(),
}
} else {
p.Data["json"] = Response{
Code: 200,
Msg: "提交成功",
Data: files,
}
}
p.ServeJSON()
}

2
go.mod
View File

@ -8,7 +8,7 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 // indirect
xorm.io/xorm v1.1.2
)

11
go.sum
View File

@ -337,8 +337,8 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@ -393,15 +393,16 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

View File

@ -91,6 +91,9 @@ func (a *Adapter) createTable() {
new(Chapter),
new(Section),
new(SectionFile),
new(SubmitFile),
new(Resource),
new(Task),
new(Survey),

View File

@ -15,6 +15,14 @@ type Section struct {
SectionMinute int `json:"sectionMinute" xorm:"default 1"`
}
type SectionFile struct {
Id int64 `json:"id" xorm:"not null pk autoincr"`
SectionId int64 `json:"sectionId" xorm:"not null index"`
FilePath string `json:"filePath"`
Name string `json:"name"`
Url string `json:"url"`
}
type SectionMinute struct {
Section `xorm:"extends"`
LearnSection `xorm:"extends"`
@ -33,6 +41,31 @@ type SectionOutline struct {
func (p *Section) GetEngine() *xorm.Session {
return adapter.Engine.Table(p)
}
func (p *SectionFile) GetEngine() *xorm.Session {
return adapter.Engine.Table(p)
}
func (p *SectionFile) Create() (err error) {
_, err = p.GetEngine().Insert(p)
return
}
func (p *SectionFile) Update() (err error) {
_, err = p.GetEngine().ID(p.Id).Update(p)
return
}
func GetSectionFiles(sid string) (files []SectionFile, err error) {
err = (&SectionFile{}).GetEngine().
Where("section_id = ?", sid).
Find(&files)
return
}
func DeleteSectionFile(fid string) (err error) {
_, err = (&SectionFile{}).GetEngine().ID(fid).Delete(&SectionFile{})
return
}
func (p *Section) Create() (err error) {
session := adapter.Engine.NewSession()

View File

@ -20,13 +20,20 @@ type Submit struct {
SubmitIntroduce string `json:"submitIntroduce" xorm:"text"`
SubmitContent string `json:"submitContent" xorm:"text"`
FilePath string `json:"filePath"`
CreateAt time.Time `json:"createAt"`
Score int `json:"score" xorm:"default 0"`
Scored bool `json:"scored" xorm:"default false"`
}
type SubmitFile struct {
Id int64 `json:"id" xorm:"not null pk autoincr"`
SubmitId int64 `json:"submitId" xorm:"not null index"`
FilePath string `json:"filePath"`
Name string `json:"name"`
Url string `json:"url"`
}
type Choice struct {
Id int64 `json:"id" xorm:"not null pk autoincr"`
SubmitId int64 `json:"submitId" xorm:"not null index"`
@ -44,6 +51,31 @@ type SubmitDetail struct {
func (p *Submit) GetEngine() *xorm.Session {
return adapter.Engine.Table(p)
}
func (p *SubmitFile) GetEngine() *xorm.Session {
return adapter.Engine.Table(p)
}
func (p *SubmitFile) Create() (err error) {
_, err = p.GetEngine().Insert(p)
return
}
func (p *SubmitFile) Update() (err error) {
_, err = p.GetEngine().ID(p.Id).Update(p)
return
}
func GetSubmitFiles(sid string) (files []SectionFile, err error) {
err = (&SubmitFile{}).GetEngine().
Where("submit_id = ?", sid).
Find(&files)
return
}
func DeleteSubmitFile(fid string) (err error) {
_, err = (&SubmitFile{}).GetEngine().ID(fid).Delete(&SubmitFile{})
return
}
func (c *Choice) GetEngine() *xorm.Session {
return adapter.Engine.Table(c)
}

View File

@ -13,7 +13,6 @@ func apiFilter(ctx *context.Context) {
if strings.HasPrefix(urlPath, "/api/project-list") ||
strings.HasPrefix(urlPath, "/api/project") ||
strings.HasPrefix(urlPath, "/api/message") ||
strings.HasPrefix(urlPath, "/api/resource") ||
strings.HasPrefix(urlPath, "/api/student") {
user := ctx.Input.Session("user")
if user == nil {

View File

@ -23,10 +23,6 @@ func init() {
beego.NSInclude(
&controllers.ProjectController{})),
beego.NSNamespace("/resource",
beego.NSInclude(
&controllers.ResourceController{})),
beego.NSNamespace("/project-list",
beego.NSInclude(
&controllers.ProjectListController{})),