-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
337 lines (224 loc) · 10 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package courses
import (
"fmt"
"time"
categories "github.com/spurtcms/categories"
"gorm.io/gorm"
)
type Filter struct {
Keyword string
Category string
Status string
Sorting string
CourseTitle string
}
type CoursesModel struct {
Userid int
DataAccess int
}
var Coursemodels CoursesModel
// Create function
type TblCourse struct {
Id int `gorm:"primaryKey;auto_increment;type:serial"`
Title string `gorm:"type:character varying"`
Description string `gorm:"type:character varying"`
ImageName string `gorm:"type:character varying"`
ImagePath string `gorm:"type:character varying"`
CategoryId string `gorm:"type:character varying"`
Status int `gorm:"type:integer"`
TenantId string `gorm:"type:character varying"`
CreatedOn time.Time `gorm:"type:timestamp without time zone;DEFAULT:NULL"`
CreatedBy int `gorm:"type:integer"`
ModifiedOn time.Time `gorm:"DEFAULT:NULL"`
ModifiedBy int `gorm:"type:integer;DEFAULT:NULL"`
DeletedOn time.Time `gorm:"DEFAULT:NULL"`
DeletedBy int `gorm:"type:integer;DEFAULT:NULL"`
IsDeleted int `gorm:"type:integer;DEFAULT:0"`
}
// List function
type TblCourses struct {
Id int `gorm:"primaryKey;auto_increment;type:serial"`
Title string `gorm:"type:character varying"`
Description string `gorm:"type:character varying"`
ImageName string `gorm:"type:character varying"`
ImagePath string `gorm:"type:character varying"`
CategoryId string `gorm:"type:character varying"`
Status int `gorm:"type:integer"`
TenantId string `gorm:"type:character varying"`
CreatedOn time.Time `gorm:"type:timestamp without time zone;DEFAULT:NULL"`
CreatedBy int `gorm:"type:integer"`
ModifiedOn time.Time `gorm:"DEFAULT:NULL"`
ModifiedBy int `gorm:"type:integer;DEFAULT:NULL"`
DeletedOn time.Time `gorm:"DEFAULT:NULL"`
DeletedBy int `gorm:"type:integer;DEFAULT:NULL"`
IsDeleted int `gorm:"type:integer;DEFAULT:0"`
DateString string `gorm:"-"`
CreatedTime string `gorm:"-"`
ProfileImagePath string `gorm:"column:profile_image_path"`
FirstName string `gorm:"column:first_name"`
LastName string `gorm:"column:last_name"`
UserName string `gorm:"column:username"`
NameString string `gorm:"-"`
}
//Create Section
type TblSection struct {
Id int `gorm:"primaryKey;auto_increment;type:serial"`
Title string `gorm:"type:character varying"`
Content string `gorm:"type:character varying"`
CourseId int `gorm:"type:integer"`
TenantId string `gorm:"type:character varying"`
OrderIndex int `gorm:"type:integer"`
CreatedOn time.Time `gorm:"type:timestamp without time zone;DEFAULT:NULL"`
CreatedBy int `gorm:"type:integer"`
ModifiedOn time.Time `gorm:"DEFAULT:NULL"`
ModifiedBy int `gorm:"type:integer;DEFAULT:NULL"`
DeletedOn time.Time `gorm:"DEFAULT:NULL"`
DeletedBy int `gorm:"type:integer;DEFAULT:NULL"`
IsDeleted int `gorm:"type:integer;DEFAULT:0"`
}
//Create Lesson fields
type TblLesson struct {
Id int `gorm:"primaryKey;auto_increment;type:serial"`
CourseId int `gorm:"type:integer"`
SectionId int `gorm:"type:integer"`
Title string `gorm:"type:character varying"`
Content string `gorm:"type:character varying"`
EmbedLink string `gorm:"type:character varying"`
FileName string `gorm:"type:character varying"`
LessonType string `gorm:"type:character varying"`
OrderIndex int `gorm:"type:integer"`
TenantId string `gorm:"type:character varying"`
CreatedOn time.Time `gorm:"type:timestamp without time zone;DEFAULT:NULL"`
CreatedBy int `gorm:"type:integer"`
ModifiedOn time.Time `gorm:"DEFAULT:NULL"`
ModifiedBy int `gorm:"type:integer;DEFAULT:NULL"`
DeletedOn time.Time `gorm:"DEFAULT:NULL"`
DeletedBy int `gorm:"type:integer;DEFAULT:NULL"`
IsDeleted int `gorm:"type:integer;DEFAULT:0"`
}
func (Coursesmodels CoursesModel) ListCourses(limit, offset int, filter Filter, tenantid string, DB *gorm.DB) (courselist []TblCourses, count int64, err error) {
query := DB.Table("tbl_courses").Select("tbl_courses.*,tbl_users.profile_image_path,tbl_users.first_name,tbl_users.last_name,tbl_users.username").Joins("inner join tbl_users on tbl_courses.created_by=tbl_users.id").Where("tbl_courses.is_deleted=0 and tbl_courses.tenant_id=?", tenantid)
if filter.Sorting == "lastUpdated" {
query = query.Order("modified_on desc")
} else if filter.Sorting == "createdDate" {
query = query.Order("created_on asc")
} else if filter.Sorting == "asc" {
query = query.Order("title asc")
} else if filter.Sorting == "desc" {
query = query.Order("title desc")
} else {
query = query.Order("tbl_courses.id desc")
}
if filter.Keyword != "" {
query = query.Where("LOWER(TRIM(tbl_courses.title)) like LOWER(TRIM(?))", "%"+filter.Keyword+"%")
}
if filter.CourseTitle != "" {
query = query.Where("LOWER(TRIM(tbl_courses.title)) like LOWER(TRIM(?))", "%"+filter.CourseTitle+"%")
}
if filter.Status != "" {
query = query.Where("tbl_courses.status=?", filter.Status)
}
if limit != 0 {
query.Limit(limit).Offset(offset).Find(&courselist)
return courselist, count, nil
}
query.Find(&courselist).Count(&count)
if query.Error != nil {
return []TblCourses{}, 0, query.Error
}
return courselist, count, nil
}
func (Coursemodels CoursesModel) CreateCourse(course TblCourse, DB *gorm.DB) error {
if err := DB.Table("tbl_courses").Create(&course).Error; err != nil {
return err
}
fmt.Println("hello world courses created")
return nil
}
func (Coursemodels CoursesModel) EditCourse(id int, tenantid string, DB *gorm.DB) (courselist TblCourse, err error) {
if err := DB.Table("tbl_courses").Where("id=? and tenant_id=? and is_deleted=0", id, tenantid).First(&courselist).Error; err != nil {
return TblCourse{}, err
}
return courselist, nil
}
func (Coursemodels CoursesModel) DeleteCourse(id int, tenantid string, deletedby int, deletedon time.Time, DB *gorm.DB) error {
if err := DB.Table("tbl_courses").Where("id=? and tenant_id=?", id, tenantid).UpdateColumns(map[string]interface{}{"is_deleted": 1, "deleted_by": deletedby, "deleted_on": deletedon}).Error; err != nil {
return err
}
return nil
}
func (Coursemodels CoursesModel) MultiSelectCourseDelete(course *TblCourse, id []int, DB *gorm.DB) error {
if err := DB.Table("tbl_courses").Where("id in (?) and tenant_id=?", id, course.TenantId).UpdateColumns(map[string]interface{}{"is_deleted": course.IsDeleted, "deleted_on": course.DeletedOn, "deleted_by": course.DeletedBy}).Error; err != nil {
return err
}
return nil
}
func (Coursemodels CoursesModel) GetCategoriseById(id []int, DB *gorm.DB, tenantid string) (category []categories.TblCategories, err error) {
if err := DB.Table("tbl_categories").Where("id in (?) and tenant_id=?", id, tenantid).Order("id asc").Find(&category).Error; err != nil {
return category, err
}
return category, nil
}
//create sections
func (Coursemodels CoursesModel) CreateSection(section TblSection, DB *gorm.DB) error {
if err := DB.Table("tbl_sections").Create(§ion).Error; err != nil {
return err
}
return nil
}
//ListSections
func (Coursemodels CoursesModel) SectionList(id int, tenantid string, DB *gorm.DB) (section []TblSection, err error) {
if err := DB.Table("tbl_sections").Where("course_id=? and tenant_id=? and is_deleted=0", id, tenantid).Find(§ion).Error; err != nil {
return []TblSection{}, err
}
return section, nil
}
//Edit Section
func (Coursemodels CoursesModel) EditSection(sectionid int, coursesid int, tenantid string, DB *gorm.DB) (section TblSection, err error) {
if err := DB.Debug().Table("tbl_sections").Where("id=? and course_id=? and tenant_id=?", sectionid, coursesid, tenantid).First(§ion).Error; err != nil {
return TblSection{}, err
}
return section, nil
}
//Section Delete
func (Coursemodels CoursesModel) DeleteSection(sectionid int, coursesid int, tenantid string, deletedby int, deletedon time.Time, DB *gorm.DB) error {
if err := DB.Table("tbl_sections").Where("id=? and course_id=? and tenant_id=?", sectionid, coursesid, tenantid).UpdateColumns(map[string]interface{}{"is_deleted": 1, "deleted_by": deletedby, "deleted_on": deletedon}).Error; err != nil {
return err
}
return nil
}
//create Lesson
func (Coursemodels CoursesModel) CreateLesson(lesson TblLesson, DB *gorm.DB) error {
if err := DB.Table("tbl_lessons").Create(&lesson).Error; err != nil {
return err
}
return nil
}
//ListLesson
func (Coursemodels CoursesModel) LessonList(id int, tenantid string, DB *gorm.DB) (lesson []TblLesson, err error) {
if err := DB.Table("tbl_lessons").Where("course_id=? and tenant_id=? and is_deleted=0", id, tenantid).Find(&lesson).Error; err != nil {
return []TblLesson{}, err
}
return lesson, nil
}
//Edit Lesson
func (Coursemodels CoursesModel) EditLesson(lessonid int, coursesid int, tenantid string, DB *gorm.DB) (lesson TblLesson, err error) {
if err := DB.Debug().Table("tbl_lessons").Where("id=? and course_id=? and tenant_id=?", lessonid, coursesid, tenantid).First(&lesson).Error; err != nil {
return TblLesson{}, err
}
return lesson, nil
}
//Delete Lesson
func (Coursemodels CoursesModel) DeleteLesson(lessonid int, coursesid int, tenantid string, deletedby int, deletedon time.Time, DB *gorm.DB) error {
if err := DB.Table("tbl_lessons").Where("id=? and course_id=? and tenant_id=?", lessonid, coursesid, tenantid).UpdateColumns(map[string]interface{}{"is_deleted": 1, "deleted_by": deletedby, "deleted_on": deletedon}).Error; err != nil {
return err
}
return nil
}
//Publish Course
func (Coursemodels CoursesModel) StatusChange(coursesid int,status int, tenantid string, DB *gorm.DB) error {
if err := DB.Table("tbl_courses").Where("id=? and tenant_id=?", coursesid, tenantid).UpdateColumns(map[string]interface{}{"status": status}).Error; err != nil {
return err
}
return nil
}