Skip to content

Commit

Permalink
feat: complete the biz store
Browse files Browse the repository at this point in the history
Signed-off-by: daz-3ux <[email protected]>
  • Loading branch information
Daz-3ux committed Sep 23, 2023
1 parent 1b45113 commit 65cf721
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.1.2
github.com/gosuri/uitable v0.0.4
github.com/jinzhu/copier v0.4.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand Down
35 changes: 35 additions & 0 deletions internal/dazBlog/biz/biz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package biz

import (
"github.com/Daz-3ux/dBlog/internal/dazBlog/biz/user"
"github.com/Daz-3ux/dBlog/internal/dazBlog/store"
)

// IBiz defines the method need to be implemented by the Biz layer
type IBiz interface {
Users() user.UserBiz
}

// ensure that biz implements the IBiz interface
var _ IBiz = (*biz)(nil)

// biz implements the IBiz interface
// biz layer is connected to the store layer in the under
type biz struct {
ds store.IStore
}

// NewBiz create an instance of type IBiz
func NewBiz(ds store.IStore) IBiz {
return &biz{ds: ds}
}

// Users return an instance of UserBiz
func (b *biz) Users() user.UserBiz {
return user.NewUserBiz(b.ds)
}
8 changes: 8 additions & 0 deletions internal/dazBlog/biz/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

// Package biz defines the business logic of the application
// business logic layer
package biz // import "github.com/Daz-3ux/dBlog/internal/dazBlog/biz"
52 changes: 52 additions & 0 deletions internal/dazBlog/biz/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package user

import (
"context"
"github.com/Daz-3ux/dBlog/internal/dazBlog/store"
"github.com/Daz-3ux/dBlog/internal/pkg/errno"
"github.com/Daz-3ux/dBlog/internal/pkg/model"
v1 "github.com/Daz-3ux/dBlog/pkg/api/dazBlog/v1"
"github.com/jinzhu/copier"
"regexp"
)

// UserBiz defines the methods implemented in the user module at the biz layer
// implement the specific implementations of the REST resources for the user
type UserBiz interface {
Create(ctx context.Context, r *v1.CreateUserRequest) error
}

// userBiz implements the UserBiz interface
type userBiz struct {
ds store.IStore
}

// ensure that userBiz implements the UserBiz interface
var _ UserBiz = (*userBiz)(nil)

// NewUserBiz create an instance of type UserBiz
func NewUserBiz(ds store.IStore) UserBiz {
return &userBiz{ds: ds}
}

// Create is the implementation of the `Create` method of the UserBiz interface
func (b *userBiz) Create(ctx context.Context, r *v1.CreateUserRequest) error {
var userM model.UserM
_ = copier.Copy(&userM, r)

if err := b.ds.Users().Create(ctx, &userM); err != nil {
if match, _ := regexp.MatchString("Duplicate entry '.*' for key 'username'", err.Error()); match {
return errno.ErrUserAlreadyExist
}

return err
}

return nil

}
6 changes: 6 additions & 0 deletions internal/dazBlog/controller/v1/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package user
2 changes: 1 addition & 1 deletion internal/dazBlog/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ IStore = (*datastore)(nil)
func NewStore(db *gorm.DB) *datastore {
// ensure that the instance is created only once
once.Do(func() {
S = &datastore{db}
S = &datastore{db: db}
})

return S
Expand Down
11 changes: 11 additions & 0 deletions internal/pkg/errno/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package errno

var (
// ErrUserAlreadyExist represents user already exists
ErrUserAlreadyExist = &Errno{HTTP: 400, Code: "FailedOperation.UserAlreadyExist", Message: "User already exists"}
)
14 changes: 14 additions & 0 deletions pkg/api/dazBlog/v1/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package v1

type CreateUserRequest struct {
Username string `json:"username" valid:"alphanum,required,stringlength(1|255)"`
Password string `json:"password" valid:"required,stringlength(6|18)"`
Nickname string `json:"nickname" valid:"required,stringlength(1|255)"`
Email string `json:"email" valid:"required,email"`
Phone string `json:"phone" valid:"required,numeric,stringlength(11|11)"`
}

0 comments on commit 65cf721

Please sign in to comment.