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