Skip to content

Commit

Permalink
Merge pull request #5 from li1553770945/dev
Browse files Browse the repository at this point in the history
feat(project):实现项目管理功能
  • Loading branch information
li1553770945 authored Apr 11, 2024
2 parents cbfcb43 + 83be2f9 commit 5c0558d
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 30 deletions.
2 changes: 2 additions & 0 deletions biz/container/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"personal-page-be/biz/internal/service/file"
"personal-page-be/biz/internal/service/global_service"
"personal-page-be/biz/internal/service/message"
"personal-page-be/biz/internal/service/project"
"personal-page-be/biz/internal/service/user"
)

Expand All @@ -35,6 +36,7 @@ func GetContainer(path string) *Container {
global_service.NewGlobalService,
message.NewMessageService,
chat.NewChatService,
project.NewProjectService,

NewContainer,
))
Expand Down
4 changes: 3 additions & 1 deletion biz/container/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions biz/internal/constant/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package constant

const MaxProjectsNum = 50
9 changes: 5 additions & 4 deletions biz/internal/domain/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

type ProjectEntity struct {
do.BaseModel
Name string `json:"file_name"`
Desc string `json:"desc"`
Link string `json:"link"`
Date string `json:"date"`
Name string `vd:"len($)>5" json:"name"`
Desc string `vd:"len($)>5" json:"desc"`
Status string `vd:"len($)>2" json:"status"`
Link string `vd:"len($)>5" json:"link"`
Period string `vd:"len($)>5" json:"period"`
}
2 changes: 1 addition & 1 deletion biz/internal/repo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type IRepository interface {
RemoveProject(projectID uint) error
GetProject(projectID uint) (*domain.ProjectEntity, error)
GetProjectsNum() (int64, error)
GetProjects(from uint, end uint) (*[]domain.ProjectEntity, error)
GetProjects(from int, end int) (*[]domain.ProjectEntity, error)
}

type Repository struct {
Expand Down
2 changes: 1 addition & 1 deletion biz/internal/repo/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (Repo *Repository) GetProjectsNum() (int64, error) {
}
func (Repo *Repository) GetProjects(start int, end int) (*[]domain.ProjectEntity, error) {
var projects []domain.ProjectEntity
err := Repo.DB.Offset(start).Limit(end - start).Find(&projects).Error
err := Repo.DB.Offset(start).Limit(end - start - 1).Find(&projects).Error
if err != nil {
return nil, err
}
Expand Down
20 changes: 20 additions & 0 deletions biz/internal/service/error_type/error_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package error_type

import "fmt"

type ErrorType struct {
Code int
Message string
}

func (e *ErrorType) Error() string {
return fmt.Sprintf("Code: %d, Message: %s", e.Code, e.Message)
}

var (
ErrBadRequest = &ErrorType{Code: 4001, Message: "参数错误"}
ErrNotFound = &ErrorType{Code: 4004, Message: "未找到相关数据"}
ErrNotLogin = &ErrorType{Code: 4003, Message: "您还未登录"}
ErrNotPermitted = &ErrorType{Code: 4103, Message: "您无权执行此操作"}
ErrInternal = &ErrorType{Code: 5001, Message: "系统错误,请联系管理员"}
)
7 changes: 0 additions & 7 deletions biz/internal/service/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ import (
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"os"
"personal-page-be/biz/internal/constant"
"personal-page-be/biz/internal/domain"
"personal-page-be/biz/internal/repo"
U "personal-page-be/biz/internal/utils"
"strconv"
"time"
)

type FileService struct {
Repo repo.IRepository
Logger *logrus.Logger
}

func GenerateFileKey() string {
return U.RandSeq(4)

Expand Down
5 changes: 5 additions & 0 deletions biz/internal/service/file/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"personal-page-be/biz/internal/repo"
)

type FileService struct {
Repo repo.IRepository
Logger *logrus.Logger
}

type IFileService interface {
UploadFile(ctx context.Context, c *app.RequestContext)
DownloadFile(ctx context.Context, c *app.RequestContext)
Expand Down
25 changes: 25 additions & 0 deletions biz/internal/service/global_service/common_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package global_service

import (
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/sessions"
"personal-page-be/biz/internal/domain"
"personal-page-be/biz/internal/service/error_type"
)

func (s *GlobalService) CheckLogin(c *app.RequestContext, needAdmin bool) (*error_type.ErrorType, *domain.UserEntity) {
session := sessions.Default(c)
v := session.Get("username")
if v == nil {
return error_type.ErrNotLogin, nil
}
user, err := s.Repo.FindUser(v.(string))
if err != nil {
return error_type.ErrNotLogin, nil
}

if needAdmin && user.Role != "admin" {
return error_type.ErrNotPermitted, nil
}
return nil, user
}
3 changes: 3 additions & 0 deletions biz/internal/service/global_service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/sirupsen/logrus"
"personal-page-be/biz/internal/domain"
"personal-page-be/biz/internal/repo"
"personal-page-be/biz/internal/service/error_type"
)

type GlobalService struct {
Expand All @@ -16,6 +18,7 @@ type IGlobalService interface {
HealthCheck(ctx context.Context, c *app.RequestContext)
DeleteFile()
StartCronDeleteFile()
CheckLogin(c *app.RequestContext, needAdmin bool) (*error_type.ErrorType, *domain.UserEntity)
}

func NewGlobalService(repo repo.IRepository, logger *logrus.Logger) IGlobalService {
Expand Down
17 changes: 10 additions & 7 deletions biz/internal/service/project/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package project
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"personal-page-be/biz/infra/config"
"github.com/sirupsen/logrus"
"personal-page-be/biz/internal/repo"
"personal-page-be/biz/internal/service/global_service"
)

type ProjectService struct {
Repo repo.IRepository
Config *config.Config
Repo repo.IRepository
GlobalService global_service.IGlobalService
Logger *logrus.Logger
}

type IProjectService interface {
AddProject(ctx context.Context, c *app.RequestContext)
RemoveProject(ctx context.Context, c *app.RequestContext)
GetPages(ctx context.Context, c *app.RequestContext)
GetNum(ctx context.Context, c *app.RequestContext)
GetProjects(ctx context.Context, c *app.RequestContext)
}

func NewProjectService(repo repo.IRepository, config *config.Config) IProjectService {
func NewProjectService(repo repo.IRepository, globalService global_service.IGlobalService, logger *logrus.Logger) IProjectService {
return &ProjectService{
Repo: repo,
Config: config,
Repo: repo,
GlobalService: globalService,
Logger: logger,
}
}
65 changes: 57 additions & 8 deletions biz/internal/service/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"personal-page-be/biz/internal/constant"
"personal-page-be/biz/internal/domain"
"personal-page-be/biz/internal/service/error_type"
"strconv"
)

Expand All @@ -22,34 +24,52 @@ func (s *ProjectService) AddProject(ctx context.Context, c *app.RequestContext)
err = s.Repo.SaveProject(&project)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"code": error_type.ErrInternal.Code,
"msg": err.Error(),
})
return
}
c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "添加成功",
"data": project.ID,
})
}

