Skip to content

Commit

Permalink
feat(site): add sort functionality to sites
Browse files Browse the repository at this point in the history
- Add sort field to Site struct and StSite model
- Implement sorting logic in site list and export functions- Update site creation and update functions to handle sort value
- Modify database queries to support sorting by sort field
- Update UI to display and allow editing of site sort order
  • Loading branch information
ch3nnn committed Jan 31, 2025
1 parent 3d88517 commit 705ccb4
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 32 deletions.
22 changes: 12 additions & 10 deletions api/v1/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (
)

type Site struct {
Id int `json:"id"` // ID
Thumb string `json:"thumb"` // 网站 logo
Title string `json:"title"` // 名称简介
Url string `json:"url"` // 链接
Category string `json:"category"` // 分类
CategoryId int `json:"category_id"` // 分类id
Description string `json:"description"` // 描述
IsUsed bool `json:"is_used"` // 是否启用
CreatedAt string `json:"created_at"` // 创建时间
UpdatedAt string `json:"updated_at"` // 更新时间
Id int `json:"id"` // ID
Thumb string `json:"thumb"` // 网站 logo
Title string `json:"title"` // 名称简介
Url string `json:"url"` // 链接
Category string `json:"category"` // 分类
CategoryId int `json:"category_id"` // 分类id
Description string `json:"description"` // 描述
IsUsed bool `json:"is_used"` // 是否启用
Sort int `json:"sort" form:"sort"` // 排序
CreatedAt string `json:"created_at"` // 创建时间
UpdatedAt string `json:"updated_at"` // 更新时间
}

type (
Expand Down Expand Up @@ -78,6 +79,7 @@ type (
Description string `json:"description" form:"description"` // 描述
IsUsed *bool `json:"is_used" form:"is_used"` // 是否启用
File *multipart.FileHeader `json:"file"` // 上传 logo 图片
Sort int `json:"sort" form:"sort"` // 排序
}

SiteUpdateResp struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/dal/model/st_site.gen.go

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

6 changes: 5 additions & 1 deletion internal/dal/query/st_site.gen.go

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

7 changes: 7 additions & 0 deletions internal/dal/repository/st_site.gen.go

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

6 changes: 3 additions & 3 deletions internal/dal/repository/st_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
// TODO Custom DaoFunc ....
// ...

FindSiteCategoryWithPage(page, pageSize int, result any, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error)
FindSiteCategoryWithPage(page, pageSize int, result any, orderColumns []field.Expr, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error)
}

// not edit interface name
Expand Down Expand Up @@ -77,7 +77,7 @@ func (d *customStSiteDao) LikeInByTitleOrDescOrURL(search string) func(dao gen.D
}
}

