github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/image.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package controllers 20 21 import ( 22 "github.com/e154/smart-home/api/stub" 23 "github.com/e154/smart-home/common" 24 "github.com/e154/smart-home/common/apperr" 25 "github.com/labstack/echo/v4" 26 ) 27 28 // ControllerImage ... 29 type ControllerImage struct { 30 *ControllerCommon 31 } 32 33 // NewControllerImage ... 34 func NewControllerImage(common *ControllerCommon) *ControllerImage { 35 return &ControllerImage{ 36 ControllerCommon: common, 37 } 38 } 39 40 // AddImage ... 41 func (c ControllerImage) ImageServiceAddImage(ctx echo.Context, _ stub.ImageServiceAddImageParams) error { 42 43 obj := &stub.ApiNewImageRequest{} 44 if err := c.Body(ctx, obj); err != nil { 45 return c.ERROR(ctx, err) 46 } 47 48 image, err := c.endpoint.Image.Add(ctx.Request().Context(), c.dto.Image.FromNewImageRequest(obj)) 49 if err != nil { 50 return c.ERROR(ctx, err) 51 } 52 53 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Image.ToImage(image))) 54 } 55 56 // GetImageById ... 57 func (c ControllerImage) ImageServiceGetImageById(ctx echo.Context, id int64) error { 58 59 image, err := c.endpoint.Image.GetById(ctx.Request().Context(), id) 60 if err != nil { 61 return c.ERROR(ctx, err) 62 } 63 64 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Image.ToImage(image))) 65 } 66 67 // UpdateImageById ... 68 func (c ControllerImage) ImageServiceUpdateImageById(ctx echo.Context, id int64, _ stub.ImageServiceUpdateImageByIdParams) error { 69 70 obj := &stub.ImageServiceUpdateImageByIdJSONBody{} 71 if err := c.Body(ctx, obj); err != nil { 72 return c.ERROR(ctx, err) 73 } 74 75 image, err := c.endpoint.Image.Update(ctx.Request().Context(), c.dto.Image.FromUpdateImageRequest(obj, id)) 76 if err != nil { 77 return c.ERROR(ctx, err) 78 } 79 80 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Image.ToImage(image))) 81 } 82 83 // GetImageList ... 84 func (c ControllerImage) ImageServiceGetImageList(ctx echo.Context, params stub.ImageServiceGetImageListParams) error { 85 86 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 87 items, total, err := c.endpoint.Image.GetList(ctx.Request().Context(), pagination) 88 if err != nil { 89 return c.ERROR(ctx, err) 90 } 91 92 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Image.ToImageListResult(items), total, pagination)) 93 } 94 95 // DeleteImageById ... 96 func (c ControllerImage) ImageServiceDeleteImageById(ctx echo.Context, id int64) error { 97 98 if err := c.endpoint.Image.Delete(ctx.Request().Context(), id); err != nil { 99 return c.ERROR(ctx, err) 100 } 101 102 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 103 } 104 105 // MuxUploadImage ... 106 func (c ControllerImage) ImageServiceUploadImage(ctx echo.Context, _ stub.ImageServiceUploadImageParams) error { 107 108 r := ctx.Request() 109 110 if err := r.ParseMultipartForm(8 << 20); err != nil { 111 log.Error(err.Error()) 112 } 113 114 form := r.MultipartForm 115 if len(form.File) == 0 { 116 return c.ERROR(ctx, apperr.ErrInvalidRequest) 117 } 118 119 images, errs := c.endpoint.Image.Upload(r.Context(), form.File) 120 121 var resultImages = make([]interface{}, 0) 122 123 for _, img := range images { 124 resultImages = append(resultImages, map[string]int64{ 125 "id": img.Id, 126 }) 127 } 128 129 return c.HTTP200(ctx, map[string]interface{}{ 130 "images": resultImages, 131 "errors": errs, 132 }) 133 } 134 135 // GetImageListByDate ... 136 func (c ControllerImage) ImageServiceGetImageListByDate(ctx echo.Context, request stub.ImageServiceGetImageListByDateParams) error { 137 images, err := c.endpoint.Image.GetListByDate(ctx.Request().Context(), common.StringValue(request.Filter)) 138 if err != nil { 139 return c.ERROR(ctx, err) 140 } 141 return c.HTTP200(ctx, c.dto.Image.ToImageList(images)) 142 } 143 144 // GetImageFilterList ... 145 func (c ControllerImage) ImageServiceGetImageFilterList(ctx echo.Context) error { 146 filters, err := c.endpoint.Image.GetFilterList(ctx.Request().Context()) 147 if err != nil { 148 return c.ERROR(ctx, err) 149 } 150 return c.HTTP200(ctx, c.dto.Image.ToFilterList(filters)) 151 }