예제 #1
0
func CreateApplicationGuide(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	contType := req.Header.Get("Content-Type")

	var ag applicationGuide.ApplicationGuide
	var err error

	// if contType == "application/json" {
	if strings.Contains(contType, "application/json") {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Error reading request body", err, rw, req)
		}

		err = json.Unmarshal(requestBody, &ag)
		if err != nil {
			apierror.GenerateError("Error decoding request body", err, rw, req)
		}
	} else {
		//else, form
		ag.Url = req.FormValue("url")
		web := req.FormValue("website_id")
		ag.FileType = req.FormValue("file_type")
		cat := req.FormValue("category_id")

		if err != nil {
			apierror.GenerateError("Error parsing form", err, rw, req)
		}
		if web != "" {
			ag.Website.ID, err = strconv.Atoi(web)
		}
		if cat != "" {
			ag.Category.CategoryID, err = strconv.Atoi(cat)
		}
		if err != nil {
			apierror.GenerateError("Error parsing category ID or website ID", err, rw, req)
		}
	}
	err = ag.Create(dtx)
	if err != nil {
		apierror.GenerateError("Error creating Application Guide", err, rw, req)
	}

	//Return JSON
	return encoding.Must(enc.Encode(ag))
}