コード例 #1
0
ファイル: topic_channel.go プロジェクト: gitter-badger/khabar
func (self *TopicChannel) Post(request *gottp.Request) {
	channelIdent := request.GetArgument("channel").(string)
	if !core.IsChannelAvailable(channelIdent) {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Channel is not supported",
		})

		return
	}

	intopic := new(db.Topic)
	request.ConvertArguments(intopic)

	err := topics.AddChannel(intopic.Ident, channelIdent, intopic.User, intopic.Organization)
	if err != nil {
		request.Raise(gottp.HttpError{http.StatusBadRequest, err.Error()})
		return
	}

	request.Write(utils.R{
		Data:       nil,
		Message:    "true",
		StatusCode: http.StatusNoContent,
	})

	return
}
コード例 #2
0
ファイル: gully.go プロジェクト: gitter-badger/khabar
func (self *Gully) Post(request *gottp.Request) {

	inputGully := new(db.Gully)
	request.ConvertArguments(inputGully)
	inputGully.PrepareSave()

	log.Println("Input :", inputGully)

	if !core.IsChannelAvailable(inputGully.Ident) {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Channel is not supported",
		})

		return
	}

	if !inputGully.IsValid(db.INSERT_OPERATION) {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Atleast one of the user, org and app_name must be present.",
		})

		return
	}

	if !utils.ValidateAndRaiseError(request, inputGully) {
		return
	}

	gly, err := gully.Get(
		inputGully.User,
		inputGully.AppName,
		inputGully.Organization,
		inputGully.Ident,
	)

	if err != nil {
		if err != mgo.ErrNotFound {
			log.Println(err)
			request.Raise(gottp.HttpError{
				http.StatusInternalServerError,
				"Unable to fetch data, Please try again later.",
			})
			return
		}
	}

	if gly != nil {
		request.Raise(gottp.HttpError{
			http.StatusConflict,
			"Channel already exists",
		})

		return
	}

	gully.Insert(inputGully)

	log.Println("Saving :", inputGully)

	request.Write(utils.R{
		StatusCode: http.StatusCreated,
		Data:       inputGully.Id,
		Message:    "Created",
	})

	return
}