source: extensions/PiwigoLib/PiwigoLib/Service/PwgTagsService.cs @ 3819

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

pwg.tags.add support

File size: 5.9 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 PwgTagsService
14    {
15        /// <summary>
16        /// Get the list of tags the user is connected can access
17        /// </summary>
18        /// <param name="sortedByCounter"></param>
19        /// <returns></returns>
20        static public List<PwgTag> GetListOfTag(Boolean sortedByCounter)
21        {
22            List<PwgTag> returnValue = new List<PwgTag>();
23
24            try
25            {
26                PwgTagsProxyResponse response = PwgTagsProxy.pwg_tags_getList(sortedByCounter);
27
28                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
29                {
30                    if (response.Erreur != null)
31                    {
32                        throw new PwgServiceException("GetListOfPwgTag, the server has return the error.",
33                            response.Erreur.Code,
34                            response.Erreur.Message);
35                    }
36                    else
37                    {
38                        throw new PwgServiceException("GetListOfPwgTag : 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("GetListOfPwgTag : a error is raised by proxy.", ex);
49            }
50            return returnValue;
51        }
52
53        /// <summary>
54        /// Get the list of all atgs in piwigo, you must be connected with a high level user
55        /// </summary>
56        /// <param name="sortedByCounter"></param>
57        /// <returns></returns>
58        static public List<PwgTag> GetAdminListOfTag()
59        {
60            List<PwgTag> returnValue = new List<PwgTag>();
61
62            try
63            {
64                PwgTagsProxyResponse response = PwgTagsProxy.pwg_tags_getAdminList();
65
66                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
67                {
68                    if (response.Erreur != null)
69                    {
70                        throw new PwgServiceException("GetAdminListOfTag, the server has return the error.",
71                            response.Erreur.Code,
72                            response.Erreur.Message);
73                    }
74                    else
75                    {
76                        throw new PwgServiceException("GetAdminListOfTag : 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("GetAdminListOfTag : a error is raised by proxy.", ex);
87            }
88            return returnValue;
89        }
90
91
92        static public Boolean AddTag(String tagName)
93        {
94            Boolean returnValue = false;
95
96            try
97            {
98                PwgTagsProxyResponse response = PwgTagsProxy.pwg_tag_add(tagName);
99
100                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
101                {
102                    if (response.Erreur != null)
103                    {
104                        throw new PwgServiceException("AddTag, the server has return the error.",
105                            response.Erreur.Code,
106                            response.Erreur.Message);
107                    }
108                    else
109                    {
110                        throw new PwgServiceException("AddTag : a error occurs during server process.");
111                    }
112                }
113                else
114                {
115                    returnValue = true;
116                }
117            }
118            catch (PwgProxyException ex)
119            {
120                throw new PwgServiceException("AddTag : a error is raised by proxy.", ex);
121            }
122            return returnValue;
123        }
124
125        /// <summary>
126        /// private: convert response to dto object
127        /// </summary>
128        /// <param name="response"></param>
129        /// <param name="session"></param>
130        static private List<PwgTag> ConvertProxyResponseToDTO(PwgTagsProxyResponse response)
131        {
132            List<PwgTag> returnValue = new List<PwgTag>();
133
134            foreach (PwgTagProxyResponse respTag in response.Tags)
135            {
136                returnValue.Add(ConvertProxyResponseToDTO(respTag));
137            }
138
139            return returnValue;
140        }
141
142        /// <summary>
143        /// private: convert response to dto object
144        /// </summary>
145        /// <param name="response"></param>
146        /// <param name="session"></param>
147        static private PwgTag ConvertProxyResponseToDTO(PwgTagProxyResponse response)
148        {
149            PwgTag returnValue = new PwgTag();
150
151            try
152            {
153                returnValue.Counter = response.Counter;
154                returnValue.Id = response.Id;
155                returnValue.Name = response.Name;
156                returnValue.UrlName = response.UrlName;
157
158                if (String.IsNullOrEmpty(response.UrlTag))
159                {
160                    returnValue.UrlTag = null;
161                }
162                else
163                {
164                    returnValue.UrlTag = (new UriBuilder(response.UrlTag)).Uri;
165                }
166            }
167            catch (Exception ex)
168            {
169                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
170            }
171
172            return returnValue;
173        }
174    }
175}
Note: See TracBrowser for help on using the repository browser.