Example #1
0
func (like *Like) deleteLike(request *restful.Request, response *restful.Response) {

	like.post.Id = like.PostId
	if like.post.GetPostById() == false {
		response.WriteHeader(http.StatusInternalServerError)
		helpers.PrintLog(request, response, like.user.Name)
		return
	}
	if err := like.post.UpdateLikeNb(-1); err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		helpers.PrintLog(request, response, like.user.Name)
		return
	}
	_, err := r.Table("likes").Get(like.Id).Delete().Run(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		helpers.PrintLog(request, response, like.user.Name)
		return
	}
	go func() {
		notif := notification.SetLikeReceive()
		notif.IdThing = like.Id
		notif.UserIdFrom = like.user.Id
		notif.RemoveNotification()
	}()

	response.WriteHeader(http.StatusOK)
	helpers.PrintLog(request, response, like.user.Name)
}
Example #2
0
func (like *Like) createLike(request *restful.Request, response *restful.Response) {
	newLike := new(Like)

	newLike.PostId = like.post.Id
	newLike.UserId = like.user.Id
	newLike.Created = time.Now()
	if err := like.post.UpdateLikeNb(1); err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		return
	}
	resp, err := r.Table("likes").Insert(newLike).RunWrite(api.Sess)
	if err != nil {
		like.post.UpdateLikeNb(-1)
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		helpers.PrintLog(request, response, like.user.Name)
		return
	}

	if like.post.UserId != like.user.Id {
		go func() {
			notif := notification.SetLikeReceive()
			notif.UserId = like.post.UserId
			notif.UserIdFrom = like.user.Id
			notif.IdThing = resp.GeneratedKeys[0]
			notif.Name = like.user.Name
			notif.IdLink = like.post.Id
			notif.CreateNotification()
		}()
	}

	newLike.Id = resp.GeneratedKeys[0]
	response.WriteHeaderAndEntity(http.StatusCreated, newLike)

	helpers.PrintLog(request, response, like.user.Name)
}