Skip to content
This repository has been archived by the owner on Nov 9, 2024. It is now read-only.

提交错分支,应该修改dev的修改成了master #4

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions biz/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -16,13 +17,15 @@ type Container struct {
GlobalService global_service.IGlobalService
MessageService message.IMessageService
ChatService chat.IChatService
ProjectService project.IProjectService
}

func NewContainer(config *config.Config, userService user.IUserService,
fileService file.IFileService,
globalService global_service.IGlobalService,
messageService message.IMessageService,
chatService chat.IChatService,
projectService project.IProjectService,
) *Container {
return &Container{
Config: config,
Expand All @@ -31,6 +34,7 @@ func NewContainer(config *config.Config, userService user.IUserService,
GlobalService: globalService,
MessageService: messageService,
ChatService: chatService,
ProjectService: projectService,
}

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

import (
"personal-page-be/biz/internal/do"
)

type ProjectEntity struct {
do.BaseModel
Name string `json:"file_name"`
Desc string `json:"desc"`
Link string `json:"link"`
Date string `json:"date"`
}
8 changes: 8 additions & 0 deletions biz/internal/dto/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dto

type AddProjectReq struct {
Name string `json:"file_name"`
Desc string `json:"desc"`
Link string `json:"link"`
Date string `json:"date"`
}
10 changes: 10 additions & 0 deletions biz/internal/repo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
SaveReply(entity *domain.ReplyEntity) error
FindMessageByID(messageId uint) (*domain.MessageEntity, error)
GetUnreadMsg() (*[]domain.MessageEntity, error)

SaveProject(project *domain.ProjectEntity) error
RemoveProject(projectID uint) error
GetProject(projectID uint) (*domain.ProjectEntity, error)
GetProjectsNum() (int64, error)
GetProjects(from uint, end uint) (*[]domain.ProjectEntity, error)
}

type Repository struct {
Expand Down Expand Up @@ -49,7 +55,11 @@
if err != nil {
panic("迁移回复模型失败:" + err.Error())
}
err = db.AutoMigrate(&domain.ProjectEntity{})
if err != nil {
panic("迁移项目模型失败:" + err.Error())
}
return &Repository{

Check failure on line 62 in biz/internal/repo/interface.go

View workflow job for this annotation

GitHub Actions / build

cannot use &Repository{…} (value of type *Repository) as IRepository value in return statement: *Repository does not implement IRepository (wrong type for method GetProjects)
DB: db,
}
}
48 changes: 48 additions & 0 deletions biz/internal/repo/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package repo

import (
"personal-page-be/biz/internal/domain"
)

func (Repo *Repository) RemoveProject(projectID uint) error {
err := Repo.DB.Delete(&domain.ProjectEntity{}, projectID).Error
if err != nil {
return err
}
return nil
}

func (Repo *Repository) SaveProject(project *domain.ProjectEntity) error {
if project.ID == 0 {
err := Repo.DB.Create(&project).Error
return err
} else {
err := Repo.DB.Save(&project).Error
return err
}
}

func (Repo *Repository) GetProject(projectID uint) (*domain.ProjectEntity, error) {
var project domain.ProjectEntity
err := Repo.DB.Where("id = ?", projectID).Limit(1).Find(&project).Error
if err != nil {
return nil, err
}
return &project, nil
}
func (Repo *Repository) GetProjectsNum() (int64, error) {
var num int64
err := Repo.DB.Model(&domain.ProjectEntity{}).Count(&num).Error
if err != nil {
return 0, err
}
return num, nil
}
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
if err != nil {
return nil, err
}
return &projects, nil
}
27 changes: 27 additions & 0 deletions biz/internal/service/project/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package project

import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"personal-page-be/biz/infra/config"
"personal-page-be/biz/internal/repo"
)

type ProjectService struct {
Repo repo.IRepository
Config *config.Config
}

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

func NewProjectService(repo repo.IRepository, config *config.Config) IProjectService {
return &ProjectService{
Repo: repo,
Config: config,
}
}
84 changes: 84 additions & 0 deletions biz/internal/service/project/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package project

import (
"context"
"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/domain"
"strconv"
)

func (s *ProjectService) AddProject(ctx context.Context, c *app.RequestContext) {
var project domain.ProjectEntity
err := c.BindAndValidate(&project)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 4001,
"msg": err.Error(),
})
return
}
err = s.Repo.SaveProject(&project)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": err.Error(),
})
return
}
c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "添加成功",
})
}

func (s *ProjectService) RemoveProject(ctx context.Context, c *app.RequestContext) {
projectID := c.Param("id")
projectIDInt, err := strconv.ParseUint(projectID, 10, 64)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 2001,
"msg": "参数错误",
})
return
}

project, err := s.Repo.GetProject(uint(projectIDInt))
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": err.Error(),
})
}

err = s.Repo.RemoveProject(project.ID)
if err != nil {
c.JSON(consts.StatusOK, utils.H{
"code": 5001,
"msg": "删除失败:" + err.Error(),
})
return
}
c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "删除成功",
})
return
}

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

c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "获取成功",
})
}

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

c.JSON(consts.StatusOK, utils.H{
"code": 0,
"msg": "获取成功",
})
}
Binary file added personal-page-be.exe
Binary file not shown.
8 changes: 7 additions & 1 deletion router.go

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

Loading