-
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.
- Loading branch information
1 parent
64a1628
commit cbfcb43
Showing
9 changed files
with
201 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
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"` | ||
} |
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 @@ | ||
package dto | ||
|
||
type AddProjectReq struct { | ||
Name string `json:"file_name"` | ||
Desc string `json:"desc"` | ||
Link string `json:"link"` | ||
Date string `json:"date"` | ||
} |
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,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 | ||
} |
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,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, | ||
} | ||
} |
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,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 not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.