This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
13 changed files
with
441 additions
and
150 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CREATE TABLE `comments_infos` ( | ||
`id` BIGINT(24) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '评论主键id', | ||
`product_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '被评论产品id', | ||
`user_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '评论者id', | ||
`content` varchar(512) DEFAULT NULL COMMENT '评论内容', | ||
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | ||
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', | ||
`deleted_at` TIMESTAMP NULL DEFAULT NULL, | ||
PRIMARY KEY (`id`), | ||
INDEX `idx_comments_infos_deleted_at` (`deleted_at`) USING BTREE, | ||
INDEX `idx_comments_infos_user_id` (`user_id`) USING BTREE, | ||
INDEX `idx_comments_infos_product_id` (`product_id`) USING BTREE | ||
) | ||
COMMENT='评论主表' | ||
COLLATE='utf8mb4_general_ci' | ||
ENGINE=INNODB | ||
; |
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,17 @@ | ||
CREATE TABLE `comments_replys` ( | ||
`id` BIGINT(24) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '评论主键id', | ||
`comment_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '评论主表id', | ||
`user_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '评论者id', | ||
`content` varchar(512) DEFAULT NULL COMMENT '评论内容', | ||
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | ||
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', | ||
`deleted_at` TIMESTAMP NULL DEFAULT NULL, | ||
PRIMARY KEY (`id`), | ||
INDEX `idx_comments_replys_deleted_at` (`deleted_at`) USING BTREE, | ||
INDEX `idx_comments_replys_user_id` (`user_id`) USING BTREE, | ||
INDEX `idx_comments_replys_comment_id` (`comment_id`) USING BTREE | ||
) | ||
COMMENT='评论回复表' | ||
COLLATE='utf8mb4_general_ci' | ||
ENGINE=INNODB | ||
; |
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
package forms | ||
|
||
// 请求评论信息表单 | ||
type CommentListByProductForm struct { | ||
ProductId uint64 `json:"product_id" form:"product_id"` | ||
PageForm | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"C:\\GoPath\\src\\funding\\controllers":1557825931298529100} | ||
{"C:\\GoPath\\src\\funding\\controllers":1557859750063378400} |
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 |
---|---|---|
@@ -1 +1,122 @@ | ||
package models | ||
|
||
import ( | ||
"funding/forms" | ||
"funding/objects" | ||
"github.com/jinzhu/gorm" | ||
"time" | ||
) | ||
|
||
// 主评论表 | ||
type CommentsInfo struct { | ||
BaseModel | ||
ProductId uint64 `json:"product_id"` // 产品 ID | ||
UserId uint64 `json:"user_id"` // 用户 ID | ||
IsSeller bool `json:"is_seller"` // 是否是对应卖家 | ||
Content string `json:"content"` // 评论内容 | ||
} | ||
|
||
// 返回给前端的评论信息 | ||
type ResultCommentInfo struct { | ||
ID uint64 `json:"id" gorm:"primary_key"` | ||
ProductId uint64 `json:"product_id"` // 产品 ID | ||
UserId uint64 `json:"user_id"` // 用户 ID | ||
IsSeller bool `json:"is_seller"` // 是否是对应卖家 | ||
Content string `json:"content"` // 评论内容 | ||
CreatedAt time.Time `json:"created_at"` // 创建时间 | ||
Username string `json:"username"` // 用户名 | ||
IconUrl string `json:"icon_url"` // 用户头像 | ||
Replys []ResultCommentReply `json:"replys"` // 回复信息 | ||
} | ||
|
||
///////////////////// 基本增删改查 ///////////////////// | ||
|
||
// 根据主评论的 ID 来获取主评论条目 | ||
func FindCommentsInfoById(commentsInfoId uint64) (*CommentsInfo, error) { | ||
var ret CommentsInfo | ||
err := db.First(&ret, commentsInfoId).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ret, nil | ||
} | ||
|
||
// 根据用户 ID 来获取主评论列表 | ||
func FindCommentsInfosByUserId(userId uint64) ([]*CommentsInfo, error) { | ||
var rets []*CommentsInfo | ||
err := db.Where("user_id = ?", userId).Find(&rets).Error | ||
return rets, err | ||
} | ||
|
||
// 根据产品 ID 来获取主评论列表 | ||
func FindCommentsInfosByProductId(productId uint64) ([]*CommentsInfo, error) { | ||
var rets []*CommentsInfo | ||
err := db.Where("product_id = ?", productId).Find(&rets).Error | ||
return rets, err | ||
} | ||
|
||
// 新增主评论 | ||
func InsertCommentsInfo(commentsInfo *CommentsInfo) error { | ||
err := db.Create(commentsInfo).Error | ||
return err | ||
} | ||
|
||
//删除主评论条目 由于这里是软删除,所以只是把 delete_at 设置了一个值,实际上还存在数据库中,但并不能用 gorm 查到 | ||
func DeleteCommentsInfoById(id uint64) error { | ||
err := db.Delete(CommentsInfo{}, "id = ?", id).Error | ||
return err | ||
} | ||
|
||
//根据 commentsInfoID 来更新其他相应的字段 | ||
func UpdateCommentsInfo(commentsInfo *CommentsInfo) error { | ||
var rec CommentsInfo | ||
err := db.First(&rec, "id = ?", commentsInfo.ID).Error | ||
if err != nil { | ||
return err | ||
} | ||
err = db.Model(&rec).Updates(commentsInfo).Error | ||
return err | ||
} | ||
|
||
///////////////////// EMD 基本增删改查 ///////////////////// | ||
// 根据产品 ID 获取对应产品的评论 | ||
const sqlGetResultCommentInfosByProductId = ` | ||
SELECT | ||
c.*,u.username,u.icon_url | ||
FROM | ||
comments_infos c JOIN | ||
users u ON c.user_id = u.id | ||
WHERE | ||
c.deleted_at IS NULL | ||
AND u.deleted_at IS NULL | ||
AND c.product_id = (?) | ||
ORDER BY | ||
id DESC | ||
` | ||
|
||
// 获取回复列表 | ||
func GetResultCommentInfosByProductId(form *forms.CommentListByProductForm) ([]ResultCommentInfo, error) { | ||
results := []ResultCommentInfo{} | ||
|
||
//TODO 先不做分页 | ||
//page, pageSize := 1, 10 | ||
//// 如果页码和每页数量大于 0 | ||
//if form.Page > 0 && form.PageSize > 0 { | ||
// page = form.Page | ||
// pageSize = form.PageSize | ||
//} | ||
|
||
err := db.Raw(sqlGetResultCommentInfosByProductId, form.ProductId).Scan(&results).Error | ||
if err != nil { | ||
return nil, resultError.NewFallFundingErr("查询评论时发生了错误") | ||
} | ||
|
||
for i, _ := range results { | ||
replys, err := GetResultCommentReplaysByCommentId(results[i].ID) | ||
if err != nil && gorm.IsRecordNotFoundError(err) { | ||
continue | ||
} | ||
results[i].Replys = replys | ||
} | ||
return results, err | ||
} |
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
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
Oops, something went wrong.