diff --git a/dao/checkin.go b/dao/checkin.go index 3056c0f..8f7cf71 100644 --- a/dao/checkin.go +++ b/dao/checkin.go @@ -71,9 +71,14 @@ func (c CheckinDAO) SaveUserActivity(userActivity *po.UserActivity) { panic(err) } } -func (c CheckinDAO) FindAllActivities() []po.Activity { +func (c CheckinDAO) FindAllActivities(isTeacher bool) []po.Activity { var arr []po.Activity - err := c.Tx.Find(&arr).Error + var err error + if isTeacher { + err = c.Tx.Find(&arr, "teacher_available=?", true).Error + } else { + err = c.Tx.Find(&arr).Error + } if err != nil { panic(err) } diff --git a/dao/user.go b/dao/user.go index dbbfea7..9fb9403 100644 --- a/dao/user.go +++ b/dao/user.go @@ -42,6 +42,7 @@ func (u UserDAO) CreateUser(username string, realName string) *po.User { Username: username, RealName: realName, RoleID: 1, + Type: 1, } err := u.Tx.Create(&user).Error if err != nil { diff --git a/data/po/checkin.go b/data/po/checkin.go index db7e4b1..644dc6a 100644 --- a/data/po/checkin.go +++ b/data/po/checkin.go @@ -7,11 +7,12 @@ import ( ) type Activity struct { - ID int `json:"id"` - Category string `json:"category"` - Name string `json:"name"` - Image string `json:"image"` - Credit int `json:"credit"` + ID int `json:"id"` + Category string `json:"category"` + Name string `json:"name"` + Image string `json:"image"` + Credit int `json:"credit"` + TeacherAvailable int `json:"teacher_available"` } func (a Activity) TableName() string { diff --git a/data/po/user.go b/data/po/user.go index e81caa3..e6e8e02 100644 --- a/data/po/user.go +++ b/data/po/user.go @@ -6,4 +6,5 @@ type User struct { RealName string `json:"real_name"` Credit int `json:"credit"` RoleID int `json:"role_id"` + Type int `json:"type"` } diff --git a/router/router.go b/router/router.go index 610d897..654e0bd 100644 --- a/router/router.go +++ b/router/router.go @@ -16,12 +16,13 @@ func Setup(engine *gin.Engine) { user.POST("/login", app.HandlerFunc(hub.Login)) user.Use(middleware.JWT(1)) user.GET("/me", app.HandlerFunc(hub.Me)) + user.POST("/set_type", app.HandlerFunc(hub.SetType)) } checkin := engine.Group("/checkin") { hub := service.ExCheckinService - checkin.GET("/list_activities", app.HandlerFunc(hub.ListActivities)) checkin.Use(middleware.JWT(1)) + checkin.GET("/list_activities", app.HandlerFunc(hub.ListActivities)) checkin.POST("/submit", app.HandlerFunc(hub.Submit)) checkin.GET("/get_my_activities_today", app.HandlerFunc(hub.GetMyActivitiesToday)) checkin.POST("/set_status", middleware.JWT(2), app.HandlerFunc(hub.SetStatus)) diff --git a/service/checkin.go b/service/checkin.go index 5deeb60..3d146cc 100644 --- a/service/checkin.go +++ b/service/checkin.go @@ -63,7 +63,12 @@ func (c CheckinService) SetStatus(aw *app.Wrapper) app.Result { return aw.Success(userActivity) } func (c CheckinService) ListActivities(aw *app.Wrapper) app.Result { - return aw.Success(checkinDAO.FindAllActivities()) + user := userDAO.FindUserByID(aw.ExtractUserClaims().UserID) + isTeacher := false + if user.Type == 2 { + isTeacher = true + } + return aw.Success(checkinDAO.FindAllActivities(isTeacher)) } func (c CheckinService) ListUserActivities(aw *app.Wrapper) app.Result { type StatusReq struct { diff --git a/service/user.go b/service/user.go index 876af7f..462e0ed 100644 --- a/service/user.go +++ b/service/user.go @@ -31,3 +31,16 @@ func (u UserService) Login(aw *app.Wrapper) app.Result { func (u UserService) Me(aw *app.Wrapper) app.Result { return aw.Success(userDAO.FindUserByID(aw.ExtractUserClaims().UserID)) } +func (u UserService) SetType(aw *app.Wrapper) app.Result { + type TypeReq struct { + Type int `form:"type" binding:"required,gte=1,lte=2"` + } + var req TypeReq + if err := aw.Ctx.ShouldBind(&req); err != nil { + return aw.Error(err.Error()) + } + user := userDAO.FindUserByID(aw.ExtractUserClaims().UserID) + user.Type = req.Type + userDAO.SaveUser(user) + return aw.Success(user) +}