func (d *customStSiteDao) FindSiteCategoryWithPage(page, pageSize int, result any, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error) {
func (d *customStSiteDao) FindSiteCategoryWithPage(page, pageSize int, result any, orderColumns []field.Expr, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error) {
return d.stSiteDo.
Select(
field.NewAsterisk(query.StSite.TableName()),
Expand All @@ -88,7 +88,7 @@ func (d *customStSiteDao) FindSiteCategoryWithPage(page, pageSize int, result an
query.StCategory.ID.EqCol(query.StSite.CategoryID),
).
Order(
query.StSite.CreatedAt.Desc(),
orderColumns...,
).
Scopes(whereFunc...).
ScanByPage(result, (page-1)*pageSize, pageSize)
Expand Down
4 changes: 4 additions & 0 deletions internal/service/index/Index.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func categorySites(sites []*model.StSite, treeNodes []*v1.TreeNode) (data []*v1.
categorySite.SiteList = append(categorySite.SiteList, *site)
}
}
// Sort 字段进行升序排序
sort.Slice(categorySite.SiteList, func(i, j int) bool {
return categorySite.SiteList[i].Sort < categorySite.SiteList[j].Sort
})

if len(categorySite.SiteList) > 0 {
data = append(data, categorySite)
Expand Down
1 change: 1 addition & 0 deletions internal/service/site/batchcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (s *service) BatchCreate(ctx context.Context, req *v1.SiteCreateReq) (*v1.S
URL: url,
CategoryID: req.CategoryID,
IsUsed: req.IsUsed,
Sort: 0,
})
if err != nil {
failURLs = append(failURLs, url)
Expand Down
7 changes: 6 additions & 1 deletion internal/service/site/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ package site
import (
"strconv"

"github.com/ch3nnn/webstack-go/internal/dal/query"
"github.com/gin-gonic/gin"
excelize "github.com/xuri/excelize/v2"
"gorm.io/gen"
"gorm.io/gen/field"

v1 "github.com/ch3nnn/webstack-go/api/v1"
"github.com/ch3nnn/webstack-go/internal/dal/repository"
Expand All @@ -22,17 +24,20 @@ var (
)

func (s *service) Export(ctx *gin.Context, req *v1.SiteExportReq) (resp *v1.SiteExportResp, err error) {
var orderColumns []field.Expr
orderColumns = append(orderColumns, query.StSite.CreatedAt.Desc())

var whereFunc []func(dao gen.Dao) gen.Dao
if req.Search != "" {
whereFunc = append(whereFunc, s.siteRepository.LikeInByTitleOrDescOrURL(req.Search))
}
if req.CategoryID != 0 {
whereFunc = append(whereFunc, s.siteRepository.WhereByCategoryID(req.CategoryID))
orderColumns = []field.Expr{query.StSite.Sort.Asc()}
}

var siteCategories []repository.SiteCategory
_, err = s.siteRepository.WithContext(ctx).FindSiteCategoryWithPage(1, 10000, &siteCategories, whereFunc...)
_, err = s.siteRepository.WithContext(ctx).FindSiteCategoryWithPage(1, 10000, &siteCategories, orderColumns, whereFunc...)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion internal/service/site/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ import (
"time"

v1 "github.com/ch3nnn/webstack-go/api/v1"
"github.com/ch3nnn/webstack-go/internal/dal/query"
"github.com/ch3nnn/webstack-go/internal/dal/repository"
"gorm.io/gen"
"gorm.io/gen/field"
)

func (s *service) List(ctx context.Context, req *v1.SiteListReq) (resp *v1.SiteListResp, err error) {
var orderColumns []field.Expr
orderColumns = append(orderColumns, query.StSite.CreatedAt.Desc())

var whereFunc []func(dao gen.Dao) gen.Dao
if req.Search != "" {
whereFunc = append(whereFunc, s.siteRepository.LikeInByTitleOrDescOrURL(req.Search))
}
if req.CategoryID != 0 {
whereFunc = append(whereFunc, s.siteRepository.WhereByCategoryID(req.CategoryID))
orderColumns = []field.Expr{query.StSite.Sort.Asc()} // 同分类网址按排序升序
}

var siteCategories []repository.SiteCategory
count, err := s.siteRepository.WithContext(ctx).FindSiteCategoryWithPage(req.Page, req.PageSize, &siteCategories, whereFunc...)
count, err := s.siteRepository.WithContext(ctx).FindSiteCategoryWithPage(req.Page, req.PageSize, &siteCategories, orderColumns, whereFunc...)
if err != nil {
return nil, err
}
Expand All @@ -41,6 +46,7 @@ func (s *service) List(ctx context.Context, req *v1.SiteListReq) (resp *v1.SiteL
CategoryId: siteCategory.StSite.CategoryID,
Description: siteCategory.StSite.Description,
IsUsed: siteCategory.StSite.IsUsed,
Sort: siteCategory.StSite.Sort,
CreatedAt: siteCategory.StSite.CreatedAt.Format(time.DateTime),
UpdatedAt: siteCategory.StSite.UpdatedAt.Format(time.DateTime),
}
Expand Down
3 changes: 3 additions & 0 deletions internal/service/site/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (s *service) Update(ctx *gin.Context, req *v1.SiteUpdateReq) (resp *v1.Site
if req.IsUsed != nil {
update["IsUsed"] = req.IsUsed
}
if req.Sort >= 0 {
update["Sort"] = req.Sort
}

_, err = s.siteRepository.WithContext(ctx).Update(update, s.siteRepository.WhereByID(req.Id))
if err != nil {
Expand Down
33 changes: 18 additions & 15 deletions web/templates/site/site_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<th>创建日期</th>
<th>更新日期</th>
<th style="text-align: center; ">状态</th>
<th style="text-align: center;">排序</th> <!-- 新增排序列 -->
<th style="text-align: center; ">操作</th>
</tr>
</thead>
Expand Down Expand Up @@ -166,8 +167,9 @@
'<td>' + value.category + '</td>' +
'<td>' + value.created_at + '</td>' +
'<td>' + value.updated_at + '</td>' +
'<td style="text-align: center; ">' + showUsedBadge + '</td>' +
'<td style="text-align: center; ">' +
'<td style="text-align: center;">' + showUsedBadge + '</td>' +
'<td style="text-align: center;">' + value.sort + '</td>' + <!-- 新增排序列 -->
'<td style="text-align: center;">' +
'<div class="btn-group">' +
' <a class="btn btn-xs btn-default btn-sync" href="#!" title=""' +
' data-id="' + value.id + '"' +
Expand All @@ -192,6 +194,7 @@
' data-title="' + value.title + '"' +
' data-description="' + value.description + '"' +
' data-category_id="' + value.category_id + '"' +
' data-sort_order="' + value.sort + '"' + <!-- 新增排序数据 -->
' data-toggle="tooltip" data-original-title="编辑">编辑</a>' +
'</div>' +
'</td>' +
Expand Down Expand Up @@ -241,7 +244,7 @@
text: '关闭',
action: function () {
const currentPage = document.querySelector('.page-link.pageActive').textContent;
getPageListData(parseInt(currentPage), 10, document.getElementById('search-keyword').value);
getPageListData(parseInt(currentPage), 10, document.getElementById('search-keyword').value, $("#category-filter").val());
}
}
}
Expand All @@ -263,9 +266,7 @@
// 详情
$(document).on('click', '.btn-detail', function () {
const business_title = $(this).attr('data-title');
const business_thumb = $(this).attr('data-thumb');
const business_description = $(this).attr('data-description');

$.alert({
title: '详情',
content: '名称简介 :' + business_title + '<br/>描述:' + business_description,
Expand Down Expand Up @@ -321,7 +322,7 @@
text: '关闭',
action: function () {
const currentPage = document.querySelector('.page-link.pageActive').textContent;
getPageListData(parseInt(currentPage), 10, document.getElementById('search-keyword').value);
getPageListData(parseInt(currentPage), 10, document.getElementById('search-keyword').value, $("#category-filter").val());
}
}
}
Expand Down Expand Up @@ -372,7 +373,7 @@
text: '关闭',
action: function () {
const currentPage = document.querySelector('.page-link.pageActive').textContent;
getPageListData(parseInt(currentPage), 10, document.getElementById('search-keyword').value);
getPageListData(parseInt(currentPage), 10, document.getElementById('search-keyword').value, $("#category-filter").val());
}
}
}
Expand All @@ -399,6 +400,8 @@
const url = $(this).attr('data-url');
const description = $(this).attr('data-description');
const category_id = $(this).attr('data-category_id');
const sort = $(this).attr('data-sort_order'); // 获取排序数据

$.confirm({
title: '编辑网站',
//content: 'url:form.html', // 也可以采用url形式
Expand Down Expand Up @@ -428,6 +431,11 @@
'<div>' +
' <label class="control-label">网站描述</label>' +
' <textarea class="form-control" maxlength="225" rows="2" id="description" placeholder="网站描述" id="title">' + description + '</textarea>' +
'</div>' +
'<div>' +
' <label>排序</label>' +
' <small class="help-block">(相同分类下网站排序)</small>'+
' <input autofocus="" type="number" placeholder="请输入排序值" class="form-control" id="sort_order" value="' + sort + '">' + <!-- 新增排序输入框 -->
'</div>',
onOpen: function () {
// bootstrap-fileinput 初始化引导文件输入插件
Expand Down Expand Up @@ -502,18 +510,15 @@
formData.append('description', description);
formData.append('file', $("#file")[0].files[0]);
formData.append('category_id', $("#category_id").val());
formData.append('sort', $("#sort_order").val()); // 添加排序值

AjaxMultipartForm(
"PUT",
"/api/admin/site/" + id,
formData,
function () {
$(this).hide();
$("#btnLoading").show();
},
function (data) {
$("#btnLoading").hide();
$("#btnOk").show();

$.alert({
title: '操作成功',
icon: 'mdi mdi-check-decagram',
Expand All @@ -524,15 +529,13 @@
text: '关闭',
action: function () {
const currentPage = $(".page-link.pageActive").text(); // 获取当前页码
getPageListData(currentPage, 10, $("#search-keyword").val()); // 重新加载当前页数据
getPageListData(currentPage, 10, $("#search-keyword").val(), $("#category-filter").val()); // 重新加载当前页数据
}
}
}
});
},
function (response) {
$("#btnLoading").hide();
$("#btnOk").show();
AjaxError(response);
}
);
Expand Down

0 comments on commit 705ccb4

Please sign in to comment.