Skip to content

Commit

Permalink
Improve schedule output
Browse files Browse the repository at this point in the history
  • Loading branch information
LikDan committed Nov 27, 2023
1 parent dcc86c2 commit 4f84851
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 213 deletions.
24 changes: 16 additions & 8 deletions internal/auth/entities/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
)

type User struct {
Id primitive.ObjectID `json:"id" bson:"_id"`
Password string `json:"-" bson:"password"`
Email string `json:"email" bson:"email"`
VerifiedEmail bool `json:"verifiedEmail" bson:"verifiedEmail"`
FirebaseToken string `json:"-" bson:"firebaseToken" encryption:""`
Login string `json:"login" bson:"login"`
PictureUrl string `json:"picture" bson:"picture" encryption:""`
StudyPlaceInfo *UserStudyPlaceInfo `json:"studyPlaceInfo" bson:"studyPlaceInfo" encryption:""`
Id primitive.ObjectID `json:"id" bson:"_id"`
Password string `json:"-" bson:"password"`
Email string `json:"email" bson:"email"`
VerifiedEmail bool `json:"verifiedEmail" bson:"verifiedEmail"`
FirebaseToken string `json:"-" bson:"firebaseToken" encryption:""`
Login string `json:"login" bson:"login"`
PictureUrl string `json:"picture" bson:"picture" encryption:""`
StudyPlaceInfo *UserStudyPlaceInfo `json:"studyPlaceInfo" bson:"studyPlaceInfo" encryption:""`
SchedulePreferences *UserSchedulePreferences `json:"schedulePreferences" bson:"schedulePreferences"`
}

