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

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

connect pwg_category_setInfo into PwgCategoriesService from proxy class

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