PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Tags.cs

#
C# | 119 lines | 88 code | 8 blank | 23 comment | 15 complexity | 75f97454b3fda498ee91385dd4f539e3 MD5 | raw file
  1using System;
  2using System.Linq;
  3using BlogEngine.Core;
  4using BlogEngine.Core.Json;
  5
  6namespace App_Code
  7{
  8    using System.Web.Services;
  9
 10    /// <summary>
 11    /// Summary description for Tags
 12    /// </summary>
 13    [WebService(Namespace = "http://tempuri.org/")]
 14    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 15    [System.Web.Script.Services.ScriptService]
 16    public class Tags : WebService
 17    {
 18
 19        /// <summary>
 20        /// Edits a tag.
 21        /// </summary>
 22        /// <param name="id">
 23        /// The row id.
 24        /// </param>
 25        /// <param name="bg">
 26        /// The background.
 27        /// </param>
 28        /// <param name="vals">
 29        /// The values.
 30        /// </param>
 31        /// <returns>
 32        /// JSON Response.
 33        /// </returns>
 34        [WebMethod]
 35        public JsonResponse Edit(string id, string bg, string[] vals)
 36        {
 37            if (!WebUtils.CheckRightsForAdminPostPages(true))
 38            {
 39                return new JsonResponse { Success = false, Message = Resources.labels.notAuthorized };
 40            }
 41            if (Utils.StringIsNullOrWhitespace(id))
 42            {
 43                return new JsonResponse { Message = Resources.labels.idArgumentNull };
 44            }
 45            if (vals == null)
 46            {
 47                return new JsonResponse { Message = Resources.labels.valsArgumentNull };
 48            }
 49            if (vals.Length == 0 || Utils.StringIsNullOrWhitespace(vals[0]))
 50            {
 51                return new JsonResponse { Message = Resources.labels.tagIsRequired };
 52            }
 53
 54            var response = new JsonResponse();
 55            try
 56            {
 57                foreach (var p in Post.Posts.ToArray())
 58                {
 59                    var tg = p.Tags.FirstOrDefault(tag => tag == id);
 60                    if(tg != null)
 61                    {
 62                        p.Tags.Remove(tg);
 63                        p.Tags.Add(vals[0]);
 64                        p.DateModified = DateTime.Now;
 65                        p.Save();
 66                    }
 67                }
 68                response.Success = true;
 69                response.Message = string.Format(Resources.labels.tagChangedFromTo, id, vals[0]);
 70            }
 71            catch (Exception ex)
 72            {
 73                Utils.Log(string.Format("Tags.Update: {0}", ex.Message));
 74                response.Message = string.Format(Resources.labels.couldNotUpdateTag, vals[0]);
 75            }
 76
 77            return response;
 78        }
 79
 80        /// <summary>
 81        /// Delete tag in all posts
 82        /// </summary>
 83        /// <param name="id">Tag</param>
 84        /// <returns>Response object</returns>
 85        [WebMethod]
 86        public JsonResponse Delete(string id)
 87        {
 88            if (!WebUtils.CheckRightsForAdminPostPages(true))
 89            {
 90                return new JsonResponse { Success = false, Message = Resources.labels.notAuthorized };
 91            }
 92            if (Utils.StringIsNullOrWhitespace(id))
 93            {
 94                return new JsonResponse { Message = "Tag is required field" };
 95            }
 96
 97            try
 98            {
 99                foreach (var p in Post.Posts.ToArray())
100                {
101                    var tg = p.Tags.FirstOrDefault(tag => tag == id);
102                    if (tg != null)
103                    {
104                        p.Tags.Remove(tg);
105                        p.DateModified = DateTime.Now;
106                        p.Save();
107                    }
108                }
109                return new JsonResponse { Success = true, Message = string.Format(Resources.labels.tagHasBeenDeleted, id) };
110            }
111            catch (Exception ex)
112            {
113                Utils.Log(string.Format("Tags.Delete: {0}", ex.Message));
114                return new JsonResponse { Message = string.Format(Resources.labels.couldNotDeleteTag, id) };
115            }
116        }
117
118    }
119}