type UserStudyPlaceInfo struct {
Expand All @@ -24,3 +25,10 @@ type UserStudyPlaceInfo struct {
Permissions []string `json:"permissions" bson:"permissions"`
Accepted bool `json:"accepted" bson:"accepted"`
}

type UserSchedulePreferences struct {
StudyPlaceID primitive.ObjectID `json:"studyPlaceID" bson:"studyPlaceID"`
Type string `json:"type" bson:"type"`
TypeID primitive.ObjectID `json:"typeID" bson:"typeID"`
ViewMode string `json:"viewMode" bson:"viewMode"`
}
1 change: 1 addition & 0 deletions internal/general/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type StudyPlace struct {
Restricted bool `json:"restricted" bson:"restricted"`
AdminID primitive.ObjectID `json:"adminID" bson:"adminID"`
AbsenceMark string `json:"absenceMark" bson:"absenceMark"`
IsSchedulePrivate bool `json:"IsSchedulePrivate" bson:"IsSchedulePrivate"`
}

type MarkType struct {
Expand Down
160 changes: 42 additions & 118 deletions internal/schedule/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
)

var NotValidParams = errors.New("not valid params")
var NoPermission = errors.New("no permission")

type Controller interface {
GetSchedule(ctx context.Context, user auth.User, studyPlaceID, role, roleName string, startDate, endDate time.Time, isGeneral bool) (entities.Schedule, error)
GetUserSchedule(ctx context.Context, user auth.User, startDate, endDate time.Time, isGeneral bool) (entities.Schedule, error)

GetGeneralSchedule(ctx context.Context, user auth.User, studyPlaceID string, role string, roleName string, startDate, endDate time.Time) (entities.Schedule, error)
GetGeneralUserSchedule(ctx context.Context, user auth.User, startDate, endDate time.Time) (entities.Schedule, error)
GetSchedule(ctx context.Context, user auth.User, studyPlaceID, role, roleName string, startDate, endDate time.Time) (entities.Schedule, error)
GetGeneralSchedule(ctx context.Context, user auth.User, studyPlaceID string, role string, roleName string) (entities.GeneralSchedule, error)

GetScheduleTypes(ctx context.Context, user auth.User, idHex string) entities.Types

Expand Down Expand Up @@ -73,123 +71,61 @@ func (s *controller) scheduleDated(start, end time.Time) (time.Time, time.Time)
return start, end
}

func (s *controller) GetSchedule(ctx context.Context, user auth.User, studyPlaceIDHex, type_, typeIDStr string, startDate, endDate time.Time, isGeneral bool) (entities.Schedule, error) {
typeID, err := primitive.ObjectIDFromHex(typeIDStr)
if err != nil {
return entities.Schedule{}, err
}

if type_ == "" || typeID.IsZero() {
return entities.Schedule{}, NotValidParams
}

var studyPlaceID primitive.ObjectID
if user.StudyPlaceInfo != nil {
studyPlaceID = user.StudyPlaceInfo.ID
}

if id, err := primitive.ObjectIDFromHex(studyPlaceIDHex); err == nil {
studyPlaceID = id
}

startDate, endDate = s.scheduleDated(startDate, endDate)

if type_ == "student" {
type_ = "group"
}

typeName, err := s.repository.GetTypeName(ctx, type_, typeID)
if err != nil {
return entities.Schedule{}, err
}
func (s *controller) proceedParams(ctx context.Context, user auth.User, studyPlaceIDHex string, type_ string, typeIDHex string) (primitive.ObjectID, string, primitive.ObjectID, error) {
studyPlaceID, err := primitive.ObjectIDFromHex(studyPlaceIDHex)
if err == nil {
err, studyPlace := s.repository.GetStudyPlaceByID(ctx, studyPlaceID)
if err != nil {
return primitive.ObjectID{}, "", primitive.ObjectID{}, NotValidParams
}

lessons, err := s.repository.GetSchedule(ctx, studyPlaceID, type_, typeID, startDate, endDate, isGeneral, false)
if err != nil {
return entities.Schedule{}, err
if studyPlace.IsSchedulePrivate && (user.SchedulePreferences == nil || user.StudyPlaceInfo.ID != studyPlace.Id) {
return primitive.ObjectID{}, "", primitive.ObjectID{}, NoPermission
}
}

return entities.Schedule{
Info: entities.Info{
StudyPlaceInfo: entities.StudyPlaceInfo{}, //todo
Type: type_,
TypeName: typeName,
StartDate: startDate,
EndDate: endDate,
Date: time.Now(),
},
Lessons: lessons,
}, nil
}

func (s *controller) GetUserSchedule(ctx context.Context, user auth.User, startDate, endDate time.Time, isGeneral bool) (entities.Schedule, error) {
if user.StudyPlaceInfo.Role == "" || user.StudyPlaceInfo.RoleName == "" {
return entities.Schedule{}, NotValidParams
}
if err != nil || studyPlaceID.IsZero() {
if user.SchedulePreferences == nil {
return primitive.ObjectID{}, "", primitive.ObjectID{}, NotValidParams
}

typeID, err := primitive.ObjectIDFromHex(user.StudyPlaceInfo.RoleName)
if err != nil {
return entities.Schedule{}, err
studyPlaceID = user.SchedulePreferences.StudyPlaceID
}

startDate, endDate = s.scheduleDated(startDate, endDate)
typeID, err := primitive.ObjectIDFromHex(typeIDHex)
if err != nil || typeID.IsZero() {
if user.SchedulePreferences == nil {
return primitive.ObjectID{}, "", primitive.ObjectID{}, NotValidParams
}

type_ := user.StudyPlaceInfo.RoleName
if type_ == "student" {
type_ = "group"
typeID = user.SchedulePreferences.TypeID
}

typeName, err := s.repository.GetTypeName(ctx, type_, typeID)
if err != nil {
return entities.Schedule{}, err
}
if type_ == "" {
if user.SchedulePreferences == nil {
return primitive.ObjectID{}, "", primitive.ObjectID{}, NotValidParams
}

lessons, err := s.repository.GetSchedule(ctx, user.StudyPlaceInfo.ID, user.StudyPlaceInfo.Role, typeID, startDate, endDate, isGeneral, false)
if err != nil {
return entities.Schedule{}, err
type_ = user.SchedulePreferences.Type
}

return entities.Schedule{
Info: entities.Info{
StudyPlaceInfo: entities.StudyPlaceInfo{}, //todo
Type: type_,
TypeName: typeName,
StartDate: startDate,
EndDate: endDate,
Date: time.Now(),
},
Lessons: lessons,
}, nil
return studyPlaceID, type_, typeID, nil
}

func (s *controller) GetGeneralSchedule(ctx context.Context, user auth.User, studyPlaceIDHex string, type_ string, typeIDStr string, startDate, endDate time.Time) (entities.Schedule, error) {
typeID, err := primitive.ObjectIDFromHex(typeIDStr)
func (s *controller) GetSchedule(ctx context.Context, user auth.User, studyPlaceIDHex, type_, typeIDHex string, startDate, endDate time.Time) (entities.Schedule, error) {
studyPlaceID, type_, typeID, err := s.proceedParams(ctx, user, studyPlaceIDHex, type_, typeIDHex)
if err != nil {
return entities.Schedule{}, err
}

if type_ == "" || typeID.IsZero() {
return entities.Schedule{}, NotValidParams
}

studyPlaceID := user.StudyPlaceInfo.ID
restricted := true
if id, err := primitive.ObjectIDFromHex(studyPlaceIDHex); err == nil && id != user.StudyPlaceInfo.ID {
studyPlaceID = id
restricted = false
}

startDate, endDate = s.scheduleDated(startDate, endDate)

if type_ == "student" {
type_ = "group"
}

typeName, err := s.repository.GetTypeName(ctx, type_, typeID)
if err != nil {
return entities.Schedule{}, err
}

lessons, err := s.repository.GetSchedule(ctx, studyPlaceID, type_, typeID, startDate, endDate, true, !restricted)
lessons, err := s.repository.GetSchedule(ctx, studyPlaceID, type_, typeID, startDate, endDate)
if err != nil {
return entities.Schedule{}, err
}
Expand All @@ -207,39 +143,27 @@ func (s *controller) GetGeneralSchedule(ctx context.Context, user auth.User, stu
}, nil
}

func (s *controller) GetGeneralUserSchedule(ctx context.Context, user auth.User, startDate, endDate time.Time) (entities.Schedule, error) {
startDate, endDate = s.scheduleDated(startDate, endDate)

typeID, err := primitive.ObjectIDFromHex(user.StudyPlaceInfo.RoleName)
if err != nil {
return entities.Schedule{}, err
}

type_ := user.StudyPlaceInfo.RoleName
if type_ == "student" {
type_ = "group"
}
func (s *controller) GetGeneralSchedule(ctx context.Context, user auth.User, studyPlaceIDHex string, type_ string, typeIDHex string) (entities.GeneralSchedule, error) {
studyPlaceID, type_, typeID, err := s.proceedParams(ctx, user, studyPlaceIDHex, type_, typeIDHex)

typeName, err := s.repository.GetTypeName(ctx, type_, typeID)
if err != nil {
return entities.Schedule{}, err
return entities.GeneralSchedule{}, err
}

lessons, err := s.repository.GetSchedule(ctx, user.StudyPlaceInfo.ID, user.StudyPlaceInfo.Role, typeID, startDate, endDate, true, false)
lessons, err := s.repository.GetGeneralSchedule(ctx, studyPlaceID, type_, typeID)
if err != nil {
return entities.Schedule{}, err
return entities.GeneralSchedule{}, err
}

return entities.Schedule{
Info: entities.Info{
return entities.GeneralSchedule{
Info: entities.GeneralInfo{
StudyPlaceInfo: entities.StudyPlaceInfo{}, //todo
Type: type_,
TypeName: typeName,
StartDate: startDate,
EndDate: endDate,
Date: time.Now(),
},
Lessons: lessons,
GeneralLessons: lessons,
}, nil
}

Expand Down Expand Up @@ -400,7 +324,7 @@ func (s *controller) UpdateLesson(ctx context.Context, lessonIDHex string, updat
Description: updateDTO.Description,
}

err, studyPlace := s.repository.GetStudyPlaceByID(ctx, user.StudyPlaceInfo.ID, false)
err, studyPlace := s.repository.GetStudyPlaceByID(ctx, user.StudyPlaceInfo.ID)
if err != nil {
return entities.Lesson{}, err
}
Expand Down
12 changes: 12 additions & 0 deletions internal/schedule/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type Schedule struct {
Lessons []Lesson `json:"lessons" bson:"lessons"`
}

type GeneralSchedule struct {
Info GeneralInfo `json:"info" bson:"info"`
GeneralLessons []GeneralLesson `json:"lessons" bson:"lessons"`
}

type DeleteLessonID struct {
ID primitive.ObjectID `apps:"trackable,collection=Lessons"`
}
Expand Down Expand Up @@ -72,6 +77,13 @@ type Info struct {
Date time.Time `json:"date" bson:"date"`
}

type GeneralInfo struct {
StudyPlaceInfo StudyPlaceInfo `json:"studyPlaceInfo" bson:"studyPlaceInfo"`
Type string `json:"type" bson:"type"`
TypeName string `json:"typeName" bson:"typeName"`
Date time.Time `json:"date" bson:"date"`
}

type StudyPlaceInfo struct {
Id primitive.ObjectID `json:"id" bson:"_id"`
Title string `json:"title" bson:"title"`
Expand Down
Loading

0 comments on commit 4f84851

Please sign in to comment.