-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: daz-3ux <[email protected]>
- Loading branch information
Showing
9 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)"` | ||
} |