mirror of https://github.com/rocboss/paopao-ce
parent
df1744fc64
commit
7636412940
@ -0,0 +1,5 @@
|
||||
// Copyright 2022 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 broker
|
@ -1,3 +1,7 @@
|
||||
// Copyright 2022 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !migration
|
||||
// +build !migration
|
||||
|
@ -1,3 +1,7 @@
|
||||
// Copyright 2022 ROC. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build migration
|
||||
// +build migration
|
||||
|
@ -1,24 +1,36 @@
|
||||
// Copyright 2022 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 core
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
|
||||
)
|
||||
|
||||
type (
|
||||
Comment = dbr.Comment
|
||||
CommentFormated = dbr.CommentFormated
|
||||
CommentReply = dbr.CommentReply
|
||||
CommentContent = dbr.CommentContent
|
||||
CommentReplyFormated = dbr.CommentReplyFormated
|
||||
)
|
||||
|
||||
// CommentService 评论检索服务
|
||||
type CommentService interface {
|
||||
GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error)
|
||||
GetCommentByID(id int64) (*model.Comment, error)
|
||||
GetCommentCount(conditions *model.ConditionsT) (int64, error)
|
||||
GetCommentReplyByID(id int64) (*model.CommentReply, error)
|
||||
GetCommentContentsByIDs(ids []int64) ([]*model.CommentContent, error)
|
||||
GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, error)
|
||||
GetComments(conditions *ConditionsT, offset, limit int) ([]*Comment, error)
|
||||
GetCommentByID(id int64) (*Comment, error)
|
||||
GetCommentCount(conditions *ConditionsT) (int64, error)
|
||||
GetCommentReplyByID(id int64) (*CommentReply, error)
|
||||
GetCommentContentsByIDs(ids []int64) ([]*CommentContent, error)
|
||||
GetCommentRepliesByID(ids []int64) ([]*CommentReplyFormated, error)
|
||||
}
|
||||
|
||||
// CommentManageService 评论管理服务
|
||||
type CommentManageService interface {
|
||||
DeleteComment(comment *model.Comment) error
|
||||
CreateComment(comment *model.Comment) (*model.Comment, error)
|
||||
CreateCommentReply(reply *model.CommentReply) (*model.CommentReply, error)
|
||||
DeleteCommentReply(reply *model.CommentReply) error
|
||||
CreateCommentContent(content *model.CommentContent) (*model.CommentContent, error)
|
||||
DeleteComment(comment *Comment) error
|
||||
CreateComment(comment *Comment) (*Comment, error)
|
||||
CreateCommentReply(reply *CommentReply) (*CommentReply, error)
|
||||
DeleteCommentReply(reply *CommentReply) error
|
||||
CreateCommentContent(content *CommentContent) (*CommentContent, error)
|
||||
}
|
||||
|
@ -1,15 +1,36 @@
|
||||
// Copyright 2022 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 core
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
|
||||
)
|
||||
|
||||
const (
|
||||
MsgTypePost = dbr.MsgTypePost
|
||||
MsgtypeComment = dbr.MsgtypeComment
|
||||
MsgTypeReply = dbr.MsgTypeReply
|
||||
MsgTypeWhisper = dbr.MsgTypeWhisper
|
||||
MsgTypeRequestingFriend = dbr.MsgTypeRequestingFriend
|
||||
MsgTypeSystem = dbr.MsgTypeSystem
|
||||
|
||||
MsgStatusUnread = dbr.MsgStatusUnread
|
||||
MsgStatusReaded = dbr.MsgStatusReaded
|
||||
)
|
||||
|
||||
type (
|
||||
Message = dbr.Message
|
||||
MessageFormated = dbr.MessageFormated
|
||||
)
|
||||
|
||||
// MessageService 消息服务
|
||||
type MessageService interface {
|
||||
CreateMessage(msg *model.Message) (*model.Message, error)
|
||||
CreateMessage(msg *Message) (*Message, error)
|
||||
GetUnreadCount(userID int64) (int64, error)
|
||||
GetMessageByID(id int64) (*model.Message, error)
|
||||
ReadMessage(message *model.Message) error
|
||||
GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*model.MessageFormated, error)
|
||||
GetMessageCount(conditions *model.ConditionsT) (int64, error)
|
||||
GetMessageByID(id int64) (*Message, error)
|
||||
ReadMessage(message *Message) error
|
||||
GetMessages(conditions *ConditionsT, offset, limit int) ([]*MessageFormated, error)
|
||||
GetMessageCount(conditions *ConditionsT) (int64, error)
|
||||
}
|
||||
|
@ -1,13 +1,22 @@
|
||||
// Copyright 2022 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 core
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
|
||||
)
|
||||
|
||||
type (
|
||||
Tag = dbr.Tag
|
||||
TagFormated = dbr.TagFormated
|
||||
)
|
||||
|
||||
// TopicService 话题服务
|
||||
type TopicService interface {
|
||||
CreateTag(tag *model.Tag) (*model.Tag, error)
|
||||
DeleteTag(tag *model.Tag) error
|
||||
GetTags(conditions *model.ConditionsT, offset, limit int) ([]*model.Tag, error)
|
||||
GetTagsByKeyword(keyword string) ([]*model.Tag, error)
|
||||
CreateTag(tag *Tag) (*Tag, error)
|
||||
DeleteTag(tag *Tag) error
|
||||
GetTags(conditions *ConditionsT, offset, limit int) ([]*Tag, error)
|
||||
GetTagsByKeyword(keyword string) ([]*Tag, error)
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
// Copyright 2022 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 core
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
|
||||
)
|
||||
|
||||
const (
|
||||
UserStatusNormal = dbr.UserStatusNormal
|
||||
UserStatusClosed = dbr.UserStatusClosed
|
||||
)
|
||||
|
||||
type (
|
||||
User = dbr.User
|
||||
Post = dbr.Post
|
||||
ConditionsT = dbr.ConditionsT
|
||||
PostFormated = dbr.PostFormated
|
||||
UserFormated = dbr.UserFormated
|
||||
PostContentFormated = dbr.PostContentFormated
|
||||
Model = dbr.Model
|
||||
)
|
@ -1,15 +1,24 @@
|
||||
// Copyright 2022 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 core
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
|
||||
)
|
||||
|
||||
type (
|
||||
WalletStatement = dbr.WalletStatement
|
||||
WalletRecharge = dbr.WalletRecharge
|
||||
)
|
||||
|
||||
// WalletService wallet service interface
|
||||
type WalletService interface {
|
||||
GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error)
|
||||
GetUserWalletBills(userID int64, offset, limit int) ([]*WalletStatement, error)
|
||||
GetUserWalletBillCount(userID int64) (int64, error)
|
||||
GetRechargeByID(id int64) (*model.WalletRecharge, error)
|
||||
CreateRecharge(userId, amount int64) (*model.WalletRecharge, error)
|
||||
HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error
|
||||
HandlePostAttachmentBought(post *model.Post, user *model.User) error
|
||||
GetRechargeByID(id int64) (*WalletRecharge, error)
|
||||
CreateRecharge(userId, amount int64) (*WalletRecharge, error)
|
||||
HandleRechargeSuccess(recharge *WalletRecharge, tradeNo string) error
|
||||
HandlePostAttachmentBought(post *Post, user *User) error
|
||||
}
|
||||
|
@ -1,36 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/plugin/soft_delete"
|
||||
)
|
||||
|
||||
// Model 公共Model
|
||||
type Model struct {
|
||||
ID int64 `gorm:"primary_key" json:"id"`
|
||||
CreatedOn int64 `json:"created_on"`
|
||||
ModifiedOn int64 `json:"modified_on"`
|
||||
DeletedOn int64 `json:"deleted_on"`
|
||||
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag" json:"is_del"`
|
||||
}
|
||||
|
||||
type ConditionsT map[string]any
|
||||
type Predicates map[string][]any
|
||||
|
||||
func (m *Model) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
nowTime := time.Now().Unix()
|
||||
|
||||
tx.Statement.SetColumn("created_on", nowTime)
|
||||
tx.Statement.SetColumn("modified_on", nowTime)
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Model) BeforeUpdate(tx *gorm.DB) (err error) {
|
||||
if !tx.Statement.Changed("modified_on") {
|
||||
tx.Statement.SetColumn("modified_on", time.Now().Unix())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
package model
|
||||
// Copyright 2022 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 (
|
||||
"time"
|
@ -1,25 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
"github.com/rocboss/paopao-ce/pkg/app"
|
||||
"github.com/rocboss/paopao-ce/pkg/errcode"
|
||||
)
|
||||
|
||||
func Admin() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if user, exist := c.Get("USER"); exist {
|
||||
if userModel, ok := user.(*model.User); ok {
|
||||
if userModel.Status == model.UserStatusNormal && userModel.IsAdmin {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
response := app.NewResponse(c)
|
||||
response.ToErrorResponse(errcode.NoAdminPermission)
|
||||
c.Abort()
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
"github.com/rocboss/paopao-ce/pkg/app"
|
||||
"github.com/rocboss/paopao-ce/pkg/cfg"
|
||||
"github.com/rocboss/paopao-ce/pkg/errcode"
|
||||
)
|
||||
|
||||
func Priv() gin.HandlerFunc {
|
||||
if cfg.If("PhoneBind") {
|
||||
return func(c *gin.Context) {
|
||||
if u, exist := c.Get("USER"); exist {
|
||||
if user, ok := u.(*model.User); ok {
|
||||
if user.Status == model.UserStatusNormal {
|
||||
if user.Phone == "" {
|
||||
response := app.NewResponse(c)
|
||||
response.ToErrorResponse(errcode.AccountNoPhoneBind)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
response := app.NewResponse(c)
|
||||
response.ToErrorResponse(errcode.UserHasBeenBanned)
|
||||
c.Abort()
|
||||
}
|
||||
} else {
|
||||
return func(c *gin.Context) {
|
||||
if u, exist := c.Get("USER"); exist {
|
||||
if user, ok := u.(*model.User); ok && user.Status == model.UserStatusNormal {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
response := app.NewResponse(c)
|
||||
response.ToErrorResponse(errcode.UserHasBeenBanned)
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type AttachmentType int
|
||||
|
||||
const (
|
||||
ATTACHMENT_TYPE_IMAGE AttachmentType = iota + 1
|
||||
ATTACHMENT_TYPE_VIDEO
|
||||
ATTACHMENT_TYPE_OTHER
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
*Model
|
||||
UserID int64 `json:"user_id"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
ImgWidth int `json:"img_width"`
|
||||
ImgHeight int `json:"img_height"`
|
||||
Type AttachmentType `json:"type"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func (a *Attachment) Create(db *gorm.DB) (*Attachment, error) {
|
||||
err := db.Create(&a).Error
|
||||
|
||||
return a, err
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Captcha struct {
|
||||
*Model
|
||||
Phone string `json:"phone"`
|
||||
Captcha string `json:"captcha"`
|
||||
UseTimes int `json:"use_times"`
|
||||
ExpiredOn int64 `json:"expired_on"`
|
||||
}
|
||||
|
||||
func (c *Captcha) Create(db *gorm.DB) (*Captcha, error) {
|
||||
err := db.Create(&c).Error
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
func (c *Captcha) Update(db *gorm.DB) error {
|
||||
return db.Model(&Captcha{}).Where("id = ? AND is_del = ?", c.Model.ID, 0).Save(c).Error
|
||||
}
|
||||
|
||||
func (c *Captcha) Get(db *gorm.DB) (*Captcha, error) {
|
||||
var captcha Captcha
|
||||
if c.Model != nil && c.ID > 0 {
|
||||
db = db.Where("id = ? AND is_del = ?", c.ID, 0)
|
||||
}
|
||||
if c.Phone != "" {
|
||||
db = db.Where("phone = ?", c.Phone)
|
||||
}
|
||||
|
||||
err := db.Last(&captcha).Error
|
||||
if err != nil {
|
||||
return &captcha, err
|
||||
}
|
||||
|
||||
return &captcha, nil
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Comment struct {
|
||||
*Model
|
||||
PostID int64 `json:"post_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
IP string `json:"ip"`
|
||||
IPLoc string `json:"ip_loc"`
|
||||
}
|
||||
|
||||
type CommentFormated struct {
|
||||
ID int64 `json:"id"`
|
||||
PostID int64 `json:"post_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
User *UserFormated `json:"user"`
|
||||
Contents []*CommentContent `json:"contents"`
|
||||
Replies []*CommentReplyFormated `json:"replies"`
|
||||
IPLoc string `json:"ip_loc"`
|
||||
CreatedOn int64 `json:"created_on"`
|
||||
ModifiedOn int64 `json:"modified_on"`
|
||||
}
|
||||
|
||||
func (c *Comment) Format() *CommentFormated {
|
||||
if c.Model == nil {
|
||||
return &CommentFormated{}
|
||||
}
|
||||
return &CommentFormated{
|
||||
ID: c.Model.ID,
|
||||
PostID: c.PostID,
|
||||
UserID: c.UserID,
|
||||
User: &UserFormated{},
|
||||
Contents: []*CommentContent{},
|
||||
Replies: []*CommentReplyFormated{},
|
||||
IPLoc: c.IPLoc,
|
||||
CreatedOn: c.CreatedOn,
|
||||
ModifiedOn: c.ModifiedOn,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Comment) Get(db *gorm.DB) (*Comment, error) {
|
||||
var comment Comment
|
||||
if c.Model != nil && c.ID > 0 {
|
||||
db = db.Where("id = ? AND is_del = ?", c.ID, 0)
|
||||
} else {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
err := db.First(&comment).Error
|
||||
if err != nil {
|
||||
return &comment, err
|
||||
}
|
||||
|
||||
return &comment, nil
|
||||
}
|
||||
|
||||
func (c *Comment) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*Comment, error) {
|
||||
var comments []*Comment
|
||||
var err error
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
if c.PostID > 0 {
|
||||
db = db.Where("id = ?", c.PostID)
|
||||
}
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err = db.Where("is_del = ?", 0).Find(&comments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func (c *Comment) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) {
|
||||
var count int64
|
||||
if c.PostID > 0 {
|
||||
db = db.Where("post_id = ?", c.PostID)
|
||||
}
|
||||
for k, v := range *conditions {
|
||||
if k != "ORDER" {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
if err := db.Model(c).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (c *Comment) Create(db *gorm.DB) (*Comment, error) {
|
||||
err := db.Create(&c).Error
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
func (c *Comment) Delete(db *gorm.DB) error {
|
||||
return db.Model(c).Where("id = ?", c.Model.ID).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (c *Comment) CommentIdsByPostId(db *gorm.DB, postId int64) (ids []int64, err error) {
|
||||
err = db.Model(c).Where("post_id = ?", postId).Select("id").Find(&ids).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Comment) DeleteByPostId(db *gorm.DB, postId int64) error {
|
||||
return db.Model(c).Where("post_id = ?", postId).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentContent struct {
|
||||
*Model
|
||||
CommentID int64 `json:"comment_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
Content string `json:"content"`
|
||||
Type PostContentT `json:"type"`
|
||||
Sort int64 `json:"sort"`
|
||||
}
|
||||
|
||||
func (c *CommentContent) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*CommentContent, error) {
|
||||
var comments []*CommentContent
|
||||
var err error
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
if c.CommentID > 0 {
|
||||
db = db.Where("id = ?", c.CommentID)
|
||||
}
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err = db.Where("is_del = ?", 0).Find(&comments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func (c *CommentContent) Create(db *gorm.DB) (*CommentContent, error) {
|
||||
err := db.Create(&c).Error
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
func (c *CommentContent) MediaContentsByCommentId(db *gorm.DB, commentIds []int64) (contents []string, err error) {
|
||||
err = db.Model(c).Where("comment_id IN ? AND type = ?", commentIds, CONTENT_TYPE_IMAGE).Select("content").Find(&contents).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *CommentContent) DeleteByCommentIds(db *gorm.DB, commentIds []int64) error {
|
||||
return db.Model(c).Where("comment_id IN ?", commentIds).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentReply struct {
|
||||
*Model
|
||||
CommentID int64 `json:"comment_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
AtUserID int64 `json:"at_user_id"`
|
||||
Content string `json:"content"`
|
||||
IP string `json:"ip"`
|
||||
IPLoc string `json:"ip_loc"`
|
||||
}
|
||||
|
||||
type CommentReplyFormated struct {
|
||||
ID int64 `json:"id"`
|
||||
CommentID int64 `json:"comment_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
User *UserFormated `json:"user"`
|
||||
AtUserID int64 `json:"at_user_id"`
|
||||
AtUser *UserFormated `json:"at_user"`
|
||||
Content string `json:"content"`
|
||||
IPLoc string `json:"ip_loc"`
|
||||
CreatedOn int64 `json:"created_on"`
|
||||
ModifiedOn int64 `json:"modified_on"`
|
||||
}
|
||||
|
||||
func (c *CommentReply) Format() *CommentReplyFormated {
|
||||
if c.Model == nil {
|
||||
return &CommentReplyFormated{}
|
||||
}
|
||||
|
||||
return &CommentReplyFormated{
|
||||
ID: c.ID,
|
||||
CommentID: c.CommentID,
|
||||
UserID: c.UserID,
|
||||
User: &UserFormated{},
|
||||
AtUserID: c.AtUserID,
|
||||
AtUser: &UserFormated{},
|
||||
Content: c.Content,
|
||||
IPLoc: c.IPLoc,
|
||||
CreatedOn: c.CreatedOn,
|
||||
ModifiedOn: c.ModifiedOn,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CommentReply) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*CommentReply, error) {
|
||||
var comments []*CommentReply
|
||||
var err error
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
if c.CommentID > 0 {
|
||||
db = db.Where("id = ?", c.CommentID)
|
||||
}
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err = db.Where("is_del = ?", 0).Find(&comments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func (c *CommentReply) Create(db *gorm.DB) (*CommentReply, error) {
|
||||
err := db.Create(&c).Error
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
func (c *CommentReply) Get(db *gorm.DB) (*CommentReply, error) {
|
||||
var reply CommentReply
|
||||
if c.Model != nil && c.ID > 0 {
|
||||
db = db.Where("id = ? AND is_del = ?", c.ID, 0)
|
||||
} else {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
err := db.First(&reply).Error
|
||||
if err != nil {
|
||||
return &reply, err
|
||||
}
|
||||
|
||||
return &reply, nil
|
||||
}
|
||||
|
||||
func (c *CommentReply) Delete(db *gorm.DB) error {
|
||||
return db.Model(&CommentReply{}).Where("id = ? AND is_del = ?", c.Model.ID, 0).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (c *CommentReply) DeleteByCommentIds(db *gorm.DB, commentIds []int64) error {
|
||||
return db.Model(c).Where("comment_id IN ?", commentIds).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
@ -1,119 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
ContactStatusRequesting int8 = iota + 1
|
||||
ContactStatusAgree
|
||||
ContactStatusReject
|
||||
ContactStatusDeleted
|
||||
)
|
||||
|
||||
type Contact struct {
|
||||
*Model
|
||||
User *User `json:"-" gorm:"foreignKey:ID;references:FriendId"`
|
||||
UserId int64 `json:"user_id"`
|
||||
FriendId int64 `json:"friend_id"`
|
||||
GroupId int64 `json:"group_id"`
|
||||
Remark string `json:"remark"`
|
||||
Status int8 `json:"status"` // 1请求好友, 2已同意好友, 3已拒绝好友, 4已删除好友
|
||||
IsTop int8 `json:"is_top"`
|
||||
IsBlack int8 `json:"is_black"`
|
||||
NoticeEnable int8 `json:"notice_enable"`
|
||||
}
|
||||
|
||||
func (c *Contact) FetchUser(db *gorm.DB) (*Contact, error) {
|
||||
var contact Contact
|
||||
err := db.Omit("User").Unscoped().Where("user_id = ? AND friend_id = ?", c.UserId, c.FriendId).First(&contact).Error
|
||||
if err != nil {
|
||||
logrus.Debugf("Contact.FetchUser fetch user error:%s", err)
|
||||
return nil, err
|
||||
}
|
||||
return &contact, nil
|
||||
}
|
||||
|
||||
func (c *Contact) GetByUserFriend(db *gorm.DB) (*Contact, error) {
|
||||
var contact Contact
|
||||
err := db.Omit("User").Where("user_id = ? AND friend_id = ?", c.UserId, c.FriendId).First(&contact).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &contact, nil
|
||||
}
|
||||
|
||||
func (c *Contact) FetchByUserFriendAll(db *gorm.DB) ([]*Contact, error) {
|
||||
var contacts []*Contact
|
||||
if err := db.Omit("User").
|
||||
Where("(user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)",
|
||||
c.UserId, c.FriendId, c.FriendId, c.UserId).
|
||||
Find(&contacts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return contacts, nil
|
||||
}
|
||||
|
||||
func (c *Contact) List(db *gorm.DB, conditions ConditionsT, offset, limit int) ([]*Contact, error) {
|
||||
var contacts []*Contact
|
||||
var err error
|
||||
tn := db.NamingStrategy.TableName("Contact") + "."
|
||||
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
for k, v := range conditions {
|
||||
if k != "ORDER" {
|
||||
db = db.Where(tn+k, v)
|
||||
}
|
||||
}
|
||||
|
||||
db.Joins("User").Order("`User`.`nickname` ASC")
|
||||
if err = db.Find(&contacts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return contacts, nil
|
||||
}
|
||||
|
||||
func (c *Contact) BeFriendIds(db *gorm.DB) (ids []int64, err error) {
|
||||
if err = db.Model(c).Omit("User").Select("user_id").Where("friend_id = ? AND status = ?", c.FriendId, ContactStatusAgree).Find(&ids).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Contact) MyFriendIds(db *gorm.DB) (ids []string, err error) {
|
||||
if err = db.Model(c).Omit("User").Select("friend_id").Where("user_id = ? AND status = ?", c.UserId, ContactStatusAgree).Find(&ids).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Contact) Count(db *gorm.DB, conditions ConditionsT) (int64, error) {
|
||||
var count int64
|
||||
|
||||
for k, v := range conditions {
|
||||
if k != "ORDER" {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
if err := db.Model(m).Omit("User").Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (c *Contact) Create(db *gorm.DB) (*Contact, error) {
|
||||
err := db.Omit("User").Create(&c).Error
|
||||
return c, err
|
||||
}
|
||||
|
||||
func (c *Contact) Update(db *gorm.DB) error {
|
||||
return db.Model(&Contact{}).Omit("User").Where("id = ?", c.Model.ID).Save(c).Error
|
||||
}
|
||||
|
||||
func (c *Contact) UpdateInUnscoped(db *gorm.DB) error {
|
||||
return db.Unscoped().Omit("User").Save(c).Error
|
||||
}
|
@ -1,148 +0,0 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type MessageT int8
|
||||
|
||||
const (
|
||||
MsgTypePost MessageT = iota + 1
|
||||
MsgtypeComment
|
||||
MsgTypeReply
|
||||
MsgTypeWhisper
|
||||
MsgTypeRequestingFriend
|
||||
MsgTypeSystem MessageT = 99
|
||||
|
||||
MsgStatusUnread = 0
|
||||
MsgStatusReaded = 1
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
*Model
|
||||
SenderUserID int64 `json:"sender_user_id"`
|
||||
ReceiverUserID int64 `json:"receiver_user_id"`
|
||||
Type MessageT `json:"type"`
|
||||
Brief string `json:"brief"`
|
||||
Content string `json:"content"`
|
||||
PostID int64 `json:"post_id"`
|
||||
CommentID int64 `json:"comment_id"`
|
||||
ReplyID int64 `json:"reply_id"`
|
||||
IsRead int8 `json:"is_read"`
|
||||
}
|
||||
|
||||
type MessageFormated struct {
|
||||
ID int64 `json:"id"`
|
||||
SenderUserID int64 `json:"sender_user_id"`
|
||||
SenderUser *UserFormated `json:"sender_user"`
|
||||
ReceiverUserID int64 `json:"receiver_user_id"`
|
||||
Type MessageT `json:"type"`
|
||||
Brief string `json:"brief"`
|
||||
Content string `json:"content"`
|
||||
PostID int64 `json:"post_id"`
|
||||
Post *PostFormated `json:"post"`
|
||||
CommentID int64 `json:"comment_id"`
|
||||
Comment *Comment `json:"comment"`
|
||||
ReplyID int64 `json:"reply_id"`
|
||||
Reply *CommentReply `json:"reply"`
|
||||
IsRead int8 `json:"is_read"`
|
||||
CreatedOn int64 `json:"created_on"`
|
||||
ModifiedOn int64 `json:"modified_on"`
|
||||
}
|
||||
|
||||
func (m *Message) Format() *MessageFormated {
|
||||
if m.Model == nil || m.Model.ID == 0 {
|
||||
return nil
|
||||
}
|
||||
mf := &MessageFormated{
|
||||
ID: m.ID,
|
||||
SenderUserID: m.SenderUserID,
|
||||
SenderUser: &UserFormated{},
|
||||
ReceiverUserID: m.ReceiverUserID,
|
||||
Type: m.Type,
|
||||
Brief: m.Brief,
|
||||
Content: m.Content,
|
||||
PostID: m.PostID,
|
||||
Post: &PostFormated{},
|
||||
CommentID: m.CommentID,
|
||||
Comment: &Comment{},
|
||||
ReplyID: m.ReplyID,
|
||||
Reply: &CommentReply{},
|
||||
IsRead: m.IsRead,
|
||||
CreatedOn: m.CreatedOn,
|
||||
ModifiedOn: m.ModifiedOn,
|
||||
}
|
||||
|
||||
return mf
|
||||
}
|
||||
|
||||
func (m *Message) Create(db *gorm.DB) (*Message, error) {
|
||||
err := db.Create(&m).Error
|
||||
|
||||
return m, err
|
||||
}
|
||||
|
||||
func (m *Message) Update(db *gorm.DB) error {
|
||||
return db.Model(&Message{}).Where("id = ? AND is_del = ?", m.Model.ID, 0).Save(m).Error
|
||||
}
|
||||
|
||||
func (m *Message) Get(db *gorm.DB) (*Message, error) {
|
||||
var message Message
|
||||
if m.Model != nil && m.ID > 0 {
|
||||
db = db.Where("id = ? AND is_del = ?", m.ID, 0)
|
||||
}
|
||||
if m.ReceiverUserID > 0 {
|
||||
db = db.Where("receiver_user_id = ?", m.ReceiverUserID)
|
||||
}
|
||||
if err := db.First(&message).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &message, nil
|
||||
}
|
||||
|
||||
func (m *Message) FetchBy(db *gorm.DB, predicates Predicates) ([]*Message, error) {
|
||||
var messages []*Message
|
||||
for k, v := range predicates {
|
||||
db = db.Where(k, v...)
|
||||
}
|
||||
db = db.Where("is_del = 0")
|
||||
if err := db.Find(&messages).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
func (c *Message) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*Message, error) {
|
||||
var messages []*Message
|
||||
var err error
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err = db.Where("is_del = ?", 0).Find(&messages).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
func (m *Message) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) {
|
||||
var count int64
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k != "ORDER" {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
if err := db.Model(m).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type PostAttachmentBill struct {
|
||||
*Model
|
||||
PostID int64 `json:"post_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
PaidAmount int64 `json:"paid_amount"`
|
||||
}
|
||||
|
||||
func (p *PostAttachmentBill) Get(db *gorm.DB) (*PostAttachmentBill, error) {
|
||||
var pas PostAttachmentBill
|
||||
if p.Model != nil && p.ID > 0 {
|
||||
db = db.Where("id = ? AND is_del = ?", p.ID, 0)
|
||||
}
|
||||
if p.PostID > 0 {
|
||||
db = db.Where("post_id = ?", p.PostID)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where("user_id = ?", p.UserID)
|
||||
}
|
||||
|
||||
err := db.First(&pas).Error
|
||||
if err != nil {
|
||||
return &pas, err
|
||||
}
|
||||
|
||||
return &pas, nil
|
||||
}
|
||||
|
||||
func (p *PostAttachmentBill) Create(db *gorm.DB) (*PostAttachmentBill, error) {
|
||||
err := db.Create(&p).Error
|
||||
|
||||
return p, err
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PostCollection struct {
|
||||
*Model
|
||||
Post *Post `json:"-"`
|
||||
PostID int64 `json:"post_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (p *PostCollection) Get(db *gorm.DB) (*PostCollection, error) {
|
||||
var star PostCollection
|
||||
tn := db.NamingStrategy.TableName("PostCollection") + "."
|
||||
|
||||
if p.Model != nil && p.ID > 0 {
|
||||
db = db.Where(tn+"id = ? AND "+tn+"is_del = ?", p.ID, 0)
|
||||
}
|
||||
if p.PostID > 0 {
|
||||
db = db.Where(tn+"post_id = ?", p.PostID)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where(tn+"user_id = ?", p.UserID)
|
||||
}
|
||||
|
||||
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC")
|
||||
err := db.First(&star).Error
|
||||
if err != nil {
|
||||
return &star, err
|
||||
}
|
||||
|
||||
return &star, nil
|
||||
}
|
||||
|
||||
func (p *PostCollection) Create(db *gorm.DB) (*PostCollection, error) {
|
||||
err := db.Omit("Post").Create(&p).Error
|
||||
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (p *PostCollection) Delete(db *gorm.DB) error {
|
||||
return db.Model(&PostCollection{}).Omit("Post").Where("id = ? AND is_del = ?", p.Model.ID, 0).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (p *PostCollection) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*PostCollection, error) {
|
||||
var collections []*PostCollection
|
||||
var err error
|
||||
tn := db.NamingStrategy.TableName("PostCollection") + "."
|
||||
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where(tn+"user_id = ?", p.UserID)
|
||||
}
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(tn+k, v)
|
||||
}
|
||||
}
|
||||
|
||||
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC")
|
||||
if err = db.Where(tn+"is_del = ?", 0).Find(&collections).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return collections, nil
|
||||
}
|
||||
|
||||
func (p *PostCollection) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) {
|
||||
var count int64
|
||||
tn := db.NamingStrategy.TableName("PostCollection") + "."
|
||||
|
||||
if p.PostID > 0 {
|
||||
db = db.Where(tn+"post_id = ?", p.PostID)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where(tn+"user_id = ?", p.UserID)
|
||||
}
|
||||
for k, v := range *conditions {
|
||||
if k != "ORDER" {
|
||||
db = db.Where(tn+k, v)
|
||||
}
|
||||
}
|
||||
|
||||
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate)
|
||||
if err := db.Model(p).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PostStar struct {
|
||||
*Model
|
||||
Post *Post `json:"-"`
|
||||
PostID int64 `json:"post_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
func (p *PostStar) Get(db *gorm.DB) (*PostStar, error) {
|
||||
var star PostStar
|
||||
tn := db.NamingStrategy.TableName("PostStar") + "."
|
||||
|
||||
if p.Model != nil && p.ID > 0 {
|
||||
db = db.Where(tn+"id = ? AND "+tn+"is_del = ?", p.ID, 0)
|
||||
}
|
||||
if p.PostID > 0 {
|
||||
db = db.Where(tn+"post_id = ?", p.PostID)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where(tn+"user_id = ?", p.UserID)
|
||||
}
|
||||
|
||||
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC")
|
||||
if err := db.First(&star).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &star, nil
|
||||
}
|
||||
|
||||
func (p *PostStar) Create(db *gorm.DB) (*PostStar, error) {
|
||||
err := db.Omit("Post").Create(&p).Error
|
||||
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (p *PostStar) Delete(db *gorm.DB) error {
|
||||
return db.Model(&PostStar{}).Omit("Post").Where("id = ? AND is_del = ?", p.Model.ID, 0).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (p *PostStar) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*PostStar, error) {
|
||||
var stars []*PostStar
|
||||
var err error
|
||||
tn := db.NamingStrategy.TableName("PostStar") + "."
|
||||
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where(tn+"user_id = ?", p.UserID)
|
||||
}
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(tn+k, v)
|
||||
}
|
||||
}
|
||||
|
||||
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC")
|
||||
if err = db.Find(&stars).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return stars, nil
|
||||
}
|
||||
|
||||
func (p *PostStar) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) {
|
||||
var count int64
|
||||
tn := db.NamingStrategy.TableName("PostStar") + "."
|
||||
|
||||
if p.PostID > 0 {
|
||||
db = db.Where(tn+"post_id = ?", p.PostID)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where(tn+"user_id = ?", p.UserID)
|
||||
}
|
||||
for k, v := range *conditions {
|
||||
if k != "ORDER" {
|
||||
db = db.Where(tn+k, v)
|
||||
}
|
||||
}
|
||||
|
||||
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate)
|
||||
if err := db.Model(p).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package rest
|
||||
|
||||
type RequestingFriendReq struct {
|
||||
UserId int64 `json:"user_id" binding:"required"`
|
||||
Greetings string `json:"greetings" binding:"required"`
|
||||
}
|
||||
|
||||
type AddFriendReq struct {
|
||||
UserId int64 `json:"user_id" binding:"required"`
|
||||
}
|
||||
|
||||
type RejectFriendReq struct {
|
||||
UserId int64 `json:"user_id" binding:"required"`
|
||||
}
|
||||
|
||||
type DeleteFriendReq struct {
|
||||
UserId int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
type ContactItem struct {
|
||||
UserId int64 `json:"user_id"`
|
||||
UserName string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
Phone string `json:"phone"`
|
||||
}
|
||||
|
||||
type ContactsResp struct {
|
||||
Contacts []ContactItem `json:"contacts"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package rest
|
||||
|
||||
import "github.com/rocboss/paopao-ce/internal/model"
|
||||
|
||||
type IndexTweetsResp struct {
|
||||
Tweets []*model.PostFormated
|
||||
Total int64
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package rest
|
||||
|
||||
type UserProfileResp struct {
|
||||
ID int64 `json:"id"`
|
||||
Nickname string `json:"nickname"`
|
||||
Username string `json:"username"`
|
||||
Status int `json:"status"`
|
||||
Avatar string `json:"avatar"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
IsFriend bool `json:"is_friend"`
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Tag struct {
|
||||
*Model
|
||||
UserID int64 `json:"user_id"`
|
||||
Tag string `json:"tag"`
|
||||
QuoteNum int64 `json:"quote_num"`
|
||||
}
|
||||
type TagFormated struct {
|
||||
ID int64 `json:"id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
User *UserFormated `json:"user"`
|
||||
Tag string `json:"tag"`
|
||||
QuoteNum int64 `json:"quote_num"`
|
||||
}
|
||||
|
||||
func (t *Tag) Format() *TagFormated {
|
||||
if t.Model == nil {
|
||||
return &TagFormated{}
|
||||
}
|
||||
|
||||
return &TagFormated{
|
||||
ID: t.ID,
|
||||
UserID: t.UserID,
|
||||
User: &UserFormated{},
|
||||
Tag: t.Tag,
|
||||
QuoteNum: t.QuoteNum,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tag) Get(db *gorm.DB) (*Tag, error) {
|
||||
var tag Tag
|
||||
if t.Model != nil && t.Model.ID > 0 {
|
||||
db = db.Where("id= ? AND is_del = ?", t.Model.ID, 0)
|
||||
} else {
|
||||
db = db.Where("tag = ? AND is_del = ?", t.Tag, 0)
|
||||
}
|
||||
|
||||
err := db.First(&tag).Error
|
||||
if err != nil {
|
||||
return &tag, err
|
||||
}
|
||||
|
||||
return &tag, nil
|
||||
}
|
||||
|
||||
func (t *Tag) Create(db *gorm.DB) (*Tag, error) {
|
||||
err := db.Create(&t).Error
|
||||
|
||||
return t, err
|
||||
}
|
||||
|
||||
func (t *Tag) Update(db *gorm.DB) error {
|
||||
return db.Model(&Tag{}).Where("id = ? AND is_del = ?", t.Model.ID, 0).Save(t).Error
|
||||
}
|
||||
|
||||
func (t *Tag) Delete(db *gorm.DB) error {
|
||||
return db.Model(t).Where("id = ?", t.Model.ID).Updates(map[string]any{
|
||||
"deleted_on": time.Now().Unix(),
|
||||
"is_del": 1,
|
||||
}).Error
|
||||
}
|
||||
|
||||
func (t *Tag) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*Tag, error) {
|
||||
var tags []*Tag
|
||||
var err error
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
if t.UserID > 0 {
|
||||
db = db.Where("user_id = ?", t.UserID)
|
||||
}
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err = db.Where("is_del = ?", 0).Find(&tags).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func (t *Tag) TagsFrom(db *gorm.DB, tags []string) (res []*Tag, err error) {
|
||||
err = db.Where("tag IN ?", tags).Find(&res).Error
|
||||
return
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
const (
|
||||
UserStatusNormal int = iota + 1
|
||||
UserStatusClosed
|
||||
)
|
||||
|
||||
type User struct {
|
||||
*Model
|
||||
Nickname string `json:"nickname"`
|
||||
Username string `json:"username"`
|
||||
Phone string `json:"phone"`
|
||||
Password string `json:"password"`
|
||||
Salt string `json:"salt"`
|
||||
Status int `json:"status"`
|
||||
Avatar string `json:"avatar"`
|
||||
Balance int64 `json:"balance"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
}
|
||||
|
||||
type UserFormated struct {
|
||||
ID int64 `json:"id"`
|
||||
Nickname string `json:"nickname"`
|
||||
Username string `json:"username"`
|
||||
Status int `json:"status"`
|
||||
Avatar string `json:"avatar"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
}
|
||||
|
||||
func (u *User) Format() *UserFormated {
|
||||
if u.Model != nil {
|
||||
return &UserFormated{
|
||||
ID: u.ID,
|
||||
Nickname: u.Nickname,
|
||||
Username: u.Username,
|
||||
Status: u.Status,
|
||||
Avatar: u.Avatar,
|
||||
IsAdmin: u.IsAdmin,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *User) Get(db *gorm.DB) (*User, error) {
|
||||
var user User
|
||||
if u.Model != nil && u.Model.ID > 0 {
|
||||
db = db.Where("id= ? AND is_del = ?", u.Model.ID, 0)
|
||||
} else if u.Phone != "" {
|
||||
db = db.Where("phone = ? AND is_del = ?", u.Phone, 0)
|
||||
} else {
|
||||
db = db.Where("username = ? AND is_del = ?", u.Username, 0)
|
||||
}
|
||||
|
||||
err := db.First(&user).Error
|
||||
if err != nil {
|
||||
return &user, err
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (u *User) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*User, error) {
|
||||
var users []*User
|
||||
var err error
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err = db.Where("is_del = ?", 0).Find(&users).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (u *User) Create(db *gorm.DB) (*User, error) {
|
||||
err := db.Create(&u).Error
|
||||
|
||||
return u, err
|
||||
}
|
||||
|
||||
func (u *User) Update(db *gorm.DB) error {
|
||||
return db.Model(&User{}).Where("id = ? AND is_del = ?", u.Model.ID, 0).Save(u).Error
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type WalletRecharge struct {
|
||||
*Model
|
||||
UserID int64 `json:"user_id"`
|
||||
Amount int64 `json:"amount"`
|
||||
TradeNo string `json:"trade_no"`
|
||||
TradeStatus string `json:"trade_status"`
|
||||
}
|
||||
|
||||
func (p *WalletRecharge) Get(db *gorm.DB) (*WalletRecharge, error) {
|
||||
var pas WalletRecharge
|
||||
if p.Model != nil && p.ID > 0 {
|
||||
db = db.Where("id = ? AND is_del = ?", p.ID, 0)
|
||||
}
|
||||
if p.UserID > 0 {
|
||||
db = db.Where("user_id = ?", p.UserID)
|
||||
}
|
||||
|
||||
err := db.First(&pas).Error
|
||||
if err != nil {
|
||||
return &pas, err
|
||||
}
|
||||
|
||||
return &pas, nil
|
||||
}
|
||||
|
||||
func (p *WalletRecharge) Create(db *gorm.DB) (*WalletRecharge, error) {
|
||||
err := db.Create(&p).Error
|
||||
|
||||
return p, err
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type WalletStatement struct {
|
||||
*Model
|
||||
UserID int64 `json:"user_id"`
|
||||
ChangeAmount int64 `json:"change_amount"`
|
||||
BalanceSnapshot int64 `json:"balance_snapshot"`
|
||||
Reason string `json:"reason"`
|
||||
PostID int64 `json:"post_id"`
|
||||
}
|
||||
|
||||
func (w *WalletStatement) Get(db *gorm.DB) (*WalletStatement, error) {
|
||||
var ws WalletStatement
|
||||
if w.Model != nil && w.ID > 0 {
|
||||
db = db.Where("id = ? AND is_del = ?", w.ID, 0)
|
||||
}
|
||||
if w.PostID > 0 {
|
||||
db = db.Where("post_id = ?", w.PostID)
|
||||
}
|
||||
if w.UserID > 0 {
|
||||
db = db.Where("user_id = ?", w.UserID)
|
||||
}
|
||||
|
||||
err := db.First(&ws).Error
|
||||
if err != nil {
|
||||
return &ws, err
|
||||
}
|
||||
|
||||
return &ws, nil
|
||||
}
|
||||
|
||||
func (w *WalletStatement) Create(db *gorm.DB) (*WalletStatement, error) {
|
||||
err := db.Create(&w).Error
|
||||
|
||||
return w, err
|
||||
}
|
||||
|
||||
func (w *WalletStatement) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*WalletStatement, error) {
|
||||
var records []*WalletStatement
|
||||
var err error
|
||||
if offset >= 0 && limit > 0 {
|
||||
db = db.Offset(offset).Limit(limit)
|
||||
}
|
||||
if w.UserID > 0 {
|
||||
db = db.Where("user_id = ?", w.UserID)
|
||||
}
|
||||
|
||||
for k, v := range *conditions {
|
||||
if k == "ORDER" {
|
||||
db = db.Order(v)
|
||||
} else {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err = db.Where("is_del = ?", 0).Find(&records).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (w *WalletStatement) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) {
|
||||
var count int64
|
||||
if w.PostID > 0 {
|
||||
db = db.Where("post_id = ?", w.PostID)
|
||||
}
|
||||
if w.UserID > 0 {
|
||||
db = db.Where("user_id = ?", w.UserID)
|
||||
}
|
||||
for k, v := range *conditions {
|
||||
if k != "ORDER" {
|
||||
db = db.Where(k, v)
|
||||
}
|
||||
}
|
||||
if err := db.Model(w).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
// Copyright 2022 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 proto
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue