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 8, 2024
1 parent 6f3a8e5 commit 3173985
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
4 changes: 2 additions & 2 deletions db/gen/model/registered_courses.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions db/gen/query/registered_courses.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions db/gorm_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ func main() {
}
g.UseDB(db)

var dataMap = map[string]func(gorm.ColumnType) (dataType string){
"jsonb": func(columnType gorm.ColumnType) (dataType string) {
return "[]byte"
},

"registered_courses_methods_enum[]": func(columnType gorm.ColumnType) (dataType string) {
return "[]string"
},
}

g.WithDataTypeMap(dataMap)

g.ApplyBasic(g.GenerateAllTable()...)

g.ApplyBasic(
Expand Down
13 changes: 10 additions & 3 deletions handler/api/rest/v3/openapi/post_registered_courses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package openapi

import (
"bytes"
"encoding/json"
)

Expand All @@ -19,16 +20,22 @@ func (body *PostRegisteredCoursesJSONRequestBody) UnmarshalJSON(data []byte) err
}

func ToPostRegisteredCoursesJSONBody0(reqBody *PostRegisteredCoursesJSONRequestBody) (ret PostRegisteredCoursesJSONBody0, err error) {
err = json.Unmarshal(reqBody.union, &ret)
dec := json.NewDecoder(bytes.NewReader(reqBody.union))
dec.DisallowUnknownFields()
err = dec.Decode(&ret)
return
}

func ToPostRegisteredCoursesJSONBody1(reqBody *PostRegisteredCoursesJSONRequestBody) (ret PostRegisteredCoursesJSONBody1, err error) {
err = json.Unmarshal(reqBody.union, &ret)
dec := json.NewDecoder(bytes.NewReader(reqBody.union))
dec.DisallowUnknownFields()
err = dec.Decode(&ret)
return
}

func ToPostRegisteredCoursesJSONBody2(reqBody *PostRegisteredCoursesJSONRequestBody) (ret PostRegisteredCoursesJSONBody2, err error) {
err = json.Unmarshal(reqBody.union, &ret)
dec := json.NewDecoder(bytes.NewReader(reqBody.union))
dec.DisallowUnknownFields()
err = dec.Decode(&ret)
return
}
29 changes: 23 additions & 6 deletions module/timetable/repository/registered_course.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/samber/lo"
"github.com/twin-te/twinte-back/base"
Expand Down Expand Up @@ -209,7 +210,7 @@ func fromDBRegisteredCourse(dbRegisteredCourse *model.RegisteredCourse) (*timeta
}

if dbRegisteredCourse.Methods != nil {
methods, err := base.MapWithErr(*dbRegisteredCourse.Methods, timetabledomain.ParseCourseMethod)
methods, err := fromDBRegisteredCourseMethods(*dbRegisteredCourse.Methods)
if err != nil {
return err
}
Expand Down Expand Up @@ -272,7 +273,7 @@ func toDBRegisteredCourse(registeredCourse *timetabledomain.RegisteredCourse, wi
}

if registeredCourse.Methods != nil {
dbRegisteredCourse.Methods = lo.ToPtr(base.MapByString(*registeredCourse.Methods))
dbRegisteredCourse.Methods = lo.ToPtr(toDBRegisteredCourseMethods(*registeredCourse.Methods))
}

if registeredCourse.Schedules != nil {
Expand All @@ -297,10 +298,20 @@ type dbRegisteredCourseSchedule struct {
Room string `json:"room"`
}

func fromDBRegisteredCourseSchedules(data []byte) ([]timetabledomain.Schedule, error) {
func fromDBRegisteredCourseMethods(dbMethods string) ([]timetabledomain.CourseMethod, error) {
dbMethods = strings.TrimPrefix(dbMethods, "{")
dbMethods = strings.TrimSuffix(dbMethods, "}")
return base.MapWithErr(strings.Split(dbMethods, ","), timetabledomain.ParseCourseMethod)
}

func toDBRegisteredCourseMethods(methods []timetabledomain.CourseMethod) string {
return fmt.Sprintf("{%s}", strings.Join(base.MapByString(methods), ","))
}

func fromDBRegisteredCourseSchedules(data string) ([]timetabledomain.Schedule, error) {
var dbRegisteredCourseSchedules []dbRegisteredCourseSchedule

if err := json.Unmarshal(data, &dbRegisteredCourseSchedules); err != nil {
if err := json.Unmarshal([]byte(data), &dbRegisteredCourseSchedules); err != nil {
return nil, err
}

Expand All @@ -314,7 +325,7 @@ func fromDBRegisteredCourseSchedules(data []byte) ([]timetabledomain.Schedule, e
})
}

func toDBRegisteredCourseSchedulesJSON(schedules []timetabledomain.Schedule) ([]byte, error) {
func toDBRegisteredCourseSchedulesJSON(schedules []timetabledomain.Schedule) (string, error) {
dbRegisteredCourseSchedules := base.Map(schedules, func(schedule timetabledomain.Schedule) *dbRegisteredCourseSchedule {
return &dbRegisteredCourseSchedule{
Module: schedule.Module.String(),
Expand All @@ -323,5 +334,11 @@ func toDBRegisteredCourseSchedulesJSON(schedules []timetabledomain.Schedule) ([]
Room: schedule.Rooms,
}
})
return json.Marshal(dbRegisteredCourseSchedules)

data, err := json.Marshal(dbRegisteredCourseSchedules)
if err != nil {
return "", err
}

return string(data), nil
}

0 comments on commit 3173985

Please sign in to comment.