source: extensions/PiwigoLib/PiwigoLib/Service/PwgCategoriesService.cs @ 7149

Last change on this file since 7149 was 7149, checked in by bayral, 14 years ago

Piwigolib is now modify for mask the implementation of proxy.
Add start version of piwigo WPF

File size: 13.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Com.Piwigo.Lib.DTO;
6using Com.Piwigo.Lib.Proxy;
7using Com.Piwigo.Lib.Proxy.Response;
8using Com.Piwigo.Lib.DTO.Helper;
9using Com.Piwigo.Lib.IService;
10
11namespace Com.Piwigo.Lib.Service
12{
13
14    internal sealed class PwgCategoriesService : IPwgCategoriesService
15    {
16        /// <summary>
17        /// Get the list of category the user is connected can access
18        /// </summary>
19        /// <param name="sortedByCounter"></param>
20        /// <returns></returns>
21        public List<PwgCategory> GetListOfCategory(Int32? CatgeroryId, Boolean? Recursive, Boolean? PublicOnly)
22        {
23            List<PwgCategory> returnValue = new List<PwgCategory>();
24
25            try
26            {
27                PwgCategoriesProxyResponse response = PwgCategoriesProxy.pwg_categories_getList(CatgeroryId, Recursive, PublicOnly);
28
29                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
30                {
31                    if (response.Erreur != null)
32                    {
33                        throw new PwgServiceException("GetListOfCategory, the server has return the error.",
34                            response.Erreur.Code,
35                            response.Erreur.Message);
36                    }
37                    else
38                    {
39                        throw new PwgServiceException("GetListOfCategory : a error occurs during server process.");
40                    }
41                }
42                else
43                {
44                    returnValue = ConvertProxyResponseToDTO(response);
45                }
46            }
47            catch (PwgProxyException ex)
48            {
49                throw new PwgServiceException("GetListOfCategory : a error is raised by proxy.", ex);
50            }
51            return returnValue;
52        }
53
54        /// <summary>
55        /// Get List of all Category, must be logged as admin
56        /// </summary>
57        /// <returns></returns>
58        public List<PwgCategory> GetAdminListOfCategory()
59        {
60            List<PwgCategory> returnValue = new List<PwgCategory>();
61
62            try
63            {
64                PwgCategoriesProxyResponse response = PwgCategoriesProxy.pwg_categories_getAdminList();
65
66                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
67                {
68                    if (response.Erreur != null)
69                    {
70                        throw new PwgServiceException("GetAdminListOfCategory, the server has return the error.",
71                            response.Erreur.Code,
72                            response.Erreur.Message);
73                    }
74                    else
75                    {
76                        throw new PwgServiceException("GetAdminListOfCategory : a error occurs during server process.");
77                    }
78                }
79                else
80                {
81                    returnValue = ConvertProxyResponseToDTO(response);
82                }
83            }
84            catch (PwgProxyException ex)
85            {
86                throw new PwgServiceException("GetAdminListOfCategory : a error is raised by proxy.", ex);
87            }
88            return returnValue;
89        }
90
91        /// <summary>
92        /// Add a category to the gallery
93        /// </summary>
94        /// <param name="categoryName"></param>
95        /// <param name="upperCatId"></param>
96        /// <param name="newId"> Id used by the new category</param>
97        /// <param name="messageInfo">Message returned by the server</param>
98        /// <returns>false, if a error occurs</returns>
99        public Boolean AddCategory(String categoryName, Int32? upperCatId, ref Int32 newId, ref String messageInfo)
100        {
101            Boolean returnValue = false;
102
103            try
104            {
105                PwgAddRequestProxyResponse response = PwgCategoriesProxy.pwg_categories_add(categoryName, upperCatId);
106
107                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
108                {
109                    if (response.Erreur != null)
110                    {
111                        throw new PwgServiceException("AddCategory, the server has return the error.",
112                            response.Erreur.Code,
113                            response.Erreur.Message);
114                    }
115                    else
116                    {
117                        throw new PwgServiceException("AddCategory : a error occurs during server process.");
118                    }
119                }
120                else
121                {
122                    returnValue = true;
123                    newId = response.NewId;
124                    messageInfo = response.Message;
125                }
126            }
127            catch (PwgProxyException ex)
128            {
129                throw new PwgServiceException("AddCategory : a error is raised by proxy.", ex);
130            }
131            return returnValue;
132        }
133
134
135        /// <summary>
136        /// Update category data
137        /// </summary>
138        /// <param name="IdCat"></param>
139        /// <param name="categoryName"></param>
140        /// <param name="categoryComment"></param>
141        /// <returns></returns>
142        public Boolean UpdateCategoryInfo(Int32 IdCat, String categoryName, String categoryComment)
143        {
144            Boolean returnValue = false;
145
146            try
147            {
148                PwgBaseProxyReponse response = PwgCategoriesProxy.pwg_categories_setInfo(IdCat, categoryName, categoryComment);
149
150                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
151                {
152                    if (response.Erreur != null)
153                    {
154                        throw new PwgServiceException("UpdateCategoryInfo, the server has return the error.",
155                            response.Erreur.Code,
156                            response.Erreur.Message);
157                    }
158                    else
159                    {
160                        throw new PwgServiceException("UpdateCategoryInfo : a error occurs during server process.");
161                    }
162                }
163                else
164                {
165                    returnValue = true;
166                }
167            }
168            catch (PwgProxyException ex)
169            {
170                throw new PwgServiceException("UpdateCategoryInfo : a error is raised by proxy.", ex);
171            }
172            return returnValue;
173        }
174
175        /// <summary>
176        /// return the list of PwgImage in the category
177        /// </summary>
178        /// <param name="categoryId"></param>
179        /// <param name="recursive"></param>
180        /// <param name="perPage"></param>
181        /// <param name="Page"></param>
182        /// <param name="Order"></param>
183        /// <param name="minRate"></param>
184        /// <param name="maxRate"></param>
185        /// <param name="minHit"></param>
186        /// <param name="maxHit"></param>
187        /// <param name="minDateAvailable"></param>
188        /// <param name="maxDateAvailable"></param>
189        /// <param name="minDateCreated"></param>
190        /// <param name="maxDateCreated"></param>
191        /// <param name="minRatio"></param>
192        /// <param name="maxRatio"></param>
193        /// <param name="withThumbnail"></param>
194        /// <param name="PageReturned"></param>
195        /// <param name="PerPageeReturned"></param>
196        /// <param name="CountReturned"></param>
197        /// <returns></returns>
198        public List<PwgImage> GetListOfImagesFormCategory(Int32 categoryId,
199                                                    Boolean? recursive,
200                                                    Int32? perPage,
201                                                    Int32? Page,
202                                                    Int32? Order,
203                                                    Int32? minRate,
204                                                    Int32? maxRate,
205                                                    Int32? minHit,
206                                                    Int32? maxHit,
207                                                    DateTime? minDateAvailable,
208                                                    DateTime? maxDateAvailable,
209                                                    DateTime? minDateCreated,
210                                                    DateTime? maxDateCreated,
211                                                    Int32? minRatio,
212                                                    Int32? maxRatio,
213                                                    Boolean? withThumbnail,
214                                                    ref Int32 PageReturned,
215                                                    ref Int32 PerPageeReturned,
216                                                    ref Int32 CountReturned)
217        {
218            List<PwgImage> returnValue = new List<PwgImage>();
219
220            try
221            {
222                PwgImagesProxyResponse response = PwgCategoriesProxy.pwg_categories_getImages(categoryId,
223                                                            recursive,
224                                                            perPage,
225                                                            Page,
226                                                            Order,
227                                                            minRate,
228                                                            maxRate,
229                                                            minHit,
230                                                            maxHit,
231                                                            minDateAvailable,
232                                                            maxDateAvailable,
233                                                            minDateCreated,
234                                                            maxDateCreated,
235                                                            minRatio,
236                                                            maxRatio,
237                                                            withThumbnail);
238
239                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
240                {
241                    if (response.Erreur != null)
242                    {
243                        throw new PwgServiceException("GetListOfImagesFormCategory, the server has return the error.",
244                            response.Erreur.Code,
245                            response.Erreur.Message);
246                    }
247                    else
248                    {
249                        throw new PwgServiceException("GetListOfImagesFormCategory : a error occurs during server process.");
250                    }
251                }
252                else
253                {
254                    PwgImagesService.ConvertProxyResponseToDTO(response, returnValue,
255                                                            ref PageReturned,
256                                                            ref PerPageeReturned,
257                                                            ref CountReturned);
258                }
259            }
260            catch (PwgProxyException ex)
261            {
262                throw new PwgServiceException("GetListOfImagesFormCategory : a error is raised by proxy.", ex);
263            }
264            return returnValue;
265        }
266
267        /// <summary>
268        /// convert response to dto object
269        /// </summary>
270        /// <param name="response"></param>
271        /// <param name="session"></param>
272        internal static List<PwgCategory> ConvertProxyResponseToDTO(PwgCategoriesProxyResponse response)
273        {
274            List<PwgCategory> returnValue = new List<PwgCategory>();
275
276            foreach (PwgCategoryProxyResponse respCat in response.Categories)
277            {
278                returnValue.Add(ConvertProxyResponseToDTO(respCat));
279            }
280
281            return returnValue;
282        }
283
284
285        /// <summary>
286        /// convert response to dto object
287        /// </summary>
288        /// <param name="response"></param>
289        /// <param name="session"></param>
290        internal static PwgCategory ConvertProxyResponseToDTO(PwgCategoryProxyResponse response)
291        {
292            PwgCategory returnValue = new PwgCategory();
293
294            try
295            {
296                returnValue.Id = response.Id;
297                returnValue.Name = response.Name;
298
299                returnValue.DeepImagesCount = response.DeepImagesCount;
300                returnValue.ImagesCount = response.ImagesCount;
301                returnValue.SubCategoriesCount = response.SubCategoriesCount;
302
303                returnValue.UpperCategoriesId = new List<int>();
304                if (response.UpperCategoryId != null)
305                {
306                    foreach (String stringId in response.UpperCategoryId.Split(','))
307                    {
308                        returnValue.UpperCategoriesId.Add(Int32.Parse(stringId));
309                    }
310                }
311
312                if (response.LastDate != null)
313                {
314                    DateTime tryDate;
315                    if (DateTime.TryParse(response.LastDate, out tryDate))
316                    {
317                        returnValue.LastDate = tryDate;
318                    }
319                }
320
321                if (String.IsNullOrEmpty(response.Url))
322                {
323                    returnValue.Url = null;
324                }
325                else
326                {
327                    returnValue.Url = (new UriBuilder(response.Url)).Uri;
328                }
329            }
330            catch (Exception ex)
331            {
332                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
333            }
334
335            return returnValue;
336        }
337    }
338}
Note: See TracBrowser for help on using the repository browser.