remove: remove non-essential entity classes

This commit is contained in:
LinkinStar 2022-10-19 11:07:58 +08:00
parent bf57cb9e54
commit cf9cd4f1bc
6 changed files with 47 additions and 300 deletions

View File

@ -1,18 +0,0 @@
package entity
import "time"
// NotificationRead notification read record
type NotificationRead struct {
ID int `xorm:"not null pk autoincr comment('id') INT(11) id"`
CreatedAt time.Time `xorm:"created comment('create time') TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"updated comment('update time') TIMESTAMP updated_at"`
UserID int64 `xorm:"not null default 0 comment('user id') BIGINT(20) user_id"`
MessageID int64 `xorm:"not null default 0 comment('message id') BIGINT(20) message_id"`
IsRead int `xorm:"not null default 1 comment('read status(unread: 1; read 2)') INT(11) is_read"`
}
// TableName notification read record table name
func (NotificationRead) TableName() string {
return "notification_read"
}

View File

@ -1,11 +0,0 @@
package entity
// UserGroup user group
type UserGroup struct {
ID int64 `xorm:"not null pk autoincr comment('user group id') unique BIGINT(20) id"`
}
// TableName user group table name
func (UserGroup) TableName() string {
return "user_group"
}

View File

@ -0,0 +1,47 @@
package migrations
import (
"fmt"
"github.com/segmentfault/answer/internal/entity"
"xorm.io/xorm"
)
var (
tables = []interface{}{
&entity.Activity{},
&entity.Answer{},
&entity.Collection{},
&entity.CollectionGroup{},
&entity.Comment{},
&entity.Config{},
&entity.Meta{},
&entity.Notification{},
&entity.Question{},
&entity.Report{},
&entity.Revision{},
&entity.SiteInfo{},
&entity.Tag{},
&entity.TagRel{},
&entity.Uniqid{},
&entity.User{},
}
)
// InitDB init db
func InitDB(engine *xorm.Engine) (err error) {
exist, err := engine.IsTableExist(&Version{})
if err != nil {
return err
}
if exist {
fmt.Println("[database] already exists")
return nil
}
err = engine.Sync(tables)
if err != nil {
return err
}
return nil
}

View File

