Skip to content

Commit

Permalink
docs: update DB and api
Browse files Browse the repository at this point in the history
Signed-off-by: daz-3ux <[email protected]>
  • Loading branch information
Daz-3ux committed Oct 16, 2023
1 parent fe4bbd5 commit 2149ff8
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 79 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ docker run --network=host \
dazblog-image:latest
```

#### 数据库配置
[初始化数据库](./docs/devel/zh-CN/conversions/DB.md)

## Documentation
### 实现功能
[openAPI文档](api/openapi/openapi.yaml)
Expand Down
56 changes: 30 additions & 26 deletions configs/dBlog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

-- MariaDB dump 10.19-11.1.2-MariaDB, for Linux (x86_64)
--
-- Host: 127.0.0.1 Database: dazBlog
-- Host: 127.0.0.1 Database: dazblog
-- ------------------------------------------------------
-- Server version 11.1.2-MariaDB-1:11.1.2+maria~ubu2204
-- Server version 10.11.5-MariaDB-1:10.11.5+maria~ubu2204

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
Expand All @@ -21,56 +21,60 @@
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Current Database: `dazBlog`
-- Current Database: `dazblog`
--

/*!40000 DROP DATABASE IF EXISTS `dazBlog`*/;
/*!40000 DROP DATABASE IF EXISTS `dazblog`*/;

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `dazBlog` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `dazblog` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci */;

USE `dazBlog`;
USE `dazblog`;

--
-- Table structure for table `posts`
-- Table structure for table `post`
--

DROP TABLE IF EXISTS `posts`;
DROP TABLE IF EXISTS `post`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `posts` (
CREATE TABLE `post` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`postID` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`username` varchar(30) NOT NULL,
`postID` varchar(100) NOT NULL,
`title` varchar(150) NOT NULL,
`content` longtext NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT current_timestamp(),
`updatedAt` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `postID` (`postID`),
UNIQUE KEY `title` (`title`),
KEY `username` (`username`)
) ENGINE=MyISAM AUTO_INCREMENT=38 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
KEY `idx_username` (`username`),
CONSTRAINT `fk_post_username` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `users`
-- Table structure for table `user`
--

DROP TABLE IF EXISTS `users`;
DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
CREATE TABLE `user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`postcount` int(11) NOT NULL DEFAULT 0,
`username` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL,
`nickname` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`phone` varchar(16) NOT NULL,
`nickname` varchar(30) NOT NULL,
`email` varchar(320) NOT NULL,
`gender` enum('Male','Female','Other') DEFAULT NULL,
`phone` varchar(16) DEFAULT NULL,
`qq` varchar(16) DEFAULT NULL,
`createdAt` timestamp NOT NULL DEFAULT current_timestamp(),
`updatedAt` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

Expand All @@ -82,4 +86,4 @@ CREATE TABLE `users` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2023-09-26 21:08:37
-- Dump completed on 2023-10-16 21:27:31
2 changes: 1 addition & 1 deletion configs/dazBlog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ db:
host: 127.0.0.1
username: root
password: passwd
database: dazBlog
database: dazblog
max-idle-connections: 100
max-open-connections: 100
max-connection-life-time: 10s
Expand Down
52 changes: 52 additions & 0 deletions docs/devel/zh-CN/conversions/DB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 为 dBlog 创建数据库
- 使用 Docker + MariaDB 创建数据库
- sqldump 数据相关详细信息查看:
[sql](/internal/pkg/model/README.md)

### 创建数据库
- 启动数据库实例
```shell
docker run -p 3306:3306 --name dazblogDB -e MARIADB_ROOT_PASSWORD=passwd -d mariadb:lts
```
- 初始化数据库
```sql
CREATE DATABASE `dazblog`;
USE `dazblog`;
```

