Exemple #1
0
func ForkSnippetById(resp http.ResponseWriter, req *http.Request) {
	routeVariables := mux.Vars(req)
	snippetId := routeVariables["snippet_id"]
	session, _ := globalStore.Get(req, "session-name")
	session.Save(req, resp)
	resp = setSessionIDAsCookie(resp, session.ID)
	snippet, err := models.FindSnippetById(snippetId)
	snippet.SessionId = session.ID
	newSnippetId, ok, err := models.CreateSnippet(snippet)
	enc := json.NewEncoder(resp)
	if err != nil {
		enc.Encode(routeinit.ApiResponse{ErrorMessage: err.Error(), Status: ok})
	} else {
		ResponseResult := SnippetCreationResponse{newSnippetId}
		enc.Encode(routeinit.ApiResponse{Result: &ResponseResult, Status: ok})
	}
	return
}
Exemple #2
0
func CreateNewSnippet(resp http.ResponseWriter, req *http.Request) {
	validated, _, enc, body := routeinit.InitHandling(req, resp, []string{
		"language_code",
	})
	if !validated {
		return
	}
	session, _ := globalStore.Get(req, "session-name")
	session.Save(req, resp)
	resp = setSessionIDAsCookie(resp, session.ID)
	language := body["language_code"].(string)
	templateSnippet, _ := template.GetTemplateByLanguage(language)
	templateSnippet.Tags = []string{language}
	templateSnippet.SessionId = session.ID
	snippetId, ok, err := models.CreateSnippet(templateSnippet)
	if err != nil {
		enc.Encode(routeinit.ApiResponse{ErrorMessage: err.Error(), Status: ok})
	} else {
		ResponseResult := SnippetCreationResponse{snippetId}
		enc.Encode(routeinit.ApiResponse{Result: &ResponseResult, Status: ok})
	}
	return
}