From 471a7420d5e732428ed2d258d187f22030db49f5 Mon Sep 17 00:00:00 2001 From: colorfulberry Date: Fri, 22 May 2020 16:48:49 +0800 Subject: [PATCH] feat: add the pages --- models/dataset.go | 22 ++++++++++++- models/error.go | 8 +++++ modules/auth/dataset.go | 11 +++++++ routers/dataset/dataset.go | 63 ++++++++++++++++++++++++++++++++++++++ routers/repo/attachment.go | 4 +++ routers/routes/routes.go | 2 ++ 6 files changed, 109 insertions(+), 1 deletion(-) diff --git a/models/dataset.go b/models/dataset.go index 0fdbea3e1..721ef3380 100644 --- a/models/dataset.go +++ b/models/dataset.go @@ -32,7 +32,7 @@ func CreateDataset(dataset *Dataset) (err error) { return nil } -// AddReleaseAttachments adds a release attachments +// AddDatasetAttachments adds a Dataset attachments func AddDatasetAttachments(DatasetID int64, attachmentUUIDs []string) (err error) { // Check attachments attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs) @@ -50,3 +50,23 @@ func AddDatasetAttachments(DatasetID int64, attachmentUUIDs []string) (err error return } + +// GetDatasetByID returns Dataset with given ID. +func GetDatasetByID(id int64) (*Dataset, error) { + rel := new(Dataset) + has, err := x. + ID(id). + Get(rel) + if err != nil { + return nil, err + } else if !has { + return nil, ErrDatasetNotExist{id} + } + + return rel, nil +} + +func UpdateDataset(ctx DBContext, rel *Dataset) error { + _, err := ctx.e.ID(rel.ID).AllCols().Update(rel) + return err +} diff --git a/models/error.go b/models/error.go index 3b05a7152..7fd61303f 100644 --- a/models/error.go +++ b/models/error.go @@ -851,6 +851,14 @@ func IsErrInvalidTagName(err error) bool { return ok } +type ErrDatasetNotExist struct { + ID int64 +} + +func (err ErrDatasetNotExist) Error() string { + return fmt.Sprintf("Dataset does not exist [id: %d]", err.ID) +} + func (err ErrInvalidTagName) Error() string { return fmt.Sprintf("release tag name is not valid [tag_name: %s]", err.TagName) } diff --git a/modules/auth/dataset.go b/modules/auth/dataset.go index c894fdd28..0b9cc3aa4 100644 --- a/modules/auth/dataset.go +++ b/modules/auth/dataset.go @@ -16,6 +16,17 @@ type CreateDatasetForm struct { Files []string } +type EditDatasetForm struct { + ID int64 `binding:"Required"` + Title string `binding:"Required"` + Category string `binding:"Required"` + Description string `binding:"Required;MaxSize(254)"` + License string `binding:"Required;MaxSize(64)"` + Task string `binding:"Required;MaxSize(64)"` + ReleaseID int64 `xorm:"INDEX"` + Files []string +} + // Validate validates the fields func (f *CreateDatasetForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { return validate(errs, ctx.Data, f, ctx.Locale) diff --git a/routers/dataset/dataset.go b/routers/dataset/dataset.go index 359189eda..ab279101a 100644 --- a/routers/dataset/dataset.go +++ b/routers/dataset/dataset.go @@ -7,6 +7,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/routers/repo" ) const ( @@ -92,6 +93,68 @@ func CreatePost(ctx *context.Context, form auth.CreateDatasetForm) { // handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form) } +// EditRelease render release edit page +func EditDataset(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("repo.dataset.edit_release") + ctx.Data["PageIsEditDataset"] = true + repo.RenderAttachmentSettings(ctx) + rel, err := models.GetDatasetByID(ctx.ParamsInt64(":id")) + if err != nil { + ctx.ServerError("GetDataset", err) + return + } + ctx.Data["ID"] = rel.ID + ctx.Data["title"] = rel.Title + ctx.Data["description"] = rel.Description + ctx.Data["category"] = rel.Category + ctx.Data["task"] = rel.Task + ctx.Data["license"] = rel.License + + ctx.HTML(200, tplCreate) +} + +func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) { + ctx.Data["Title"] = ctx.Tr("repo.dataset.edit_release") + ctx.Data["PageIsEditDataset"] = true + + rel, err := models.GetDatasetByID(ctx.ParamsInt64(":id")) + if err != nil { + ctx.ServerError("GetDataset", err) + return + } + ctx.Data["ID"] = rel.ID + ctx.Data["title"] = rel.Title + ctx.Data["description"] = rel.Description + ctx.Data["category"] = rel.Category + ctx.Data["task"] = rel.Task + ctx.Data["license"] = rel.License + + if ctx.HasError() { + ctx.HTML(200, tplCreate) + return + } + + var attachmentUUIDs []string + if setting.Attachment.Enabled { + attachmentUUIDs = form.Files + } + + rel.Title = form.Title + rel.Description = form.Description + rel.Category = form.Category + rel.Task = form.Task + rel.License = form.License + if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil { + log.Error("%v", err) + } + + if err = models.AddDatasetAttachments(rel.ID, attachmentUUIDs); err != nil { + log.Error("%v", err) + } + + ctx.Redirect(setting.AppSubURL + "/datasets") +} + func Delete(ctx *context.Context) { log.Debug("[dataset] Delete...\n") } diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go index 479e8b395..100572de7 100644 --- a/routers/repo/attachment.go +++ b/routers/repo/attachment.go @@ -17,6 +17,10 @@ import ( "code.gitea.io/gitea/modules/upload" ) +func RenderAttachmentSettings(ctx *context.Context) { + renderAttachmentSettings(ctx) +} + func renderAttachmentSettings(ctx *context.Context) { ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled ctx.Data["AttachmentStoreType"] = setting.Attachment.StoreType diff --git a/routers/routes/routes.go b/routers/routes/routes.go index aca964d9f..3e15b8432 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -1006,6 +1006,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/create", dataset.Create) m.Post("/create", bindIgnErr(auth.CreateDatasetForm{}), dataset.CreatePost) m.Post("/delete", dataset.Delete) + m.Get("/edit/:id", dataset.EditDataset) + m.Post("/edit/:id", bindIgnErr(auth.EditDatasetForm{}), dataset.EditDatasetPost) }, ignSignIn) // ***** END: DataSet***** m.Group("/notifications", func() {