示例#1
0
func RegisterHandler(ctx *middleware.Context, formErr binding.Errors, user model.UserRegisterForm) {
	switch ctx.R.Method {
	case "POST":
		ctx.JoinFormErrors(formErr)
		if !ctx.HasError() {
			dbUser := model.User{Username: user.Username, Password: user.Password, Email: user.Email}

			if exist, err := dbUser.ExistUsername(); exist {
				PanicIf(err)
				ctx.AddFieldError("username", Translate(ctx.S.Get("Lang").(string), "message.error.already.exists"))
			}

			if exist, err := dbUser.ExistEmail(); exist {
				PanicIf(err)
				ctx.AddFieldError("email", Translate(ctx.S.Get("Lang").(string), "message.error.already.exists"))
			}

			if !ctx.HasError() {
				dbUser.Password = Md5(user.Password)
				err := dbUser.Insert()
				PanicIf(err)
				ctx.AddMessage(Translate(ctx.S.Get("Lang").(string), "message.register.success"))
			} else {
				ctx.Set("User", user)
			}
			ctx.HTML(200, "user/register", ctx)
		} else {
			ctx.Set("User", user)
			ctx.HTML(200, "user/register", ctx)
		}
	default:
		ctx.HTML(200, "user/register", ctx)
	}
}
示例#2
0
func LoginHandler(ctx *middleware.Context, formErr binding.Errors, loginUser model.UserLoginForm) {
	switch ctx.R.Method {
	case "POST":
		ctx.JoinFormErrors(formErr)
		password := Md5(loginUser.Password)
		user := &model.User{Username: loginUser.Username, Password: password}
		if !ctx.HasError() {
			if has, err := user.Exist(); has {
				PanicIf(err)
				if user.Locked {
					ctx.Set("User", user)
					ctx.AddError(Translate(ctx.S.Get("Lang").(string), "message.error.invalid.username.or.password"))
					ctx.HTML(200, "user/login", ctx)
					return
				}
				ctx.S.Set("SignedUser", user)
				Log.Info(user.Username, " login")
				ctx.Redirect("/admin/dashboard")
			} else {
				ctx.Set("User", user)
				ctx.AddError(Translate(ctx.S.Get("Lang").(string), "message.error.invalid.username.or.password"))
				ctx.HTML(200, "user/login", ctx)
			}
		} else {
			ctx.HTML(200, "user/login", ctx)
		}
	default:
		ctx.HTML(200, "user/login", ctx)
	}
}
示例#3
0
func PublishBlog(ctx *middleware.Context, blog model.Blog) {
	switch ctx.R.Method {
	case "POST":
		if blog.Title == "" || blog.Content == "" {
			ctx.AddError(Translate(ctx.S.Get("Lang").(string), "message.error.publish.failed"))
		} else {
			tags := ctx.R.PostForm["tags"]
			blog.SetTags(tags)
			signedUser := ctx.S.Get("SignedUser").(model.User)
			blog.State = "PUBLISHED"
			blog.PublishDate = time.Now()
			if blog.Version == 0 {
				blog.Priority = 5
				blog.Author = signedUser
				blog.CreateUser = signedUser.Username
				err := blog.Insert()
				PanicIf(err)
			} else {
				err := blog.Update()
				PanicIf(err)
			}
		}
		ctx.Redirect("/blog/view/" + IntString(blog.Id))
	default:
		tags, err := blog.GetAllTags()
		PanicIf(err)
		ctx.Set("Tags", tags)

		ctx.HTML(200, "blog/edit", ctx)
	}
}
示例#4
0
func SaveBlog(ctx *middleware.Context, blog model.Blog) {
	if blog.Title == "" || blog.Content == "" {
		ctx.AddError(Translate(ctx.S.Get("Lang").(string), "message.error.save.failed"))
	} else {
		tags := ctx.R.PostForm["tags"]
		blog.SetTags(tags)
		signedUser := ctx.S.Get("SignedUser").(model.User)
		blog.UpdateUser = signedUser.Username
		if blog.Version == 0 {
			blog.State = "DRAFT"
			blog.Priority = 5
			blog.Author = signedUser
			blog.CreateUser = signedUser.Username
			err := blog.Insert()
			PanicIf(err)
		} else {
			err := blog.Update()
			PanicIf(err)
		}
		dbBlog, err := blog.GetBlogById()
		PanicIf(err)
		ctx.Set("Blog", dbBlog)

		ctx.AddMessage(Translate(ctx.S.Get("Lang").(string), "message.save.success"))
	}

	tags, err := blog.GetAllTags()
	PanicIf(err)
	ctx.Set("Tags", tags)

	ctx.HTML(200, "blog/edit", ctx)
}
示例#5
0
func ViewFeedback(ctx *middleware.Context, params martini.Params) {
	id := params["id"]
	feedback := new(model.Feedback)
	feedback.Id = ParseInt(id)
	err := feedback.SetViewed(true)
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#6
0
func LiftUser(ctx *middleware.Context, params martini.Params) {
	id := params["id"]
	user := new(model.User)
	user.Id = ParseInt(id)
	err := user.SetLock(false)
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#7
0
func FeedbackInfo(ctx *middleware.Context) {
	feedback := new(model.Feedback)
	feedback.Viewed = false
	feedbackArray, count, err := feedback.Info()
	PanicIf(err)
	ctx.Set("Array", feedbackArray)
	ctx.Set("Count", count)
	ctx.JSON(200, ctx.Response)
}
示例#8
0
func DeleteLink(ctx *middleware.Context, params martini.Params) {
	id := params["id"]
	link := new(model.Link)
	link.Id = ParseInt(id)
	err := link.Delete()
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#9
0
func PermitBlog(ctx *middleware.Context, params martini.Params) {
	id := params["id"]
	blog := new(model.Blog)
	blog.Id = ParseInt(id)
	err := blog.Forbid(false)
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#10
0
func DeleteFeedbackArray(ctx *middleware.Context) {
	feedbackArray := ctx.R.FormValue("feedbackArray")
	feedback := new(model.Feedback)
	var res []int
	json.Unmarshal([]byte(feedbackArray), &res)
	err := feedback.DeleteFeedbackArray(res)
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#11
0
func DeleteBlogArray(ctx *middleware.Context) {
	blogArray := ctx.R.FormValue("blogArray")
	blog := new(model.Blog)
	var res []int
	json.Unmarshal([]byte(blogArray), &res)
	err := blog.DeleteBlogArray(res)
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#12
0
func DeleteLinkArray(ctx *middleware.Context) {
	linkArray := ctx.R.FormValue("linkArray")
	link := new(model.Link)
	var res []int
	json.Unmarshal([]byte(linkArray), &res)
	err := link.DeleteLinkArray(res)
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#13
0
func EditLink(ctx *middleware.Context, params martini.Params) {
	id := params["id"]
	link := new(model.Link)
	link.Id = ParseInt(id)
	err := link.GetLink()
	PanicIf(err)
	ctx.Set("Link", link)

	ctx.HTML(200, "link/edit", ctx)
}
示例#14
0
func DeleteUsers(ctx *middleware.Context) {
	users := ctx.R.FormValue("Users")
	var res []int
	json.Unmarshal([]byte(users), &res)
	user := new(model.User)
	err := user.DeleteUsers(res)
	PanicIf(err)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}
示例#15
0
func InsertLink(ctx *middleware.Context, link model.Link) {
	switch ctx.R.Method {
	case "POST":
		err := link.Insert()
		PanicIf(err)
		ctx.Set("success", true)
		ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.send.success"))
		ctx.Redirect("/link/all")
	default:
		ctx.HTML(200, "link/edit", ctx)
	}
}
示例#16
0
func ContactHandler(ctx *middleware.Context, feedback model.Feedback) {
	switch ctx.R.Method {
	case "POST":
		err := feedback.Insert()
		PanicIf(err)
		ctx.Set("success", true)
		ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.send.success"))
		ctx.JSON(200, ctx.Response)
	default:
		ctx.HTML(200, "contact", ctx)
	}
}
示例#17
0
func SetRole(ctx *middleware.Context) {
	id := ctx.R.PostFormValue("Id")
	roleId := ctx.R.PostFormValue("RoleId")
	version := ctx.R.PostFormValue("Version")
	user := new(model.User)
	user.Id = ParseInt(id)
	user.Role.Id = ParseInt(roleId)
	user.Version = ParseInt(version)
	err := user.SetRole()
	PanicIf(err)
	ctx.Set("success", true)
	Log.Info("User: "******" roleId set to ", roleId)
	ctx.JSON(200, ctx.Response)
}
示例#18
0
func EditBlog(ctx *middleware.Context, params martini.Params) {
	id := params["id"]
	blog := new(model.Blog)
	blog.Id = ParseInt(id)
	err := blog.GetBlog()
	PanicIf(err)
	ctx.Set("Blog", blog)

	tags, err := blog.GetAllTags()
	PanicIf(err)
	ctx.Set("Tags", tags)

	ctx.HTML(200, "blog/edit", ctx)
}
示例#19
0
func SettingsHandler(ctx *middleware.Context, settings model.Settings) {
	if ctx.R.Method == "POST" {
		err := settings.Update()
		PanicIf(err)
		dbSettings := model.GetSettings()
		ctx.AddMessage(Translate(ctx.S.Get("Lang").(string), "message.change.success"))
		ctx.S.Set("Settings", dbSettings)
	}
	user := &model.User{}
	users, err := user.SelectAll()
	PanicIf(err)
	ctx.Set("Users", users)

	ctx.HTML(200, "admin/settings", ctx)
}
示例#20
0
func DashboardHandler(ctx *middleware.Context) {
	visit := new(model.Visit)
	visit.SetPageActive(true)
	visit.SetPageSize(10)
	pageNo := ParseInt(ctx.R.FormValue("page"))
	visit.SetPageNo(pageNo)
	visit.AddSortProperty("create_date", "desc")
	visitList, total, err := visit.SearchByPage()
	PanicIf(err)

	visit.SetTotalRecord(total)
	visit.Result = visitList
	ctx.Set("Visit", visit)
	ctx.HTML(200, "admin/dashboard", ctx)
}
示例#21
0
func Blog(ctx *middleware.Context) {
	blog := new(model.Blog)
	blog.SetPageActive(true)
	blog.SetPageSize(10)
	pageNo := ParseInt(ctx.R.FormValue("page"))
	blog.SetPageNo(pageNo)
	blog.State = "PUBLISHED"
	blog.AddSortProperty("publish_date", "desc")
	blogList, total, err := blog.SearchByPage(true)
	PanicIf(err)

	blog.SetTotalRecord(total)
	blog.Result = blogList
	ctx.Set("Blog", blog)

	tags, err := blog.GetAllTags()
	PanicIf(err)
	ctx.Set("Tags", tags)

	ctx.HTML(200, "blog", ctx)
}
示例#22
0
func DeleteComment(ctx *middleware.Context, params martini.Params) {
	blogId := ParseInt(params["blogId"])
	seq := ParseInt(params["seq"])
	comment := model.Comment{Blog: model.Blog{Id: blogId}, Seq: seq}

	err := comment.Delete()
	if err != nil {
		ctx.Set("success", false)
		ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.error.delete.failed"))
	} else {
		ctx.Set("success", true)
		ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.delete.success"))
	}
	ctx.JSON(200, ctx.Response)
}
示例#23
0
func AllUserHandler(ctx *middleware.Context) {
	switch ctx.R.Method {
	case "POST":
		user := new(model.User)
		user.SetPageActive(true)
		user.SetPageSize(ParseInt(ctx.R.FormValue("iDisplayLength")))
		user.SetDisplayStart(ParseInt(ctx.R.FormValue("iDisplayStart")))
		columnNum := ctx.R.FormValue("iSortCol_0")
		sortColumn := ctx.R.FormValue("mDataProp_" + columnNum)
		user.AddSortProperty(sortColumn, ctx.R.FormValue("sSortDir_0"))
		users, total, err := user.SearchByPage()
		PanicIf(err)
		ctx.Set("aaData", users)
		ctx.Set("iTotalDisplayRecords", total)
		ctx.Set("iTotalRecords", total)
		ctx.JSON(200, ctx.Response)
	default:
		ctx.HTML(200, "user/allUser", ctx)
	}
}
示例#24
0
func AllFeedback(ctx *middleware.Context) {
	switch ctx.R.Method {
	case "POST":
		feedback := new(model.Feedback)
		feedback.SetPageActive(true)
		feedback.SetPageSize(ParseInt(ctx.R.FormValue("iDisplayLength")))
		feedback.SetDisplayStart(ParseInt(ctx.R.FormValue("iDisplayStart")))
		columnNum := ctx.R.FormValue("iSortCol_0")
		sortColumn := ctx.R.FormValue("mDataProp_" + columnNum)
		feedback.AddSortProperty(sortColumn, ctx.R.FormValue("sSortDir_0"))
		feedbackArray, total, err := feedback.SearchByPage()
		PanicIf(err)
		ctx.Set("aaData", feedbackArray)
		ctx.Set("iTotalDisplayRecords", total)
		ctx.Set("iTotalRecords", total)
		ctx.JSON(200, ctx.Response)
	default:
		ctx.HTML(200, "feedback/allFeedback", ctx)
	}
}
示例#25
0
func AllBlog(ctx *middleware.Context) {
	switch ctx.R.Method {
	case "POST":
		blog := new(model.Blog)
		blog.SetPageActive(true)
		blog.SetPageSize(ParseInt(ctx.R.FormValue("iDisplayLength")))
		blog.SetDisplayStart(ParseInt(ctx.R.FormValue("iDisplayStart")))
		columnNum := ctx.R.FormValue("iSortCol_0")
		sortColumn := ctx.R.FormValue("mDataProp_" + columnNum)
		blog.AddSortProperty(sortColumn, ctx.R.FormValue("sSortDir_0"))
		blogList, total, err := blog.SearchByPage(false)
		PanicIf(err)
		ctx.Set("aaData", blogList)
		ctx.Set("iTotalDisplayRecords", total)
		ctx.Set("iTotalRecords", total)
		ctx.JSON(200, ctx.Response)
	default:
		ctx.HTML(200, "blog/allBlog", ctx)
	}
}
示例#26
0
func Comment(ctx *middleware.Context) {
	id := ParseInt(ctx.R.PostFormValue("blogId"))
	name := ctx.R.PostFormValue("name")
	email := ctx.R.PostFormValue("email")
	www := ctx.R.PostFormValue("www")
	content := ctx.R.PostFormValue("content")
	blog := model.Blog{Id: id}
	if exist, err := blog.Exist(); exist {
		PanicIf(err)
		comment := model.Comment{Blog: model.Blog{Id: id}, Name: name, Email: email, Www: www, Content: content}
		if comment.Content == "" {
			ctx.Set("success", false)
			ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.error.submit.failed"))
		} else if blog.ForbidComment {
			ctx.Set("success", false)
			ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.error.forbid.comment"))
		} else {
			comment.Ip = GetRemoteIp(ctx.R)
			err := comment.Insert()
			PanicIf(err)
			ctx.Set("success", true)
			ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.submit.success"))
		}
	} else {
		ctx.Set("success", false)
		ctx.Set("message", Translate(ctx.S.Get("Lang").(string), "message.error.blog.not.exists"))
	}
	ctx.JSON(200, ctx.Response)
}
示例#27
0
func LangHandler(ctx *middleware.Context, params martini.Params) {
	lang := params["lang"]
	ctx.S.Set("Lang", lang)
	ctx.Set("success", true)
	ctx.JSON(200, ctx.Response)
}