mirror of https://github.com/rocboss/paopao-ce
parent
bc0530efc7
commit
199f77e70c
@ -0,0 +1,87 @@
|
|||||||
|
// Copyright 2023 ROC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package dbr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Following struct {
|
||||||
|
*Model
|
||||||
|
User *User `json:"-" gorm:"foreignKey:ID;references:FollowId"`
|
||||||
|
UserId int64 `json:"user_id"`
|
||||||
|
FollowId int64 `json:"friend_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Following) GetFollowing(db *gorm.DB, userId, followId int64) (*Following, error) {
|
||||||
|
var following Following
|
||||||
|
err := db.Omit("User").Unscoped().Where("user_id = ? AND follow_id = ?", userId, followId).First(&following).Error
|
||||||
|
if err != nil {
|
||||||
|
logrus.Debugf("Following.GetFollowing get following error:%s", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &following, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Following) DelFollowing(db *gorm.DB, userId, followId int64) error {
|
||||||
|
return db.Omit("User").Unscoped().Where("user_id = ? AND follow_id = ?", userId, followId).Delete(f).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Following) ListFollows(db *gorm.DB, userId int64, limit int, offset int) (res []*Following, total int64, err error) {
|
||||||
|
db = db.Model(f).Where("user_id=?", userId)
|
||||||
|
if err = db.Count(&total).Error; err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if offset >= 0 && limit > 0 {
|
||||||
|
db = db.Offset(offset).Limit(limit)
|
||||||
|
}
|
||||||
|
db.Joins("User").Order(clause.OrderByColumn{Column: clause.Column{Table: "User", Name: "nickname"}, Desc: false})
|
||||||
|
if err = db.Find(&res).Error; err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Following) ListFollowingIds(db *gorm.DB, userId int64, limit, offset int) (ids []int64, total int64, err error) {
|
||||||
|
db = db.Model(f).Where("follow_id=?", userId)
|
||||||
|
if err = db.Count(&total).Error; err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if offset >= 0 && limit > 0 {
|
||||||
|
db = db.Offset(offset).Limit(limit)
|
||||||
|
}
|
||||||
|
if err = db.Omit("User").Select("user_id").Find(&ids).Error; err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Following) FollowCount(db *gorm.DB, userId int64) (follows int64, followings int64, err error) {
|
||||||
|
if err = db.Model(f).Where("user_id=?", userId).Count(&follows).Error; err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = db.Model(f).Where("follow_id=?", userId).Count(&followings).Error; err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Following) IsFollow(db *gorm.DB, userId int64, followId int64) bool {
|
||||||
|
if _, err := s.GetFollowing(db, userId, followId); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Following) Create(db *gorm.DB) (*Following, error) {
|
||||||
|
err := db.Omit("User").Create(f).Error
|
||||||
|
return f, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Following) UpdateInUnscoped(db *gorm.DB) error {
|
||||||
|
return db.Unscoped().Omit("User").Save(c).Error
|
||||||
|
}
|
@ -0,0 +1,104 @@
|
|||||||
|
// Copyright 2023 ROC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package jinzhu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/rocboss/paopao-ce/internal/core"
|
||||||
|
"github.com/rocboss/paopao-ce/internal/core/ms"
|
||||||
|
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ core.FollowingManageService = (*followingManageSrv)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
type followingManageSrv struct {
|
||||||
|
db *gorm.DB
|
||||||
|
f *dbr.Following
|
||||||
|
u *dbr.User
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFollowingManageService(db *gorm.DB) core.FollowingManageService {
|
||||||
|
return &followingManageSrv{
|
||||||
|
db: db,
|
||||||
|
f: &dbr.Following{},
|
||||||
|
u: &dbr.User{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *followingManageSrv) FollowUser(userId int64, followId int64) error {
|
||||||
|
if _, err := s.f.GetFollowing(s.db, userId, followId); err != nil {
|
||||||
|
following := &dbr.Following{
|
||||||
|
UserId: userId,
|
||||||
|
FollowId: followId,
|
||||||
|
}
|
||||||
|
if _, err = following.Create(s.db); err != nil {
|
||||||
|
logrus.Errorf("contactManageSrv.fetchOrNewContact create new contact err:%s", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *followingManageSrv) UnfollowUser(userId int64, followId int64) error {
|
||||||
|
return s.f.DelFollowing(s.db, userId, followId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *followingManageSrv) ListFollows(userId int64, limit, offset int) (*ms.ContactList, error) {
|
||||||
|
follows, totoal, err := s.f.ListFollows(s.db, userId, limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &ms.ContactList{
|
||||||
|
Total: totoal,
|
||||||
|
}
|
||||||
|
for _, f := range follows {
|
||||||
|
res.Contacts = append(res.Contacts, ms.ContactItem{
|
||||||
|
UserId: f.User.ID,
|
||||||
|
Username: f.User.Username,
|
||||||
|
Nickname: f.User.Nickname,
|
||||||
|
Avatar: f.User.Avatar,
|
||||||
|
IsFollow: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *followingManageSrv) ListFollowings(userId int64, limit, offset int) (*ms.ContactList, error) {
|
||||||
|
followingIds, totoal, err := s.f.ListFollowingIds(s.db, userId, limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
followings, err := s.u.ListUserInfoById(s.db, followingIds)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &ms.ContactList{
|
||||||
|
Total: totoal,
|
||||||
|
}
|
||||||
|
for _, user := range followings {
|
||||||
|
res.Contacts = append(res.Contacts, ms.ContactItem{
|
||||||
|
UserId: user.ID,
|
||||||
|
Username: user.Username,
|
||||||
|
Nickname: user.Nickname,
|
||||||
|
Avatar: user.Avatar,
|
||||||
|
IsFollow: s.IsFollow(userId, user.ID),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *followingManageSrv) GetFollowCount(userId int64) (int64, int64, error) {
|
||||||
|
return s.f.FollowCount(s.db, userId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *followingManageSrv) IsFollow(userId int64, followId int64) bool {
|
||||||
|
if _, err := s.f.GetFollowing(s.db, userId, followId); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS `p_following`;
|
@ -0,0 +1,11 @@
|
|||||||
|
CREATE TABLE `p_following` (
|
||||||
|
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`user_id` bigint unsigned NOT NULL,
|
||||||
|
`follow_id` bigint unsigned NOT NULL,
|
||||||
|
`is_del` tinyint NOT NULL DEFAULT 0, -- 是否删除, 0否, 1是
|
||||||
|
`created_on` bigint unsigned NOT NULL DEFAULT '0',
|
||||||
|
`modified_on` bigint unsigned NOT NULL DEFAULT '0',
|
||||||
|
`deleted_on` bigint unsigned NOT NULL DEFAULT '0',
|
||||||
|
PRIMARY KEY (`id`) USING BTREE,
|
||||||
|
KEY `idx_following_user_follow` (`user_id`,`follow_id`) USING BTREE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS p_following;
|
@ -0,0 +1,10 @@
|
|||||||
|
CREATE TABLE p_following (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
user_id BIGINT NOT NULL,
|
||||||
|
follow_id BIGINT NOT NULL,
|
||||||
|
is_del SMALLINT NOT NULL DEFAULT 0, -- 是否删除, 0否, 1是
|
||||||
|
created_on BIGINT NOT NULL DEFAULT 0,
|
||||||
|
modified_on BIGINT NOT NULL DEFAULT 0,
|
||||||
|
deleted_on BIGINT NOT NULL DEFAULT 0
|
||||||
|
);
|
||||||
|
CREATE INDEX idx_following_user_follow ON p_following USING btree (user_id, follow_id);
|
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS "p_following";
|
@ -0,0 +1,15 @@
|
|||||||
|
CREATE TABLE "p_following" (
|
||||||
|
"id" integer NOT NULL,
|
||||||
|
"user_id" integer NOT NULL,
|
||||||
|
"follow_id" integer NOT NULL,
|
||||||
|
"is_del" integer NOT NULL,
|
||||||
|
"created_on" integer NOT NULL,
|
||||||
|
"modified_on" integer NOT NULL,
|
||||||
|
"deleted_on" integer NOT NULL,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
CREATE INDEX "idx_following_user_follow"
|
||||||
|
ON "p_following" (
|
||||||
|
"user_id" ASC,
|
||||||
|
"follow_id" ASC
|
||||||
|
);
|
Loading…
Reference in new issue