初始化项目框架,完成部分接口开发
This commit is contained in:
92
internal/logic/admin/admin.go
Normal file
92
internal/logic/admin/admin.go
Normal file
@ -0,0 +1,92 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/consts"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/model/entity"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
"server/utility/encrypt"
|
||||
"server/utility/jwt"
|
||||
)
|
||||
|
||||
type sAdmin struct{}
|
||||
|
||||
func init() {
|
||||
service.RegisterAdmin(New())
|
||||
}
|
||||
|
||||
func New() service.IAdmin {
|
||||
return &sAdmin{}
|
||||
}
|
||||
|
||||
func (s *sAdmin) Login(ctx context.Context, in *model.AdminLoginIn) (out *model.AdminLoginOut, err error) {
|
||||
admin, err := dao.Admins.Ctx(ctx).Where(dao.Admins.Columns().Username, in.Username).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("database_query_failed")
|
||||
}
|
||||
|
||||
if admin.IsEmpty() {
|
||||
return nil, ecode.Auth
|
||||
}
|
||||
|
||||
if !encrypt.ComparePassword(admin[dao.Admins.Columns().PasswordHash].String(), in.Password) {
|
||||
return nil, ecode.Password
|
||||
}
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{
|
||||
Role: consts.AdminRoleCode,
|
||||
UserId: admin[dao.Admins.Columns().Id].Int64(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("token_generation_failed")
|
||||
}
|
||||
return &model.AdminLoginOut{
|
||||
Token: token,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sAdmin) Info(ctx context.Context, in *model.AdminInfoIn) (out *model.AdminInfoOut, err error) {
|
||||
exist, err := dao.Admins.Ctx(ctx).WherePri(in.AdminId).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("admin_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Auth.Sub("admin_not_found")
|
||||
}
|
||||
var admin entity.Admins
|
||||
err = dao.Admins.Ctx(ctx).WherePri(in.AdminId).Scan(&admin)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("admin_query_failed")
|
||||
}
|
||||
return &model.AdminInfoOut{
|
||||
AdminId: admin.Id,
|
||||
Username: admin.Username,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sAdmin) EditPass(ctx context.Context, in *model.AdminEditPassIn) (out *model.AdminEditPassOut, err error) {
|
||||
admin, err := dao.Admins.Ctx(ctx).WherePri(in.AdminId).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("admin_query_failed")
|
||||
}
|
||||
if admin.IsEmpty() {
|
||||
return nil, ecode.Auth.Sub("admin_not_found")
|
||||
}
|
||||
if !encrypt.ComparePassword(admin[dao.Admins.Columns().PasswordHash].String(), in.OldPass) {
|
||||
return nil, ecode.Password.Sub("password_incorrect")
|
||||
}
|
||||
hash, err := encrypt.EncryptPassword(in.NewPass)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("password_encryption_failed")
|
||||
}
|
||||
_, err = dao.Admins.Ctx(ctx).WherePri(in.AdminId).Data(do.Admins{PasswordHash: hash}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("password_update_failed")
|
||||
}
|
||||
return &model.AdminEditPassOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
138
internal/logic/book/book.go
Normal file
138
internal/logic/book/book.go
Normal file
@ -0,0 +1,138 @@
|
||||
package book
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sBook struct{}
|
||||
|
||||
func New() service.IBook {
|
||||
return &sBook{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterBook(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of books
|
||||
func (s *sBook) List(ctx context.Context, in *model.BookListIn) (out *model.BookListOut, err error) {
|
||||
out = &model.BookListOut{}
|
||||
m := dao.Books.Ctx(ctx)
|
||||
if in.Title != "" {
|
||||
m = m.Where(dao.Books.Columns().Title+" like ?", "%"+in.Title+"%")
|
||||
}
|
||||
if in.CategoryId != 0 {
|
||||
m = m.Where(dao.Books.Columns().CategoryId, in.CategoryId)
|
||||
}
|
||||
if in.AuthorId != 0 {
|
||||
m = m.Where(dao.Books.Columns().AuthorId, in.AuthorId)
|
||||
}
|
||||
if in.Status != 0 {
|
||||
m = m.Where(dao.Books.Columns().Status, in.Status)
|
||||
}
|
||||
if in.IsRecommended != 0 {
|
||||
m = m.Where(dao.Books.Columns().IsRecommended, in.IsRecommended)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBook) Create(ctx context.Context, in *model.BookAddIn) (out *model.BookCRUDOut, err error) {
|
||||
exist, err := dao.Books.Ctx(ctx).
|
||||
Where(dao.Books.Columns().Title, in.Title).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("book_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("book_exists")
|
||||
}
|
||||
|
||||
if _, err := dao.Books.Ctx(ctx).Data(do.Books{
|
||||
AuthorId: in.AuthorId,
|
||||
CategoryId: in.CategoryId,
|
||||
Title: in.Title,
|
||||
CoverUrl: in.CoverUrl,
|
||||
Description: in.Description,
|
||||
Status: in.Status,
|
||||
Tags: in.Tags,
|
||||
IsRecommended: in.IsRecommended,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("book_create_failed")
|
||||
}
|
||||
|
||||
return &model.BookCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sBook) Update(ctx context.Context, in *model.BookEditIn) (out *model.BookCRUDOut, err error) {
|
||||
exist, err := dao.Books.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("book_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("book_not_found")
|
||||
}
|
||||
|
||||
exist, err = dao.Books.Ctx(ctx).
|
||||
Where(dao.Books.Columns().Title, in.Title).
|
||||
Where("id != ?", in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("book_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("book_exists")
|
||||
}
|
||||
|
||||
_, err = dao.Books.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Data(do.Books{
|
||||
AuthorId: in.AuthorId,
|
||||
CategoryId: in.CategoryId,
|
||||
Title: in.Title,
|
||||
CoverUrl: in.CoverUrl,
|
||||
Description: in.Description,
|
||||
Status: in.Status,
|
||||
Tags: in.Tags,
|
||||
IsRecommended: in.IsRecommended,
|
||||
}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("book_update_failed")
|
||||
}
|
||||
|
||||
return &model.BookCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sBook) Delete(ctx context.Context, in *model.BookDelIn) (out *model.BookCRUDOut, err error) {
|
||||
exist, err := dao.Books.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("book_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("book_not_found")
|
||||
}
|
||||
|
||||
_, err = dao.Books.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("book_delete_failed")
|
||||
}
|
||||
|
||||
return &model.BookCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
123
internal/logic/category/category.go
Normal file
123
internal/logic/category/category.go
Normal file
@ -0,0 +1,123 @@
|
||||
package category
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sCategory struct {
|
||||
}
|
||||
|
||||
func New() service.ICategory {
|
||||
return &sCategory{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterCategory(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of categories
|
||||
func (s *sCategory) List(ctx context.Context, in *model.CategoryListIn) (out *model.CategoryListOut, err error) {
|
||||
out = &model.CategoryListOut{}
|
||||
m := dao.Categories.Ctx(ctx)
|
||||
if in.Type != 0 {
|
||||
m = m.Where(dao.Categories.Columns().Type, in.Type)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sCategory) Create(ctx context.Context, in *model.CategoryAddIn) (out *model.CategoryCRUDOut, err error) {
|
||||
if in.Type != 1 && in.Type != 2 {
|
||||
return nil, ecode.Params.Sub("category_type_invalid")
|
||||
}
|
||||
exist, err := dao.Categories.Ctx(ctx).
|
||||
Where(dao.Categories.Columns().Name, in.Name).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("category_exists")
|
||||
}
|
||||
|
||||
if _, err := dao.Categories.Ctx(ctx).Data(do.Categories{
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("category_create_failed")
|
||||
}
|
||||
|
||||
return &model.CategoryCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sCategory) Update(ctx context.Context, in *model.CategoryEditIn) (out *model.CategoryCRUDOut, err error) {
|
||||
if in.Type != 1 && in.Type != 2 {
|
||||
return nil, ecode.Params.Sub("category_type_invalid")
|
||||
}
|
||||
exist, err := dao.Categories.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("category_not_found")
|
||||
}
|
||||
|
||||
exist, err = dao.Categories.Ctx(ctx).
|
||||
Where(dao.Categories.Columns().Name, in.Name).
|
||||
Where("id != ?", in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("category_exists")
|
||||
}
|
||||
|
||||
// Update category
|
||||
_, err = dao.Categories.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Data(do.Categories{
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_update_failed")
|
||||
}
|
||||
|
||||
return &model.CategoryCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sCategory) Delete(ctx context.Context, in *model.CategoryDelIn) (out *model.CategoryCRUDOut, err error) {
|
||||
exist, err := dao.Categories.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("category_not_found")
|
||||
}
|
||||
|
||||
// Soft delete category
|
||||
_, err = dao.Categories.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_delete_failed")
|
||||
}
|
||||
|
||||
return &model.CategoryCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
98
internal/logic/chapter/chapter.go
Normal file
98
internal/logic/chapter/chapter.go
Normal file
@ -0,0 +1,98 @@
|
||||
package chapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sChapter struct{}
|
||||
|
||||
func New() service.IChapter {
|
||||
return &sChapter{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterChapter(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of chapters
|
||||
func (s *sChapter) List(ctx context.Context, in *model.ChapterListIn) (out *model.ChapterListOut, err error) {
|
||||
out = &model.ChapterListOut{}
|
||||
m := dao.Chapters.Ctx(ctx)
|
||||
if in.BookId != 0 {
|
||||
m = m.Where(dao.Chapters.Columns().BookId, in.BookId)
|
||||
}
|
||||
if in.Title != "" {
|
||||
m = m.Where(dao.Chapters.Columns().Title+" like ?", "%"+in.Title+"%")
|
||||
}
|
||||
if in.IsLocked != 0 {
|
||||
m = m.Where(dao.Chapters.Columns().IsLocked, in.IsLocked)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sChapter) Create(ctx context.Context, in *model.ChapterAddIn) (out *model.ChapterCRUDOut, err error) {
|
||||
if _, err := dao.Chapters.Ctx(ctx).Data(do.Chapters{
|
||||
BookId: in.BookId,
|
||||
Title: in.Title,
|
||||
Content: in.Content,
|
||||
WordCount: in.WordCount,
|
||||
Sort: in.Sort,
|
||||
IsLocked: in.IsLocked,
|
||||
RequiredScore: in.RequiredScore,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_create_failed")
|
||||
}
|
||||
return &model.ChapterCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sChapter) Update(ctx context.Context, in *model.ChapterEditIn) (out *model.ChapterCRUDOut, err error) {
|
||||
exist, err := dao.Chapters.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("chapter_not_found")
|
||||
}
|
||||
_, err = dao.Chapters.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Data(do.Chapters{
|
||||
BookId: in.BookId,
|
||||
Title: in.Title,
|
||||
Content: in.Content,
|
||||
WordCount: in.WordCount,
|
||||
Sort: in.Sort,
|
||||
IsLocked: in.IsLocked,
|
||||
RequiredScore: in.RequiredScore,
|
||||
}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_update_failed")
|
||||
}
|
||||
return &model.ChapterCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sChapter) Delete(ctx context.Context, in *model.ChapterDelIn) (out *model.ChapterCRUDOut, err error) {
|
||||
exist, err := dao.Chapters.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("chapter_not_found")
|
||||
}
|
||||
_, err = dao.Chapters.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_delete_failed")
|
||||
}
|
||||
return &model.ChapterCRUDOut{Success: true}, nil
|
||||
}
|
||||
47
internal/logic/feedback/feedback.go
Normal file
47
internal/logic/feedback/feedback.go
Normal file
@ -0,0 +1,47 @@
|
||||
package feedback
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sFeedback struct{}
|
||||
|
||||
func New() service.IFeedback {
|
||||
return &sFeedback{}
|
||||
}
|
||||
func init() {
|
||||
service.RegisterFeedback(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of feedbacks
|
||||
func (s *sFeedback) List(ctx context.Context, in *model.FeedbackListIn) (out *model.FeedbackListOut, err error) {
|
||||
out = &model.FeedbackListOut{}
|
||||
m := dao.Feedbacks.Ctx(ctx)
|
||||
if in.UserId != 0 {
|
||||
m = m.Where(dao.Feedbacks.Columns().UserId, in.UserId)
|
||||
}
|
||||
if in.Status != 0 {
|
||||
m = m.Where(dao.Feedbacks.Columns().Status, in.Status)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Create adds a new feedback
|
||||
func (s *sFeedback) Create(ctx context.Context, in *model.FeedbackAddIn) (out *model.FeedbackCRUDOut, err error) {
|
||||
if _, err := dao.Feedbacks.Ctx(ctx).Data(do.Feedbacks{
|
||||
UserId: in.UserId,
|
||||
Content: in.Content,
|
||||
Status: 1, // 默认未处理
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("feedback_create_failed")
|
||||
}
|
||||
return &model.FeedbackCRUDOut{Success: true}, nil
|
||||
}
|
||||
16
internal/logic/logic.go
Normal file
16
internal/logic/logic.go
Normal file
@ -0,0 +1,16 @@
|
||||
// ==========================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
_ "server/internal/logic/admin"
|
||||
_ "server/internal/logic/book"
|
||||
_ "server/internal/logic/category"
|
||||
_ "server/internal/logic/chapter"
|
||||
_ "server/internal/logic/feedback"
|
||||
_ "server/internal/logic/read_record"
|
||||
_ "server/internal/logic/tag"
|
||||
_ "server/internal/logic/user"
|
||||
)
|
||||
68
internal/logic/read_record/read_record.go
Normal file
68
internal/logic/read_record/read_record.go
Normal file
@ -0,0 +1,68 @@
|
||||
package read_record
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sReadRecord struct{}
|
||||
|
||||
func New() service.IReadRecord {
|
||||
return &sReadRecord{}
|
||||
}
|
||||
func init() {
|
||||
service.RegisterReadRecord(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of read records
|
||||
func (s *sReadRecord) List(ctx context.Context, in *model.ReadRecordListIn) (out *model.ReadRecordListOut, err error) {
|
||||
out = &model.ReadRecordListOut{}
|
||||
m := dao.ReadRecords.Ctx(ctx)
|
||||
if in.UserId != 0 {
|
||||
m = m.Where(dao.ReadRecords.Columns().UserId, in.UserId)
|
||||
}
|
||||
if in.BookId != 0 {
|
||||
m = m.Where(dao.ReadRecords.Columns().BookId, in.BookId)
|
||||
}
|
||||
if in.ChapterId != 0 {
|
||||
m = m.Where(dao.ReadRecords.Columns().ChapterId, in.ChapterId)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Create adds a new read record
|
||||
func (s *sReadRecord) Create(ctx context.Context, in *model.ReadRecordAddIn) (out *model.ReadRecordCRUDOut, err error) {
|
||||
if _, err := dao.ReadRecords.Ctx(ctx).Data(do.ReadRecords{
|
||||
UserId: in.UserId,
|
||||
BookId: in.BookId,
|
||||
ChapterId: in.ChapterId,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("read_record_create_failed")
|
||||
}
|
||||
return &model.ReadRecordCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
// Delete removes a read record by id
|
||||
func (s *sReadRecord) Delete(ctx context.Context, in *model.ReadRecordDelIn) (out *model.ReadRecordCRUDOut, err error) {
|
||||
exist, err := dao.ReadRecords.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("read_record_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("read_record_not_found")
|
||||
}
|
||||
_, err = dao.ReadRecords.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("read_record_delete_failed")
|
||||
}
|
||||
return &model.ReadRecordCRUDOut{Success: true}, nil
|
||||
}
|
||||
119
internal/logic/tag/tag.go
Normal file
119
internal/logic/tag/tag.go
Normal file
@ -0,0 +1,119 @@
|
||||
package tag
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sTag struct{}
|
||||
|
||||
func New() service.ITag {
|
||||
return &sTag{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterTag(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of tags
|
||||
func (s *sTag) List(ctx context.Context, in *model.TagListIn) (out *model.TagListOut, err error) {
|
||||
out = &model.TagListOut{}
|
||||
m := dao.Tags.Ctx(ctx)
|
||||
if in.Name != "" {
|
||||
m = m.Where(dao.Tags.Columns().Name+" like ?", "%"+in.Name+"%")
|
||||
}
|
||||
if in.Type != 0 {
|
||||
m = m.Where(dao.Tags.Columns().Type, in.Type)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sTag) Create(ctx context.Context, in *model.TagAddIn) (out *model.TagCRUDOut, err error) {
|
||||
exist, err := dao.Tags.Ctx(ctx).
|
||||
Where(dao.Tags.Columns().Name, in.Name).
|
||||
Where(dao.Tags.Columns().Type, in.Type).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("tag_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("tag_exists")
|
||||
}
|
||||
|
||||
if _, err := dao.Tags.Ctx(ctx).Data(do.Tags{
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("tag_create_failed")
|
||||
}
|
||||
|
||||
return &model.TagCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sTag) Update(ctx context.Context, in *model.TagEditIn) (out *model.TagCRUDOut, err error) {
|
||||
exist, err := dao.Tags.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("tag_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("tag_not_found")
|
||||
}
|
||||
|
||||
exist, err = dao.Tags.Ctx(ctx).
|
||||
Where(dao.Tags.Columns().Name, in.Name).
|
||||
Where(dao.Tags.Columns().Type, in.Type).
|
||||
Where("id != ?", in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("tag_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("tag_exists")
|
||||
}
|
||||
|
||||
_, err = dao.Tags.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Data(do.Tags{
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("tag_update_failed")
|
||||
}
|
||||
|
||||
return &model.TagCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sTag) Delete(ctx context.Context, in *model.TagDelIn) (out *model.TagCRUDOut, err error) {
|
||||
exist, err := dao.Tags.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("tag_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("tag_not_found")
|
||||
}
|
||||
|
||||
_, err = dao.Tags.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("tag_delete_failed")
|
||||
}
|
||||
|
||||
return &model.TagCRUDOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
136
internal/logic/user/user.go
Normal file
136
internal/logic/user/user.go
Normal file
@ -0,0 +1,136 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/model/entity"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
"server/utility/encrypt"
|
||||
"server/utility/jwt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type sUser struct{}
|
||||
|
||||
func New() service.IUser {
|
||||
return &sUser{}
|
||||
}
|
||||
func init() {
|
||||
service.RegisterUser(New())
|
||||
}
|
||||
|
||||
func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.UserLoginOut, err error) {
|
||||
user, err := dao.Users.Ctx(ctx).Where(do.Users{Email: in.Email}).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("database_query_failed")
|
||||
}
|
||||
if user == nil {
|
||||
return nil, ecode.Auth // 账户名或密码不正确
|
||||
}
|
||||
var entityUser entity.Users
|
||||
if err = user.Struct(&entityUser); err != nil {
|
||||
return nil, ecode.Fail.Sub("data_conversion_failed")
|
||||
}
|
||||
if !encrypt.ComparePassword(entityUser.PasswordHash, in.Password) {
|
||||
return nil, ecode.Password // 密码不正确
|
||||
}
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{UserId: entityUser.Id, Role: "user"})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("token_generation_failed")
|
||||
}
|
||||
return &model.UserLoginOut{Token: token}, nil
|
||||
}
|
||||
|
||||
func (s *sUser) Register(ctx context.Context, in *model.UserRegisterIn) (out *model.UserRegisterOut, err error) {
|
||||
if in.Password != in.Password2 {
|
||||
return nil, ecode.Password.Sub("password_mismatch")
|
||||
}
|
||||
exist, err := dao.Users.Ctx(ctx).Where(do.Users{Email: in.Email}).Count()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("database_query_failed")
|
||||
}
|
||||
if exist > 0 {
|
||||
return nil, ecode.EmailExist // 该邮箱已被注册
|
||||
}
|
||||
hash, err := encrypt.EncryptPassword(in.Password)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("password_encryption_failed")
|
||||
}
|
||||
_, err = dao.Users.Ctx(ctx).Data(do.Users{
|
||||
Email: in.Email,
|
||||
PasswordHash: hash,
|
||||
Username: strings.Split(in.Email, "@")[0],
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("registration_failed")
|
||||
}
|
||||
return &model.UserRegisterOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sUser) Info(ctx context.Context, in *model.UserInfoIn) (out *model.UserInfoOut, err error) {
|
||||
user, err := dao.Users.Ctx(ctx).Where(do.Users{Id: in.UserId}).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("database_query_failed")
|
||||
}
|
||||
if user == nil {
|
||||
return nil, ecode.Auth.Sub("user_not_found")
|
||||
}
|
||||
var entityUser entity.Users
|
||||
if err = user.Struct(&entityUser); err != nil {
|
||||
return nil, ecode.Fail.Sub("data_conversion_failed")
|
||||
}
|
||||
return &model.UserInfoOut{
|
||||
UserId: entityUser.Id,
|
||||
Username: entityUser.Username,
|
||||
Email: entityUser.Email,
|
||||
Avatar: entityUser.Avatar,
|
||||
Points: entityUser.Points, // 如有积分表可补充
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sUser) Delete(ctx context.Context, in *model.UserDeleteIn) (out *model.UserDeleteOut, err error) {
|
||||
// FIXME
|
||||
|
||||
return &model.UserDeleteOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sUser) Code(ctx context.Context, in *model.UserCodeIn) (out *model.UserCodeOut, err error) {
|
||||
exist, err := dao.Users.Ctx(ctx).Where(do.Users{Email: in.Email}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("database_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Params.Sub("email_not_found")
|
||||
}
|
||||
return &model.UserCodeOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sUser) EditPass(ctx context.Context, in *model.UserEditPassIn) (out *model.UserEditPassOut, err error) {
|
||||
exist, err := dao.Users.Ctx(ctx).Where(do.Users{Email: in.Email}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("database_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Params.Sub("email_not_found")
|
||||
}
|
||||
if in.Sign != "123456" {
|
||||
return nil, ecode.Params.Sub("sign_error")
|
||||
}
|
||||
if in.Password != in.Password2 {
|
||||
return nil, ecode.Password.Sub("password_mismatch")
|
||||
}
|
||||
hash, err := encrypt.EncryptPassword(in.Password)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("password_encryption_failed")
|
||||
}
|
||||
_, err = dao.Users.Ctx(ctx).Where(do.Users{Email: in.Email}).Data(do.Users{PasswordHash: hash}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("password_update_failed")
|
||||
}
|
||||
return &model.UserEditPassOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
66
internal/logic/user_follow_author/user_follow_author.go
Normal file
66
internal/logic/user_follow_author/user_follow_author.go
Normal file
@ -0,0 +1,66 @@
|
||||
package user_follow_author
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sUserFollowAuthor struct{}
|
||||
|
||||
// List retrieves a paginated list of user follow authors
|
||||
func (s *sUserFollowAuthor) List(ctx context.Context, in *model.UserFollowAuthorListIn) (out *model.UserFollowAuthorListOut, err error) {
|
||||
out = &model.UserFollowAuthorListOut{}
|
||||
m := dao.UserFollowAuthors.Ctx(ctx)
|
||||
if in.UserId != 0 {
|
||||
m = m.Where(dao.UserFollowAuthors.Columns().UserId, in.UserId)
|
||||
}
|
||||
if in.AuthorId != 0 {
|
||||
m = m.Where(dao.UserFollowAuthors.Columns().AuthorId, in.AuthorId)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Create adds a new user follow author
|
||||
func (s *sUserFollowAuthor) Create(ctx context.Context, in *model.UserFollowAuthorAddIn) (out *model.UserFollowAuthorCRUDOut, err error) {
|
||||
exist, err := dao.UserFollowAuthors.Ctx(ctx).
|
||||
Where(dao.UserFollowAuthors.Columns().UserId, in.UserId).
|
||||
Where(dao.UserFollowAuthors.Columns().AuthorId, in.AuthorId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("user_follow_author_exists")
|
||||
}
|
||||
if _, err := dao.UserFollowAuthors.Ctx(ctx).Data(do.UserFollowAuthors{
|
||||
UserId: in.UserId,
|
||||
AuthorId: in.AuthorId,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_create_failed")
|
||||
}
|
||||
return &model.UserFollowAuthorCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
// Delete removes a user follow author by id
|
||||
func (s *sUserFollowAuthor) Delete(ctx context.Context, in *model.UserFollowAuthorDelIn) (out *model.UserFollowAuthorCRUDOut, err error) {
|
||||
exist, err := dao.UserFollowAuthors.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("user_follow_author_not_found")
|
||||
}
|
||||
_, err = dao.UserFollowAuthors.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_delete_failed")
|
||||
}
|
||||
return &model.UserFollowAuthorCRUDOut{Success: true}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user