source: extensions/PiwigoLib/PiwigoLib/Service/PwgImagesService.cs @ 3861

Last change on this file since 3861 was 3861, checked in by bayral, 15 years ago

support for pwg.images.rate

File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Globalization;
5
6using Com.Piwigo.Lib.DTO;
7using Com.Piwigo.Lib.Proxy;
8using Com.Piwigo.Lib.Proxy.Response;
9using Com.Piwigo.Lib.DTO.Helper;
10
11namespace Com.Piwigo.Lib.Service
12{
13
14    static public class PwgImagesService
15    {
16        static public PwgImageRate RateImage(Int32 imageId, Int32 imageRate)
17        {
18            PwgImageRate returnValue = new PwgImageRate();
19
20            try
21            {
22                PwgImageRateProxyResponse response = PwgImagesProxy.pwg_images_rate(imageId, imageRate);
23
24                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
25                {
26                    if (response.Erreur != null)
27                    {
28                        throw new PwgServiceException("RateImage, the server has return the error.",
29                            response.Erreur.Code,
30                            response.Erreur.Message);
31                    }
32                    else
33                    {
34                        throw new PwgServiceException("RateImage : a error occurs during server process.");
35                    }
36                }
37                else
38                {
39                    returnValue = ConvertProxyResponseToDTO(response);
40                }
41            }
42            catch (PwgProxyException ex)
43            {
44                throw new PwgServiceException("RateImage : a error is raised by proxy.", ex);
45            }
46            return returnValue;
47        }
48
49        /// <summary>
50        /// private: convert response to dto object
51        /// </summary>
52        /// <param name="response"></param>
53        /// <param name="session"></param>
54        static public PwgImageRate ConvertProxyResponseToDTO(PwgImageRateProxyResponse response)
55        {
56            PwgImageRate returnValue = new PwgImageRate();
57            Double aDouble = 0;
58            NumberStyles style = NumberStyles.AllowDecimalPoint;
59            CultureInfo en_us = CultureInfo.CreateSpecificCulture("en-US");
60
61            returnValue.Count = response.Count;
62
63            returnValue.Stdev = 0.0 ;
64            if (Double.TryParse(response.Stdev, style, en_us, out aDouble))
65            {
66                returnValue.Stdev = aDouble;
67            }
68
69            returnValue.Average = 0.0;
70            if (Double.TryParse(response.Average, style, en_us, out aDouble))
71            {
72                returnValue.Average = aDouble;
73            }
74
75
76            return returnValue;
77        }
78
79        /// <summary>
80        /// private: convert response to dto object
81        /// </summary>
82        /// <param name="response"></param>
83        /// <param name="session"></param>
84        static public void ConvertProxyResponseToDTO(PwgImagesProxyResponse response,
85                                                      List<PwgImage> lstImage, 
86                                                      ref Int32 Page, ref Int32 PerPagee, ref Int32 Count)
87        {
88                ConvertProxyResponseToDTO(response.ImageList, lstImage, ref Page, ref PerPagee, ref Count);
89        }
90
91        /// <summary>
92        /// private: convert response to dto object
93        /// </summary>
94        /// <param name="response"></param>
95        /// <param name="session"></param>
96        static public void ConvertProxyResponseToDTO(PwgImageListProxyResponse response,
97                                                      List<PwgImage> lstImage, 
98                                                      ref Int32 Page, ref Int32 PerPagee, ref Int32 Count)
99        {
100            Page = response.Page;
101            PerPagee = response.PerPage;
102            Count = response.Count;
103
104            if (response.Images != null)
105            {
106                foreach (PwgImageProxyResponse respImg in response.Images)
107                {
108                    lstImage.Add(ConvertProxyResponseToDTO(respImg));
109                }
110            }
111        }
112
113        /// <summary>
114        /// private: convert response to dto object
115        /// </summary>
116        /// <param name="response"></param>
117        /// <param name="session"></param>
118        static public PwgImage ConvertProxyResponseToDTO(PwgImageProxyResponse response)
119        {
120            PwgImage returnValue = new PwgImage();
121
122            try
123            {
124                returnValue.Counter = response.Counter;
125                returnValue.Id = response.Id;
126                returnValue.File = response.File;
127                returnValue.Height = response.Height;
128                returnValue.Width = response.Width;
129
130                if (String.IsNullOrEmpty(response.UrlElement))
131                {
132                    returnValue.UrlElement = null;
133                }
134                else
135                {
136                    returnValue.UrlElement = (new UriBuilder(response.UrlElement)).Uri;
137                }
138
139                if (String.IsNullOrEmpty(response.UrlThunb))
140                {
141                    returnValue.UrlThunb = null;
142                }
143                else
144                {
145                    returnValue.UrlThunb = (new UriBuilder(response.UrlThunb)).Uri;
146                }
147
148                if (String.IsNullOrEmpty(response.UrlHighDef))
149                {
150                    returnValue.UrlHighDef = null;
151                }
152                else
153                {
154                    returnValue.UrlHighDef = (new UriBuilder(response.UrlHighDef)).Uri;
155                }
156
157                returnValue.Tags = new List<PwgTag>();
158                if (response.Tags != null)
159                {
160                    foreach (PwgTagProxyResponse tagResp in response.Tags)
161                    {
162                        PwgTag aTag = PwgTagsService.ConvertProxyResponseToDTO(tagResp);
163                        returnValue.Tags.Add(aTag);
164                    }
165                }
166
167                returnValue.Categories = new List<PwgCategory>();
168                if (response.Categories != null)
169                {
170                    foreach (PwgCategoryProxyResponse catResp in response.Categories)
171                    {
172                        PwgCategory aCat = PwgCategoriesService.ConvertProxyResponseToDTO(catResp);
173                        returnValue.Categories.Add(aCat);
174                    }
175                }
176
177            }
178            catch (Exception ex)
179            {
180                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
181            }
182
183            return returnValue;
184        }
185    }
186}
Note: See TracBrowser for help on using the repository browser.