diff --git a/api/interface.go b/api/interface.go index a0543ca..ecf1a60 100644 --- a/api/interface.go +++ b/api/interface.go @@ -25,6 +25,7 @@ type ILinkV1 interface { GetLinkFriend(ctx context.Context, req *request.GetLinkFriendReq) (res *request.GetLinkFriendRes, err error) AddLinkFriend(ctx context.Context, req *request.AddLinkFriendReq) (res *request.AddLinkFriendRes, err error) GetSortAndLink(ctx context.Context, req *request.GetSortAndLinkReq) (res *request.GetSortAndLinkRes, err error) + DelLinkFriend(ctx context.Context, req *request.DelLinkFriendReq) (res *request.DelLinkFriendRes, err error) } type IUserV1 interface { diff --git a/api/request/linkApi.go b/api/request/linkApi.go index c8a00d3..7f96937 100644 --- a/api/request/linkApi.go +++ b/api/request/linkApi.go @@ -11,7 +11,11 @@ type AddLinkFriendReq struct { type GetSortAndLinkReq struct { g.Meta `path:"/sort" tags:"获取分类和友链" method:"get" summary:"获取分类和友链"` } +type DelLinkFriendReq struct { + g.Meta `path:"/delete" tags:"删除友链" method:"delete" summary:"删除友链"` +} type GetLinkFriendRes struct{} type AddLinkFriendRes struct{} type GetSortAndLinkRes struct{} +type DelLinkFriendRes struct{} diff --git a/internal/controller/auth/link/custom/friend/friendController.go b/internal/controller/auth/link/custom/friend/friendController.go index 885ae77..baae686 100644 --- a/internal/controller/auth/link/custom/friend/friendController.go +++ b/internal/controller/auth/link/custom/friend/friendController.go @@ -85,3 +85,39 @@ func (*ControllerV1) AddLinkFriend(ctx context.Context, _ *request.AddLinkFriend } return res, err } + +// DelLinkFriend +// +// 删除友链 +func (*ControllerV1) DelLinkFriend(ctx context.Context, _ *request.DelLinkFriendReq) (res *request.DelLinkFriendRes, err error) { + req := ghttp.RequestFromCtx(ctx) + // 获取业务 + delFriendVO := entity.LinkDelFriendVO{} + err = req.GetRequestStruct(&delFriendVO) + if err == nil { + // 检查对象 + errStruct := g.Validator().Data(delFriendVO).Run(ctx) + if errStruct == nil { + hasDel, info := linkService().DelLinkFriendCustom(req, delFriendVO) + if hasDel { + ResultUtil.Success(req, "删除成功", nil) + } else { + switch info { + case "FriendLinkDoesNotExist": + ResultUtil.ErrorNoData(req, ErrorCode.FriendLinkDoesNotExist) + case "DelLinkFriendError": + ResultUtil.ErrorNoData(req, ErrorCode.ServerDatabaseInteriorError) + default: + ResultUtil.ErrorNoData(req, ErrorCode.ServerUnknownError) + } + } + } else { + g.Log().Cat("Struct").Cat("Link").Notice(ctx, errStruct.Map()) + ResultUtil.Error(req, ErrorCode.RequestBodyError, errStruct.Error()) + } + } else { + g.Log().Cat("Struct").Cat("Link").Error(ctx, err.Error()) + ResultUtil.Error(req, ErrorCode.RequestBodyError, err.Error()) + } + return res, err +} diff --git a/internal/dao/linkDAO/linkFriendDAO.go b/internal/dao/linkDAO/linkFriendDAO.go index bbcbceb..08cd3b1 100644 --- a/internal/dao/linkDAO/linkFriendDAO.go +++ b/internal/dao/linkDAO/linkFriendDAO.go @@ -108,6 +108,28 @@ func GetBlogForName(linkName string) *do.BlogDO { } } +// GetBlogForId +// +// 检查是否已存在此博客 +func GetBlogForId(id uint64) *do.BlogDO { + // 数据库读取信息 + var blogDO do.BlogDO + result, err := g.Model("xf_blog").Where("id = ?", id).One() + if err == nil { + if !result.IsEmpty() { + _ = result.Struct(&blogDO) + g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表成功提取", id, "博客信息") + return &blogDO + } else { + g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表中没有", id, "博客友链相关信息") + return nil + } + } else { + g.Log().Cat("Database").Cat("Blog").Error(context.Background(), err.Error()) + return nil + } +} + // GetBlogForDomain // // 检查是否存在相似链接 @@ -144,3 +166,18 @@ func CreateBlog(newBlogDO do.BlogDO) bool { return false } } + +// DeleteBlog +// +// 删除博客 +func DeleteBlog(id uint64) bool { + // 数据库读取信息 + _, err := g.Model("xf_blog").Where("id = ?", id).Delete() + if err == nil { + g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表成功删除", id, "博客信息") + return true + } else { + g.Log().Cat("Database").Cat("Blog").Error(context.Background(), err.Error()) + return false + } +} diff --git a/internal/logic/LinkServiceImpl/FriendServiceImpl.go b/internal/logic/LinkServiceImpl/FriendServiceImpl.go index 424c647..95c1fda 100644 --- a/internal/logic/LinkServiceImpl/FriendServiceImpl.go +++ b/internal/logic/LinkServiceImpl/FriendServiceImpl.go @@ -101,3 +101,21 @@ func (*DefaultLinkImpl) AddLinkFriendCustom(req *ghttp.Request, addFriendVO enti return false, nil, "FriendLinkAlreadyExists" } } + +// DelLinkFriendCustom +// +// 删除友链 +func (*DefaultLinkImpl) DelLinkFriendCustom(_ *ghttp.Request, delFriendVO entity.LinkDelFriendVO) (bool, string) { + // 检查是否已存在此博客 + blogDO := linkDAO.GetBlogForId(delFriendVO.Id) + if blogDO != nil { + // 删除博客 + if linkDAO.DeleteBlog(*blogDO.Id) { + return true, "" + } else { + return false, "DelLinkFriendError" + } + } else { + return false, "FriendLinkDoesNotExist" + } +} diff --git a/internal/model/entity/linkDelFriendVO.go b/internal/model/entity/linkDelFriendVO.go new file mode 100644 index 0000000..6378cea --- /dev/null +++ b/internal/model/entity/linkDelFriendVO.go @@ -0,0 +1,6 @@ +package entity + +type LinkDelFriendVO struct { + Id uint64 `json:"id" v:"required|regex:^[0-9]+$#请输入id|id格式错误"` + LinkEmail string `json:"link_email" v:"required|email#请输入邮箱|邮箱格式错误"` +} diff --git a/internal/service/LinkService/linkService.go b/internal/service/LinkService/linkService.go index 3de91f9..c870bd9 100644 --- a/internal/service/LinkService/linkService.go +++ b/internal/service/LinkService/linkService.go @@ -15,4 +15,5 @@ type LinkService interface { GetLinkFriend(*ghttp.Request) GetSortAndLink(*ghttp.Request) AddLinkFriendCustom(*ghttp.Request, entity.LinkAddFriendVO) (bool, *do.BlogDO, string) + DelLinkFriendCustom(*ghttp.Request, entity.LinkDelFriendVO) (bool, string) } diff --git a/utility/ErrorCode/ErrorCode.go b/utility/ErrorCode/ErrorCode.go index 3fdbb59..7550f30 100644 --- a/utility/ErrorCode/ErrorCode.go +++ b/utility/ErrorCode/ErrorCode.go @@ -29,6 +29,7 @@ var ( LinkAddressError = ErrorCode{output: "LinkAddressError", code: 40303, message: "链接地址错误"} LikeLinkAlreadyExists = ErrorCode{output: "LikeLinkAlreadyExists", code: 40304, message: "链接已存在"} LinkAcceptDenied = ErrorCode{output: "LinkAcceptDenied", code: 40305, message: "链接接受协议拒绝"} + FriendLinkDoesNotExist = ErrorCode{output: "FriendLinkDoesNotExist", code: 40306, message: "友链不存在"} ServerUnknownError = ErrorCode{output: "ServerUnknownError", code: 50000, message: "服务器未知错误"} ServerDatabaseInteriorError = ErrorCode{output: "ServerDatabaseInteriorError", code: 50001, message: "服务器数据库内部错误"} )