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