diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-09-24 13:02:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-24 13:02:49 +0800 |
commit | 5a438ee3c0303efcb9d1935ff521917fe8a109e8 (patch) | |
tree | 8f36452631fc8a6d077addbbf857da6d50024a0a /models/context.go | |
parent | 730065a3dc72683460b95ba1486dba8ec355a373 (diff) | |
download | gitea-5a438ee3c0303efcb9d1935ff521917fe8a109e8.tar.gz gitea-5a438ee3c0303efcb9d1935ff521917fe8a109e8.zip |
Move all mail related codes from models to services/mailer (#7200)
* move all mail related codes from models to modules/mailer
* fix lint
* use DBContext instead Engine
* use WithContext not WithEngine
* Use DBContext instead of Engine
* don't use defer when sess.Close()
* move DBContext to context.go and add some methods
* move mailer from modules/ to services
* fix lint
* fix tests
* fix fmt
* add gitea copyright
* fix tests
* don't expose db functions
* make code clear
* add DefaultDBContext
* fix build
* fix bug
Diffstat (limited to 'models/context.go')
-rw-r--r-- | models/context.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/models/context.go b/models/context.go new file mode 100644 index 0000000000..5f47c595a2 --- /dev/null +++ b/models/context.go @@ -0,0 +1,55 @@ +// Copyright 2019 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 + +// DBContext represents a db context +type DBContext struct { + e Engine +} + +// DefaultDBContext represents a DBContext with default Engine +func DefaultDBContext() DBContext { + return DBContext{x} +} + +// Committer represents an interface to Commit or Close the dbcontext +type Committer interface { + Commit() error + Close() +} + +// TxDBContext represents a transaction DBContext +func TxDBContext() (DBContext, Committer, error) { + sess := x.NewSession() + if err := sess.Begin(); err != nil { + sess.Close() + return DBContext{}, nil, err + } + + return DBContext{sess}, sess, nil +} + +// WithContext represents executing database operations +func WithContext(f func(ctx DBContext) error) error { + return f(DBContext{x}) +} + +// WithTx represents executing database operations on a trasaction +func WithTx(f func(ctx DBContext) error) error { + sess := x.NewSession() + if err := sess.Begin(); err != nil { + sess.Close() + return err + } + + if err := f(DBContext{sess}); err != nil { + sess.Close() + return err + } + + err := sess.Commit() + sess.Close() + return err +} |