package server import ( "cmp" "context" "slices" "connectrpc.com/connect" "github.com/tigorlazuardi/bluemage/go/api" "github.com/tigorlazuardi/bluemage/go/gen/converter" images "github.com/tigorlazuardi/bluemage/go/gen/proto/images/v1" "github.com/tigorlazuardi/bluemage/go/pkg/errs" ) type ImageHandler struct { API *api.API } var imageConverter = converter.ImageConverterImpl{} func (im *ImageHandler) ListImages(ctx context.Context, request *connect.Request[images.ListImagesRequest]) (*connect.Response[images.ListImagesResponse], error) { if request.Msg.Limit < 1 { request.Msg.Limit = 50 } if request.Msg.Limit > 1000 { request.Msg.Limit = 1000 } listRequest := imageConverter.ProtoListImagesRequestToAPIImagesRequest(request.Msg) list, err := im.API.ImageList(ctx, listRequest) if err != nil { return nil, errs.IntoConnectError(err) } group := jetImagesToProtoGroupedByDeviceImages(list) resp := &images.ListImagesResponse{ Devices: group, } if request.Msg.After > 0 && len(group) >= int(request.Msg.Limit) { resp.After = &list[len(list)-1].CreatedAt } if request.Msg.Before > 0 && len(group) >= int(request.Msg.Limit) { resp.Before = &list[0].CreatedAt } return connect.NewResponse(resp), nil } func (im *ImageHandler) DeleteImages(ctx context.Context, request *connect.Request[images.DeleteImagesRequest]) (*connect.Response[images.DeleteImagesResponse], error) { panic("not implemented") // TODO: Implement } func (im *ImageHandler) GetImage(ctx context.Context, request *connect.Request[images.GetImageRequest]) (*connect.Response[images.GetImageResponse], error) { panic("not implemented") // TODO: Implement } func jetImagesToProtoGroupedByDeviceImages(jetImages []api.ImageListResponse) (out []*images.GroupedByDeviceImages) { if jetImages == nil { return nil } if len(jetImages) == 0 { return []*images.GroupedByDeviceImages{} } type device struct { slug string name string } grouped := map[device]map[string][]*images.Image{} for _, jetImage := range jetImages { device := device{ slug: jetImage.Device, name: jetImage.DeviceName, } subreddit := jetImage.Subreddit if grouped[device] == nil { grouped[device] = map[string][]*images.Image{} } if grouped[device][subreddit] == nil { grouped[device][subreddit] = []*images.Image{} } grouped[device][subreddit] = append(grouped[device][subreddit], imageConverter.JetImageToProtoImage(jetImage.Images)) } out = make([]*images.GroupedByDeviceImages, 0, len(grouped)) for device, subreddits := range grouped { group := &images.GroupedByDeviceImages{ Slug: device.slug, Name: device.name, Subreddits: make([]*images.GroupedBySubredditImages, 0, len(subreddits)), } for subreddit, img := range subreddits { group.Subreddits = append(group.Subreddits, &images.GroupedBySubredditImages{ Subreddit: subreddit, Images: img, }) } slices.SortFunc(group.Subreddits, func(a, b *images.GroupedBySubredditImages) int { return cmp.Compare(a.Subreddit, b.Subreddit) }) out = append(out, group) } slices.SortFunc(out, func(a, b *images.GroupedByDeviceImages) int { return cmp.Compare(a.Name, b.Name) }) return out }