From 3173985d210f9b73dc469c9082d0b5ee6152dc1e Mon Sep 17 00:00:00 2001 From: hayato24s Date: Fri, 8 Mar 2024 14:19:56 +0000 Subject: [PATCH] WIP --- db/gen/model/registered_courses.gen.go | 4 +-- db/gen/query/registered_courses.gen.go | 12 ++++---- db/gorm_gen.go | 12 -------- .../v3/openapi/post_registered_courses.go | 13 +++++++-- .../timetable/repository/registered_course.go | 29 +++++++++++++++---- 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/db/gen/model/registered_courses.gen.go b/db/gen/model/registered_courses.gen.go index 1df678d..5f79f93 100644 --- a/db/gen/model/registered_courses.gen.go +++ b/db/gen/model/registered_courses.gen.go @@ -15,8 +15,8 @@ type RegisteredCourse struct { Name *string `gorm:"column:name;type:text" json:"name"` Instractor *string `gorm:"column:instractor;type:text" json:"instractor"` Credit *float64 `gorm:"column:credit;type:numeric" json:"credit"` - Methods *[]string `gorm:"column:methods;type:registered_courses_methods_enum[]" json:"methods"` - Schedules *[]byte `gorm:"column:schedules;type:jsonb" json:"schedules"` + Methods *string `gorm:"column:methods;type:registered_courses_methods_enum[]" json:"methods"` + Schedules *string `gorm:"column:schedules;type:jsonb" json:"schedules"` Memo string `gorm:"column:memo;type:text;not null" json:"memo"` Attendance int32 `gorm:"column:attendance;type:integer;not null" json:"attendance"` Absence int32 `gorm:"column:absence;type:integer;not null" json:"absence"` diff --git a/db/gen/query/registered_courses.gen.go b/db/gen/query/registered_courses.gen.go index 5006f47..94c9177 100644 --- a/db/gen/query/registered_courses.gen.go +++ b/db/gen/query/registered_courses.gen.go @@ -34,8 +34,8 @@ func newRegisteredCourse(db *gorm.DB, opts ...gen.DOOption) registeredCourse { _registeredCourse.Name = field.NewString(tableName, "name") _registeredCourse.Instractor = field.NewString(tableName, "instractor") _registeredCourse.Credit = field.NewFloat64(tableName, "credit") - _registeredCourse.Methods = field.NewField(tableName, "methods") - _registeredCourse.Schedules = field.NewBytes(tableName, "schedules") + _registeredCourse.Methods = field.NewString(tableName, "methods") + _registeredCourse.Schedules = field.NewString(tableName, "schedules") _registeredCourse.Memo = field.NewString(tableName, "memo") _registeredCourse.Attendance = field.NewInt32(tableName, "attendance") _registeredCourse.Absence = field.NewInt32(tableName, "absence") @@ -57,8 +57,8 @@ type registeredCourse struct { Name field.String Instractor field.String Credit field.Float64 - Methods field.Field - Schedules field.Bytes + Methods field.String + Schedules field.String Memo field.String Attendance field.Int32 Absence field.Int32 @@ -86,8 +86,8 @@ func (r *registeredCourse) updateTableName(table string) *registeredCourse { r.Name = field.NewString(table, "name") r.Instractor = field.NewString(table, "instractor") r.Credit = field.NewFloat64(table, "credit") - r.Methods = field.NewField(table, "methods") - r.Schedules = field.NewBytes(table, "schedules") + r.Methods = field.NewString(table, "methods") + r.Schedules = field.NewString(table, "schedules") r.Memo = field.NewString(table, "memo") r.Attendance = field.NewInt32(table, "attendance") r.Absence = field.NewInt32(table, "absence") diff --git a/db/gorm_gen.go b/db/gorm_gen.go index de4a42b..953069e 100644 --- a/db/gorm_gen.go +++ b/db/gorm_gen.go @@ -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( diff --git a/handler/api/rest/v3/openapi/post_registered_courses.go b/handler/api/rest/v3/openapi/post_registered_courses.go index cfe931b..d2006f0 100644 --- a/handler/api/rest/v3/openapi/post_registered_courses.go +++ b/handler/api/rest/v3/openapi/post_registered_courses.go @@ -1,6 +1,7 @@ package openapi import ( + "bytes" "encoding/json" ) @@ -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 } diff --git a/module/timetable/repository/registered_course.go b/module/timetable/repository/registered_course.go index b1ab900..c377d83 100644 --- a/module/timetable/repository/registered_course.go +++ b/module/timetable/repository/registered_course.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "strings" "github.com/samber/lo" "github.com/twin-te/twinte-back/base" @@ -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 } @@ -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 { @@ -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 } @@ -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(), @@ -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 }