diff --git a/go.mod b/go.mod index 191b158..4205881 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 425f205..3e8773d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/dazBlog/biz/biz.go b/internal/dazBlog/biz/biz.go new file mode 100644 index 0000000..fd3d527 --- /dev/null +++ b/internal/dazBlog/biz/biz.go @@ -0,0 +1,35 @@ +// Copyright 2023 daz-3ux(杨鹏达) . 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) +} diff --git a/internal/dazBlog/biz/doc.go b/internal/dazBlog/biz/doc.go new file mode 100644 index 0000000..898437f --- /dev/null +++ b/internal/dazBlog/biz/doc.go @@ -0,0 +1,8 @@ +// Copyright 2023 daz-3ux(杨鹏达) . 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" diff --git a/internal/dazBlog/biz/user/user.go b/internal/dazBlog/biz/user/user.go new file mode 100644 index 0000000..e09818c --- /dev/null +++ b/internal/dazBlog/biz/user/user.go @@ -0,0 +1,52 @@ +// Copyright 2023 daz-3ux(杨鹏达) . 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 + +} diff --git a/internal/dazBlog/controller/v1/user/user.go b/internal/dazBlog/controller/v1/user/user.go new file mode 100644 index 0000000..3c463c4 --- /dev/null +++ b/internal/dazBlog/controller/v1/user/user.go @@ -0,0 +1,6 @@ +// Copyright 2023 daz-3ux(杨鹏达) . 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 diff --git a/internal/dazBlog/store/store.go b/internal/dazBlog/store/store.go index 01b62f0..fd5ac3a 100644 --- a/internal/dazBlog/store/store.go +++ b/internal/dazBlog/store/store.go @@ -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 diff --git a/internal/pkg/errno/user.go b/internal/pkg/errno/user.go new file mode 100644 index 0000000..06638cc --- /dev/null +++ b/internal/pkg/errno/user.go @@ -0,0 +1,11 @@ +// Copyright 2023 daz-3ux(杨鹏达) . 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"} +) diff --git a/pkg/api/dazBlog/v1/user.go b/pkg/api/dazBlog/v1/user.go new file mode 100644 index 0000000..fb935f4 --- /dev/null +++ b/pkg/api/dazBlog/v1/user.go @@ -0,0 +1,14 @@ +// Copyright 2023 daz-3ux(杨鹏达) . 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)"` +}