github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/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 dto
    20  
    21  import (
    22  	stub "github.com/e154/smart-home/api/stub"
    23  	"github.com/e154/smart-home/common"
    24  	m "github.com/e154/smart-home/models"
    25  )
    26  
    27  // Image ...
    28  type Image struct{}
    29  
    30  // NewImageDto ...
    31  func NewImageDto() Image {
    32  	return Image{}
    33  }
    34  
    35  // ToImage ...
    36  func (i Image) ToImage(image *m.Image) (result *stub.ApiImage) {
    37  	result = &stub.ApiImage{
    38  		Id:        image.Id,
    39  		Thumb:     image.Thumb,
    40  		Image:     image.Image,
    41  		MimeType:  image.MimeType,
    42  		Title:     image.Title,
    43  		Size:      image.Size,
    44  		Name:      image.Name,
    45  		Url:       image.Url,
    46  		CreatedAt: image.CreatedAt,
    47  	}
    48  	return
    49  }
    50  
    51  // ToImageShort ...
    52  func (i Image) ToImageShort(image *m.Image) (result *stub.ApiImage) {
    53  	result = &stub.ApiImage{
    54  		Id:   image.Id,
    55  		Name: image.Name,
    56  		Url:  image.Url,
    57  	}
    58  	return
    59  }
    60  
    61  // FromNewImageRequest ...
    62  func (i Image) FromNewImageRequest(req *stub.ApiNewImageRequest) (image *m.Image) {
    63  	image = &m.Image{
    64  		Thumb:    req.Thumb,
    65  		Image:    req.Image,
    66  		MimeType: req.MimeType,
    67  		Title:    req.Title,
    68  		Name:     req.Name,
    69  	}
    70  	return
    71  }
    72  
    73  // FromUpdateImageRequest ...
    74  func (i Image) FromUpdateImageRequest(req *stub.ImageServiceUpdateImageByIdJSONBody, id int64) (image *m.Image) {
    75  	image = &m.Image{
    76  		Id:       id,
    77  		Thumb:    req.Thumb,
    78  		Image:    req.Image,
    79  		MimeType: req.MimeType,
    80  		Title:    req.Title,
    81  		Name:     req.Name,
    82  		Size:     req.Size,
    83  	}
    84  	return
    85  }
    86  
    87  // ToImageListResult ...
    88  func (i Image) ToImageListResult(images []*m.Image) []*stub.ApiImage {
    89  
    90  	var items = make([]*stub.ApiImage, 0, len(images))
    91  	for _, item := range images {
    92  		items = append(items, &stub.ApiImage{
    93  			Id:        item.Id,
    94  			Thumb:     item.Thumb,
    95  			Url:       item.Url,
    96  			Image:     item.Image,
    97  			MimeType:  item.MimeType,
    98  			Title:     item.Title,
    99  			Size:      item.Size,
   100  			Name:      item.Name,
   101  			CreatedAt: item.CreatedAt,
   102  		})
   103  	}
   104  
   105  	return items
   106  }
   107  
   108  // ToImageList ...
   109  func (i Image) ToImageList(items []*m.Image) (result *stub.ApiGetImageListByDateResult) {
   110  
   111  	result = &stub.ApiGetImageListByDateResult{
   112  		Items: make([]stub.ApiImage, 0, len(items)),
   113  	}
   114  
   115  	for _, item := range items {
   116  		result.Items = append(result.Items, stub.ApiImage{
   117  			Id:        item.Id,
   118  			Thumb:     item.Thumb,
   119  			Url:       item.Url,
   120  			Image:     item.Image,
   121  			MimeType:  item.MimeType,
   122  			Title:     item.Title,
   123  			Size:      item.Size,
   124  			Name:      item.Name,
   125  			CreatedAt: item.CreatedAt,
   126  		})
   127  	}
   128  
   129  	return
   130  }
   131  
   132  // ToFilterList ...
   133  func (i Image) ToFilterList(items []*m.ImageFilterList) (result stub.ApiGetImageFilterListResult) {
   134  
   135  	result = stub.ApiGetImageFilterListResult{
   136  		Items: make([]stub.GetImageFilterListResultfilter, 0, len(items)),
   137  	}
   138  
   139  	for _, item := range items {
   140  		result.Items = append(result.Items, stub.GetImageFilterListResultfilter{
   141  			Date:  item.Date,
   142  			Count: int32(item.Count),
   143  		})
   144  	}
   145  
   146  	return
   147  }
   148  
   149  func ImportImage(from *stub.ApiImage) (*int64, *m.Image) {
   150  	if from == nil {
   151  		return nil, nil
   152  	}
   153  	return common.Int64(from.Id), &m.Image{
   154  		Id:       from.Id,
   155  		Thumb:    from.Thumb,
   156  		Image:    from.Image,
   157  		MimeType: from.MimeType,
   158  		Title:    from.Title,
   159  		Name:     from.Name,
   160  		Size:     from.Size,
   161  	}
   162  }