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

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

Complete pwg.categories.* support

File size: 11.3 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        static public List<PwgImage> GetListOfImagesFormCategory(Int32 categoryId,
134                                                    Boolean? recursive,
135                                                    Int32? perPage,
136                                                    Int32? Page,
137                                                    Int32? Order,
138                                                    Int32? minRate,
139                                                    Int32? maxRate,
140                                                    Int32? minHit,
141                                                    Int32? maxHit,
142                                                    DateTime? minDateAvailable,
143                                                    DateTime? maxDateAvailable,
144                                                    DateTime? minDateCreated,
145                                                    DateTime? maxDateCreated,
146                                                    Int32? minRatio,
147                                                    Int32? maxRatio,
148                                                    Boolean? withThumbnail,
149                                                    ref Int32 PageReturned,
150                                                    ref Int32 PerPageeReturned,
151                                                    ref Int32 CountReturned)
152        {
153            List<PwgImage> returnValue = new List<PwgImage>();
154
155            try
156            {
157                PwgImagesProxyResponse response = PwgCategoriesProxy.pwg_categories_getImages(categoryId,
158                                                            recursive,
159                                                            perPage,
160                                                            Page,
161                                                            Order,
162                                                            minRate,
163                                                            maxRate,
164                                                            minHit,
165                                                            maxHit,
166                                                            minDateAvailable,
167                                                            maxDateAvailable,
168                                                            minDateCreated,
169                                                            maxDateCreated,
170                                                            minRatio,
171                                                            maxRatio,
172                                                            withThumbnail);
173
174                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
175                {
176                    if (response.Erreur != null)
177                    {
178                        throw new PwgServiceException("GetListOfImagesFormCategory, the server has return the error.",
179                            response.Erreur.Code,
180                            response.Erreur.Message);
181                    }
182                    else
183                    {
184                        throw new PwgServiceException("GetListOfImagesFormCategory : a error occurs during server process.");
185                    }
186                }
187                else
188                {
189                    PwgImagesService.ConvertProxyResponseToDTO(response, returnValue,
190                                                            ref PageReturned,
191                                                            ref PerPageeReturned,
192                                                            ref CountReturned);
193                }
194            }
195            catch (PwgProxyException ex)
196            {
197                throw new PwgServiceException("GetListOfImagesFormCategory : a error is raised by proxy.", ex);
198            }
199            return returnValue;
200        }
201
202        /// <summary>
203        /// convert response to dto object
204        /// </summary>
205        /// <param name="response"></param>
206        /// <param name="session"></param>
207        static public List<PwgCategory> ConvertProxyResponseToDTO(PwgCategoriesProxyResponse response)
208        {
209            List<PwgCategory> returnValue = new List<PwgCategory>();
210
211            foreach (PwgCategoryProxyResponse respCat in response.Categories)
212            {
213                returnValue.Add(ConvertProxyResponseToDTO(respCat));
214            }
215
216            return returnValue;
217        }
218
219
220        /// <summary>
221        /// convert response to dto object
222        /// </summary>
223        /// <param name="response"></param>
224        /// <param name="session"></param>
225        static public PwgCategory ConvertProxyResponseToDTO(PwgCategoryProxyResponse response)
226        {
227            PwgCategory returnValue = new PwgCategory();
228
229            try
230            {
231                returnValue.Id = response.Id;
232                returnValue.Name = response.Name;
233
234                returnValue.DeepImagesCount = response.DeepImagesCount;
235                returnValue.ImagesCount = response.ImagesCount;
236                returnValue.SubCategoriesCount = response.SubCategoriesCount;
237
238                returnValue.UpperCategoriesId = new List<int>();
239                if (response.UpperCategoryId != null)
240                {
241                    foreach (String stringId in response.UpperCategoryId.Split(','))
242                    {
243                        returnValue.UpperCategoriesId.Add(Int32.Parse(stringId));
244                    }
245                }
246
247                if (response.LastDate != null)
248                {
249                    DateTime tryDate;
250                    if (DateTime.TryParse(response.LastDate, out tryDate))
251                    {
252                        returnValue.LastDate = tryDate;
253                    }
254                }
255
256                if (String.IsNullOrEmpty(response.Url))
257                {
258                    returnValue.Url = null;
259                }
260                else
261                {
262                    returnValue.Url = (new UriBuilder(response.Url)).Uri;
263                }
264            }
265            catch (Exception ex)
266            {
267                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
268            }
269
270            return returnValue;
271        }
272    }
273}
Note: See TracBrowser for help on using the repository browser.