Skip to content

Commit

Permalink
Improve edit lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
LikDan committed Nov 16, 2023
1 parent b49f71a commit c545b6f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 40 deletions.
29 changes: 17 additions & 12 deletions internal/schedule/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Controller interface {

AddLesson(ctx context.Context, lesson dto2.AddLessonDTO, user auth.User) (entities.Lesson, error)
GetLessonByID(ctx context.Context, user auth.User, idHex string) (entities.Lesson, error)
UpdateLesson(ctx context.Context, lesson dto2.UpdateLessonDTO, user auth.User) error
UpdateLesson(ctx context.Context, lessonID string, lesson dto2.UpdateLessonDTO, user auth.User) (entities.Lesson, error)
DeleteLesson(ctx context.Context, idHex string, user auth.User) error

GetLessonsByDateAndID(ctx context.Context, user auth.User, idHex string) ([]entities.Lesson, error)
Expand Down Expand Up @@ -255,8 +255,8 @@ func (s *controller) AddLesson(ctx context.Context, addDTO dto2.AddLessonDTO, us
PrimaryColor: addDTO.PrimaryColor,
SecondaryColor: addDTO.SecondaryColor,
Type: addDTO.Type,
EndDate: addDTO.EndDate,
StartDate: addDTO.StartDate,
EndDate: addDTO.EndDate.UTC(),
StartDate: addDTO.StartDate.UTC(),
LessonIndex: addDTO.LessonIndex,
SubjectID: addDTO.SubjectID,
GroupID: addDTO.GroupID,
Expand All @@ -270,21 +270,26 @@ func (s *controller) AddLesson(ctx context.Context, addDTO dto2.AddLessonDTO, us

s.apps.AsyncEvent(user.StudyPlaceInfo.ID, "AddLesson", lesson)

return lesson, nil
return s.repository.GetLessonByID(ctx, lesson.Id)
}

func (s *controller) UpdateLesson(ctx context.Context, updateDTO dto2.UpdateLessonDTO, user auth.User) error {
func (s *controller) UpdateLesson(ctx context.Context, lessonIDHex string, updateDTO dto2.UpdateLessonDTO, user auth.User) (entities.Lesson, error) {
if err := s.validator.UpdateLesson(updateDTO); err != nil {
return err
return entities.Lesson{}, err
}

lessonID, err := primitive.ObjectIDFromHex(lessonIDHex)
if err != nil {
return entities.Lesson{}, err
}

lesson := entities.Lesson{
Id: updateDTO.Id,
Id: lessonID,
StudyPlaceId: user.StudyPlaceInfo.ID,
PrimaryColor: updateDTO.PrimaryColor,
SecondaryColor: updateDTO.SecondaryColor,
EndDate: updateDTO.EndDate,
StartDate: updateDTO.StartDate,
StartDate: updateDTO.StartDate.UTC(),
EndDate: updateDTO.EndDate.UTC(),
LessonIndex: updateDTO.LessonIndex,
SubjectID: updateDTO.SubjectID,
GroupID: updateDTO.GroupID,
Expand All @@ -298,7 +303,7 @@ func (s *controller) UpdateLesson(ctx context.Context, updateDTO dto2.UpdateLess

err, studyPlace := s.repository.GetStudyPlaceByID(ctx, user.StudyPlaceInfo.ID, false)
if err != nil {
return err
return entities.Lesson{}, err
}

var lessonType general.LessonType
Expand All @@ -318,14 +323,14 @@ func (s *controller) UpdateLesson(ctx context.Context, updateDTO dto2.UpdateLess
}

if err = s.repository.FilterLessonMarks(ctx, lesson.Id, marks); err != nil {
return err
return entities.Lesson{}, err
}

err = s.repository.UpdateLesson(ctx, lesson)

s.apps.AsyncEvent(user.StudyPlaceInfo.ID, "UpdateLesson", lesson)

return err
return s.repository.GetLessonByID(ctx, lesson.Id)
}

func (s *controller) DeleteLesson(ctx context.Context, idHex string, user auth.User) error {
Expand Down
10 changes: 1 addition & 9 deletions internal/schedule/controllers/validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,5 @@ func (s *schedule) AddLesson(dto dto.AddLessonDTO) error {
}

func (s *schedule) UpdateLesson(dto dto.UpdateLessonDTO) error {
if err := s.AddLesson(dto.AddLessonDTO); err != nil {
return err
}

if dto.Id.IsZero() {
return errors.Wrap(ValidationError, "not valid id")
}

return nil
return s.AddLesson(dto.AddLessonDTO)
}
7 changes: 3 additions & 4 deletions internal/schedule/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ type AddScheduleInfoDTO struct {

type UpdateLessonDTO struct {
AddLessonDTO
Id primitive.ObjectID `json:"id" binding:"req"`
Title string `json:"title"`
Homework string `json:"homework"`
Description string `json:"description"`
Title string `json:"title"`
Homework string `json:"homework"`
Description string `json:"description"`
}
21 changes: 13 additions & 8 deletions internal/schedule/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ func NewScheduleHandler(middleware auth.Middleware, controller controllers.Contr

group.POST("/info", h.MemberAuth("editSchedule"), h.AddScheduleInfo)

group.GET("lessons/:id", h.MemberAuth(), h.GetLessonByID) //todo change endpoint to :id
group.POST("/list", h.MemberAuth("editSchedule"), h.AddLessons)
group.POST("lessons", h.MemberAuth("editSchedule"), h.AddLesson)
group.PUT("", h.MemberAuth("editJournal"), h.UpdateLesson)
group.DELETE(":id", h.MemberAuth("editSchedule"), h.DeleteLesson)
group.DELETE("between/:startDate/:endDate", h.MemberAuth("editSchedule"), h.RemoveLessonsBetweenDates)

lessons := group.Group("lessons")
{
lessons.GET(":id", h.MemberAuth(), h.GetLessonByID)
lessons.POST("", h.MemberAuth("editSchedule"), h.AddLesson)
lessons.PUT(":id", h.MemberAuth("editSchedule"), h.UpdateLesson)
lessons.DELETE(":id", h.MemberAuth("editSchedule"), h.DeleteLesson)
}

group.POST("/general/list", h.MemberAuth("editSchedule"), h.AddGeneralLessons)
group.GET("/general/list", h.MemberAuth(), h.GetGeneralLessonsList)

Expand Down Expand Up @@ -287,7 +291,7 @@ func (s *handler) AddLesson(ctx *gin.Context) {
return
}

ctx.JSON(http.StatusOK, lesson)
ctx.JSON(http.StatusCreated, lesson)
}

// AddScheduleInfo godoc
Expand All @@ -314,14 +318,15 @@ func (s *handler) AddScheduleInfo(ctx *gin.Context) {
// @Router / [put]
func (s *handler) UpdateLesson(ctx *gin.Context) {
user := s.GetUser(ctx)
id := ctx.Param("id")

var lesson dto.UpdateLessonDTO
if err := ctx.BindJSON(&lesson); err != nil {
var lessonDTO dto.UpdateLessonDTO
if err := ctx.BindJSON(&lessonDTO); err != nil {
ctx.JSON(http.StatusBadRequest, err.Error())
return
}

err := s.controller.UpdateLesson(ctx, lesson, user)
lesson, err := s.controller.UpdateLesson(ctx, id, lessonDTO, user)
if err != nil {
_ = ctx.Error(err)
return
Expand Down
31 changes: 24 additions & 7 deletions internal/schedule/repositories/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,27 @@ func (s *repository) AddLesson(ctx context.Context, lesson entities.Lesson) erro
}

func (s *repository) GetLessonByID(ctx context.Context, id primitive.ObjectID) (lesson entities.Lesson, err error) {
opt := options.FindOne()
opt.Projection = bson.M{"marks": 0, "absences": 0}
cursor, err := s.lessons.Aggregate(ctx, bson.A{
bson.M{"$match": bson.M{"_id": id}},
bson.M{"$lookup": bson.M{"from": "StudyPlaceUsers", "localField": "teacherID", "foreignField": "_id", "as": "teacher"}},
bson.M{"$lookup": bson.M{"from": "Groups", "localField": "groupID", "foreignField": "_id", "as": "group"}},
bson.M{"$lookup": bson.M{"from": "Subjects", "localField": "subjectID", "foreignField": "_id", "as": "subject"}},
bson.M{"$lookup": bson.M{"from": "Rooms", "localField": "roomID", "foreignField": "_id", "as": "room"}},
bson.M{
"$addFields": bson.M{
"subject": bson.M{"$first": "$subject.subject"},
"room": bson.M{"$first": "$room.room"},
"teacher": bson.M{"$first": "$teacher.roleName"},
"group": bson.M{"$first": "$group.group"},
},
},
})
if err != nil {
return entities.Lesson{}, err
}

err = s.lessons.FindOne(ctx, bson.M{"_id": id}, opt).Decode(&lesson)
cursor.Next(ctx)
err = cursor.Decode(&lesson)
return
}

Expand All @@ -332,10 +349,10 @@ func (s *repository) UpdateLesson(ctx context.Context, lesson entities.Lesson) e
"type": lesson.Type,
"endDate": lesson.EndDate,
"startDate": lesson.StartDate,
"subject": lesson.Subject,
"group": lesson.Group,
"teacher": lesson.Teacher,
"room": lesson.Room,
"subjectID": lesson.SubjectID,
"groupID": lesson.GroupID,
"teacherID": lesson.TeacherID,
"roomID": lesson.RoomID,
"title": lesson.Title,
"homework": lesson.Homework,
"description": lesson.Description,
Expand Down

0 comments on commit c545b6f

Please sign in to comment.