fix: bad types from sql
This commit is contained in:
parent
4c7d03cddc
commit
673ce48fac
5 changed files with 91 additions and 57 deletions
|
|
@ -17,6 +17,22 @@ func NewServer(db *sqlc.Queries) Server {
|
|||
return Server{db: db}
|
||||
}
|
||||
|
||||
func sql2StorageType(s *sqlc.StorageTypeT) (*oapi.ImageStorageType, error) {
|
||||
if s == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var t oapi.ImageStorageType
|
||||
switch *s {
|
||||
case sqlc.StorageTypeTLocal:
|
||||
t = oapi.Local
|
||||
case sqlc.StorageTypeTS3:
|
||||
t = oapi.S3
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected storage type: %s", *s)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (s Server) mapTitle(ctx context.Context, title sqlc.GetTitleByIDRow) (oapi.Title, error) {
|
||||
|
||||
oapi_title := oapi.Title{
|
||||
|
|
@ -70,7 +86,13 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.GetTitleByIDRow) (oapi.
|
|||
oapi_studio.Poster = &oapi.Image{}
|
||||
oapi_studio.Poster.Id = title.StudioIllustID
|
||||
oapi_studio.Poster.ImagePath = title.StudioImagePath
|
||||
oapi_studio.Poster.StorageType = &title.StudioStorageType
|
||||
|
||||
s, err := sql2StorageType(title.StudioStorageType)
|
||||
if err != nil {
|
||||
return oapi.Title{}, fmt.Errorf("mapTitle, studio storage type: %v", err)
|
||||
}
|
||||
oapi_studio.Poster.StorageType = s
|
||||
|
||||
}
|
||||
}
|
||||
oapi_title.Studio = &oapi_studio
|
||||
|
|
@ -80,7 +102,11 @@ func (s Server) mapTitle(ctx context.Context, title sqlc.GetTitleByIDRow) (oapi.
|
|||
if title.PosterID != nil {
|
||||
oapi_image.Id = title.PosterID
|
||||
oapi_image.ImagePath = title.TitleImagePath
|
||||
oapi_image.StorageType = &title.TitleStorageType
|
||||
s, err := sql2StorageType(title.TitleStorageType)
|
||||
if err != nil {
|
||||
return oapi.Title{}, fmt.Errorf("mapTitle, title starage type: %v", err)
|
||||
}
|
||||
oapi_image.StorageType = s
|
||||
}
|
||||
oapi_title.Poster = &oapi_image
|
||||
|
||||
|
|
|
|||
|
|
@ -81,56 +81,56 @@ func (s Server) GetTagsByTitleId(ctx context.Context, id int64) (oapi.Tags, erro
|
|||
return oapi_tag_names, nil
|
||||
}
|
||||
|
||||
func (s Server) GetImage(ctx context.Context, id int64) (*oapi.Image, error) {
|
||||
// func (s Server) GetImage(ctx context.Context, id int64) (*oapi.Image, error) {
|
||||
|
||||
var oapi_image oapi.Image
|
||||
// var oapi_image oapi.Image
|
||||
|
||||
sqlc_image, err := s.db.GetImageByID(ctx, id)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil //todo: error reference in db
|
||||
}
|
||||
return &oapi_image, fmt.Errorf("query GetImageByID: %v", err)
|
||||
}
|
||||
// sqlc_image, err := s.db.GetImageByID(ctx, id)
|
||||
// if err != nil {
|
||||
// if err == pgx.ErrNoRows {
|
||||
// return nil, nil //todo: error reference in db
|
||||
// }
|
||||
// return &oapi_image, fmt.Errorf("query GetImageByID: %v", err)
|
||||
// }
|
||||
|
||||
//can cast and dont use brain cause all this fields required in image table
|
||||
oapi_image.Id = &sqlc_image.ID
|
||||
oapi_image.ImagePath = &sqlc_image.ImagePath
|
||||
storageTypeStr := string(sqlc_image.StorageType)
|
||||
oapi_image.StorageType = &storageTypeStr
|
||||
// //can cast and dont use brain cause all this fields required in image table
|
||||
// oapi_image.Id = &sqlc_image.ID
|
||||
// oapi_image.ImagePath = &sqlc_image.ImagePath
|
||||
// storageTypeStr := string(sqlc_image.StorageType)
|
||||
// oapi_image.StorageType = string(storageTypeStr)
|
||||
|
||||
return &oapi_image, nil
|
||||
}
|
||||
// return &oapi_image, nil
|
||||
// }
|
||||
|
||||
func (s Server) GetStudio(ctx context.Context, id int64) (*oapi.Studio, error) {
|
||||
// func (s Server) GetStudio(ctx context.Context, id int64) (*oapi.Studio, error) {
|
||||
|
||||
var oapi_studio oapi.Studio
|
||||
// var oapi_studio oapi.Studio
|
||||
|
||||
sqlc_studio, err := s.db.GetStudioByID(ctx, id)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return &oapi_studio, fmt.Errorf("query GetStudioByID: %v", err)
|
||||
}
|
||||
// sqlc_studio, err := s.db.GetStudioByID(ctx, id)
|
||||
// if err != nil {
|
||||
// if err == pgx.ErrNoRows {
|
||||
// return nil, nil
|
||||
// }
|
||||
// return &oapi_studio, fmt.Errorf("query GetStudioByID: %v", err)
|
||||
// }
|
||||
|
||||
oapi_studio.Id = sqlc_studio.ID
|
||||
oapi_studio.Name = sqlc_studio.StudioName
|
||||
oapi_studio.Description = sqlc_studio.StudioDesc
|
||||
// oapi_studio.Id = sqlc_studio.ID
|
||||
// oapi_studio.Name = sqlc_studio.StudioName
|
||||
// oapi_studio.Description = sqlc_studio.StudioDesc
|
||||
|
||||
if sqlc_studio.IllustID == nil {
|
||||
return &oapi_studio, nil
|
||||
}
|
||||
oapi_illust, err := s.GetImage(ctx, *sqlc_studio.IllustID)
|
||||
if err != nil {
|
||||
return &oapi_studio, fmt.Errorf("GetImage: %v", err)
|
||||
}
|
||||
if oapi_illust != nil {
|
||||
oapi_studio.Poster = oapi_illust
|
||||
}
|
||||
// if sqlc_studio.IllustID == nil {
|
||||
// return &oapi_studio, nil
|
||||
// }
|
||||
// oapi_illust, err := s.GetImage(ctx, *sqlc_studio.IllustID)
|
||||
// if err != nil {
|
||||
// return &oapi_studio, fmt.Errorf("GetImage: %v", err)
|
||||
// }
|
||||
// if oapi_illust != nil {
|
||||
// oapi_studio.Poster = oapi_illust
|
||||
// }
|
||||
|
||||
return &oapi_studio, nil
|
||||
}
|
||||
// return &oapi_studio, nil
|
||||
// }
|
||||
|
||||
func (s Server) GetTitlesTitleId(ctx context.Context, request oapi.GetTitlesTitleIdRequestObject) (oapi.GetTitlesTitleIdResponseObject, error) {
|
||||
var oapi_title oapi.Title
|
||||
|
|
@ -233,6 +233,11 @@ func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObje
|
|||
// StudioImagePath: title.StudioImagePath,
|
||||
}
|
||||
|
||||
// if title.TitleStorageType != nil {
|
||||
// s := *title.TitleStorageType
|
||||
// _title.TitleStorageType = string(s)
|
||||
// }
|
||||
|
||||
t, err := s.mapTitle(ctx, _title)
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue