Skip to content

Commit

Permalink
MOD:DB
Browse files Browse the repository at this point in the history
  • Loading branch information
timidsmile committed May 10, 2018
1 parent bc0034c commit d11a9b3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 15 deletions.
22 changes: 22 additions & 0 deletions action/session/Login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@ import (
"github.com/gin-gonic/gin"
"net/http"
"fmt"
"github.com/timidsmile/pspace/service"
"github.com/timidsmile/pspace/model"
)

func LoginAction(c *gin.Context) {
value, exist := c.GetQuery("key")
if !exist {
value = "master"
}

s := service.UserBasicService{}
ret := s.GetBlogAdmin(1,"123")

fmt.Println(ret)

if ret == nil {
user := &model.UserBasic{
UserID:"123",
UserName: "test",
Mobile: "test",
Email: "test",
Passwd: "test",
NickName: "test",
AvataUrl: "test",
Status: 1,
}

s.Insert(user);
}
c.Data(http.StatusOK, "text/plain", []byte(fmt.Sprintf("goodbye %s !\n", value)))
return
}
6 changes: 3 additions & 3 deletions components/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func InitDb(cfg *setting.Config) error {
var err error
dbCfg := dsn(cfg.PspaceDb)

fmt.Println(dbCfg)

if Db, err = gorm.Open("mysql", dbCfg); err != nil {
return err
}

if cfg.Debug {
Db.LogMode(true)
}
Db.LogMode(true)

return nil
}
2 changes: 1 addition & 1 deletion config/dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ host = "127.0.0.1"
port = 3306
user = "root"
pwd = "root"
dbname = "pspace"
name = "pspace"
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func main() {

router := router.LoadRouters()

router.Run("127.0.0.1:8080") // listen and serve on 0.0.0.0:8080
router.Run() // listen and serve on 0.0.0.0:8080
}
2 changes: 1 addition & 1 deletion model/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package model
type BasicMdl struct {
ID uint64 `gorm:"primary_key" json:"id"`
CreateTime uint64 `json:"createdTime"`
UpdatedTime uint64 `json:"updatedTime"`
UpdateTime uint64 `json:"updatedTime"`
}
17 changes: 10 additions & 7 deletions model/userBasic.go → model/userBasicMdl.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package model

type User struct {
type UserBasic struct {
BasicMdl
UserID string `gorm:"size:32 column:user_id" json:"userID"`
UserName string `gorm:"size:32 column:user_name" json:"userName"`
mobile string `gorm:"size:32" json:"mobile"`
email string `gorm:"size:32" json:"email"`
passwd string `gorm:"size:32" json:"passwd"`
Nickname string `gorm:"size:32" json:"nickname"`
AvatarURL string `gorm:"size:255" json:"avatarURL"`
status int8 `gorm:"size:255" json:"status"`
Mobile string `gorm:"size:32" json:"mobile"`
Email string `gorm:"size:32" json:"email"`
Passwd string `gorm:"size:32" json:"passwd"`
NickName string `gorm:"size:32" json:"nickname"`
AvataUrl string `gorm:"size:255" json:"avatarURL"` // TODO: AvatarUrl
Status int8 `gorm:"size:255" json:"status"`

}

Expand All @@ -28,3 +28,6 @@ type User struct {
*/


func (UserBasic) TableName() string {
return "user_basic"
}
16 changes: 14 additions & 2 deletions service/userBasicService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/timidsmile/pspace/model"
)

type userBasicService struct {
type UserBasicService struct {
mutex *sync.Mutex
}

func (u *userBasicService) insert(user *model.User) error {
func (u *UserBasicService) Insert(user *model.UserBasic) error {
tx := components.Db.Begin()
if err := tx.Create(user).Error; nil != err {
tx.Rollback()
Expand All @@ -21,3 +21,15 @@ func (u *userBasicService) insert(user *model.User) error {

return nil
}


func (srv *UserBasicService) GetBlogAdmin(id uint64, userID string) *model.UserBasic {
ret := &model.UserBasic{}
if err := components.Db.Where("`id` = ? AND `user_id` = ?",
1,userID).Order("`id` asc").First(ret).Error; nil != err {

return nil
}

return ret
}

0 comments on commit d11a9b3

Please sign in to comment.