@ -1,69 +0,0 @@
package notification
import (
"context"
"github.com/segmentfault/answer/internal/base/data"
"github.com/segmentfault/answer/internal/base/pager"
"github.com/segmentfault/answer/internal/base/reason"
"github.com/segmentfault/answer/internal/entity"
"github.com/segmentfault/answer/internal/service/notification"
"github.com/segmentfault/pacman/errors"
)
// notificationReadRepo notificationRead repository
type notificationReadRepo struct {
data *data.Data
}
// NewNotificationReadRepo new repository
func NewNotificationReadRepo(data *data.Data) notification.NotificationReadRepo {
return &notificationReadRepo{
data: data,
}
}
// AddNotificationRead add notification read record
func (nr *notificationReadRepo) AddNotificationRead(ctx context.Context, notificationRead *entity.NotificationRead) (err error) {
_, err = nr.data.DB.Insert(notificationRead)
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
// RemoveNotificationRead delete notification read record
func (nr *notificationReadRepo) RemoveNotificationRead(ctx context.Context, id int) (err error) {
_, err = nr.data.DB.ID(id).Delete(&entity.NotificationRead{})
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
// UpdateNotificationRead update notification read record
func (nr *notificationReadRepo) UpdateNotificationRead(ctx context.Context, notificationRead *entity.NotificationRead) (err error) {
_, err = nr.data.DB.ID(notificationRead.ID).Update(notificationRead)
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
// GetNotificationRead get notification read record one
func (nr *notificationReadRepo) GetNotificationRead(ctx context.Context, id int) (
notificationRead *entity.NotificationRead, exist bool, err error) {
notificationRead = &entity.NotificationRead{}
exist, err = nr.data.DB.ID(id).Get(notificationRead)
if err != nil {
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
// GetNotificationReadList get notification read record list all
func (nr *notificationReadRepo) GetNotificationReadList(ctx context.Context, notificationRead *entity.NotificationRead) (notificationReadList []*entity.NotificationRead, err error) {
notificationReadList = make([]*entity.NotificationRead, 0)
err = nr.data.DB.Find(notificationReadList, notificationRead)
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
return
}
// GetNotificationReadPage get notification read record page
func (nr *notificationReadRepo) GetNotificationReadPage(ctx context.Context, page, pageSize int, notificationRead *entity.NotificationRead) (notificationReadList []*entity.NotificationRead, total int64, err error) {
notificationReadList = make([]*entity.NotificationRead, 0)
total, err = pager.Help(page, pageSize, notificationReadList, notificationRead, nr.data.DB.NewSession())
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
return
}

View File

@ -1,101 +0,0 @@
package notification
import (
"context"
"github.com/jinzhu/copier"
"github.com/segmentfault/answer/internal/base/pager"
"github.com/segmentfault/answer/internal/base/reason"
"github.com/segmentfault/answer/internal/entity"
"github.com/segmentfault/answer/internal/schema"
"github.com/segmentfault/pacman/errors"
)
// NotificationReadRepo notificationRead repository
type NotificationReadRepo interface {
AddNotificationRead(ctx context.Context, notificationRead *entity.NotificationRead) (err error)
RemoveNotificationRead(ctx context.Context, id int) (err error)
UpdateNotificationRead(ctx context.Context, notificationRead *entity.NotificationRead) (err error)
GetNotificationRead(ctx context.Context, id int) (notificationRead *entity.NotificationRead, exist bool, err error)
GetNotificationReadList(ctx context.Context, notificationRead *entity.NotificationRead) (notificationReads []*entity.NotificationRead, err error)
GetNotificationReadPage(ctx context.Context, page, pageSize int, notificationRead *entity.NotificationRead) (notificationReads []*entity.NotificationRead, total int64, err error)
}
// NotificationReadService user service
type NotificationReadService struct {
notificationReadRepo NotificationReadRepo
}
func NewNotificationReadService(notificationReadRepo NotificationReadRepo) *NotificationReadService {
return &NotificationReadService{
notificationReadRepo: notificationReadRepo,
}
}
// AddNotificationRead add notification read record
func (ns *NotificationReadService) AddNotificationRead(ctx context.Context, req *schema.AddNotificationReadReq) (err error) {
notificationRead := &entity.NotificationRead{}
_ = copier.Copy(notificationRead, req)
return ns.notificationReadRepo.AddNotificationRead(ctx, notificationRead)
}
// RemoveNotificationRead delete notification read record
func (ns *NotificationReadService) RemoveNotificationRead(ctx context.Context, id int) (err error) {
return ns.notificationReadRepo.RemoveNotificationRead(ctx, id)
}
// UpdateNotificationRead update notification read record
func (ns *NotificationReadService) UpdateNotificationRead(ctx context.Context, req *schema.UpdateNotificationReadReq) (err error) {
notificationRead := &entity.NotificationRead{}
_ = copier.Copy(notificationRead, req)
return ns.notificationReadRepo.UpdateNotificationRead(ctx, notificationRead)
}
// GetNotificationRead get notification read record one
func (ns *NotificationReadService) GetNotificationRead(ctx context.Context, id int) (resp *schema.GetNotificationReadResp, err error) {
notificationRead, exist, err := ns.notificationReadRepo.GetNotificationRead(ctx, id)
if err != nil {
return
}
if !exist {
return nil, errors.BadRequest(reason.UnknownError)
}
resp = &schema.GetNotificationReadResp{}
_ = copier.Copy(resp, notificationRead)
return resp, nil
}
// GetNotificationReadList get notification read record list all
func (ns *NotificationReadService) GetNotificationReadList(ctx context.Context, req *schema.GetNotificationReadListReq) (resp *[]schema.GetNotificationReadResp, err error) {
notificationRead := &entity.NotificationRead{}
_ = copier.Copy(notificationRead, req)
notificationReads, err := ns.notificationReadRepo.GetNotificationReadList(ctx, notificationRead)
if err != nil {
return
}
resp = &[]schema.GetNotificationReadResp{}
_ = copier.Copy(resp, notificationReads)
return
}
// GetNotificationReadWithPage get notification read record list page
func (ns *NotificationReadService) GetNotificationReadWithPage(ctx context.Context, req *schema.GetNotificationReadWithPageReq) (pageModel *pager.PageModel, err error) {
notificationRead := &entity.NotificationRead{}
_ = copier.Copy(notificationRead, req)
page := req.Page
pageSize := req.PageSize
notificationReads, total, err := ns.notificationReadRepo.GetNotificationReadPage(ctx, page, pageSize, notificationRead)
if err != nil {
return
}
resp := &[]schema.GetNotificationReadResp{}
_ = copier.Copy(resp, notificationReads)
return pager.NewPageModel(total, resp), nil
}

View File

@ -1,101 +0,0 @@
package service
import (
"context"
"github.com/jinzhu/copier"
"github.com/segmentfault/answer/internal/base/pager"
"github.com/segmentfault/answer/internal/base/reason"
"github.com/segmentfault/answer/internal/entity"
"github.com/segmentfault/answer/internal/schema"
"github.com/segmentfault/pacman/errors"
)
// UserGroupRepo userGroup repository
type UserGroupRepo interface {
AddUserGroup(ctx context.Context, userGroup *entity.UserGroup) (err error)
RemoveUserGroup(ctx context.Context, id int) (err error)
UpdateUserGroup(ctx context.Context, userGroup *entity.UserGroup) (err error)
GetUserGroup(ctx context.Context, id int) (userGroup *entity.UserGroup, exist bool, err error)
GetUserGroupList(ctx context.Context, userGroup *entity.UserGroup) (userGroups []*entity.UserGroup, err error)
GetUserGroupPage(ctx context.Context, page, pageSize int, userGroup *entity.UserGroup) (userGroups []*entity.UserGroup, total int64, err error)
}
// UserGroupService user service
type UserGroupService struct {
userGroupRepo UserGroupRepo
}
func NewUserGroupService(userGroupRepo UserGroupRepo) *UserGroupService {
return &UserGroupService{
userGroupRepo: userGroupRepo,
}
}
// AddUserGroup add user group
func (us *UserGroupService) AddUserGroup(ctx context.Context, req *schema.AddUserGroupReq) (err error) {
userGroup := &entity.UserGroup{}
_ = copier.Copy(userGroup, req)
return us.userGroupRepo.AddUserGroup(ctx, userGroup)
}
// RemoveUserGroup delete user group
func (us *UserGroupService) RemoveUserGroup(ctx context.Context, id int) (err error) {
return us.userGroupRepo.RemoveUserGroup(ctx, id)
}
// UpdateUserGroup update user group
func (us *UserGroupService) UpdateUserGroup(ctx context.Context, req *schema.UpdateUserGroupReq) (err error) {
userGroup := &entity.UserGroup{}
_ = copier.Copy(userGroup, req)
return us.userGroupRepo.UpdateUserGroup(ctx, userGroup)
}
// GetUserGroup get user group one
func (us *UserGroupService) GetUserGroup(ctx context.Context, id int) (resp *schema.GetUserGroupResp, err error) {
userGroup, exist, err := us.userGroupRepo.GetUserGroup(ctx, id)
if err != nil {
return
}
if !exist {
return nil, errors.BadRequest(reason.UnknownError)
}
resp = &schema.GetUserGroupResp{}
_ = copier.Copy(resp, userGroup)
return resp, nil
}
// GetUserGroupList get user group list all
func (us *UserGroupService) GetUserGroupList(ctx context.Context, req *schema.GetUserGroupListReq) (resp *[]schema.GetUserGroupResp, err error) {
userGroup := &entity.UserGroup{}
_ = copier.Copy(userGroup, req)
userGroups, err := us.userGroupRepo.GetUserGroupList(ctx, userGroup)
if err != nil {
return
}
resp = &[]schema.GetUserGroupResp{}
_ = copier.Copy(resp, userGroups)
return
}
// GetUserGroupWithPage get user group list page
func (us *UserGroupService) GetUserGroupWithPage(ctx context.Context, req *schema.GetUserGroupWithPageReq) (pageModel *pager.PageModel, err error) {
userGroup := &entity.UserGroup{}
_ = copier.Copy(userGroup, req)
page := req.Page
pageSize := req.PageSize
userGroups, total, err := us.userGroupRepo.GetUserGroupPage(ctx, page, pageSize, userGroup)
if err != nil {
return
}
resp := &[]schema.GetUserGroupResp{}
_ = copier.Copy(resp, userGroups)
return pager.NewPageModel(total, resp), nil
}