// Create creates a new topic func (*TopicsController) Create(ctx *gin.Context) { var topicIn tat.TopicCreateJSON ctx.Bind(&topicIn) var user = tat.User{} found, err := userDB.FindByUsername(&user, getCtxUsername(ctx)) if !found { ctx.JSON(http.StatusUnauthorized, gin.H{"error": "User unknown"}) return } else if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."}) return } var topic tat.Topic topic.Topic = topicIn.Topic topic.Description = topicIn.Description err = topicDB.Insert(&topic, &user) if err != nil { log.Errorf("Error while InsertTopic %s", err) ctx.JSON(tat.Error(err)) return } ctx.JSON(http.StatusCreated, topic) }
func insertTopicDM(userFrom, userTo tat.User) (tat.Topic, error) { var topic = tat.Topic{} topicName := "/Private/" + userFrom.Username + "/DM/" + userTo.Username topic.Topic = topicName topic.Description = userTo.Fullname if err := topicDB.Insert(&topic, &userFrom); err != nil { log.Errorf("Error while InsertTopic %s", err) return topic, err } return topic, nil }