示例#1
0
func deleteBlogHandler(res *wcg.Response, req *wcg.Request) {
	driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
	err := driver.DeleteBlog(req.Param("blog_id"), req.User.Id())
	if err != nil {
		if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else if err == models.ErrBlogNotOwned {
			res.WriteJsonWithStatus(403, nil, no_permission)
		} else if err == models.ErrBlogNotFound {
			res.WriteJsonWithStatus(404, nil, not_found)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}
	res.WriteJsonWithStatus(200, nil, ok)
}
示例#2
0
func deletePostHandler(res *wcg.Response, req *wcg.Request) {
	blog, _, err := fetchBlogForUpdate(res, req, false)
	if err != nil {
		return
	}
	post, err := fetchPostForUpdate(req, res, blog)
	if err != nil {
		return
	}
	err = models.NewPostDriver(gae.NewContext(req), req.Logger).DeletePost(post)
	if err != nil {
		if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}
	res.WriteJsonWithStatus(200, nil, ok)
}
示例#3
0
func updatePostHandler(res *wcg.Response, req *wcg.Request) {
	blog, _, err := fetchBlogForUpdate(res, req, false)
	if err != nil {
		return
	}
	post, err := fetchPostForUpdate(req, res, blog)
	if err != nil {
		return
	}
	post.Title = req.Form("title")
	post.Content = req.Form("content")
	post.IsDraft = req.Form("is_draft") == "true"
	post.IsNew = false
	post.PostDate, err = time.Parse(wcg.FormDateFormat, req.Form("post_date"))
	post.Tags = strings.Split(req.Form("tags"), ",")
	for i, v := range post.Tags {
		post.Tags[i] = strings.TrimSpace(v)
	}

	if err != nil {
		res.WriteHeader(400)
		res.WriteString("Invalid date format.")
		res.End()
	}
	driver := models.NewPostDriver(gae.NewContext(req), req.Logger)
	if AppConfig.GithubMarkdown {
		driver.HttpClient = gae.NewHttpClient(req)
	}
	err = driver.SavePost(post)
	if err != nil {
		if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}

	req.Logger.Info("A post is created at %s.", post.Id)
	res.WriteJsonWithStatus(200, nil, ok)
}
示例#4
0
func TestCreateBlog(t *testing.T) {
	c, _ := aetest.NewContext(nil)
	defer c.Close()

	driver := NewBlogDriver(c, wcg.NullLogger)

	_, err := driver.CreateBlog("+1", "title", "description", "ownerid")
	if !supports.IsValidationError(err) {
		t.Errorf("CreateBlog should return ValidationError, but got %v", err)
	}

	_, err = driver.CreateBlog("myblog", "title", "description", "ownerid")
	if err != nil {
		t.Errorf("CreateBlog should not return error, but got %v", err)
	}

	_, err = driver.CreateBlog("myblog", "title", "description", "ownerid")
	if err != ErrBlogAlreadyExists {
		t.Errorf("CreateBlog should return ErrBlogAlreadyExists, but got %v", err)
	}
}
示例#5
0
func createBlogHandler(res *wcg.Response, req *wcg.Request) {
	driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
	blog, err := driver.CreateBlog(
		req.Form("path"),
		req.Form("title"),
		req.Form("description"),
		req.User.Id(),
	)
	if err != nil {
		if err == models.ErrBlogAlreadyExists {
			res.WriteJsonWithStatus(409, nil, already_taken)
		} else if supports.IsValidationError(err) {
			res.WriteJsonWithStatus(400, nil, err)
		} else {
			res.RenderInternalError(err.Error())
		}
		return
	}
	res.WriteJsonWithStatus(201, map[string]string{
		"location": wcg.AbsoluteUrl(req, "/"+blog.Id),
	}, ok)
}