Save content form on gist create error (#420)
This commit is contained in:
@@ -567,6 +567,32 @@ func (gist *Gist) TopicsSlice() []string {
|
||||
return topics
|
||||
}
|
||||
|
||||
func (gist *Gist) ToDTO() (*GistDTO, error) {
|
||||
files, err := gist.Files("HEAD", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileDTOs := make([]FileDTO, 0, len(files))
|
||||
for _, file := range files {
|
||||
fileDTOs = append(fileDTOs, FileDTO{
|
||||
Filename: file.Filename,
|
||||
Content: file.Content,
|
||||
})
|
||||
}
|
||||
|
||||
return &GistDTO{
|
||||
Title: gist.Title,
|
||||
Description: gist.Description,
|
||||
URL: gist.URL,
|
||||
Files: fileDTOs,
|
||||
VisibilityDTO: VisibilityDTO{
|
||||
Private: gist.Private,
|
||||
},
|
||||
Topics: strings.Join(gist.TopicsSlice(), " "),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// -- DTO -- //
|
||||
|
||||
type GistDTO struct {
|
||||
@@ -580,6 +606,10 @@ type GistDTO struct {
|
||||
VisibilityDTO
|
||||
}
|
||||
|
||||
func (dto *GistDTO) HasMetadata() bool {
|
||||
return dto.Title != "" || dto.Description != "" || dto.URL != "" || dto.Topics != ""
|
||||
}
|
||||
|
||||
type VisibilityDTO struct {
|
||||
Private Visibility `validate:"number,min=0,max=2" form:"private"`
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ func ProcessCreate(ctx *context.Context) error {
|
||||
Content: escapedValue,
|
||||
})
|
||||
}
|
||||
ctx.SetData("dto", dto)
|
||||
|
||||
err = ctx.Validate(dto)
|
||||
if err != nil {
|
||||
|
||||
@@ -10,12 +10,12 @@ import (
|
||||
func Edit(ctx *context.Context) error {
|
||||
gist := ctx.GetData("gist").(*db.Gist)
|
||||
|
||||
files, err := gist.Files("HEAD", false)
|
||||
gistDto, err := gist.ToDTO()
|
||||
if err != nil {
|
||||
return ctx.ErrorRes(500, "Error fetching files from repository", err)
|
||||
return ctx.ErrorRes(500, "Error getting gist data", err)
|
||||
}
|
||||
|
||||
ctx.SetData("files", files)
|
||||
ctx.SetData("dto", gistDto)
|
||||
ctx.SetData("htmlTitle", ctx.TrH("gist.edit.edit-gist", gist.Title))
|
||||
|
||||
return ctx.Html("edit.html")
|
||||
|
||||
@@ -49,7 +49,7 @@ func (s *Server) registerMiddlewares() {
|
||||
return nil
|
||||
},
|
||||
}))
|
||||
s.echo.Use(middleware.Recover())
|
||||
//s.echo.Use(middleware.Recover())
|
||||
s.echo.Use(middleware.Secure())
|
||||
s.echo.Use(Middleware(sessionInit).toEcho())
|
||||
|
||||
@@ -79,9 +79,9 @@ func (s *Server) registerMiddlewares() {
|
||||
|
||||
func (s *Server) errorHandler(err error, ctx echo.Context) {
|
||||
var httpErr *echo.HTTPError
|
||||
data := ctx.Request().Context().Value(context.DataKeyStr).(echo.Map)
|
||||
if errors.As(err, &httpErr) {
|
||||
acceptJson := strings.Contains(ctx.Request().Header.Get("Accept"), "application/json")
|
||||
data := ctx.Request().Context().Value(context.DataKeyStr).(echo.Map)
|
||||
data["error"] = err
|
||||
if acceptJson {
|
||||
if err := ctx.JSON(httpErr.Code, httpErr); err != nil {
|
||||
@@ -96,7 +96,12 @@ func (s *Server) errorHandler(err error, ctx echo.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Fatal().Err(err).Send()
|
||||
log.Error().Err(err).Send()
|
||||
httpErr = echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
data["error"] = httpErr
|
||||
if err := ctx.Render(500, "error", data); err != nil {
|
||||
log.Fatal().Err(err).Send()
|
||||
}
|
||||
}
|
||||
|
||||
func dataInit(next Handler) Handler {
|
||||
|
||||
Reference in New Issue
Block a user