From 18cc4bb1a7ab6951b8e1c54ec37f746e993a3eaa Mon Sep 17 00:00:00 2001 From: jeff Date: Thu, 17 Oct 2019 19:26:10 +0800 Subject: [PATCH 01/11] add point to user --- models/repo_list.go | 1 + models/user.go | 1 + routers/home.go | 2 +- routers/routes/routes.go | 1 + routers/user/profile.go | 22 ++++++++++++++++++++++ templates/explore/users.tmpl | 2 +- templates/user/profile.tmpl | 17 ++++++++++++++++- 7 files changed, 43 insertions(+), 3 deletions(-) diff --git a/models/repo_list.go b/models/repo_list.go index 692d4d002f5a4..8dd882d1d7e3f 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -163,6 +163,7 @@ const ( SearchOrderByStarsReverse SearchOrderBy = "num_stars DESC" SearchOrderByForks SearchOrderBy = "num_forks ASC" SearchOrderByForksReverse SearchOrderBy = "num_forks DESC" + SearchOrderByPoint SearchOrderBy = "Point DESC" ) // SearchRepository returns repositories based on search options, diff --git a/models/user.go b/models/user.go index 030e23c383afb..48068c2839a5f 100644 --- a/models/user.go +++ b/models/user.go @@ -118,6 +118,7 @@ type User struct { Salt string `xorm:"VARCHAR(10)"` Language string `xorm:"VARCHAR(5)"` Description string + Point int `xorm:"NOT NULL DEFAULT 0"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/routers/home.go b/routers/home.go index f8fb849c8e17b..baeb9c4d7413e 100644 --- a/routers/home.go +++ b/routers/home.go @@ -210,7 +210,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN orderBy = models.SearchOrderByAlphabetically default: ctx.Data["SortType"] = "alphabetically" - orderBy = models.SearchOrderByAlphabetically + orderBy = models.SearchOrderByPoint } opts.Keyword = strings.Trim(ctx.Query("q"), " ") diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 9572ea80390a4..6695459951ffa 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -520,6 +520,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:username", func() { m.Get("/action/:action", user.Action) + m.Post("/action/transfer_point", user.TransferP) }, reqSignIn) if macaron.Env == macaron.DEV { diff --git a/routers/user/profile.go b/routers/user/profile.go index 8a62ddeac026c..62db36f668ae5 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -9,6 +9,7 @@ import ( "fmt" "path" "strings" + "strconv" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" @@ -270,3 +271,24 @@ func Action(ctx *context.Context) { ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink()) } + +func TransferP(ctx *context.Context) { + //减少发送者点数 + //增加接收者点数 + //创建transfer记录 + u := GetUserByParams(ctx) + var err error + Qty, err := strconv.Atoi(ctx.Query("qty")) + if err != nil { + return + } + err = models.TransferPoint(ctx.User.ID, + ctx.Query("why"), + u.ID, + Qty) + if err != nil { + ctx.ServerError("Transfer", err) + return + } + ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink()) +} diff --git a/templates/explore/users.tmpl b/templates/explore/users.tmpl index 32a36931c70b9..8c0f6d96edbeb 100644 --- a/templates/explore/users.tmpl +++ b/templates/explore/users.tmpl @@ -9,7 +9,7 @@
- {{.Name}} {{.FullName}} + {{.Name}} {{.FullName}} {{.Point}}
{{if .Location}} {{.Location}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 52c6e3ec3f589..f4d9dbe78ce2c 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -15,7 +15,7 @@ {{end}}
{{if .Owner.FullName}}{{.Owner.FullName}}{{end}} - {{.Owner.Name}} + {{.Owner.Name}} {{.Owner.Point}}
From 96186294cc75bc8e8ed187fcaf16cc79944c104d Mon Sep 17 00:00:00 2001 From: jeff Date: Thu, 17 Oct 2019 19:43:37 +0800 Subject: [PATCH 02/11] add transfer table --- models/transfer_point.go | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 models/transfer_point.go diff --git a/models/transfer_point.go b/models/transfer_point.go new file mode 100644 index 0000000000000..e8939699bb804 --- /dev/null +++ b/models/transfer_point.go @@ -0,0 +1,41 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "code.gitea.io/gitea/modules/timeutil" +) + +type Transfer struct { + ID int64 `xorm:"pk autoincr"` + FromID int64 + ToID int64 + Why string + Qty int + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` +} + +func TransferPoint(FromID int64, Why string, ToID int64, Qty int) (err error) { + + sess := x.NewSession() + //判断是否足够转 + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + +// if _, err = sess.Insert(&Transfer{FromID: FromID, ToID: ToID, Why: Why, Qty: Qty}); err != nil { +// return err +// } + + if _, err = sess.Exec("UPDATE `user` SET point = point + ? WHERE id = ?", Qty, ToID); err != nil { + return err + } + + if _, err = sess.Exec("UPDATE `user` SET point = point - ? WHERE id = ?", Qty, FromID); err != nil { + return err + } + return sess.Commit() +} From e4deaf39ef79e83941230ce966be9986de170ece Mon Sep 17 00:00:00 2001 From: jeff Date: Fri, 18 Oct 2019 13:10:50 +0800 Subject: [PATCH 03/11] generate table, add error but fail --- models/models.go | 1 + models/transfer_point.go | 6 +++--- routers/user/profile.go | 5 +++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/models/models.go b/models/models.go index ea550cb839fed..57c971c5edece 100644 --- a/models/models.go +++ b/models/models.go @@ -113,6 +113,7 @@ func init() { new(OAuth2AuthorizationCode), new(OAuth2Grant), new(Task), + new(Transfer), ) gonicNames := []string{"SSL", "UID"} diff --git a/models/transfer_point.go b/models/transfer_point.go index e8939699bb804..3916dbde0641d 100644 --- a/models/transfer_point.go +++ b/models/transfer_point.go @@ -26,9 +26,9 @@ func TransferPoint(FromID int64, Why string, ToID int64, Qty int) (err error) { return err } -// if _, err = sess.Insert(&Transfer{FromID: FromID, ToID: ToID, Why: Why, Qty: Qty}); err != nil { -// return err -// } + if _, err = sess.Insert(&Transfer{FromID: FromID, ToID: ToID, Why: Why, Qty: Qty}); err != nil { + return err + } if _, err = sess.Exec("UPDATE `user` SET point = point + ? WHERE id = ?", Qty, ToID); err != nil { return err diff --git a/routers/user/profile.go b/routers/user/profile.go index 62db36f668ae5..f3ebaa0351821 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -280,6 +280,11 @@ func TransferP(ctx *context.Context) { var err error Qty, err := strconv.Atoi(ctx.Query("qty")) if err != nil { + ctx.Flash.Error("请输入数字") + return + } + if ctx.User.Point < Qty { + ctx.Flash.Error("余额不足!") return } err = models.TransferPoint(ctx.User.ID, From f32466468d43ca636a770c5a1c4c261fa28c028a Mon Sep 17 00:00:00 2001 From: jeff Date: Mon, 28 Oct 2019 00:03:59 +0800 Subject: [PATCH 04/11] fix #1 --- templates/user/profile.tmpl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index f4d9dbe78ce2c..7701257deb88e 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -94,13 +94,12 @@
-
+
- 发送 - + +
- {{end}} From 27f884a7ceb60c98cb2d9e8099f2725b68817262 Mon Sep 17 00:00:00 2001 From: jeff Date: Mon, 28 Oct 2019 19:36:34 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E6=8E=A2=E7=B4=A2=E9=A1=B5=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E7=A7=AF=E5=88=86=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/transfer_point.go | 45 ++++++++++++++++++++++++++ routers/home.go | 59 +++++++++++++++++++++++++++++++++++ routers/routes/routes.go | 1 + templates/explore/navbar.tmpl | 3 ++ templates/explore/trans.tmpl | 23 ++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 templates/explore/trans.tmpl diff --git a/models/transfer_point.go b/models/transfer_point.go index 3916dbde0641d..02b8746ac44a4 100644 --- a/models/transfer_point.go +++ b/models/transfer_point.go @@ -5,7 +5,10 @@ package models import ( + "fmt" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "xorm.io/builder" ) type Transfer struct { @@ -17,6 +20,48 @@ type Transfer struct { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` } +// SearchTransOptions contains the options for searching +type SearchTransOptions struct { + Keyword string + OrderBy SearchOrderBy + Page int + PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum +} + +func (opts *SearchTransOptions) toConds() builder.Cond { + var cond builder.Cond = builder.Gt{"ID": 0} + return cond +} + +func SearchTrans(opts *SearchTransOptions) (trans []*Transfer, _ int64, _ error) { + cond := opts.toConds() + count, err := x.Where(cond).Count(new(Transfer)) + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) + } + + if opts.PageSize == 0 || opts.PageSize > setting.UI.ExplorePagingNum { + opts.PageSize = setting.UI.ExplorePagingNum + } + if opts.Page <= 0 { + opts.Page = 1 + } + if len(opts.OrderBy) == 0 { + opts.OrderBy = SearchOrderByIDReverse + } + + sess := x.Where(cond) + if opts.PageSize > 0 { + sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) + } + if opts.PageSize == -1 { + opts.PageSize = int(count) + } + + trans = make([]*Transfer, 0, opts.PageSize) + return trans, count, sess.OrderBy(opts.OrderBy.String()).Find(&trans) +} + func TransferPoint(FromID int64, Why string, ToID int64, Qty int) (err error) { sess := x.NewSession() diff --git a/routers/home.go b/routers/home.go index baeb9c4d7413e..bea20533f57c2 100644 --- a/routers/home.go +++ b/routers/home.go @@ -28,6 +28,8 @@ const ( tplExploreUsers base.TplName = "explore/users" // tplExploreOrganizations explore organizations page template tplExploreOrganizations base.TplName = "explore/organizations" + + tplExploreTrans base.TplName = "explore/trans" // tplExploreCode explore code page template tplExploreCode base.TplName = "explore/code" ) @@ -270,6 +272,63 @@ func ExploreOrganizations(ctx *context.Context) { }, tplExploreOrganizations) } +// RenderUserSearch render user search page +func RenderTransSearch(ctx *context.Context, opts *models.SearchTransOptions, tplName base.TplName) { + opts.Page = ctx.QueryInt("page") + if opts.Page <= 1 { + opts.Page = 1 + } + + var ( + trans []*models.Transfer + count int64 + err error + orderBy models.SearchOrderBy + ) + + ctx.Data["SortType"] = ctx.Query("sort") + switch ctx.Query("sort") { + case "newest": + orderBy = models.SearchOrderByIDReverse + case "oldest": + orderBy = models.SearchOrderByID + default: + ctx.Data["SortType"] = "alphabetically" + orderBy = models.SearchOrderByIDReverse + } + + opts.Keyword = strings.Trim(ctx.Query("q"), " ") + opts.OrderBy = orderBy + if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) { + trans, count, err = models.SearchTrans(opts) + if err != nil { + ctx.ServerError("SearchTrans", err) + return + } + } + ctx.Data["Keyword"] = opts.Keyword + ctx.Data["Total"] = count + ctx.Data["Trans"] = trans + ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled + + pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5) + pager.SetDefaultParams(ctx) + ctx.Data["Page"] = pager + + ctx.HTML(200, tplName) +} + +func ExploreTrans(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("explore") + ctx.Data["PageIsExplore"] = true + ctx.Data["PageIsExploreTrans"] = true + ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled + + RenderTransSearch(ctx, &models.SearchTransOptions{ + PageSize: setting.UI.ExplorePagingNum, + }, tplExploreTrans) +} + // ExploreCode render explore code page func ExploreCode(ctx *context.Context) { if !setting.Indexer.RepoIndexerEnabled { diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 6695459951ffa..f5aec25233c42 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -268,6 +268,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("", func(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore/repos") }) + m.Get("/trans", routers.ExploreTrans) m.Get("/repos", routers.ExploreRepos) m.Get("/users", routers.ExploreUsers) m.Get("/organizations", routers.ExploreOrganizations) diff --git a/templates/explore/navbar.tmpl b/templates/explore/navbar.tmpl index 3bd52645e2165..8343b7aa9c8c2 100644 --- a/templates/explore/navbar.tmpl +++ b/templates/explore/navbar.tmpl @@ -1,4 +1,7 @@ {{if not .Repository.IsFork}} +
+ + +
+
+ + % +
From c87726f9fb246236d901491fcc6e30ad9f4fc876 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 29 Oct 2019 01:51:50 +0800 Subject: [PATCH 07/11] =?UTF-8?q?WIP-=E5=A2=9E=E5=8A=A0=E6=8D=90=E8=B5=A0?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/pull.go | 25 +++++++++++++++++++++++++ models/transfer_point.go | 23 ++++++++++++++++++++++- routers/repo/fund.go | 31 +++++++++++++++++++++++++++++++ routers/routes/routes.go | 2 ++ templates/repo/fund/list.tmpl | 35 +++++++++++++++++++++++++++++++++++ templates/repo/header.tmpl | 7 ++++++- 6 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 routers/repo/fund.go create mode 100644 templates/repo/fund/list.tmpl diff --git a/models/pull.go b/models/pull.go index 962e433fb0759..d4f1cb2513923 100644 --- a/models/pull.go +++ b/models/pull.go @@ -437,6 +437,14 @@ func (pr *PullRequest) SetMerged() (err error) { return err } + if err = pr.GetBaseRepo(); err != nil { + return err + } + + if err = pr.GetHeadRepo(); err != nil { + return err + } + if err = pr.Issue.changeStatus(sess, pr.Merger, true); err != nil { return fmt.Errorf("Issue.changeStatus: %v", err) } @@ -444,6 +452,23 @@ func (pr *PullRequest) SetMerged() (err error) { return fmt.Errorf("update pull request: %v", err) } + err = TransferPoint(pr.BaseRepo.OwnerID, + "Contribution to My Project ", + pr.HeadRepo.OwnerID, + int(pr.BaseRepo.NextPoint)) + + if err != nil { + return fmt.Errorf("Transfer", err) + } + var balancePoint = pr.BaseRepo.Point - pr.BaseRepo.NextPoint + var nextPoint = int64(float64(balancePoint) * float64(pr.BaseRepo.Percent) * 0.01) + pr.BaseRepo.Point = balancePoint + pr.BaseRepo.NextPoint = nextPoint + //TODO NextPoint没被更新 + if _, err = sess.ID(pr.BaseRepo.ID).Cols("Point, NextPoint").Update(pr.BaseRepo); err != nil { + return fmt.Errorf("update base repo point: %v", err) + } + if err = sess.Commit(); err != nil { return fmt.Errorf("Commit: %v", err) } diff --git a/models/transfer_point.go b/models/transfer_point.go index 02b8746ac44a4..ac608fe025d15 100644 --- a/models/transfer_point.go +++ b/models/transfer_point.go @@ -65,7 +65,7 @@ func SearchTrans(opts *SearchTransOptions) (trans []*Transfer, _ int64, _ error) func TransferPoint(FromID int64, Why string, ToID int64, Qty int) (err error) { sess := x.NewSession() - //判断是否足够转 + defer sess.Close() if err = sess.Begin(); err != nil { return err @@ -84,3 +84,24 @@ func TransferPoint(FromID int64, Why string, ToID int64, Qty int) (err error) { } return sess.Commit() } + +// Fund contains the fund information +type Fund struct { + ID int64 `xorm:"pk autoincr"` + Name string + RepoID int64 `xorm:"INDEX"` + Qty int64 +} + +type FundList []*Fund + +// GetFunds returns a list of Funds of given repository. +func GetFunds(repoID int64, page int) (FundList, error) { + funds := make([]*Fund, 0, setting.UI.IssuePagingNum) + sess := x.Where("repo_id = ? ", repoID) + if page > 0 { + sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum) + } + + return funds, sess.Find(&funds) +} diff --git a/routers/repo/fund.go b/routers/repo/fund.go new file mode 100644 index 0000000000000..78a57eb0311cf --- /dev/null +++ b/routers/repo/fund.go @@ -0,0 +1,31 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/context" +) + +const ( + tplFunds base.TplName = "repo/fund/list" +) + +// Funds render repository branch page +func Funds(ctx *context.Context) { + ctx.Data["Title"] = "Funds" + ctx.Data["IsRepoToolbarFunds"] = true + page := ctx.QueryInt("page") + funds, err := models.GetFunds(ctx.Repo.Repository.ID, page) + if err != nil { + ctx.ServerError("GetFunds", err) + return + } + ctx.Data["Funds"] = funds + ctx.HTML(200, tplFunds) +} + diff --git a/routers/routes/routes.go b/routers/routes/routes.go index f5aec25233c42..d864b8694fd9d 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -838,6 +838,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/raw/*", repo.WikiRaw) }, repo.MustEnableWiki) +// m.Get("/funds", repo.Funds) + m.Group("/activity", func() { m.Get("", repo.Activity) m.Get("/:period", repo.Activity) diff --git a/templates/repo/fund/list.tmpl b/templates/repo/fund/list.tmpl new file mode 100644 index 0000000000000..3a2273c7dda0a --- /dev/null +++ b/templates/repo/fund/list.tmpl @@ -0,0 +1,35 @@ +{{template "base/head" .}} +
+ {{template "repo/header" .}} +
+ {{template "base/alert" .}} + {{template "repo/sub_menu" .}} +

