Exemple #1
0
// PATCH /repos/:repo_id/reviews/:review_id
func (r *reviews) Update(c *gin.Context) {
	type updateParams struct {
		Smell      string `json:"smell"`
		Status     string `json:"status"`
		AssignedTo uint   `json:"assigned_to"`
	}

	var params updateParams
	review := c.MustGet("review").(model.Review)
	db := database.GetDB()

	if c.Bind(&params) == nil {
		updates := model.Review{}

		if params.Smell != "" {
			var smell model.Smell
			if err := db.Where("name = ?", params.Smell).Find(&smell).Error; err != nil {
				c.AbortWithError(http.StatusBadRequest, errors.New("Invalid smell"))
			}

			updates.SmellId = smell.ID
		}
		if params.Status != "" {
			var status model.Status
			if err := db.Where("name = ?", params.Status).Find(&status).Error; err != nil {
				c.AbortWithError(http.StatusBadRequest, errors.New("Invalid status"))
			}

			updates.StatusId = status.ID
		}
		if params.AssignedTo > 0 {
			updates.AssignedToId = params.AssignedTo
		}

		if c.IsAborted() {
			return
		} else if updates.SmellId == 0 && updates.StatusId == 0 && updates.AssignedToId == 0 {
			c.String(http.StatusNotModified, "")
		} else {
			if err := db.Model(&review).Updates(updates).Error; err != nil {
				c.AbortWithError(http.StatusInternalServerError, err)
			} else {
				c.String(http.StatusNoContent, "")
			}
		}
	}
}
Exemple #2
0
// POST /repos/:repo_id/reviews
func (r *reviews) Create(c *gin.Context) {
	var params createParam
	user := c.MustGet("user").(model.User)
	repo := c.MustGet("repo").(model.Repository)
	db := database.GetDB()

	if c.Bind(&params) == nil {
		review := model.Review{}

		if params.Smell == "" {
			params.Smell = smellDefault
		}

		var smell model.Smell
		var status model.Status

		review.RepositoryId = repo.ID
		review.CreatedById = user.ID
		review.Sha = params.Sha
		review.File = params.File
		review.Line = params.Line
		review.AssignedToId = params.AssignedToId
		if err := db.Where("name = ?", params.Smell).Find(&smell).Error; err != nil {
			c.AbortWithError(http.StatusBadRequest, errors.New("Invalid smell"))
		} else {
			review.SmellId = smell.ID
			review.Smell = smell
		}

		if err := db.Where("name = ?", statusDefault).Find(&status).Error; err != nil {
			c.AbortWithError(http.StatusBadRequest, errors.New("Invalid status"))
		} else {
			review.StatusId = status.ID
			review.Status = status
		}

		if c.IsAborted() {
			return
		}
		if err := db.Create(&review).Error; err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
		} else {
			c.JSON(http.StatusCreated, review)
		}
	}
}