func (s *ProjectService) RemoveProject(ctx context.Context, c *app.RequestContext) {
err0, _ := s.GlobalService.CheckLogin(c, true)
if err0 != nil {
c.JSON(consts.StatusOK, utils.H{
"code": err0.Code,
"msg": err0.Message,
})
return
}

projectID := c.Param("id")
projectIDInt, err := strconv.ParseUint(projectID, 10, 64)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 2001,
"msg": "参数错误",
"code": error_type.ErrBadRequest.Code,
"msg": error_type.ErrBadRequest.Message,
})
return
}

project, err := s.Repo.GetProject(uint(projectIDInt))

if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": err.Error(),
})
return
}
if project.ID == 0 {
c.JSON(consts.StatusOK, utils.H{
"code": error_type.ErrNotFound.Code,
"msg": error_type.ErrNotFound.Message,
})
return
}

err = s.Repo.RemoveProject(project.ID)
Expand All @@ -67,18 +87,47 @@ func (s *ProjectService) RemoveProject(ctx context.Context, c *app.RequestContex
return
}

func (s *ProjectService) GetPages(ctx context.Context, c *app.RequestContext) {

func (s *ProjectService) GetNum(ctx context.Context, c *app.RequestContext) {
count, err := s.Repo.GetProjectsNum()
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": err.Error(),
})
}
c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "获取成功",
"data": count,
})
}

func (s *ProjectService) GetProjects(ctx context.Context, c *app.RequestContext) {
start := c.DefaultQuery("start", "0")
startInt, err := strconv.Atoi(start)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 4001,
"msg": err.Error(),
})
}

end := c.DefaultQuery("end", strconv.Itoa(startInt+10))
endInt, err := strconv.Atoi(end)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 4001,
"msg": err.Error(),
})
}

if endInt-startInt > constant.MaxProjectsNum {
endInt = startInt + constant.MaxProjectsNum + 1
}

projects, err := s.Repo.GetProjects(startInt, endInt)

c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "获取成功",
"data": projects,
})
}
2 changes: 1 addition & 1 deletion router.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5c0558d

Please sign in to comment.