Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hayato24s committed Mar 6, 2024
1 parent 0e19a97 commit 22fdd60
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 29 deletions.
6 changes: 3 additions & 3 deletions handler/api/rest/v3/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,21 @@ func fromApiTimetableDay(q openapi.SearchCourseTimetableQueryDays, module timeta
ret = append(ret, schedules...)
}

if q.Intensive != nil {
if q.Intensive != nil && q.Intensive.N0 != nil && *q.Intensive.N0 {
ret = append(ret, timetabledomain.Schedule{
Module: module,
Day: timetabledomain.DayIntensive,
})
}

if q.AnyTime != nil {
if q.AnyTime != nil && q.AnyTime.N0 != nil && *q.AnyTime.N0 {
ret = append(ret, timetabledomain.Schedule{
Module: module,
Day: timetabledomain.DayAnyTime,
})
}

if q.Appointment != nil {
if q.Appointment != nil && q.Appointment.N0 != nil && *q.Appointment.N0 {
ret = append(ret, timetabledomain.Schedule{
Module: module,
Day: timetabledomain.DayAppointment,
Expand Down
8 changes: 8 additions & 0 deletions module/schoolcalendar/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ type UseCase interface {
//
// [Authentication] not required
GetModuleDetails(ctx context.Context, year shareddomain.AcademicYear) ([]*schoolcalendardomain.ModuleDetail, error)

// GetModuleByDate returns the module corresponding to the given date.
//
// [Authentication] not required
//
// [Error Code]
// - shared.NotFound
GetModuleByDate(ctx context.Context, date civil.Date) (schoolcalendardomain.Module, error)
}
4 changes: 3 additions & 1 deletion module/schoolcalendar/port/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ type ListEventsConds struct {
// ModuleDetail

type ListModuleDetailsConds struct {
Year *shareddomain.AcademicYear
Year *shareddomain.AcademicYear
StartBeforeOrEqual *civil.Date
EndAfterOrEqual *civil.Date
}
16 changes: 13 additions & 3 deletions module/schoolcalendar/repository/module_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ func (r *impl) ListModuleDetails(ctx context.Context, conds schoolcalendarport.L
})
}

moduleDetails = base.Map(moduleDetails, func(moduleDetail *schoolcalendardomain.ModuleDetail) *schoolcalendardomain.ModuleDetail {
return moduleDetail.Clone()
})
if conds.StartBeforeOrEqual != nil {
moduleDetails = lo.Filter(moduleDetails, func(moduleDetail *schoolcalendardomain.ModuleDetail, _ int) bool {
return moduleDetail.Start.Before(*conds.StartBeforeOrEqual) || moduleDetail.Start == *conds.StartBeforeOrEqual
})
}

if conds.EndAfterOrEqual != nil {
moduleDetails = lo.Filter(moduleDetails, func(moduleDetail *schoolcalendardomain.ModuleDetail, _ int) bool {
return moduleDetail.End.After(*conds.EndAfterOrEqual) || moduleDetail.End == *conds.EndAfterOrEqual
})
}

moduleDetails = base.MapByClone(moduleDetails)

return moduleDetails, nil
}
Expand Down
20 changes: 20 additions & 0 deletions module/schoolcalendar/usecase/module_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package schoolcalendarusecase

import (
"context"
"fmt"

"cloud.google.com/go/civil"
"github.com/twin-te/twinte-back/apperr"
schoolcalendardomain "github.com/twin-te/twinte-back/module/schoolcalendar/domain"
schoolcalendarport "github.com/twin-te/twinte-back/module/schoolcalendar/port"
shareddomain "github.com/twin-te/twinte-back/module/shared/domain"
sharederr "github.com/twin-te/twinte-back/module/shared/err"
sharedport "github.com/twin-te/twinte-back/module/shared/port"
)

Expand All @@ -14,3 +18,19 @@ func (uc *impl) GetModuleDetails(ctx context.Context, year shareddomain.Academic
Year: &year,
}, sharedport.LockNone)
}

func (uc *impl) GetModuleByDate(ctx context.Context, date civil.Date) (schoolcalendardomain.Module, error) {
moduleDetails, err := uc.r.ListModuleDetails(ctx, schoolcalendarport.ListModuleDetailsConds{
StartBeforeOrEqual: &date,
EndAfterOrEqual: &date,
}, sharedport.LockNone)
if err != nil {
return 0, err
}

if len(moduleDetails) == 0 {
return 0, apperr.New(sharederr.CodeNotFound, fmt.Sprintf("not found module corresponding to the date %s", date))
}

return moduleDetails[0].Module, nil
}
54 changes: 32 additions & 22 deletions module/timetable/repository/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,49 @@ func (r *impl) SearchCourses(ctx context.Context, conds timetableport.SearchCour
}

// Filter by keywords
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(conds.Keywords, func(keyword string) bool {
return strings.Contains(course.Name.String(), keyword)
if len(conds.Keywords) != 0 {
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(conds.Keywords, func(keyword string) bool {
return strings.Contains(course.Name.String(), keyword)
})
})
})
}

// Filter by code prefixes
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(conds.CodePrefixes.Included, func(code string) bool {
return strings.HasPrefix(course.Name.String(), code)
if len(conds.CodePrefixes.Included) != 0 {
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(conds.CodePrefixes.Included, func(code string) bool {
return strings.HasPrefix(course.Code.String(), code)
})
})
})
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(conds.CodePrefixes.Excluded, func(code string) bool {
return !strings.HasPrefix(course.Name.String(), code)
}
if len(conds.CodePrefixes.Excluded) != 0 {
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(conds.CodePrefixes.Excluded, func(code string) bool {
return !strings.HasPrefix(course.Code.String(), code)
})
})
})
}

// Filter by schedules
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(conds.Schedules.FullyIncluded, func(s1 timetabledomain.Schedule) bool {
return lo.SomeBy(course.Schedules, func(s2 timetabledomain.Schedule) bool {
return s1.Module == s2.Module && s1.Day == s2.Day && s1.Period == s2.Period
if len(conds.Schedules.FullyIncluded) != 0 {
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.EveryBy(course.Schedules, func(s1 timetabledomain.Schedule) bool {
return lo.SomeBy(conds.Schedules.FullyIncluded, func(s2 timetabledomain.Schedule) bool {
return s1.Module == s2.Module && s1.Day == s2.Day && s1.Period == s2.Period
})
})
})
})
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.SomeBy(conds.Schedules.PartiallyOverlapped, func(s1 timetabledomain.Schedule) bool {
return lo.SomeBy(course.Schedules, func(s2 timetabledomain.Schedule) bool {
return s1.Module == s2.Module && s1.Day == s2.Day && s1.Period == s2.Period
}
if len(conds.Schedules.PartiallyOverlapped) != 0 {
courses = lo.Filter(courses, func(course *timetabledomain.Course, _ int) bool {
return lo.SomeBy(course.Schedules, func(s1 timetabledomain.Schedule) bool {
return lo.SomeBy(conds.Schedules.PartiallyOverlapped, func(s2 timetabledomain.Schedule) bool {
return s1.Module == s2.Module && s1.Day == s2.Day && s1.Period == s2.Period
})
})
})
})
}

// Apply offset
courses = courses[lo.Clamp(conds.Offset, 0, len(courses)):]
Expand Down

0 comments on commit 22fdd60

Please sign in to comment.