Beispiel #1
0
// DeleteFile deletes a file.
func DeleteFile(c *gin.Context) (int, error) {
	log.Debug("deleteFile performed")
	var targetFile model.File
	id := c.Params.ByName("id")
	if db.ORM.First(&targetFile, id).RecordNotFound() {
		return http.StatusNotFound, errors.New("File is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, targetFile.UserId)
	if err != nil {
		return status, err
	}
	switch config.UploadTarget {
	case "S3":
		s3UploadPath := config.UploadS3Path + strconv.FormatInt(targetFile.UserId, 10) + "/"
		log.Debugf("s3UploadPath %s", s3UploadPath)
		err = aws.DelFromMyBucket(s3UploadPath, targetFile.Name)
		if err != nil {
			return http.StatusInternalServerError, err
		}
	case "LOCAL":
		err = file.DeleteLocal(targetFile.Name)
		if err != nil {
			return http.StatusInternalServerError, err
		}
	}

	if db.ORM.Delete(&targetFile).Delete(targetFile).Error != nil {
		return http.StatusInternalServerError, errors.New("File is not deleted.")
	}
	return http.StatusOK, nil
}
Beispiel #2
0
// DeleteLocation deletes a location.
func DeleteLocation(c *gin.Context) (int, error) {
	var location model.Location
	id := c.Params.ByName("id")
	if db.ORM.First(&location, id).RecordNotFound() {
		return http.StatusNotFound, errors.New("Location is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, location.UserId)
	if err != nil {
		return status, err
	}
	if db.ORM.Delete(&location).Error != nil {
		return http.StatusBadRequest, errors.New("Location is not deleted.")
	}
	return http.StatusOK, nil
}
Beispiel #3
0
// DeleteArticle deletes an article.
func DeleteArticle(c *gin.Context) (int, error) {
	var article model.Article
	id := c.Params.ByName("id")
	if db.ORM.First(&article, id).RecordNotFound() {
		return http.StatusNotFound, errors.New("Article is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, article.UserId)
	if err != nil {
		return status, err
	}
	if db.ORM.Delete(&article).Error != nil {
		return http.StatusBadRequest, errors.New("Article is not deleted.")
	}
	return http.StatusOK, nil
}
Beispiel #4
0
// CreateComment creates a comment.
func CreateComment(c *gin.Context, item interface{}) (int, error) {
	var form CreateCommentForm
	var comment model.Comment
	c.BindWith(&form, binding.Form)
	log.Debugf("comment_form : %v", form)
	status, err := userPermission.CurrentUserIdentical(c, form.UserId)
	if err != nil {
		return status, err
	}
	if db.ORM.First(item, form.ParentId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Item is not found.")
	}
	modelHelper.AssignValue(&comment, &form)
	db.ORM.Model(item).Association("Comments").Append(comment)
	return http.StatusOK, nil
}
Beispiel #5
0
// CreateLiking create a liking.
func CreateLiking(c *gin.Context, item interface{}) (int, error) {
	var form CreateLikingForm
	var likingUser model.User
	c.BindWith(&form, binding.Form)
	log.Debugf("liking_form : %v", form)
	if db.ORM.First(item, form.ParentId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Item is not found.")
	}
	if db.ORM.First(&likingUser, form.UserId).RecordNotFound() {
		return http.StatusNotFound, errors.New("User is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, likingUser.Id)
	if err != nil {
		return status, err
	}
	db.ORM.Model(item).Association("Likings").Append(likingUser)
	return http.StatusOK, nil
}
Beispiel #6
0
// UpdateFile updates a file.
func UpdateFile(c *gin.Context) (model.File, int, error) {
	var file model.File
	var form FileForm
	id := c.Params.ByName("id")
	c.BindWith(&form, binding.Form)
	if db.ORM.First(&file, id).RecordNotFound() {
		return file, http.StatusNotFound, errors.New("File is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, file.UserId)
	if err != nil {
		return file, status, err
	}
	file.Name = form.Name
	file.Size = form.Size
	if db.ORM.Save(&file).Error != nil {
		return file, http.StatusInternalServerError, errors.New("File is not updated.")
	}
	return file, http.StatusOK, nil
}
Beispiel #7
0
// UpdateComment updates a comment.
func UpdateComment(c *gin.Context, item interface{}) (int, error) {
	var form CommentForm
	var comment model.Comment
	c.BindWith(&form, binding.Form)
	if db.ORM.First(item, form.ParentId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Item is not found.")
	}
	if db.ORM.First(&comment, form.CommentId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Comment is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, comment.UserId)
	if err != nil {
		return status, err
	}
	comment.Content = form.Content
	if db.ORM.Save(&comment).Error != nil {
		return http.StatusInternalServerError, errors.New("Article comment's count is not updated.")
	}
	return http.StatusOK, nil
}
Beispiel #8
0
// DeleteComment deletes a comment.
func DeleteComment(c *gin.Context, item interface{}) (int, error) {
	itemId := c.Params.ByName("id")
	commentId := c.Params.ByName("commentId")
	var comment model.Comment
	log.Debugf("item id : %d , comment id : %d \n", itemId, commentId)
	if db.ORM.First(item, itemId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Item is not found.")
	}
	if db.ORM.First(&comment, commentId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Comment is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, comment.UserId)
	if err != nil {
		return status, err
	}
	db.ORM.Model(item).Association("Comments").Delete(comment)
	if db.ORM.Delete(&comment).Error != nil {
		return http.StatusInternalServerError, errors.New("Comment is not deleted.")
	}
	return http.StatusOK, nil
}
Beispiel #9
0
// UpdateArticle updates an article.
func UpdateArticle(c *gin.Context) (model.Article, int, error) {
	var article model.Article
	var form ArticleForm
	id := c.Params.ByName("id")
	c.BindWith(&form, binding.Form)
	if db.ORM.First(&article, id).RecordNotFound() {
		return article, http.StatusNotFound, errors.New("Article is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, article.UserId)
	if err != nil {
		return article, status, err
	}
	article.Title = form.Title
	article.Url = form.Url
	article.ImageName = form.ImageName
	article.Content = form.Content
	if db.ORM.Save(&article).Error != nil {
		return article, http.StatusBadRequest, errors.New("Article is not updated.")
	}
	return article, http.StatusOK, nil
}
Beispiel #10
0
// DeleteLiking deletes liking.
func DeleteLiking(c *gin.Context, item interface{}) (int, error) {
	itemId := c.Params.ByName("id")
	userId := c.Params.ByName("userId")
	var likingUser model.User
	log.Debugf("item id : %d , user id : %d \n", itemId, userId)
	if db.ORM.First(item, itemId).RecordNotFound() {
		return http.StatusNotFound, errors.New("Item is not found.")
	}
	if db.ORM.First(&likingUser, userId).RecordNotFound() {
		return http.StatusNotFound, errors.New("User is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, likingUser.Id)
	if err != nil {
		return status, err
	}
	db.ORM.Model(item).Association("Likings").Delete(likingUser)
	likingUserCount := db.ORM.Where("id = ?", likingUser.Id).Model(item).Association("Likings").Count()
	if likingUserCount != 0 {
		return http.StatusInternalServerError, errors.New("Article liking's is not deleted.")
	}
	return http.StatusOK, nil
}
Beispiel #11
0
// UpdateLocation updates a location.
func UpdateLocation(c *gin.Context) (model.Location, int, error) {
	var location model.Location
	var form LocationForm
	id := c.Params.ByName("id")
	c.BindWith(&form, binding.Form)
	if db.ORM.First(&location, id).RecordNotFound() {
		return location, http.StatusNotFound, errors.New("Location is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, location.UserId)
	if err != nil {
		return location, status, err
	}
	location.Name = form.Name
	location.Address = form.Address
	location.Latitude = form.Latitude
	location.Longitude = form.Longitude
	location.Url = form.Url
	location.Content = form.Content
	if db.ORM.Save(&location).Error != nil {
		return location, http.StatusBadRequest, errors.New("Location is not updated.")
	}
	return location, http.StatusOK, nil
}