1 Star 4 Fork 0

jingshanccc / course

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
course.go 8.87 KB
一键复制 编辑 原始数据 按行查看 历史
micahchen 提交于 2021-05-26 23:07 . [feature] #90, #267
package handler
import (
"context"
"gitee.com/jingshanccc/course/course/dao"
"gitee.com/jingshanccc/course/course/proto/dto"
"gitee.com/jingshanccc/course/public"
"gitee.com/jingshanccc/course/public/config"
"gitee.com/jingshanccc/course/public/proto/basic"
"gitee.com/jingshanccc/course/public/util"
"github.com/micro/go-micro/v2/errors"
"gorm.io/gorm"
"strings"
"time"
)
//CourseList: get course page
func (c *CourseServiceHandler) CourseList(ctx context.Context, in *dto.CoursePageDto, out *dto.CoursePageDto) error {
count, courseDtos, err := courseDao.List(in)
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
_ = util.CopyProperties(out, in)
out.Rows = courseDtos
out.Total = count
return nil
}
//MyCourse: 我的课程
func (c *CourseServiceHandler) MyCourse(ctx context.Context, in *basic.String, out *dto.CourseDtoList) error {
courseDtos, err := memberCourseDao.MyCourse(in.Str)
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
out.Rows = courseDtos
return nil
}
//AddToMyCourse: 添加到我的课程
func (c *CourseServiceHandler) AddToMyCourse(ctx context.Context, in *dto.MemberCourseDto, out *basic.String) error {
var data dao.MemberCourse
_ = util.CopyProperties(&data, in)
data.LastLearn = "1 1 0"
data.CreateAt = time.Now()
data.UpdateAt = data.CreateAt
err := memberCourseDao.AddToMyCourse(&data)
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
courseDao.UpdateEnroll(in.CourseId)
return nil
}
//CourseInfo: 课程学习情况
func (c *CourseServiceHandler) CourseInfo(ctx context.Context, in *basic.StringList, out *basic.String) error {
info, err := memberCourseDao.CourseInfo(in.Rows[0], in.Rows[1])
out.Str = info
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
return nil
}
//SaveLearnInfo: 保存课程学习进度
func (c *CourseServiceHandler) SaveLearnInfo(ctx context.Context, in *basic.StringList, out *basic.String) error {
if len(in.Rows) != 3 || len(strings.Split(in.Rows[1], " ")) != 3 {
exception := public.NewBusinessException(public.VALID_PARM_ERROR)
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
err := memberCourseDao.SaveLearnInfo(in.Rows)
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
return nil
}
//CarouselCourse: 轮播图课程
func (c *CourseServiceHandler) CarouselCourse(ctx context.Context, in *basic.String, out *dto.CourseDtoList) error {
courseDtos, err := courseDao.CarouselCourse()
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
out.Rows = courseDtos
return nil
}
//NewPublishCourse: 新上好课
func (c *CourseServiceHandler) NewPublishCourse(ctx context.Context, in *basic.String, out *dto.CourseDtoList) error {
courseDtos, err := courseDao.NewPublish()
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
out.Rows = courseDtos
return nil
}
//RelatedCourse: 新上好课
func (c *CourseServiceHandler) RelatedCourse(ctx context.Context, in *basic.String, out *dto.CourseDtoList) error {
ccds, _ := courseCategoryDao.SelectByCourseId(in.Str)
courseDtos, err := courseDao.SelectCourseByIds(courseCategoryDao.SelectCourseIds(ccds...), false)
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
if len(courseDtos) == 1 {
courseDtos, _ = courseDao.NewPublish()
}
out.Rows = courseDtos
return nil
}
//CategoryCourse: 分类搜索课程
func (c *CourseServiceHandler) CategoryCourse(ctx context.Context, in *basic.String, out *dto.CourseDtoList) error {
// 通过 分类id 获取分类id和其父分类id 获取两个id 对应的所有课程id
var ids []string
var err *public.BusinessException
var courseDtos []*dto.CourseDto
if in.Str != "" {
ids = courseCategoryDao.SelectCourseIds(in.Str)
courseDtos, err = courseDao.SelectCourseByIds(ids, false)
} else {
courseDtos, err = courseDao.SelectCourseByIds(ids, true)
}
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
out.Rows = courseDtos
return nil
}
//DownloadCourseContent: 下载课程讲义
func (c *CourseServiceHandler) DownloadCourseContent(ctx context.Context, in *basic.String, out *basic.String) error {
content, exception := courseDao.FindContent(in.Str)
if exception != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
out.Str = util.TransToHtml(content.Content)
return nil
}
//CourseDetail: 课程详情
func (c *CourseServiceHandler) CourseDetail(ctx context.Context, in *basic.String, out *dto.CourseDto) error {
courseDb := courseDao.SelectByProperty("id", in.Str)
if courseDb.Id != "" {
_ = util.CopyProperties(out, courseDb)
teacherDb, _ := teacherDao.SelectByProperty("id", courseDb.TeacherId)
chapters, _ := chapterDao.SelectByProperty("course_id", courseDb.Id)
sections, _ := sectionDao.SelectByProperty("course_id", courseDb.Id)
out.Chapters = chapters
out.Teacher = teacherDb
out.Sections = sections
}
return nil
}
//SaveCourse: 保存/更新课程
func (c *CourseServiceHandler) SaveCourse(ctx context.Context, in *dto.CourseDto, out *dto.CourseDto) error {
cd, err := courseDao.Save(in)
if err != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, err.Error(), err.Code())
}
_ = util.CopyProperties(out, cd)
return nil
}
//DeleteCourse: 删除课程
func (c *CourseServiceHandler) DeleteCourse(ctx context.Context, in *basic.StringList, out *basic.String) error {
exception := courseDao.Delete(in.Rows)
if exception != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
return nil
}
//SortCourse: 更新课程排序
func (c *CourseServiceHandler) SortCourse(ctx context.Context, in *dto.SortDto, out *basic.String) error {
var exception *public.BusinessException
var tx *gorm.DB
var err error
defer func() {
if exception != nil {
tx.Rollback()
err = errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
} else {
err = nil
}
}()
tx = public.DB.Begin()
exception = courseDao.UpdateSort(tx, in)
if in.NewSort > in.OldSort {
exception = courseDao.MoveSortsForward(tx, in)
}
if in.OldSort > in.NewSort {
exception = courseDao.MoveSortsBackward(tx, in)
}
tx.Commit()
return err
}
//FindCourseContent: 查找课程内容
func (c *CourseServiceHandler) FindCourseContent(ctx context.Context, in *basic.String, out *dto.CourseContentDto) error {
content, exception := courseDao.FindContent(in.Str)
if exception != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
out.Id = content.Id
out.Content = content.Content
return nil
}
//SaveCourseContent: 保存课程内容
func (c *CourseServiceHandler) SaveCourseContent(ctx context.Context, in *dto.CourseContentDto, out *basic.String) error {
exception := courseDao.SaveContent(in)
if exception != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
return nil
}
//ListCourseFile: 获取课程文件
func (c *CourseServiceHandler) ListCourseFile(ctx context.Context, in *basic.String, out *dto.CourseFileDtoList) error {
list, exception := courseFileDao.List(in.Str)
if exception != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
out.Rows = list
return nil
}
//SaveCourseFile: 保存课程文件
func (c *CourseServiceHandler) SaveCourseFile(ctx context.Context, in *dto.CourseFileDto, out *dto.CourseFileDto) error {
fileDto, exception := courseFileDao.Save(in)
if exception != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
_ = util.CopyProperties(out, fileDto)
return nil
}
//DeleteCourseFile: 删除课程文件
func (c *CourseServiceHandler) DeleteCourseFile(ctx context.Context, in *basic.String, out *basic.String) error {
exception := courseFileDao.Delete(in.Str)
if exception != nil {
return errors.New(config.Conf.BasicConfig.BasicName+config.Conf.Services["course"].Name, exception.Error(), exception.Code())
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jingshanccc/course.git
git@gitee.com:jingshanccc/course.git
jingshanccc
course
course
23f538baa694

搜索帮助

344bd9b3 5694891 D2dac590 5694891