+ {{.i18n.Tr "repo.default_branch"}} +

+ +
+ + + + + + + +
+ {{range .Funds}} + {{.Name}} {{.Qty}} + {{end}} + +
+ {{.CsrfTokenHtml}} + + + + + +
+
+
+ +{{template "base/footer" .}} diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index 05be58a7e44f7..91397c2dadc1a 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -11,7 +11,9 @@ {{.Owner.Name}}
/
{{.Name}} - 项目积分剩余{{.Point}},下一贡献奖励{{.Percent}}%,即 {{.NextPoint}}积分 + {{if gt .NextPoint 0}} +
项目积分剩余{{.Point}},下一贡献奖励{{.Percent}}%,即 {{.NextPoint}}积分
+ {{end}} {{if and .RelAvatarLink .IsPrivate}}{{end}} {{if .IsArchived}}{{end}} {{if .IsMirror}}
{{$.i18n.Tr "repo.mirror_from"}} {{MirrorAddress $.Mirror}}
{{end}} @@ -94,6 +96,9 @@ {{.i18n.Tr "repo.activity"}} {{end}} + + 捐赠 + {{template "custom/extra_tabs" .}} From 07354bdea4dba19f3db31b0dc9e565c036cd5c0f Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 29 Oct 2019 11:45:25 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E6=8D=90=E8=B5=A0=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/models.go | 1 + models/transfer_point.go | 22 ++++++++++++++++++ routers/repo/fund.go | 40 +++++++++++++++++++++++++++++++- routers/repo/repo.go | 1 + routers/routes/routes.go | 7 ++++-- templates/repo/fund/list.tmpl | 43 +++++++++++++---------------------- templates/repo/header.tmpl | 4 ++-- 7 files changed, 86 insertions(+), 32 deletions(-) diff --git a/models/models.go b/models/models.go index 57c971c5edece..0a0fbb96b6194 100644 --- a/models/models.go +++ b/models/models.go @@ -114,6 +114,7 @@ func init() { new(OAuth2Grant), new(Task), new(Transfer), + new(Fund), ) gonicNames := []string{"SSL", "UID"} diff --git a/models/transfer_point.go b/models/transfer_point.go index ac608fe025d15..c84ebb1324000 100644 --- a/models/transfer_point.go +++ b/models/transfer_point.go @@ -105,3 +105,25 @@ func GetFunds(repoID int64, page int) (FundList, error) { return funds, sess.Find(&funds) } + +func NewFund(name string, repoID int64, qty int64)(err error){ + sess := x.NewSession() + + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + + if _, err = sess.Insert(&Fund{Name: name, RepoID: repoID, Qty: qty}); err != nil { + return err + } + + if _, err = sess.Exec("UPDATE `repository` SET point = point + ? WHERE id = ?", qty, repoID); err != nil { + return err + } + + if _, err = sess.Exec("UPDATE `repository` SET next_point = point * percent * 0.01 WHERE id = ?", repoID); err != nil { + return err + } + return sess.Commit() +} diff --git a/routers/repo/fund.go b/routers/repo/fund.go index 78a57eb0311cf..ae4042dbdfb42 100644 --- a/routers/repo/fund.go +++ b/routers/repo/fund.go @@ -6,6 +6,7 @@ package repo import ( + "strconv" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -18,7 +19,7 @@ const ( // Funds render repository branch page func Funds(ctx *context.Context) { ctx.Data["Title"] = "Funds" - ctx.Data["IsRepoToolbarFunds"] = true + ctx.Data["PageIsFund"] = true page := ctx.QueryInt("page") funds, err := models.GetFunds(ctx.Repo.Repository.ID, page) if err != nil { @@ -29,3 +30,40 @@ func Funds(ctx *context.Context) { ctx.HTML(200, tplFunds) } +func Funding(ctx *context.Context) { + //减少发送者点数 + //增加接收者点数 + //创建transfer记录 + var err error + Qty, err := strconv.Atoi(ctx.Query("qty")) + if err != nil { + ctx.Flash.Error("请输入数字") + return + } + var toid int + var repoid int + toid, err = strconv.Atoi(ctx.Query("toid")) + repoid, err = strconv.Atoi(ctx.Query("repoid")) + if ctx.User.Point < Qty { + ctx.Flash.Error("余额不足!") + return + } + err = models.TransferPoint(ctx.User.ID, + ctx.Query("why"), + int64(toid), + Qty) + if err != nil { + ctx.ServerError("Transfer", err) + return + } + err = models.NewFund(ctx.User.Name, + int64(repoid), + int64(Qty)) + if err != nil { + ctx.ServerError("Transfer", err) + return + } + +// ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink()) +} + diff --git a/routers/repo/repo.go b/routers/repo/repo.go index bfd0c771b0585..46873328fc5de 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -481,3 +481,4 @@ func Status(ctx *context.Context) { "err": task.Errors, }) } + diff --git a/routers/routes/routes.go b/routers/routes/routes.go index d864b8694fd9d..7c12ced7ff739 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -838,8 +838,11 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/raw/*", repo.WikiRaw) }, repo.MustEnableWiki) -// m.Get("/funds", repo.Funds) - + m.Group("/funds", func(){ + m.Get("", repo.Funds) + m.Post("/action/funding",repo.Funding) + },context.RepoRef()) + m.Group("/activity", func() { m.Get("", repo.Activity) m.Get("/:period", repo.Activity) diff --git a/templates/repo/fund/list.tmpl b/templates/repo/fund/list.tmpl index 3a2273c7dda0a..ed1e06feda2e0 100644 --- a/templates/repo/fund/list.tmpl +++ b/templates/repo/fund/list.tmpl @@ -3,33 +3,22 @@ {{template "repo/header" .}}
{{template "base/alert" .}} - {{template "repo/sub_menu" .}} -

- {{.i18n.Tr "repo.default_branch"}} -

- -
- - - - - - - -
- {{range .Funds}} - {{.Name}} {{.Qty}} - {{end}} - -
- {{.CsrfTokenHtml}} - - - - - -
-
+
+
+ {{.CsrfTokenHtml}} + + + + + + +
+
+
    + {{range .Funds}} +
  • {{.Name}} {{.Qty}}
  • + {{end}} +
{{template "base/footer" .}} diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index 91397c2dadc1a..d3c15fcd39abe 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -96,8 +96,8 @@ {{.i18n.Tr "repo.activity"}} {{end}} - - 捐赠 + + 捐赠 {{template "custom/extra_tabs" .}} From a97e5c1f065c047a546a96d784f3cb47a46870bf Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 29 Oct 2019 23:12:22 +0800 Subject: [PATCH 09/11] improve --- models/pull.go | 27 ++++++++++++++++----------- models/transfer_point.go | 10 +++++----- routers/repo/fund.go | 6 ++---- routers/user/profile.go | 4 ++-- templates/explore/repo_list.tmpl | 2 +- templates/explore/trans.tmpl | 3 +-- templates/explore/users.tmpl | 2 +- templates/repo/fund/list.tmpl | 16 ++++++++-------- templates/user/profile.tmpl | 7 +++---- 9 files changed, 39 insertions(+), 38 deletions(-) diff --git a/models/pull.go b/models/pull.go index d4f1cb2513923..0083523fdca91 100644 --- a/models/pull.go +++ b/models/pull.go @@ -441,10 +441,6 @@ func (pr *PullRequest) SetMerged() (err error) { return err } - if err = pr.GetHeadRepo(); err != nil { - return err - } - if err = pr.Issue.changeStatus(sess, pr.Merger, true); err != nil { return fmt.Errorf("Issue.changeStatus: %v", err) } @@ -452,21 +448,30 @@ func (pr *PullRequest) SetMerged() (err error) { return fmt.Errorf("update pull request: %v", err) } - err = TransferPoint(pr.BaseRepo.OwnerID, - "Contribution to My Project ", - pr.HeadRepo.OwnerID, - int(pr.BaseRepo.NextPoint)) + var FromID = pr.BaseRepo.OwnerName + var Why = "奖励"+pr.BaseRepo.Name+ "项目的贡献" + var ToID = pr.HeadUserName + var Qty = int(pr.BaseRepo.NextPoint) - if err != nil { + if _, err = sess.Insert(&Transfer{FromID: FromID, ToID: ToID, Why: Why, Qty: Qty}); err != nil { return fmt.Errorf("Transfer", err) } + + if _, err = sess.Exec("UPDATE `user` SET point = point + ? WHERE name = ?", Qty, ToID); err != nil { + return fmt.Errorf("Add Point", err) + } + + if _, err = sess.Exec("UPDATE `user` SET point = point - ? WHERE name = ?", Qty, FromID); err != nil { + return fmt.Errorf("Subtract Point", err) + } + var balancePoint = pr.BaseRepo.Point - pr.BaseRepo.NextPoint var nextPoint = int64(float64(balancePoint) * float64(pr.BaseRepo.Percent) * 0.01) pr.BaseRepo.Point = balancePoint pr.BaseRepo.NextPoint = nextPoint //TODO NextPoint没被更新 - if _, err = sess.ID(pr.BaseRepo.ID).Cols("Point, NextPoint").Update(pr.BaseRepo); err != nil { - return fmt.Errorf("update base repo point: %v", err) + if _, err = sess.ID(pr.BaseRepo.ID).Cols("point, nextpoint").Update(pr.BaseRepo); err != nil { + return fmt.Errorf("Update base repo point: %v", err) } if err = sess.Commit(); err != nil { diff --git a/models/transfer_point.go b/models/transfer_point.go index c84ebb1324000..ea1b89494ec0e 100644 --- a/models/transfer_point.go +++ b/models/transfer_point.go @@ -13,8 +13,8 @@ import ( type Transfer struct { ID int64 `xorm:"pk autoincr"` - FromID int64 - ToID int64 + FromID string + ToID string Why string Qty int CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` @@ -62,7 +62,7 @@ func SearchTrans(opts *SearchTransOptions) (trans []*Transfer, _ int64, _ error) return trans, count, sess.OrderBy(opts.OrderBy.String()).Find(&trans) } -func TransferPoint(FromID int64, Why string, ToID int64, Qty int) (err error) { +func TransferPoint(FromID string, Why string, ToID string, Qty int) (err error) { sess := x.NewSession() @@ -75,11 +75,11 @@ func TransferPoint(FromID int64, Why string, ToID int64, Qty int) (err error) { return err } - if _, err = sess.Exec("UPDATE `user` SET point = point + ? WHERE id = ?", Qty, ToID); err != nil { + if _, err = sess.Exec("UPDATE `user` SET point = point + ? WHERE name = ?", Qty, ToID); err != nil { return err } - if _, err = sess.Exec("UPDATE `user` SET point = point - ? WHERE id = ?", Qty, FromID); err != nil { + if _, err = sess.Exec("UPDATE `user` SET point = point - ? WHERE name = ?", Qty, FromID); err != nil { return err } return sess.Commit() diff --git a/routers/repo/fund.go b/routers/repo/fund.go index ae4042dbdfb42..4a4aaa0d4cef2 100644 --- a/routers/repo/fund.go +++ b/routers/repo/fund.go @@ -40,17 +40,15 @@ func Funding(ctx *context.Context) { ctx.Flash.Error("请输入数字") return } - var toid int var repoid int - toid, err = strconv.Atoi(ctx.Query("toid")) repoid, err = strconv.Atoi(ctx.Query("repoid")) if ctx.User.Point < Qty { ctx.Flash.Error("余额不足!") return } - err = models.TransferPoint(ctx.User.ID, + err = models.TransferPoint(ctx.User.Name, ctx.Query("why"), - int64(toid), + ctx.Query("toid"), Qty) if err != nil { ctx.ServerError("Transfer", err) diff --git a/routers/user/profile.go b/routers/user/profile.go index f3ebaa0351821..2725a23e40729 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -287,9 +287,9 @@ func TransferP(ctx *context.Context) { ctx.Flash.Error("余额不足!") return } - err = models.TransferPoint(ctx.User.ID, + err = models.TransferPoint(ctx.User.Name, ctx.Query("why"), - u.ID, + u.Name, Qty) if err != nil { ctx.ServerError("Transfer", err) diff --git a/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl index b1aec3b4d8322..5903e19c5a6b4 100644 --- a/templates/explore/repo_list.tmpl +++ b/templates/explore/repo_list.tmpl @@ -9,7 +9,7 @@ {{if or $.PageIsExplore $.PageIsProfileStarList }}{{if .Owner}}{{.Owner.Name}} / {{end}}{{end}}{{.Name}} {{if .IsArchived}}{{end}} - 下一贡献奖励 {{.NextPoint}} 积分 + {{if gt .NextPoint 0}}下一贡献奖励 {{.NextPoint}} 积分{{end}} {{if .IsPrivate}} {{else if .IsFork}} diff --git a/templates/explore/trans.tmpl b/templates/explore/trans.tmpl index ac933c790c151..8bb3feac81716 100644 --- a/templates/explore/trans.tmpl +++ b/templates/explore/trans.tmpl @@ -8,8 +8,7 @@ {{range .Trans}}
- - {{.FromID}} {{.Why}} {{.Qty}} {{.ToID}} + {{.CreatedUnix.FormatShort}} {{.FromID}}转了{{.Qty}}积分给{{.ToID}} ,以{{.Why}}。
{{else}} diff --git a/templates/explore/users.tmpl b/templates/explore/users.tmpl index 8c0f6d96edbeb..3b69a66155753 100644 --- a/templates/explore/users.tmpl +++ b/templates/explore/users.tmpl @@ -9,7 +9,7 @@
- {{.Name}} {{.FullName}} {{.Point}} + {{.Name}} {{.FullName}} 积分 {{.Point}}
{{if .Location}} {{.Location}} diff --git a/templates/repo/fund/list.tmpl b/templates/repo/fund/list.tmpl index ed1e06feda2e0..5670c87520054 100644 --- a/templates/repo/fund/list.tmpl +++ b/templates/repo/fund/list.tmpl @@ -6,19 +6,19 @@
{{.CsrfTokenHtml}} - - +
-
    - {{range .Funds}} -
  • {{.Name}} {{.Qty}}
  • - {{end}} -
+
    + {{range .Funds}} +
  • {{.Name}} {{.Qty}}
  • + {{end}} +
- +
+
{{template "base/footer" .}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 7701257deb88e..f1b77b59cbd6a 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -15,7 +15,7 @@ {{end}}
{{if .Owner.FullName}}{{.Owner.FullName}}{{end}} - {{.Owner.Name}} {{.Owner.Point}} + {{.Owner.Name}}