|
9 | 9 | "database/sql" |
10 | 10 | "fmt" |
11 | 11 | "net/http" |
| 12 | + "strings" |
12 | 13 |
|
13 | 14 | "github.com/gin-contrib/sessions" |
14 | 15 | "github.com/gin-contrib/sessions/cookie" |
@@ -707,6 +708,61 @@ func (ct *Controller) voteAnswer(c *gin.Context) { |
707 | 708 |
|
708 | 709 | } |
709 | 710 |
|
| 711 | +// exportRetrospective godoc |
| 712 | +// |
| 713 | +// @Summary Export Retrospective |
| 714 | +// @Tags Retrospective |
| 715 | +// @Accept json |
| 716 | +// @Produce json |
| 717 | +// @Produce text/markdown |
| 718 | +// @Param export body types.RetrospectiveExportRequest true "Export Retrospective" |
| 719 | +// @Success 200 {object} types.Retrospective "Retrospective Object (JSON) or Markdown file" |
| 720 | +// @Failure 400 {string} string "Invalid input" |
| 721 | +// @Failure 404 {string} string "Not Found" |
| 722 | +// @Failure 500 {string} string "Internal error" |
| 723 | +// @Router /retrospective/export [post] |
| 724 | +func (ct *Controller) exportRetrospective(c *gin.Context) { |
| 725 | + var input types.RetrospectiveExportRequest |
| 726 | + if err := c.BindJSON(&input); err != nil { |
| 727 | + ct.logger.Error("error parsing body content", zap.Error(err)) |
| 728 | + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body content"}) |
| 729 | + return |
| 730 | + } |
| 731 | + |
| 732 | + if err := input.Validate(); err != nil { |
| 733 | + ct.logger.Error("invalid input", zap.Error(err)) |
| 734 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 735 | + return |
| 736 | + } |
| 737 | + |
| 738 | + retro, err := ct.service.GetRetrospective(c, input.RetrospectiveID) |
| 739 | + if err == sql.ErrNoRows { |
| 740 | + ct.logger.Error("retrospective not found", zap.String("id", input.RetrospectiveID.String())) |
| 741 | + c.JSON(http.StatusNotFound, gin.H{"error": "restrospective not found"}) |
| 742 | + return |
| 743 | + } |
| 744 | + |
| 745 | + if err != nil { |
| 746 | + ct.logger.Error("error getting retrospective", zap.Error(err)) |
| 747 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"}) |
| 748 | + return |
| 749 | + } |
| 750 | + |
| 751 | + filename := fmt.Sprintf("retrospective-%s", strings.ReplaceAll(retro.Name, " ", "_")) |
| 752 | + switch input.ExportType { |
| 753 | + case types.ExportTypeMarkdown: |
| 754 | + |
| 755 | + markdown := ct.service.ConvertRetrospectiveToMarkdown(c, retro) |
| 756 | + c.Header("Content-Type", "text/markdown") |
| 757 | + c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.md\"", filename)) |
| 758 | + c.String(http.StatusOK, markdown) |
| 759 | + default: |
| 760 | + c.Header("Content-Type", "application/json") |
| 761 | + c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.json\"", filename)) |
| 762 | + c.JSON(http.StatusOK, retro) |
| 763 | + } |
| 764 | +} |
| 765 | + |
710 | 766 | // @license.name MIT |
711 | 767 | // @license.url https://github.com/simple-retro/api/blob/master/LICENSE |
712 | 768 | func (ct *Controller) Start() { |
@@ -739,6 +795,7 @@ func (ct *Controller) Start() { |
739 | 795 | api.GET("/retrospective/:id", ct.getRetrospective) |
740 | 796 | api.PATCH("/retrospective/:id", ct.updateRetrospective) |
741 | 797 | api.DELETE("/retrospective/:id", ct.deleteRetrospective) |
| 798 | + api.POST("/retrospective/export", ct.exportRetrospective) |
742 | 799 | api.GET("/hello/:id", ct.subscribeChanges) |
743 | 800 | api.GET("/limits", ct.getLimits) |
744 | 801 |
|
|
0 commit comments