init:搭建了整个项目的框架,初步写好了用户系统
This commit is contained in:
43
internal/api/user.go
Normal file
43
internal/api/user.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package api
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type CreateUserResponse struct {
|
||||
}
|
||||
|
||||
type RenameByIDRequest struct {
|
||||
ID uint `json:"id"`
|
||||
NewUsername string `json:"new_username"`
|
||||
}
|
||||
|
||||
type RenameByIDResponse struct {
|
||||
}
|
||||
|
||||
type FindByIDRequest struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
|
||||
type FindByIDResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
type FindByUsernameRequest struct {
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
type FindByUsernameResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
type ChangePasswordRequest struct {
|
||||
ID uint `json:"id"`
|
||||
NewPassword string `json:"new_password"`
|
||||
}
|
||||
|
||||
type ChangePasswordResponse struct {
|
||||
}
|
||||
38
internal/config/loadconfig.go
Normal file
38
internal/config/loadconfig.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig `yaml:"server"`
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Port string `yaml:"port"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
DBName string `yaml:"dbname"`
|
||||
}
|
||||
|
||||
func Load(filename string) (Config, error) {
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
34
internal/database/database.go
Normal file
34
internal/database/database.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"feedsystem_video_go/internal/config"
|
||||
"feedsystem_video_go/internal/model"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NewDB(dbcfg config.DatabaseConfig) (*gorm.DB, error) {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
dbcfg.User, dbcfg.Password, dbcfg.Host, dbcfg.Port, dbcfg.DBName)
|
||||
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func AutoMigrate(db *gorm.DB) error {
|
||||
return db.AutoMigrate(&model.User{})
|
||||
}
|
||||
|
||||
func CloseDB(db *gorm.DB) error {
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sqlDB.Close()
|
||||
}
|
||||
86
internal/handler/user.go
Normal file
86
internal/handler/user.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"feedsystem_video_go/internal/api"
|
||||
"feedsystem_video_go/internal/model"
|
||||
"feedsystem_video_go/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
userService *service.UserService
|
||||
}
|
||||
|
||||
func NewUserHandler(userService *service.UserService) *UserHandler {
|
||||
return &UserHandler{userService: userService}
|
||||
}
|
||||
func (h *UserHandler) CreateUser(c *gin.Context) {
|
||||
var req api.CreateUserRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.userService.CreateUser(&model.User{
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
}); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"message": "user created"})
|
||||
}
|
||||
|
||||
func (h *UserHandler) RenameByID(c *gin.Context) {
|
||||
var req api.RenameByIDRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.userService.RenameByID(req.ID, req.NewUsername); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"message": "user renamed"})
|
||||
}
|
||||
|
||||
func (h *UserHandler) ChangePassword(c *gin.Context) {
|
||||
var req api.ChangePasswordRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.userService.ChangePassword(req.ID, req.NewPassword); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"message": "password changed"})
|
||||
}
|
||||
|
||||
func (h *UserHandler) FindByID(c *gin.Context) {
|
||||
var req api.FindByIDRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if user, err := h.userService.FindByID(req.ID); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, user)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *UserHandler) FindByUsername(c *gin.Context) {
|
||||
var req api.FindByUsernameRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if user, err := h.userService.FindByUsername(req.Username); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, user)
|
||||
}
|
||||
}
|
||||
7
internal/model/user.go
Normal file
7
internal/model/user.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package model
|
||||
|
||||
type User struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Username string `gorm:"unique" json:"username"`
|
||||
Password string `json:"-"`
|
||||
}
|
||||
52
internal/repository/user.go
Normal file
52
internal/repository/user.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"feedsystem_video_go/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
}
|
||||
|
||||
func (ur *UserRepository) CreateUser(user *model.User) error {
|
||||
if err := ur.db.Create(user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) RenameByID(id uint, newUsername string) error {
|
||||
if err := ur.db.Model(&model.User{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) ChangePassword(id uint, newPassword string) error {
|
||||
if err := ur.db.Model(&model.User{}).Where("id = ?", id).Update("password", newPassword).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) FindByID(id uint) (*model.User, error) {
|
||||
var user model.User
|
||||
if err := ur.db.First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) FindByUsername(username string) (*model.User, error) {
|
||||
var user model.User
|
||||
if err := ur.db.Where("username = ?", username).First(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
28
internal/router/router.go
Normal file
28
internal/router/router.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"feedsystem_video_go/internal/handler"
|
||||
"feedsystem_video_go/internal/repository"
|
||||
"feedsystem_video_go/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SetRouter(db *gorm.DB) *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
userRepository := repository.NewUserRepository(db)
|
||||
userService := service.NewUserService(userRepository)
|
||||
userHandler := handler.NewUserHandler(userService)
|
||||
userGroup := r.Group("/user")
|
||||
{
|
||||
userGroup.POST("/register", userHandler.CreateUser)
|
||||
userGroup.POST("/rename", userHandler.RenameByID)
|
||||
userGroup.POST("/changePassword", userHandler.ChangePassword)
|
||||
userGroup.POST("/findByID", userHandler.FindByID)
|
||||
userGroup.POST("/findByUsername", userHandler.FindByUsername)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
51
internal/service/user.go
Normal file
51
internal/service/user.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"feedsystem_video_go/internal/model"
|
||||
"feedsystem_video_go/internal/repository"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
userRepository *repository.UserRepository
|
||||
}
|
||||
|
||||
func NewUserService(userRepository *repository.UserRepository) *UserService {
|
||||
return &UserService{userRepository: userRepository}
|
||||
}
|
||||
|
||||
func (us *UserService) CreateUser(user *model.User) error {
|
||||
if err := us.userRepository.CreateUser(user); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) RenameByID(id uint, newUsername string) error {
|
||||
if err := us.userRepository.RenameByID(id, newUsername); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) ChangePassword(id uint, newPassword string) error {
|
||||
if err := us.userRepository.ChangePassword(id, newPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) FindByID(id uint) (*model.User, error) {
|
||||
if user, err := us.userRepository.FindByID(id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return user, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (us *UserService) FindByUsername(username string) (*model.User, error) {
|
||||
if user, err := us.userRepository.FindByUsername(username); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return user, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user