- 初始化用户表
```sql
CREATE TABLE `user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`postcount` int(11) NOT NULL DEFAULT '0',
`username` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL,
`nickname` varchar(30) NOT NULL,
`email` varchar(320) NOT NULL,
`gender` ENUM('Male', 'Female', 'Other') DEFAULT NULL,
`phone` varchar(16),
`qq` varchar(16),
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB, AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```

- 初始化博客表
```sql
CREATE TABLE `post` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`postID` varchar(100) NOT NULL,
`title` varchar(150) NOT NULL,
`content` longtext NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
PRIMARY KEY (`id`),
UNIQUE KEY `postID` (`postID`),
KEY `idx_username` (`username`),
CONSTRAINT fk_post_username FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
22 changes: 15 additions & 7 deletions internal/dazBlog/biz/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func (b *userBiz) Get(ctx context.Context, id string) (*v1.GetUserResponse, erro

// List is the implementation of the `List` method of the UserBiz interface
func (b *userBiz) List(ctx context.Context, offset, limit int) (*v1.ListUserResponse, error) {
count, list, err := b.ds.Users().List(ctx, offset, limit)
userCount, list, err := b.ds.Users().List(ctx, offset, limit)
if err != nil {
log.C(ctx).Errorw("failed to list users from storage", "err", err)
return nil, err
}

users := make([]*v1.UserInfo, 0, len(list))
for _, user := range list {
count, _, err := b.ds.Posts().List(ctx, user.Username, 0, 0)
postCount, _, err := b.ds.Posts().List(ctx, user.Username, 0, 0)
if err != nil {
log.C(ctx).Errorw("Failed to list posts", "err", err)
return nil, err
Expand All @@ -105,16 +105,18 @@ func (b *userBiz) List(ctx context.Context, offset, limit int) (*v1.ListUserResp
Username: user.Username,
Nickname: user.Nickname,
Email: user.Email,
Gender: user.Gender,
Phone: user.Phone,
PostCount: count,
QQ: user.QQ,
PostCount: postCount,
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: user.UpdatedAt.Format("2006-01-02 15:04:05"),
})
}

log.C(ctx).Debugw("Get users from storage", "count", count)
log.C(ctx).Debugw("Get users from storage", "count", userCount)

return &v1.ListUserResponse{TotalCount: count, Users: users}, nil
return &v1.ListUserResponse{TotalCount: userCount, Users: users}, nil
}

// Update is the implementation of the `Update` method of the UserBiz interface
Expand All @@ -124,15 +126,21 @@ func (b *userBiz) Update(ctx context.Context, username string, user *v1.UpdateUs
return err
}

if *user.Nickname != "" {
userM.Nickname = *user.Nickname
}
if *user.Email != "" {
userM.Email = *user.Email
}
if *user.Nickname != "" {
userM.Nickname = *user.Nickname
if *user.Gender != "" {
userM.Gender = *user.Gender
}
if *user.Phone != "" {
userM.Phone = *user.Phone
}
if *user.QQ != "" {
userM.QQ = *user.QQ
}

if err := b.ds.Users().Update(ctx, userM); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions internal/dazBlog/controller/v1/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (ctrl *UserController) ListUsers(ctx context.Context, r *pb.ListUsersReques
Username: u.Username,
Nickname: u.Nickname,
Email: u.Email,
Gender: u.Gender,
Phone: u.Phone,
Qq: u.QQ,
Postcount: u.PostCount,
CreatedAt: timestamppb.New(createdAt),
UpdatedAt: timestamppb.New(updatedAt),
Expand Down
20 changes: 20 additions & 0 deletions internal/pkg/model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package model

import (
"sync/atomic"
"time"

"gorm.io/gorm"
Expand Down Expand Up @@ -33,3 +34,22 @@ func (p *PostM) BeforeCreate(tx *gorm.DB) error {

return nil
}

func (p *PostM) AfterCreate(tx *gorm.DB) error {
if tx.Error != nil {
return tx.Error
}

var user UserM
if err := tx.Where("username = ?", p.Username).First(&user).Error; err != nil {
return err
}

atomic.AddInt64(&user.PostCount, 1)

if err := tx.Save(&user).Error; err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions internal/pkg/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type UserM struct {
Password string `gorm:"column:password"` // password of the user
Nickname string `gorm:"column:nickname"` // nickname of the user
Email string `gorm:"column:email"` // email of the user
Gender string `gorm:"column:gender"` // gender of the user
Phone string `gorm:"column:phone"` // phone number of the user
QQ string `gorm:"column:qq"` // qq number of the user
CreatedAt time.Time `gorm:"column:createdAt"` // time when the user was created
UpdatedAt time.Time `gorm:"column:updatedAt"` // time when the user was updated
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/api/dazBlog/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ package v1
// CreateUserRequest specifies the request parameters for
// `POST /v1/users`
type CreateUserRequest struct {
Postcount int64 `json:"postcount" valid:"required,stringlength(1|255)"`
Username string `json:"username" valid:"alphanum,required,stringlength(1|255)"`
Postcount int64 `json:"postcount" valid:"stringlength(1|255)"`
Username string `json:"username" valid:"alphanum,required,stringlength(1|30)"`
Password string `json:"password" valid:"required,stringlength(6|18)"`
Nickname string `json:"nickname" valid:"required,stringlength(1|255)"`
Nickname string `json:"nickname" valid:"required,stringlength(1|30)"`
Email string `json:"email" valid:"required,email"`
Gender string `json:"gender" valid:"required"`
Phone string `json:"phone" valid:"required,numeric,stringlength(11|11)"`
QQ string `json:"qq" valid:"numeric,stringlength(5|16)"`
}

// GetUserResponse specifies the response parameters for
// `GET /v1/users/{username}`
type GetUserResponse UserInfo

// UserInfo is the user's all information
// UserInfo is the user's all information that can be listed
type UserInfo struct {
PostCount int64 `json:"postcount"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Email string `json:"email"`
Gender string `json:"gender"`
Phone string `json:"phone"`
PostCount int64 `json:"postcount"`
QQ string `json:"qq"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
Expand All @@ -48,9 +52,11 @@ type ListUserResponse struct {
// UpdateUserRequest specifies the request parameters for
// `PUT /v1/users/{username}`
type UpdateUserRequest struct {
Nickname *string `json:"nickname" valid:"required,stringlength(1|255)"`
Nickname *string `json:"nickname" valid:"stringlength(1|255)"`
Email *string `json:"email" valid:"email"`
Gender *string `json:"gender" valid:"stringlength(1|10)"`
Phone *string `json:"phone" valid:"numeric,stringlength(11|11)"`
QQ *string `json:"qq" valid:"numeric,stringlength(5|16)"`
}

// ChangePasswordRequest specifies the request parameters for
Expand Down
Loading

0 comments on commit 2149ff8

Please sign in to comment.